2016-08-11 19:26:39 +08:00
import { Plugin , CordovaInstance } from './plugin' ;
import { Observable } from 'rxjs/Observable' ;
declare var cordova : any ;
2016-07-18 01:56:52 +08: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 19:26:39 +08: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 ( ) ;
* ` ` `
* /
@Plugin ( {
plugin : 'cordova-plugin-inappbrowser' ,
pluginRef : 'cordova.InAppBrowser' ,
repo : 'https://github.com/apache/cordova-plugin-inappbrowser'
} )
export class InAppBrowser {
2016-07-18 01:56:52 +08:00
2016-08-11 19:26:39 +08:00
static open ( url : string , target? : string , options? : string ) : void {
console . warn ( 'Native: Your current usage of the InAppBrowser plugin is depreciated as of ionic-native@1.3.8. Please check the Ionic Native docs for the latest usage details.' ) ;
}
private _objectInstance : any ;
2016-07-18 01:56:52 +08:00
/ * *
2016-08-11 19:26:39 +08: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-18 01:56:52 +08:00
* /
2016-08-11 19:26:39 +08: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-18 01:56:52 +08:00
/ * *
* Displays an InAppBrowser window that was opened hidden . Calling this has no effect
* if the InAppBrowser was already visible .
* /
2016-08-11 19:26:39 +08:00
@CordovaInstance ( { sync : true } )
show ( ) : void { }
/ * *
* Closes the InAppBrowser window .
* /
@CordovaInstance ( { sync : true } )
close ( ) : void { }
2016-07-18 01:56:52 +08:00
/ * *
* Injects JavaScript code into the InAppBrowser window .
* @param script Details of the script to run , specifying either a file or code key .
* /
2016-08-11 19:26:39 +08:00
@CordovaInstance ( )
executeScript ( script : { file? : string , code? : string } ) : Promise < any > { return ; }
2016-07-18 01:56:52 +08:00
/ * *
* Injects CSS into the InAppBrowser window .
* @param css Details of the script to run , specifying either a file or code key .
* /
2016-08-11 19:26:39 +08:00
@CordovaInstance ( )
insertCss ( css : { file? : string , code? : string } ) : Promise < any > { return ; }
2016-07-18 01:56:52 +08:00
2016-08-01 02:24:15 +08:00
2016-07-18 01:56:52 +08:00
/ * *
2016-08-11 19:26:39 +08:00
* A method that allows you to listen to events happening in the browser .
* @param event Event name
* @returns { Observable < any > } Returns back an observable that will listen to the event on subscribe , and will stop listening to the event on unsubscribe .
2016-07-18 01:56:52 +08:00
* /
2016-08-11 19:26:39 +08:00
on ( event : string ) : Observable < InAppBrowserEvent > {
return new Observable < InAppBrowserEvent > ( ( observer ) = > {
2016-08-13 23:30:52 +08:00
this . _objectInstance . addEventListener ( event , observer . next . bind ( observer ) ) ;
2016-08-13 23:19:46 +08:00
return ( ) = > this . _objectInstance . removeEventListener ( event , observer . next . bind ( observer ) ) ;
2016-08-11 19:26:39 +08:00
} ) ;
}
2016-07-18 01:56:52 +08:00
}