2016-08-11 07:26:39 -04:00
import { Plugin , CordovaInstance } from './plugin' ;
2016-09-21 15:04:46 -05:00
import { Observable } from 'rxjs/Observable' ;
2016-08-31 18:02:15 -03:00
2016-08-11 07:26:39 -04:00
declare var cordova : any ;
2016-07-17 19:56:52 +02:00
export interface InAppBrowserEvent extends Event {
/** the eventname, either loadstart, loadstop, loaderror, or exit. */
type : string ;
/** the URL that was loaded. */
url : string ;
/** the error code, only in the case of loaderror. */
code : number ;
/** the error message, only in the case of loaderror. */
message : string ;
}
2016-08-11 07:26:39 -04:00
/ * *
* @name InAppBrowser
* @description Launches in app Browser
* @usage
* ` ` ` typescript
* import { InAppBrowser } from 'ionic-native' ;
*
*
* . . .
*
*
* let browser = new InAppBrowser ( 'https://ionic.io' , '_system' ) ;
* browser . executeScript ( . . . ) ;
* browser . insertCSS ( . . . ) ;
* browser . close ( ) ;
* ` ` `
2016-12-06 09:18:37 -05:00
* @interfaces
* InAppBrowserEvent
2016-08-11 07:26:39 -04:00
* /
@Plugin ( {
2016-10-27 12:48:50 -05:00
pluginName : 'InAppBrowser' ,
2016-08-11 07:26:39 -04:00
plugin : 'cordova-plugin-inappbrowser' ,
pluginRef : 'cordova.InAppBrowser' ,
repo : 'https://github.com/apache/cordova-plugin-inappbrowser'
} )
export class InAppBrowser {
2016-07-17 19:56:52 +02:00
2016-11-09 14:14:26 -05:00
/ * *
* @private
* /
2016-08-11 07:26:39 -04:00
static open ( url : string , target? : string , options? : string ) : void {
2016-11-02 16:49:34 -05:00
console . warn ( 'Native: Your current usage of the InAppBrowser plugin is deprecated as of ionic-native@1.3.8. Please check the Ionic Native docs for the latest usage details.' ) ;
2016-08-11 07:26:39 -04:00
}
private _objectInstance : any ;
2016-07-17 19:56:52 +02:00
/ * *
2016-08-11 07:26:39 -04:00
* Opens a URL in a new InAppBrowser instance , the current browser instance , or the system browser .
* @param url The URL to load .
* @param target The target in which to load the URL , an optional parameter that defaults to _self .
* @param options Options for the InAppBrowser . Optional , defaulting to : location = yes .
* The options string must not contain any blank space , and each feature ' s
* name / value pairs must be separated by a comma . Feature names are case insensitive .
2016-07-17 19:56:52 +02:00
* /
2016-08-11 07:26:39 -04:00
constructor ( url : string , target? : string , options? : string ) {
try {
this . _objectInstance = cordova . InAppBrowser . open ( url , target , options ) ;
} catch ( e ) {
window . open ( url ) ;
console . warn ( 'Native: InAppBrowser is not installed or you are running on a browser. Falling back to window.open, all instance methods will NOT work.' ) ;
}
}
2016-07-17 19:56:52 +02:00
/ * *
* Displays an InAppBrowser window that was opened hidden . Calling this has no effect
* if the InAppBrowser was already visible .
* /
2016-08-11 07:26:39 -04:00
@CordovaInstance ( { sync : true } )
show ( ) : void { }
/ * *
* Closes the InAppBrowser window .
* /
@CordovaInstance ( { sync : true } )
close ( ) : void { }
2016-07-17 19:56:52 +02:00
/ * *
* Injects JavaScript code into the InAppBrowser window .
* @param script Details of the script to run , specifying either a file or code key .
2016-11-29 16:40:50 -06:00
* @returns { Promise < any > }
2016-07-17 19:56:52 +02:00
* /
2016-08-11 07:26:39 -04:00
@CordovaInstance ( )
executeScript ( script : { file? : string , code? : string } ) : Promise < any > { return ; }
2016-07-17 19:56:52 +02:00
/ * *
* Injects CSS into the InAppBrowser window .
2016-11-29 16:40:50 -06:00
* @param { Object } Details of the script to run , specifying either a file or code key .
* @returns { Promise < any > }
2016-07-17 19:56:52 +02:00
* /
2016-08-11 07:26:39 -04:00
@CordovaInstance ( )
insertCss ( css : { file? : string , code? : string } ) : Promise < any > { return ; }
2016-07-17 19:56:52 +02:00
/ * *
2016-08-11 07:26:39 -04:00
* A method that allows you to listen to events happening in the browser .
2016-11-29 16:40:50 -06:00
* @param { string } name of the event
* @returns { Observable < InAppBrowserEvent > } Returns back an observable that will listen to the event on subscribe , and will stop listening to the event on unsubscribe .
2016-07-17 19:56:52 +02:00
* /
2016-08-11 07:26:39 -04:00
on ( event : string ) : Observable < InAppBrowserEvent > {
return new Observable < InAppBrowserEvent > ( ( observer ) = > {
2016-08-13 11:30:52 -04:00
this . _objectInstance . addEventListener ( event , observer . next . bind ( observer ) ) ;
2016-08-13 11:19:46 -04:00
return ( ) = > this . _objectInstance . removeEventListener ( event , observer . next . bind ( observer ) ) ;
2016-08-11 07:26:39 -04:00
} ) ;
}
2016-12-06 09:18:37 -05:00
2016-07-17 19:56:52 +02:00
}