From 8cb701a3da67a4d2cde4fb6a60e5a88f8d4ad2bb Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Mon, 4 Nov 2013 11:52:57 -0600 Subject: [PATCH] Added platform provider. Fixes #67 Now you can easily detect the running platform and wait for the device to be ready even if that ready even already fired. --- dist/js/ionic-angular.js | 78 ++++++++++++++++++- js/ext/angular/src/directive/ionicPlatform.js | 67 ++++++++++++++++ js/ext/angular/src/ionicAngular.js | 10 ++- 3 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 js/ext/angular/src/directive/ionicPlatform.js diff --git a/dist/js/ionic-angular.js b/dist/js/ionic-angular.js index 29a856e710..ff695210df 100644 --- a/dist/js/ionic-angular.js +++ b/dist/js/ionic-angular.js @@ -2,15 +2,21 @@ * Create a wrapping module to ease having to include too many * modules. */ -angular.module('ionic.ui', ['ionic.ui.content', + +angular.module('ionic.ui', [ + 'ionic.ui.content', 'ionic.ui.tabs', 'ionic.ui.nav', 'ionic.ui.sideMenu', 'ionic.ui.list', 'ionic.ui.checkbox', - 'ionic.ui.toggle' + 'ionic.ui.toggle', ]); +angular.module('ionic', [ + 'ionic.platform', + 'ionic.ui' +]) ; angular.module('ionic.service.actionSheet', ['ionic.service.templateLoad', 'ionic.ui.actionSheet']) @@ -736,6 +742,74 @@ angular.module('ionic.ui.nav', ['ionic.service.templateLoad', 'ionic.service.ges })(); ; +(function() { +'use strict'; + +angular.module('ionic.platform', []) + +/** + * The platformProvider makes it easy to set and detect which platform + * the app is currently running on. It has some auto detection built in + * for PhoneGap and Cordova. This provider also takes care of + * initializing some defaults that depend on the platform, such as the + * height of header bars on iOS 7. + */ +.provider('platform', function() { + var platform = 'unknown'; + var isPlatformReady = false; + + if(window.cordova || window.PhoneGap || window.phonegap) { + platform = 'cordova'; + } + + console.log('Detected platform', platform); + + var isReady = function() { + if(platform == 'cordova') { + return window.device; + } + return true; + }; + + // We need to do some stuff as soon as we know the platform, + // like adjust header margins for iOS 7, etc. + setTimeout(function afterReadyWait() { + if(isReady()) { + ionic.Platform.detect(); + } else { + setTimeout(afterReadyWait, 50); + } + }, 10); + + return { + setPlatform: function(p) { + platform = p; + }, + $get: ['$q', '$timeout', function($q, $timeout) { + return { + ready: function(cb) { + var self = this; + var q = $q.defer(); + + $timeout(function readyWait() { + if(isReady()) { + isPlatformReady = true; + q.resolve(); + cb(); + } else { + $timeout(readyWait, 50); + } + }, 50); + + return q.promise; + } + }; + }] + }; +}); + +})(ionic); +; ; (function() { 'use strict'; diff --git a/js/ext/angular/src/directive/ionicPlatform.js b/js/ext/angular/src/directive/ionicPlatform.js new file mode 100644 index 0000000000..10771a986a --- /dev/null +++ b/js/ext/angular/src/directive/ionicPlatform.js @@ -0,0 +1,67 @@ +(function() { +'use strict'; + +angular.module('ionic.platform', []) + +/** + * The platformProvider makes it easy to set and detect which platform + * the app is currently running on. It has some auto detection built in + * for PhoneGap and Cordova. This provider also takes care of + * initializing some defaults that depend on the platform, such as the + * height of header bars on iOS 7. + */ +.provider('platform', function() { + var platform = 'unknown'; + var isPlatformReady = false; + + if(window.cordova || window.PhoneGap || window.phonegap) { + platform = 'cordova'; + } + + console.log('Detected platform', platform); + + var isReady = function() { + if(platform == 'cordova') { + return window.device; + } + return true; + }; + + // We need to do some stuff as soon as we know the platform, + // like adjust header margins for iOS 7, etc. + setTimeout(function afterReadyWait() { + if(isReady()) { + ionic.Platform.detect(); + } else { + setTimeout(afterReadyWait, 50); + } + }, 10); + + return { + setPlatform: function(p) { + platform = p; + }, + $get: ['$q', '$timeout', function($q, $timeout) { + return { + ready: function(cb) { + var self = this; + var q = $q.defer(); + + $timeout(function readyWait() { + if(isReady()) { + isPlatformReady = true; + q.resolve(); + cb(); + } else { + $timeout(readyWait, 50); + } + }, 50); + + return q.promise; + } + }; + }] + }; +}); + +})(ionic); diff --git a/js/ext/angular/src/ionicAngular.js b/js/ext/angular/src/ionicAngular.js index 444a54c634..23c10eabfb 100644 --- a/js/ext/angular/src/ionicAngular.js +++ b/js/ext/angular/src/ionicAngular.js @@ -2,12 +2,18 @@ * Create a wrapping module to ease having to include too many * modules. */ -angular.module('ionic.ui', ['ionic.ui.content', + +angular.module('ionic.ui', [ + 'ionic.ui.content', 'ionic.ui.tabs', 'ionic.ui.nav', 'ionic.ui.sideMenu', 'ionic.ui.list', 'ionic.ui.checkbox', - 'ionic.ui.toggle' + 'ionic.ui.toggle', ]); +angular.module('ionic', [ + 'ionic.platform', + 'ionic.ui' +])