mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
99 lines
2.4 KiB
JavaScript
99 lines
2.4 KiB
JavaScript
(function() {
|
|
'use strict';
|
|
|
|
angular.module('ionic.service.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 = 'web';
|
|
var isPlatformReady = false;
|
|
|
|
if(window.cordova || window.PhoneGap || window.phonegap) {
|
|
platform = 'cordova';
|
|
}
|
|
|
|
var isReady = function() {
|
|
if(platform == 'cordova') {
|
|
return window.device || window.Cordova;
|
|
}
|
|
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 {
|
|
/**
|
|
* Some platforms have hardware back buttons, so this is one way to bind to it.
|
|
*
|
|
* @param {function} cb the callback to trigger when this event occurs
|
|
*/
|
|
onHardwareBackButton: function(cb) {
|
|
this.ready(function() {
|
|
document.addEventListener('backbutton', cb, false);
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Remove an event listener for the backbutton.
|
|
*
|
|
* @param {function} fn the listener function that was originally bound.
|
|
*/
|
|
offHardwareBackButton: function(fn) {
|
|
this.ready(function() {
|
|
document.removeEventListener('backbutton', fn);
|
|
});
|
|
},
|
|
|
|
is: function(type) {
|
|
return ionic.Platform.is(type);
|
|
},
|
|
|
|
/**
|
|
* Trigger a callback once the device is ready, or immediately if the device is already
|
|
* ready.
|
|
*/
|
|
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);
|