|
| 1 | +import { Plugin, CordovaInstance } from './plugin'; |
| 2 | +import { Observable } from 'rxjs/Observable'; |
| 3 | +import { InAppBrowserEvent } from './inappbrowser'; |
| 4 | +declare var cordova: any; |
| 5 | +/** |
| 6 | + * @name ThemableBrowser |
| 7 | + * @description |
| 8 | + * In-app browser that allows styling. |
| 9 | + * |
| 10 | + * @usage |
| 11 | + * ``` |
| 12 | + * import { ThemableBrowser } from 'ionic-native'; |
| 13 | + * |
| 14 | + * // can add options from the original InAppBrowser in a JavaScript object form (not string) |
| 15 | + * // This options object also takes additional parameters introduced by the ThemableBrowser plugin |
| 16 | + * // This example only shows the additional parameters for ThemableBrowser |
| 17 | + * // Note that that `image` and `imagePressed` values refer to resources that are stored in your app |
| 18 | + * let options = { |
| 19 | + * statusbar: { |
| 20 | + * color: '#ffffffff' |
| 21 | + * }, |
| 22 | + * toolbar: { |
| 23 | + * height: 44, |
| 24 | + * color: '#f0f0f0ff' |
| 25 | + * }, |
| 26 | + * title: { |
| 27 | + * color: '#003264ff', |
| 28 | + * showPageTitle: true |
| 29 | + * }, |
| 30 | + * backButton: { |
| 31 | + * image: 'back', |
| 32 | + * imagePressed: 'back_pressed', |
| 33 | + * align: 'left', |
| 34 | + * event: 'backPressed' |
| 35 | + * }, |
| 36 | + * forwardButton: { |
| 37 | + * image: 'forward', |
| 38 | + * imagePressed: 'forward_pressed', |
| 39 | + * align: 'left', |
| 40 | + * event: 'forwardPressed' |
| 41 | + * }, |
| 42 | + * closeButton: { |
| 43 | + * image: 'close', |
| 44 | + * imagePressed: 'close_pressed', |
| 45 | + * align: 'left', |
| 46 | + * event: 'closePressed' |
| 47 | + * }, |
| 48 | + * customButtons: [ |
| 49 | + * { |
| 50 | + * image: 'share', |
| 51 | + * imagePressed: 'share_pressed', |
| 52 | + * align: 'right', |
| 53 | + * event: 'sharePressed' |
| 54 | + * } |
| 55 | + * ], |
| 56 | + * menu: { |
| 57 | + * image: 'menu', |
| 58 | + * imagePressed: 'menu_pressed', |
| 59 | + * title: 'Test', |
| 60 | + * cancel: 'Cancel', |
| 61 | + * align: 'right', |
| 62 | + * items: [ |
| 63 | + * { |
| 64 | + * event: 'helloPressed', |
| 65 | + * label: 'Hello World!' |
| 66 | + * }, |
| 67 | + * { |
| 68 | + * event: 'testPressed', |
| 69 | + * label: 'Test!' |
| 70 | + * } |
| 71 | + * ] |
| 72 | + * }, |
| 73 | + * backButtonCanClose: true |
| 74 | + * }; |
| 75 | + * |
| 76 | + * let browser = new ThemeableBrowser('https://ionic.io', '_blank', options); |
| 77 | + * |
| 78 | + * ``` |
| 79 | + * We suggest that you refer to the plugin's repository for additional information on usage that may not be covered here. |
| 80 | + */ |
| 81 | +@Plugin({ |
| 82 | + plugin: 'cordova-plugin-themeablebrowser', |
| 83 | + pluginRef: 'cordova.ThemeableBrowser', |
| 84 | + repo: 'https://github.com/initialxy/cordova-plugin-themeablebrowser' |
| 85 | +}) |
| 86 | +export class ThemableBrowser { |
| 87 | + private _objectInstance: any; |
| 88 | + |
| 89 | + constructor(url: string, target: string, styleOptions: ThemeableBrowserOptions) { |
| 90 | + this._objectInstance = cordova.ThemableBrowser.open(arguments); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Displays an browser window that was opened hidden. Calling this has no effect |
| 95 | + * if the browser was already visible. |
| 96 | + */ |
| 97 | + @CordovaInstance({sync: true}) |
| 98 | + show(): void { } |
| 99 | + |
| 100 | + /** |
| 101 | + * Closes the browser window. |
| 102 | + */ |
| 103 | + @CordovaInstance({sync: true}) |
| 104 | + close(): void { } |
| 105 | + |
| 106 | + /** |
| 107 | + * Reloads the current page |
| 108 | + */ |
| 109 | + @CordovaInstance({ sync: true }) |
| 110 | + reload(): void { } |
| 111 | + |
| 112 | + /** |
| 113 | + * Injects JavaScript code into the browser window. |
| 114 | + * @param script Details of the script to run, specifying either a file or code key. |
| 115 | + */ |
| 116 | + @CordovaInstance() |
| 117 | + executeScript(script: {file?: string, code?: string}): Promise<any> {return; } |
| 118 | + |
| 119 | + /** |
| 120 | + * Injects CSS into the browser window. |
| 121 | + * @param css Details of the script to run, specifying either a file or code key. |
| 122 | + */ |
| 123 | + @CordovaInstance() |
| 124 | + insertCss(css: {file?: string, code?: string}): Promise<any> {return; } |
| 125 | + |
| 126 | + /** |
| 127 | + * A method that allows you to listen to events happening in the browser. |
| 128 | + * Available events are: `ThemableBrowserError`, `ThemableBrowserWarning`, `critical`, `loadfail`, `unexpected`, `undefined` |
| 129 | + * @param event Event name |
| 130 | + * @returns {Observable<any>} Returns back an observable that will listen to the event on subscribe, and will stop listening to the event on unsubscribe. |
| 131 | + */ |
| 132 | + on(event: string): Observable<InAppBrowserEvent> { |
| 133 | + return new Observable<InAppBrowserEvent>((observer) => { |
| 134 | + this._objectInstance.addEventListener(event, observer.next.bind(observer)); |
| 135 | + return () => this._objectInstance.removeEventListener(event, observer.next.bind(observer)); |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | +} |
| 140 | + |
| 141 | +export interface ThemeableBrowserOptions { |
| 142 | + statusbar?: { color: string; }; |
| 143 | + toobar?: { |
| 144 | + height?: number; |
| 145 | + color?: string; |
| 146 | + }; |
| 147 | + title?: { color: string; }; |
| 148 | + backButton?: ThemableBrowserButton; |
| 149 | + forwardButton?: ThemableBrowserButton; |
| 150 | + closeButton?: ThemableBrowserButton; |
| 151 | + customButtons?: ThemableBrowserButton[]; |
| 152 | + menu?: { |
| 153 | + image?: string; |
| 154 | + imagePressed?: string; |
| 155 | + title?: string; |
| 156 | + cancel?: string; |
| 157 | + align?: string; |
| 158 | + items?: { |
| 159 | + event: string; |
| 160 | + label: string; |
| 161 | + }[]; |
| 162 | + }; |
| 163 | + backButtonCanClose?: boolean; |
| 164 | + |
| 165 | + // inAppBrowser options |
| 166 | + location?: string; |
| 167 | + hidden?: string; |
| 168 | + clearcache?: string; |
| 169 | + clearsessioncache?: string; |
| 170 | + zoom?: string; |
| 171 | + hardwareback?: string; |
| 172 | + mediaPlaybackRequiresUserAction?: string; |
| 173 | + shouldPauseOnSuspsend?: string; |
| 174 | + closebuttoncaption?: string; |
| 175 | + disallowoverscroll?: string; |
| 176 | + enableViewportScale?: string; |
| 177 | + allowInlineMediaPlayback?: string; |
| 178 | + keyboardDisplayRequiresUserAction?: string; |
| 179 | + suppressesIncrementalRendering?: string; |
| 180 | + presentationstyle?: string; |
| 181 | + transitionstyle?: string; |
| 182 | + toolbarposition?: string; |
| 183 | + fullscreen?: string; |
| 184 | +} |
| 185 | + |
| 186 | +export interface ThemableBrowserButton { |
| 187 | + wwwImage?: string; |
| 188 | + image?: string; |
| 189 | + wwwImagePressed?: string; |
| 190 | + imagePressed?: string; |
| 191 | + wwwImageDensity?: number; |
| 192 | + align?: string; |
| 193 | + event?: string; |
| 194 | +} |
0 commit comments