mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
Create $ionicPlatform.on(type, callback) to make it easier to add Cordova event listeners. Closes #2219
228 lines
7.0 KiB
JavaScript
228 lines
7.0 KiB
JavaScript
var PLATFORM_BACK_BUTTON_PRIORITY_VIEW = 100;
|
|
var PLATFORM_BACK_BUTTON_PRIORITY_SIDE_MENU = 150;
|
|
var PLATFORM_BACK_BUTTON_PRIORITY_MODAL = 200;
|
|
var PLATFORM_BACK_BUTTON_PRIORITY_ACTION_SHEET = 300;
|
|
var PLATFORM_BACK_BUTTON_PRIORITY_POPUP = 400;
|
|
var PLATFORM_BACK_BUTTON_PRIORITY_LOADING = 500;
|
|
|
|
function componentConfig(defaults) {
|
|
defaults.$get = function() { return defaults; };
|
|
return defaults;
|
|
}
|
|
|
|
IonicModule
|
|
.constant('$ionicPlatformDefaults', {
|
|
'ios': {
|
|
'$ionicNavBarConfig': {
|
|
transition: 'nav-title-slide-ios',//nav-title-slide-ios7',
|
|
alignTitle: 'center',
|
|
backButtonIcon: 'ion-ios7-arrow-back'
|
|
},
|
|
'$ionicNavViewConfig': {
|
|
//transition: 'slide-left-right-ios'
|
|
transition: 'slide-ios'
|
|
},
|
|
'$ionicTabsConfig': {
|
|
type: '',
|
|
position: ''
|
|
}
|
|
},
|
|
'android': {
|
|
'$ionicNavBarConfig': {
|
|
transition: 'nav-title-slide-full',
|
|
alignTitle: 'center',
|
|
backButtonIcon: 'ion-ios7-arrow-back'
|
|
},
|
|
'$ionicNavViewConfig': {
|
|
transition: 'slide-full'
|
|
},
|
|
'$ionicTabsConfig': {
|
|
type: 'tabs-striped',
|
|
position: ''
|
|
}
|
|
}
|
|
});
|
|
|
|
|
|
IonicModule.config([
|
|
'$ionicPlatformDefaults',
|
|
|
|
'$injector',
|
|
|
|
function($ionicPlatformDefaults, $injector) {
|
|
var platform = ionic.Platform.platform();
|
|
|
|
var applyConfig = function(platformDefaults) {
|
|
forEach(platformDefaults, function(defaults, constantName) {
|
|
extend($injector.get(constantName), defaults);
|
|
});
|
|
};
|
|
|
|
switch(platform) {
|
|
case 'ios':
|
|
applyConfig($ionicPlatformDefaults.ios);
|
|
break;
|
|
case 'android':
|
|
applyConfig($ionicPlatformDefaults.android);
|
|
break;
|
|
}
|
|
}]);
|
|
|
|
/**
|
|
* @ngdoc service
|
|
* @name $ionicPlatform
|
|
* @module ionic
|
|
* @description
|
|
* An angular abstraction of {@link ionic.utility:ionic.Platform}.
|
|
*
|
|
* Used to detect the current platform, as well as do things like override the
|
|
* Android back button in PhoneGap/Cordova.
|
|
*/
|
|
IonicModule
|
|
.provider('$ionicPlatform', function() {
|
|
return {
|
|
$get: ['$q', '$rootScope', function($q, $rootScope) {
|
|
var self = {
|
|
|
|
/**
|
|
* @ngdoc method
|
|
* @name $ionicPlatform#onHardwareBackButton
|
|
* @description
|
|
* Some platforms have a hardware back button, so this is one way to
|
|
* bind to it.
|
|
* @param {function} callback the callback to trigger when this event occurs
|
|
*/
|
|
onHardwareBackButton: function(cb) {
|
|
ionic.Platform.ready(function() {
|
|
document.addEventListener('backbutton', cb, false);
|
|
});
|
|
},
|
|
|
|
/**
|
|
* @ngdoc method
|
|
* @name $ionicPlatform#offHardwareBackButton
|
|
* @description
|
|
* Remove an event listener for the backbutton.
|
|
* @param {function} callback The listener function that was
|
|
* originally bound.
|
|
*/
|
|
offHardwareBackButton: function(fn) {
|
|
ionic.Platform.ready(function() {
|
|
document.removeEventListener('backbutton', fn);
|
|
});
|
|
},
|
|
|
|
/**
|
|
* @ngdoc method
|
|
* @name $ionicPlatform#registerBackButtonAction
|
|
* @description
|
|
* Register a hardware back button action. Only one action will execute
|
|
* when the back button is clicked, so this method decides which of
|
|
* the registered back button actions has the highest priority.
|
|
*
|
|
* For example, if an actionsheet is showing, the back button should
|
|
* close the actionsheet, but it should not also go back a page view
|
|
* or close a modal which may be open.
|
|
*
|
|
* @param {function} callback Called when the back button is pressed,
|
|
* if this listener is the highest priority.
|
|
* @param {number} priority Only the highest priority will execute.
|
|
* @param {*=} actionId The id to assign this action. Default: a
|
|
* random unique id.
|
|
* @returns {function} A function that, when called, will deregister
|
|
* this backButtonAction.
|
|
*/
|
|
$backButtonActions: {},
|
|
registerBackButtonAction: function(fn, priority, actionId) {
|
|
|
|
if(!self._hasBackButtonHandler) {
|
|
// add a back button listener if one hasn't been setup yet
|
|
self.$backButtonActions = {};
|
|
self.onHardwareBackButton(self.hardwareBackButtonClick);
|
|
self._hasBackButtonHandler = true;
|
|
}
|
|
|
|
var action = {
|
|
id: (actionId ? actionId : ionic.Utils.nextUid()),
|
|
priority: (priority ? priority : 0),
|
|
fn: fn
|
|
};
|
|
self.$backButtonActions[action.id] = action;
|
|
|
|
// return a function to de-register this back button action
|
|
return function() {
|
|
delete self.$backButtonActions[action.id];
|
|
};
|
|
},
|
|
|
|
/**
|
|
* @private
|
|
*/
|
|
hardwareBackButtonClick: function(e){
|
|
// loop through all the registered back button actions
|
|
// and only run the last one of the highest priority
|
|
var priorityAction, actionId;
|
|
for(actionId in self.$backButtonActions) {
|
|
if(!priorityAction || self.$backButtonActions[actionId].priority >= priorityAction.priority) {
|
|
priorityAction = self.$backButtonActions[actionId];
|
|
}
|
|
}
|
|
if(priorityAction) {
|
|
priorityAction.fn(e);
|
|
return priorityAction;
|
|
}
|
|
},
|
|
|
|
is: function(type) {
|
|
return ionic.Platform.is(type);
|
|
},
|
|
|
|
/**
|
|
* @ngdoc method
|
|
* @name $ionicPlatform#on
|
|
* @description
|
|
* Add Cordova event listeners, such as `pause`, `resume`, `volumedownbutton`, `batterylow`,
|
|
* `offline`, etc. More information about available event types can be found in
|
|
* [Cordova's event documentation](https://cordova.apache.org/docs/en/edge/cordova_events_events.md.html#Events).
|
|
* @param {string} type Cordova [event type](https://cordova.apache.org/docs/en/edge/cordova_events_events.md.html#Events).
|
|
* @param {function} callback Called when the Cordova event is fired.
|
|
* @returns {function} Returns a deregistration function to remove the event listener.
|
|
*/
|
|
on: function(type, cb) {
|
|
ionic.Platform.ready(function(){
|
|
document.addEventListener(type, cb, false);
|
|
});
|
|
return function() {
|
|
ionic.Platform.ready(function(){
|
|
document.removeEventListener(type, cb);
|
|
});
|
|
};
|
|
},
|
|
|
|
/**
|
|
* @ngdoc method
|
|
* @name $ionicPlatform#ready
|
|
* @description
|
|
* Trigger a callback once the device is ready,
|
|
* or immediately if the device is already ready.
|
|
* @param {function=} callback The function to call.
|
|
* @returns {promise} A promise which is resolved when the device is ready.
|
|
*/
|
|
ready: function(cb) {
|
|
var q = $q.defer();
|
|
|
|
ionic.Platform.ready(function(){
|
|
q.resolve();
|
|
cb && cb();
|
|
});
|
|
|
|
return q.promise;
|
|
}
|
|
};
|
|
return self;
|
|
}]
|
|
};
|
|
|
|
});
|
|
|