diff --git a/src/components/tabs/test/basic/main.js b/src/components/tabs/test/basic/main.js index 7e020f891a..571d8958b3 100644 --- a/src/components/tabs/test/basic/main.js +++ b/src/components/tabs/test/basic/main.js @@ -1,6 +1,7 @@ import {bootstrap} from 'angular2/core'; import {Component, Template} from 'angular2/angular2'; import {View, Tabs, Tab} from 'ionic2/components'; +import {webview} from 'ionic2/webview/webview' @Component({ selector: '[ion-app]' }) @@ -11,7 +12,11 @@ import {View, Tabs, Tab} from 'ionic2/components'; class IonicApp { constructor() { console.log('IonicApp Start') + + console.log(webview.name, webview.isWebView) } } + + bootstrap(IonicApp) diff --git a/src/ionic2.js b/src/ionic2.js index 5eaa385732..f6d17ad60b 100644 --- a/src/ionic2.js +++ b/src/ionic2.js @@ -1,2 +1,4 @@ export * from 'ionic2/components' +export * from 'ionic2/platform/platform' +export * from 'ionic2/webview/webview' diff --git a/src/platform/detect.js b/src/platform/detect.js deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/platform/platform.js b/src/platform/platform.js index ef7ac6f36a..9d81b50ee0 100644 --- a/src/platform/platform.js +++ b/src/platform/platform.js @@ -21,6 +21,7 @@ class PlatformController { get() { return this.current } + getName() { return this.current && this.current.name } @@ -43,7 +44,7 @@ class PlatformController { detect() { for (let name in this.registry) { - if (this.registry[name].matcher()) { + if (this.registry[name].isMatch()) { return this.registry[name] } } @@ -59,14 +60,13 @@ var ua = window.navigator.userAgent // TODO(ajoslin): move these to their own files platform.register({ name: 'android', - matcher() { + isMatch() { return queryPlatform == 'android' || /android/i.test(ua) } }) platform.register({ name: 'ios', - // For now always default to ios - matcher() { + isMatch() { return queryPlatform === 'ios' || /ipad|iphone|ipod/i.test(ua) } }) diff --git a/src/util/util.js b/src/util/util.js index b60836a834..3ad60d8688 100644 --- a/src/util/util.js +++ b/src/util/util.js @@ -30,7 +30,8 @@ export function defaults(dest) { export const isString = val => typeof val === 'string' export const isFunction = val => typeof val === 'function' -export const isDefined = val => typeof val === 'undefined' +export const isDefined = val => typeof val !== 'undefined' +export const isUndefined = val => typeof val === 'undefined' export const isObject = val => typeof val === 'object' export const isArray = Array.isArray diff --git a/src/webview/webview.js b/src/webview/webview.js index 188fd15ad5..11420f504a 100644 --- a/src/webview/webview.js +++ b/src/webview/webview.js @@ -22,3 +22,97 @@ // webview: returns cordova, trigger.io, browser // isWebview: true if its cordova, trigger. False if its browser + +import * as util from 'ionic2/util' + +class WebView { + constructor(options) { + util.extend(this, options) + } +} + +let registry = {} + +class WebViewController { + current: WebView; + + constructor() { + let defaultProperties = { + name: null, + isWebView: false + } + for (let target in defaultProperties) { + this.__defineGetter__(target, () => { + return this.proxy(target, null, defaultProperties[target]) + }) + } + + let defaultMethods = { + exitApp: util.noop, + showStatusBar: util.noop, + fullScreen: util.noop + } + for (let target in defaultMethods) { + this[target] = () => { + return this.proxy(target, null, defaultMethods[target]) + } + } + } + + get() { + if (util.isUndefined(this.current)) { + this.set(this.detect()) + } + return this.current + } + + set(webview) { + this.current = webview + } + + detect() { + for (let name in registry) { + if (registry[name].isMatch()) { + return registry[name] + } + } + return null + } + + proxy(target, args, fallback) { + let webview = this.get() + if (webview && webview[target]) { + if (util.isFunction(webview[target])) { + return webview[target].apply(this, args) + } + return webview[target] + } + return fallback + } + + register(webview) { + if (!webview instanceof WebView) webview = new WebView(webview) + registry[webview.name] = webview + } + +} + +export let webview = new WebViewController() + +webview.register({ + name: 'cordova', + isMatch() { + return true;//util.isDefined(window.cordova) + } +}) +webview.register({ + name: 'node-webkit', + isMatch() { + // if (util.isDefined(process) && util.isDefined(require)) { + // try { + // return util.isDefined(require('nw.gui')); + // } catch (e) {} + // } + return false + } +})