From 69e7d7c69f6052fec4c8d75d9fbe98147f833fcc Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Fri, 26 Jun 2015 10:49:03 -0500 Subject: [PATCH] Platform.ready() for excellence --- ionic/components/app/test/snapcat/index.js | 6 +- ionic/components/modal/test/basic/index.js | 4 ++ ionic/platform/platform.js | 64 +++++++++++++++++++--- ionic/platform/registry.js | 11 ++-- ionic/util/dom.js | 28 +--------- ionic/util/render/dom.js | 24 -------- ionic/util/render/element.js | 15 ----- 7 files changed, 68 insertions(+), 84 deletions(-) delete mode 100644 ionic/util/render/dom.js delete mode 100644 ionic/util/render/element.js diff --git a/ionic/components/app/test/snapcat/index.js b/ionic/components/app/test/snapcat/index.js index 146b2d2cb8..700ccc06a2 100644 --- a/ionic/components/app/test/snapcat/index.js +++ b/ionic/components/app/test/snapcat/index.js @@ -121,8 +121,7 @@ class FadeIn extends Animation { this .easing('ease') .duration(250) - .fadeIn() - .before.addClass('show-modal'); + .fadeIn(); } } @@ -134,8 +133,7 @@ class FadeOut extends Animation { this .easing('ease') .duration(250) - .fadeOut() - .after.removeClass('show-modal'); + .fadeOut(); } } diff --git a/ionic/components/modal/test/basic/index.js b/ionic/components/modal/test/basic/index.js index 828d37e4c3..75e1a70847 100644 --- a/ionic/components/modal/test/basic/index.js +++ b/ionic/components/modal/test/basic/index.js @@ -175,6 +175,10 @@ export function main(ionicBootstrap) { console.log('isRTL', app.isRTL()) console.log('lang', app.lang()) + Platform.ready().then(() => { + console.log('Platform.ready') + }); + }); } diff --git a/ionic/platform/platform.js b/ionic/platform/platform.js index f364b9dce2..daa08d6276 100644 --- a/ionic/platform/platform.js +++ b/ionic/platform/platform.js @@ -1,4 +1,5 @@ import * as util from '../util/util'; +import * as dom from '../util/dom'; export class PlatformCtrl { @@ -10,6 +11,29 @@ export class PlatformCtrl { this._default = null; } + + // Methods + // ********************************************** + + ready() { + // no ready method was provided by an engine + // fallback to use dom ready instead + // if a ready method was provide then it would + // override the default method + return dom.ready(); + } + + domReady() { + return dom.ready(); + } + + windowLoad() { + return dom.windowLoad(); + } + + // Properties + // ********************************************** + url(val) { if (arguments.length) { this._url = val; @@ -49,14 +73,6 @@ export class PlatformCtrl { } } - isPlatform(queryValue, userAgentExpression) { - if (!userAgentExpression) { - userAgentExpression = queryValue; - } - return (this.matchQuery(queryValue)) || - (this.matchUserAgent(userAgentExpression) !== null); - } - width(val) { if (arguments.length) { this._w = val; @@ -71,6 +87,10 @@ export class PlatformCtrl { return this._h || 0; } + + // Registry + // ********************************************** + register(platformConfig) { this._registry[platformConfig.name] = platformConfig; } @@ -83,6 +103,14 @@ export class PlatformCtrl { this._default = platformName; } + isPlatform(queryValue, userAgentExpression) { + if (!userAgentExpression) { + userAgentExpression = queryValue; + } + return (this.matchQuery(queryValue)) || + (this.matchUserAgent(userAgentExpression) !== null); + } + load() { let rootPlatformNode = null; let engineNode = null; @@ -128,6 +156,10 @@ export class PlatformCtrl { engineNode.child(rootPlatformNode); rootPlatformNode.parent(engineNode); rootPlatformNode = engineNode; + + // add any events which the engine would provide + // for example, Cordova provides its own ready event + util.extend(this, engineNode.methods()); } let platformNode = rootPlatformNode; @@ -136,6 +168,14 @@ export class PlatformCtrl { platformNode = platformNode.child(); } + // make sure the root noot is actually the root + // incase a node was inserted before the root + platformNode = rootPlatformNode.parent(); + while (platformNode) { + rootPlatformNode = platformNode; + platformNode = platformNode.parent(); + } + platformNode = rootPlatformNode; while (platformNode) { // set the array of active platforms with @@ -202,7 +242,9 @@ function insertSuperset(platformNode) { let supersetPlatform = new PlatformNode(supersetPlaformName); supersetPlatform.parent(platformNode.parent()); supersetPlatform.child(platformNode); - supersetPlatform.parent().child(supersetPlatform); + if (supersetPlatform.parent()) { + supersetPlatform.parent().child(supersetPlatform); + } platformNode.parent(supersetPlatform); } } @@ -227,6 +269,10 @@ class PlatformNode { return this.c.superset; } + methods() { + return this.c.methods || {}; + } + parent(val) { if (arguments.length) { this._parent = val; diff --git a/ionic/platform/registry.js b/ionic/platform/registry.js index 3d01ad1f09..2e6f558061 100644 --- a/ionic/platform/registry.js +++ b/ionic/platform/registry.js @@ -121,16 +121,15 @@ Platform.register({ name: 'cordova', isEngine: true, methods: { - onReady: function() { - return new Promise(resolve => { - setTimeout(function() { - resolve(); - }, 1000); + ready: function() { + return Platform.windowLoad().then(() => { + return new Promise(resolve => { + document.addEventListener("deviceready", resolve); + }); }); } }, isMatch(p) { - return true; return !!(window.cordova || window.PhoneGap || window.phonegap); } }); diff --git a/ionic/util/dom.js b/ionic/util/dom.js index 70b0af68b3..e756ea338b 100644 --- a/ionic/util/dom.js +++ b/ionic/util/dom.js @@ -29,8 +29,6 @@ export function rafPromise() { return new Promise(resolve => raf(resolve)); } -export const isSVG = val => window.SVGElement && (val instanceof window.SVGElement); - export let CSS = {}; (function() { // transform @@ -116,7 +114,7 @@ function cssPromise(el:Element, eventNames, animationName) { export function ready() { return new Promise(resolve => { if (document.readyState === 'complete' || document.readyState === 'interactive') { - setTimeout(resolve); + resolve(); } else { @@ -135,7 +133,7 @@ export function ready() { export function windowLoad() { return new Promise(resolve => { if (document.readyState === 'complete') { - setTimeout(resolve); + resolve(); } else { function completed() { @@ -147,25 +145,3 @@ export function windowLoad() { } }); } - -export function hasAttribute(el: Element, attributeName) { - return el.hasAttribute(attributeName); -} - -export function addClass(el: Element, ...classNames) { - for (let c of classNames) { - el.classList.add(c); - } -} - -export function getChildIndex(el: Element) { - let child; - let parent = el.parentNode; - for(let i = 0, j = parent.children.length; i < j; i++) { - child = parent.children[i]; - if(child === el) { - return i; - } - } - return -1; -} diff --git a/ionic/util/render/dom.js b/ionic/util/render/dom.js deleted file mode 100644 index 6819d6194b..0000000000 --- a/ionic/util/render/dom.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * TODO: Wait until the new ElementRef stuff lands in Angular2. -import {RenderedElement} from 'ionic/util/render/element'; - -export class DomRenderedElement extends RenderedElement { - constructor(domElement: Element) { - super(domElement) - } - - removeClass(className) { - this.element.classList.remove(classList); - } - addClass(...classNames) { - for(let c of classNames) { - this.element.classList.add(c); - } - } - removeClass(...classNames) { - for(let c of classes) { - this.element.classList.remove(c); - } - } -} -*/ diff --git a/ionic/util/render/element.js b/ionic/util/render/element.js deleted file mode 100644 index 81fb308e3b..0000000000 --- a/ionic/util/render/element.js +++ /dev/null @@ -1,15 +0,0 @@ -/** - * TODO: Wait until the new ElementRef stuff lands in Angular2 -export class RenderedElement { - constructor(element) { - this.element = element; -} - _notImplemented(fnName) { - console.error("RenderedElement." + fnName + "addClass is not implemented. Use a concrete class like DomRenderedElement instead."); - } - addClass(...classNames) { this._notImplemented('addClass'); } - removeClass(className) { this._notImplemented('removeClass'); } - removeClasses(...classNames) { this._notImplemented('removeClasses'); } -} - - */