From f4b8236c8da2bee6c13f6af67bfb4d96bc2d3162 Mon Sep 17 00:00:00 2001 From: Eric Horodyski Date: Wed, 15 Feb 2017 16:57:16 -0500 Subject: [PATCH] feat(inappbrowser): add interface for IAB options (#1065) * Add InAppBrowserOptions Interface for better tooling. * feat(inappbrowser): add interface for IAB options * Add more constructor tests. * Add missing iOS options. --- src/plugins/inappbrowser.ts | 60 +++++++++++++++++++++++++++---- test/plugins/inappbrowser.spec.ts | 30 ++++++++++++++++ 2 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 test/plugins/inappbrowser.spec.ts diff --git a/src/plugins/inappbrowser.ts b/src/plugins/inappbrowser.ts index 15e377611..c530d7c44 100644 --- a/src/plugins/inappbrowser.ts +++ b/src/plugins/inappbrowser.ts @@ -3,6 +3,50 @@ import { Observable } from 'rxjs/Observable'; declare var cordova: any; +export interface InAppBrowserOptions { + /** Set to yes or no to ruen the InAppBrowser's location bar on or off. */ + location?: 'yes' | 'no'; + /** Set to yes to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. + * Omit or set to no (default) to have the browser open and load normally. */ + hidden?: 'yes' | 'no'; + /** Set to yes to have the browser's cookie cache cleared before the new window is opened. */ + clearcache?: 'yes'; + /** Set to yes to have the session cookie cache cleared before the new window is opened. */ + clearsessioncache?: 'yes'; + /** (Android Only) set to yes to show Android browser's zoom controls, set to no to hide them. Default value is yes. */ + zoom?: 'yes' | 'no'; + /** Set to yes to use the hardware back button to navigate backwards through the InAppBrowser's history. + * If there is no previous page, the InAppBrowser will close. The default value is yes, so you must set it to no if you want the back button to simply close the InAppBrowser. */ + hardwareback?: 'yes' | 'no'; + /** Set to yes to prevent HTML5 audio or video from autoplaying (defaults to no). */ + mediaPlaybackRequiresUserAction?: 'yes' | 'no'; + /** (Android Only) Set to yes to make InAppBrowser WebView to pause/resume with the app to stop background audio (this may be required to avoid Google Play issues) */ + shouldPauseOnSuspend?: 'yes' | 'no'; + /** (iOS Only) Set to a string to use as the Done button's caption. Note that you need to localize this value yourself. */ + closebuttoncaption?: string; + /** (iOS Only) Set to yes or no (default is no). Turns on/off the UIWebViewBounce property. */ + disallowoverscroll?: 'yes' | 'no'; + /** (iOS Only) Set to yes or no to turn the toolbar on or off for the InAppBrowser (defaults to yes) */ + toolbar?: 'yes' | 'no'; + /** (iOS Only) Set to yes or no to prevent viewport scaling through a meta tag (defaults to no). */ + enableViewportScale?: 'yes' | 'no'; + /** (iOS Only) Set to yes or no to allow in-line HTML5 media playback, displaying within the browser window rather than a device-specific playback interface. + * The HTML's video element must also include the webkit-playsinline attribute (defaults to no) */ + allowInlineMediaPlayback?: 'yes' | 'no'; + /** (iOS Only) Set to yes or no to open the keyboard when form elements receive focus via JavaScript's focus() call (defaults to yes). */ + keyboardDisplayRequiresUserAction?: 'yes' | 'no'; + /** (iOS Only) Set to yes or no to wait until all new view content is received before being rendered (defaults to no). */ + suppressesIncrementalRendering?: 'yes' | 'no'; + /** (iOS Only) Set to pagesheet, formsheet or fullscreen to set the presentation style (defaults to fullscreen). */ + presentationstyle?: 'pagesheet' | 'formsheet' | 'fullscreen'; + /** (iOS Only) Set to fliphorizontal, crossdissolve or coververtical to set the transition style (defaults to coververtical). */ + transitionstyle?: 'fliphorizontal' | 'crossdissolve' | 'coververtical'; + /** (iOS Only) Set to top or bottom (default is bottom). Causes the toolbar to be at the top or bottom of the window. */ + toolbarposition?: 'top' | 'bottom'; + /** (Windows only) Set to yes to create the browser control without a border around it. + * Please note that if location=no is also specified, there will be no control presented to user to close IAB window. */ + fullscreen?: 'yes'; +} export interface InAppBrowserEvent extends Event { /** the eventname, either loadstart, loadstop, loaderror, or exit. */ type: string; @@ -50,8 +94,12 @@ export class InAppBrowser { * 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. */ - constructor(url: string, target?: string, options?: string) { + constructor(url: string, target?: string, options?: InAppBrowserOptions); + constructor(url: string, target?: string, options?: string); + constructor(url: string, target?: string, options?: string | InAppBrowserOptions) { try { + if (options && typeof options !== 'string') + options = Object.keys(options).map(key => `${key}=${options[key]}`).join(','); this._objectInstance = cordova.InAppBrowser.open(url, target, options); } catch (e) { window.open(url); @@ -63,20 +111,20 @@ export class InAppBrowser { * Displays an InAppBrowser window that was opened hidden. Calling this has no effect * if the InAppBrowser was already visible. */ - @CordovaInstance({sync: true}) + @CordovaInstance({ sync: true }) show(): void { } /** * Closes the InAppBrowser window. */ - @CordovaInstance({sync: true}) + @CordovaInstance({ sync: true }) close(): void { } /** * Hides an InAppBrowser window that is currently shown. Calling this has no effect * if the InAppBrowser was already hidden. */ - @CordovaInstance({sync: true}) + @CordovaInstance({ sync: true }) hide(): void { } /** @@ -85,7 +133,7 @@ export class InAppBrowser { * @returns {Promise} */ @CordovaInstance() - executeScript(script: {file?: string, code?: string}): Promise {return; } + executeScript(script: { file?: string, code?: string }): Promise { return; } /** * Injects CSS into the InAppBrowser window. @@ -93,7 +141,7 @@ export class InAppBrowser { * @returns {Promise} */ @CordovaInstance() - insertCSS(css: {file?: string, code?: string}): Promise {return; } + insertCSS(css: { file?: string, code?: string }): Promise { return; } /** * A method that allows you to listen to events happening in the browser. diff --git a/test/plugins/inappbrowser.spec.ts b/test/plugins/inappbrowser.spec.ts new file mode 100644 index 000000000..9c7c3eee6 --- /dev/null +++ b/test/plugins/inappbrowser.spec.ts @@ -0,0 +1,30 @@ +import { InAppBrowser, InAppBrowserEvent, InAppBrowserOptions } from '../../src/plugins/inappbrowser'; + +declare var window: any; + +window.cordova = { + InAppBrowser: { + open: window.open + } +}; + +describe('InAppBrowser', () => { + + const options: InAppBrowserOptions = { hidden: 'yes', hardwareback: 'no' }; + let object; + + it('should create an object using strings and InAppBrowserOptions signature', () => { + object = new InAppBrowser('http://google.com', '_self', options); + expect(object).toBeDefined(); + }); + + it('should create an object using string only signature', () => { + object = new InAppBrowser('http://google.com', '_self', 'location=no'); + expect(object).toBeDefined(); + }); + + it('should create an object with the least amount of parameters', () => { + object = new InAppBrowser('http://google.com'); + expect(object).toBeDefined(); + }); +});