-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(inappbrowser): implement instance based wrapper #305
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
00263de
feat(inappbrowser): the inAppBrowser class is no longer static
ihadeed fa26f94
feat(inappbrowser): fallback on window.open if plugin not available
ihadeed 968b031
change warning text to match expected release version
ihadeed eaa6df3
Merge branch 'master' into new-browser
ihadeed 681f0c0
Merge branch 'new-browser' of https://github.com/driftyco/ionic-nativ…
ihadeed 49ce3d0
fix conflicts
ihadeed f51499a
docs(inappbrowser): add docs for new implementation
ihadeed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { Cordova, Plugin } from './plugin'; | ||
|
||
import { Plugin, CordovaInstance } from './plugin'; | ||
import { Observable } from 'rxjs/Observable'; | ||
declare var cordova: any; | ||
|
||
export interface InAppBrowserEvent extends Event { | ||
/** the eventname, either loadstart, loadstop, loaderror, or exit. */ | ||
|
@@ -12,76 +13,72 @@ export interface InAppBrowserEvent extends Event { | |
message: string; | ||
} | ||
|
||
export interface InAppBrowserRef { | ||
/** | ||
* Adds a listener for an event from the InAppBrowser. | ||
* @param type the event to listen for | ||
* loadstart: event fires when the InAppBrowser starts to load a URL. | ||
* loadstop: event fires when the InAppBrowser finishes loading a URL. | ||
* loaderror: event fires when the InAppBrowser encounters an error when loading a URL. | ||
* exit: event fires when the InAppBrowser window is closed. | ||
* @param callback the function that executes when the event fires. The function is | ||
* passed an InAppBrowserEvent object as a parameter. | ||
*/ | ||
addEventListener(type: string, callback: (event: InAppBrowserEvent) => void); | ||
@Plugin({ | ||
plugin: 'cordova-plugin-inappbrowser', | ||
pluginRef: 'cordova.InAppBrowser', | ||
repo: 'https://github.com/apache/cordova-plugin-inappbrowser' | ||
}) | ||
export class InAppBrowser { | ||
|
||
static open(url: string, target?: string, options?: string): void { | ||
console.warn('Native: Your current usage of the InAppBrowser plugin is depreciated as of [email protected]. Please check the Ionic Native docs for the latest usage details.'); | ||
} | ||
|
||
private _objectInstance: any; | ||
|
||
/** | ||
* Removes a listener for an event from the InAppBrowser. | ||
* @param type The event to stop listening for. | ||
* loadstart: event fires when the InAppBrowser starts to load a URL. | ||
* loadstop: event fires when the InAppBrowser finishes loading a URL. | ||
* loaderror: event fires when the InAppBrowser encounters an error when loading a URL. | ||
* exit: event fires when the InAppBrowser window is closed. | ||
* @param callback the function that executes when the event fires. The function is | ||
* passed an InAppBrowserEvent object as a parameter. | ||
* 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. | ||
*/ | ||
removeEventListener(type: string, callback: (event: InAppBrowserEvent) => void); | ||
|
||
/** Closes the InAppBrowser window. */ | ||
close(); | ||
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.'); | ||
} | ||
} | ||
|
||
/** | ||
* Displays an InAppBrowser window that was opened hidden. Calling this has no effect | ||
* if the InAppBrowser was already visible. | ||
*/ | ||
show(); | ||
@CordovaInstance({sync: true}) | ||
show(): void { } | ||
|
||
/** | ||
* Closes the InAppBrowser window. | ||
*/ | ||
@CordovaInstance({sync: true}) | ||
close(): void { } | ||
|
||
/** | ||
* Injects JavaScript code into the InAppBrowser window. | ||
* @param script Details of the script to run, specifying either a file or code key. | ||
* @param callback The function that executes after the JavaScript code is injected. | ||
* If the injected script is of type code, the callback executes with | ||
* a single parameter, which is the return value of the script, wrapped in an Array. | ||
* For multi-line scripts, this is the return value of the last statement, | ||
* or the last expression evaluated. | ||
*/ | ||
executeScript(script: { file?: string, code?: string }, callback?: (result?: any) => void); | ||
@CordovaInstance() | ||
executeScript(script: {file?: string, code?: string}): Promise<any> {return; } | ||
|
||
/** | ||
* Injects CSS into the InAppBrowser window. | ||
* @param css Details of the script to run, specifying either a file or code key. | ||
* @param callback The function that executes after the CSS is injected. | ||
*/ | ||
insertCSS(css: { file?: string, code?: string }, callback?: () => void); | ||
} | ||
|
||
@Plugin({ | ||
plugin: 'cordova-plugin-inappbrowser', | ||
pluginRef: 'cordova.InAppBrowser' | ||
}) | ||
export class InAppBrowser { | ||
@CordovaInstance() | ||
insertCss(css: {file?: string, code?: string}): Promise<any> {return; } | ||
|
||
/** | ||
* 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. | ||
* 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. | ||
*/ | ||
@Cordova({ | ||
sync: true | ||
}) | ||
static open(url: string, target?: string, options?: string): InAppBrowserRef { return; } | ||
|
||
on(event: string): Observable<InAppBrowserEvent> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the purpose of this function? Is this generic and something we could abstract for other plugins? |
||
return new Observable<InAppBrowserEvent>((observer) => { | ||
this._objectInstance.addEventListener(event, observer.next.bind); | ||
return () => this._objectInstance.removeEventListener(event, observer.next.bind); | ||
}); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a quick comment on very basic usage of this? The instance method stuff is a bit new for people and we haven't talked a ton about it.