Files
ionic-framework/js/utils/platform.js
Adam Bradley 5c300dd3e0 refactor(platform): Rename isCordova() to isWebView()
BREAKING CHANGE: ionic.Platform.isCordova() has been renamed to
ionic.Platform.isWebView()
2014-04-07 11:56:11 -05:00

337 lines
10 KiB
JavaScript

(function(window, document, ionic) {
/**
* @ngdoc utility
* @name ionic.Platform
* @module ionic
*/
ionic.Platform = {
/**
* @ngdoc property
* @name ionic.Platform#isReady
* @returns {boolean} Whether the device is ready.
*/
isReady: false,
/**
* @ngdoc property
* @name ionic.Platform#isFullScreen
* @returns {boolean} Whether the device is fullscreen.
*/
isFullScreen: false,
/**
* @ngdoc property
* @name ionic.Platform#platforms
* @returns {Array(string)} An array of all platforms found.
*/
platforms: null,
/**
* @ngdoc property
* @name ionic.Platform#grade
* @returns {string} What grade the current platform is.
*/
grade: null,
ua: navigator.userAgent,
/**
* @ngdoc method
* @name ionic.Platform#ready
* @description
* Trigger a callback once the device is ready, or immediately
* if the device is already ready. This method can be run from
* anywhere and does not need to be wrapped by any additonal methods.
* When the app is within a WebView (Cordova), it'll fire
* the callback once the device is ready. If the app is within
* a web browser, it'll fire the callback after `window.load`.
* @param {function} callback The function to call.
*/
ready: function(cb) {
// run through tasks to complete now that the device is ready
if(this.isReady) {
cb();
} else {
// the platform isn't ready yet, add it to this array
// which will be called once the platform is ready
readyCallbacks.push(cb);
}
},
/**
* @private
*/
detect: function() {
ionic.Platform._checkPlatforms();
ionic.requestAnimationFrame(function(){
// only add to the body class if we got platform info
for(var i = 0; i < ionic.Platform.platforms.length; i++) {
document.body.classList.add('platform-' + ionic.Platform.platforms[i]);
}
document.body.classList.add('grade-' + ionic.Platform.grade);
});
},
/**
* @ngdoc method
* @name ionic.Platform#device
* @description Return the current device (given by cordova).
* @returns {object} The device object.
*/
device: function() {
if(window.device) return window.device;
if(this.isWebView()) console.error('device plugin required');
return {};
},
_checkPlatforms: function(platforms) {
this.platforms = [];
this.grade = 'a';
if(this.isWebView()) {
this.platforms.push('webview');
this.platforms.push('cordova');
}
if(this.isIPad()) this.platforms.push('ipad');
var platform = this.platform();
if(platform) {
this.platforms.push(platform);
var version = this.version();
if(version) {
var v = version.toString();
if(v.indexOf('.') > 0) {
v = v.replace('.', '_');
} else {
v += '_0';
}
this.platforms.push(platform + v.split('_')[0]);
this.platforms.push(platform + v);
if(this.isAndroid() && version < 4.4) {
this.grade = (version < 4 ? 'c' : 'b');
}
}
}
},
/**
* @ngdoc method
* @name ionic.Platform#isWebView
* @returns {boolean} Check if we are running within a WebView (such as Cordova).
*/
isWebView: function() {
return !(!window.cordova && !window.PhoneGap && !window.phonegap);
},
/**
* @ngdoc method
* @name ionic.Platform#isIPad
* @returns {boolean} Whether we are running on iPad.
*/
isIPad: function() {
return this.ua.toLowerCase().indexOf('ipad') >= 0;
},
/**
* @ngdoc method
* @name ionic.Platform#isIOS
* @returns {boolean} Whether we are running on iOS.
*/
isIOS: function() {
return this.is('ios');
},
/**
* @ngdoc method
* @name ionic.Platform#isAndroid
* @returns {boolean} Whether we are running on Android.
*/
isAndroid: function() {
return this.is('android');
},
/**
* @ngdoc method
* @name ionic.Platform#platform
* @returns {string} The name of the current platform.
*/
platform: function() {
// singleton to get the platform name
if(platformName === null) this.setPlatform(this.device().platform);
return platformName;
},
/**
* @private
*/
setPlatform: function(n) {
if(typeof n != 'undefined' && n !== null && n.length) {
platformName = n.toLowerCase();
} else if(this.ua.indexOf('Android') > 0) {
platformName = 'android';
} else if(this.ua.indexOf('iPhone') > -1 || this.ua.indexOf('iPad') > -1 || this.ua.indexOf('iPod') > -1) {
platformName = 'ios';
} else {
platformName = '';
}
},
/**
* @ngdoc method
* @name ionic.Platform#version
* @returns {string} The version of the current device platform.
*/
version: function() {
// singleton to get the platform version
if(platformVersion === null) this.setVersion(this.device().version);
return platformVersion;
},
/**
* @private
*/
setVersion: function(v) {
if(typeof v != 'undefined' && v !== null) {
v = v.split('.');
v = parseFloat(v[0] + '.' + (v.length > 1 ? v[1] : 0));
if(!isNaN(v)) {
platformVersion = v;
return;
}
}
platformVersion = 0;
// fallback to user-agent checking
var pName = this.platform();
var versionMatch = {
'android': /Android (\d+).(\d+)?/,
'ios': /OS (\d+)_(\d+)?/
};
if(versionMatch[pName]) {
v = this.ua.match( versionMatch[pName] );
if(v.length > 2) {
platformVersion = parseFloat( v[1] + '.' + v[2] );
}
}
},
// Check if the platform is the one detected by cordova
is: function(type) {
type = type.toLowerCase();
// check if it has an array of platforms
if(this.platforms) {
for(var x = 0; x < this.platforms.length; x++) {
if(this.platforms[x] === type) return true;
}
}
// exact match
var pName = this.platform();
if(pName) {
return pName === type.toLowerCase();
}
// A quick hack for to check userAgent
return this.ua.toLowerCase().indexOf(type) >= 0;
},
/**
* @ngdoc method
* @name ionic.Platform#exitApp
* @description Exit the app.
*/
exitApp: function() {
this.ready(function(){
navigator.app && navigator.app.exitApp && navigator.app.exitApp();
});
},
/**
* @ngdoc method
* @name ionic.Platform#showStatusBar
* @description Shows or hides the device status bar (in Cordova).
* @param {boolean} shouldShow Whether or not to show the status bar.
*/
showStatusBar: function(val) {
// Only useful when run within cordova
this._showStatusBar = val;
this.ready(function(){
// run this only when or if the platform (cordova) is ready
ionic.requestAnimationFrame(function(){
if(ionic.Platform._showStatusBar) {
// they do not want it to be full screen
window.StatusBar && window.StatusBar.show();
document.body.classList.remove('status-bar-hide');
} else {
// it should be full screen
window.StatusBar && window.StatusBar.hide();
document.body.classList.add('status-bar-hide');
}
});
});
},
/**
* @ngdoc method
* @name ionic.Platform#fullScreen
* @description
* Sets whether the app is fullscreen or not (in Cordova).
* @param {boolean=} showFullScreen Whether or not to set the app to fullscreen. Defaults to true.
* @param {boolean=} showStatusBar Whether or not to show the device's status bar. Defaults to false.
*/
fullScreen: function(showFullScreen, showStatusBar) {
// showFullScreen: default is true if no param provided
this.isFullScreen = (showFullScreen !== false);
// add/remove the fullscreen classname to the body
ionic.DomUtil.ready(function(){
// run this only when or if the DOM is ready
ionic.requestAnimationFrame(function(){
if(ionic.Platform.isFullScreen) {
document.body.classList.add('fullscreen');
} else {
document.body.classList.remove('fullscreen');
}
});
// showStatusBar: default is false if no param provided
ionic.Platform.showStatusBar( (showStatusBar === true) );
});
}
};
var platformName = null, // just the name, like iOS or Android
platformVersion = null, // a float of the major and minor, like 7.1
readyCallbacks = [];
// setup listeners to know when the device is ready to go
function onWindowLoad() {
if(ionic.Platform.isWebView()) {
// the window and scripts are fully loaded, and a cordova/phonegap
// object exists then let's listen for the deviceready
document.addEventListener("deviceready", onPlatformReady, false);
} else {
// the window and scripts are fully loaded, but the window object doesn't have the
// cordova/phonegap object, so its just a browser, not a webview wrapped w/ cordova
onPlatformReady();
}
window.removeEventListener("load", onWindowLoad, false);
}
window.addEventListener("load", onWindowLoad, false);
function onPlatformReady() {
// the device is all set to go, init our own stuff then fire off our event
ionic.Platform.isReady = true;
ionic.Platform.detect();
for(var x=0; x<readyCallbacks.length; x++) {
// fire off all the callbacks that were added before the platform was ready
readyCallbacks[x]();
}
readyCallbacks = [];
ionic.trigger('platformready', { target: document });
ionic.requestAnimationFrame(function(){
document.body.classList.add('platform-ready');
});
}
})(this, document, ionic);