webview stuff

This commit is contained in:
Adam Bradley
2015-04-21 09:20:11 -05:00
parent 25adc1d1f8
commit 241163eb9f
6 changed files with 169 additions and 86 deletions

View File

@@ -1,7 +1,10 @@
import {bootstrap} from 'angular2/core';
import {Component, Template} from 'angular2/angular2';
import {View, Tabs, Tab} from 'ionic2/components';
import {bootstrap} from 'angular2/core'
import {Component, Template} from 'angular2/angular2'
import {View, Tabs, Tab} from 'ionic2/components'
import {webview} from 'ionic2/webview/webview'
import {cordova} from 'ionic2/webview/cordova/cordova'
import {nodeWebkit} from 'ionic2/webview/node-webkit/node-webkit'
import * as util from 'ionic2/util'
@Component({ selector: '[ion-app]' })
@@ -13,10 +16,21 @@ class IonicApp {
constructor() {
console.log('IonicApp Start')
console.log(webview.name, webview.isWebView)
console.log(webview.getName(), webview.is('cordova'))
webview.ready().then(() => {
console.log('webviewready')
})
webview.fullScreen(true).then((isShown) => {
console.log('fullScreen', isShown)
})
webview.showStatusBar(true).then((isShown) => {
console.log('showStatusBar', isShown)
})
}
}
bootstrap(IonicApp)

View File

@@ -2,3 +2,5 @@
export * from 'ionic2/components'
export * from 'ionic2/platform/platform'
export * from 'ionic2/webview/webview'
export * from 'ionic2/webview/cordova/cordova'
export * from 'ionic2/webview/node-webkit/node-webkit'

View File

@@ -6,7 +6,7 @@ const nativeCancelRaf = window.cancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
window.webkitCancelRequestAnimationFrame
export const raf = nativeRaf || function(callback) {
export const raf = nativeRaf || function(callback) {
return window.setTimeout(callback, 16.6667)
}
export const rafCancel = nativeRaf ? nativeCancelRaf : function(id) {
@@ -34,7 +34,7 @@ if (window.ontransitionend === undefined && window.onwebkittransitionend !== und
}
export function transitionEndPromise(el:Element) {
return new Promise(resolve => {
return new Promise(resolve => {
css.transitionEnd.split(' ').forEach(eventName => {
el.addEventListener(eventName, onTransitionEnd)
})
@@ -50,3 +50,39 @@ export function transitionEndPromise(el:Element) {
}
})
}
export function ready() {
return new Promise(resolve => {
if (document.readyState === 'complete' || document.readyState === 'interactive') {
setTimeout(resolve)
} else {
function completed() {
resolve()
document.removeEventListener('DOMContentLoaded', completed, false)
window.removeEventListener('load', completed, false)
}
document.addEventListener('DOMContentLoaded', completed, false)
window.addEventListener('load', completed, false)
}
})
}
export function windowLoad() {
return new Promise(resolve => {
if (document.readyState === 'complete') {
setTimeout(resolve)
} else {
function completed() {
resolve()
window.removeEventListener('load', completed, false)
}
window.addEventListener('load', completed, false)
}
})
}

View File

@@ -0,0 +1,32 @@
import {webview} from '../webview'
webview.register({
name: 'cordova',
isMatch() {
return !(!window.cordova && !window.PhoneGap && !window.phonegap)
},
ready() {
return new Promise(resolve => {
setTimeout(resolve, 1000)
})
},
fullScreen(shouldShow) {
return new Promise(resolve => {
setTimeout(function() {
resolve(shouldShow)
}, 1000)
})
},
showStatusBar(shouldShow) {
return new Promise(resolve => {
setTimeout(function() {
resolve(shouldShow)
}, 1000)
})
},
exitApp() {
return new Promise(resolve => {
setTimeout(resolve, 1000)
})
}
})

View File

@@ -0,0 +1,28 @@
import {webview} from '../webview'
webview.register({
name: 'node-webkit',
isMatch() {
try {
return util.isDefined(process) && util.isDefined(require) && util.isDefined(require('nw.gui'))
} catch (e) {}
return false
},
ready() {
return new Promise(resolve => {
setTimeout(resolve, 1000)
})
},
fullScreen(shouldShow) {
return new Promise(resolve => {
setTimeout(function() {
resolve(shouldShow)
}, 1000)
})
},
exitApp() {
return new Promise(resolve => {
setTimeout(resolve, 1000)
})
}
})

View File

@@ -1,28 +1,4 @@
// platformReady: Cordova said it's ready
// domReady: DOM is ready
// ready: if cordova, it's the same as platformReady, if browser, same as dom ready
// windowLoad: All scripts have been loaded, window ready
// fullScreen
// showStatusBar
// exitApp
// is: is('ios') type of check
// platformName: returns ios
// platformVersion: returns 8.2
// webview: returns cordova, trigger.io, browser
// isWebview: true if its cordova, trigger. False if its browser
import * as util from 'ionic2/util'
class WebView {
@@ -32,42 +8,63 @@ class WebView {
}
let registry = {}
let defaultWebView;
let activeWebView;
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])
let self = this
let proxyMethods = 'ready fullScreen showStatusBar exitApp'.split(' ')
for (let x = 0; x < proxyMethods.length; x++) {
this[proxyMethods[x]] = function() {
return self.proxy(proxyMethods[x], arguments)
}
}
}
proxy(target, args) {
let webview = this.get()
if (webview && webview[target]) {
return webview[target].apply(this, args)
}
return new Promise(resolve => {}, reject => {
reject()
})
}
is(name) {
return this.getName() === name
}
isWebView() {
return !!this.get().isWebView
}
getName() {
return this.get().name
}
get() {
if (util.isUndefined(this.current)) {
if (util.isUndefined(activeWebView)) {
this.set(this.detect())
}
return this.current
return activeWebView || defaultWebView
}
set(webview) {
this.current = webview
activeWebView = webview
}
setDefault(webview) {
if (!webview instanceof WebView) webview = new WebView(webview)
defaultWebView = webview
}
register(webview) {
if (!webview instanceof WebView) webview = new WebView(webview)
webview.isWebView = true
registry[webview.name] = webview
}
detect() {
@@ -79,40 +76,14 @@ class WebViewController {
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
}
webview.setDefault({
name: 'default',
ready: util.dom.windowLoad
})