mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
177 lines
5.2 KiB
JavaScript
177 lines
5.2 KiB
JavaScript
|
|
var LOADING_TPL =
|
|
'<div class="loading">' +
|
|
'</div>';
|
|
|
|
var LOADING_HIDE_DEPRECATED = '$ionicLoading instance.hide() has been deprecated. Use $ionicLoading.hide().';
|
|
var LOADING_SHOW_DEPRECATED = '$ionicLoading instance.show() has been deprecated. Use $ionicLoading.show().';
|
|
var LOADING_SET_DEPRECATED = '$ionicLoading instance.setContent() has been deprecated. Use $ionicLoading.show({ template: \'my content\' }).';
|
|
|
|
/**
|
|
* @ngdoc service
|
|
* @name $ionicLoading
|
|
* @module ionic
|
|
* @description
|
|
* An overlay that can be used to indicate activity while blocking user
|
|
* interaction.
|
|
*
|
|
* @usage
|
|
* ```js
|
|
* angular.module('LoadingApp', ['ionic'])
|
|
* .controller('LoadingCtrl', function($scope, $ionicLoading) {
|
|
* $scope.show = function() {
|
|
* $ionicLoading.show({
|
|
* template: 'Loading...'
|
|
* });
|
|
* };
|
|
* $scope.hide = function(){
|
|
* $ionicLoading.hide();
|
|
* };
|
|
* });
|
|
* ```
|
|
*/
|
|
IonicModule
|
|
.factory('$ionicLoading', [
|
|
'$document',
|
|
'$ionicTemplateLoader',
|
|
'$ionicBackdrop',
|
|
'$timeout',
|
|
'$q',
|
|
'$log',
|
|
'$compile',
|
|
function($document, $ionicTemplateLoader, $ionicBackdrop, $timeout, $q, $log, $compile) {
|
|
|
|
var loaderInstance;
|
|
//default value
|
|
var loadingShowDelay = $q.when();
|
|
|
|
return {
|
|
/**
|
|
* @ngdoc method
|
|
* @name $ionicLoading#show
|
|
* @description Shows a loading indicator. If the indicator is already shown,
|
|
* it will set the options given and keep the indicator shown.
|
|
* @param {object} opts The options for the loading indicator. Available properties:
|
|
* - `{string=}` `template` The html content of the indicator.
|
|
* - `{string=}` `templateUrl` The url of an html template to load as the content of the indicator.
|
|
* - `{boolean=}` `noBackdrop` Whether to hide the backdrop.
|
|
* - `{number=}` `delay` How many milliseconds to delay showing the indicator.
|
|
* - `{number=}` `duration` How many milliseconds to wait until automatically
|
|
* hiding the indicator.
|
|
*/
|
|
show: showLoader,
|
|
/**
|
|
* @ngdoc method
|
|
* @name $ionicLoading#hide
|
|
* @description Hides the loading indicator, if shown.
|
|
*/
|
|
hide: hideLoader,
|
|
/**
|
|
* @private for testing
|
|
*/
|
|
_getLoader: getLoader
|
|
};
|
|
|
|
function getLoader() {
|
|
if (!loaderInstance) {
|
|
loaderInstance = $ionicTemplateLoader.compile({
|
|
template: LOADING_TPL,
|
|
appendTo: $document[0].body
|
|
})
|
|
.then(function(loader) {
|
|
|
|
var self = loader;
|
|
|
|
loader.show = function(options) {
|
|
var templatePromise = options.templateUrl ?
|
|
$ionicTemplateLoader.load(options.templateUrl) :
|
|
//options.content: deprecated
|
|
$q.when(options.template || options.content || '');
|
|
|
|
|
|
if (!this.isShown) {
|
|
//options.showBackdrop: deprecated
|
|
this.hasBackdrop = !options.noBackdrop && options.showBackdrop !== false;
|
|
if (this.hasBackdrop) {
|
|
$ionicBackdrop.retain();
|
|
}
|
|
}
|
|
|
|
if (options.duration) {
|
|
$timeout.cancel(this.durationTimeout);
|
|
this.durationTimeout = $timeout(
|
|
angular.bind(this, this.hide),
|
|
+options.duration
|
|
);
|
|
}
|
|
|
|
templatePromise.then(function(html) {
|
|
if (html) {
|
|
self.element.html(html);
|
|
$compile(self.element.contents())(self.scope);
|
|
}
|
|
|
|
//Don't show until template changes
|
|
if (self.isShown) {
|
|
self.element.addClass('visible');
|
|
ionic.DomUtil.centerElementByMarginTwice(self.element[0]);
|
|
ionic.requestAnimationFrame(function() {
|
|
self.isShown && self.element.addClass('active');
|
|
});
|
|
}
|
|
});
|
|
|
|
this.isShown = true;
|
|
};
|
|
loader.hide = function() {
|
|
if (this.isShown) {
|
|
if (this.hasBackdrop) {
|
|
$ionicBackdrop.release();
|
|
}
|
|
self.element.removeClass('active');
|
|
setTimeout(function() {
|
|
!self.isShown && self.element.removeClass('visible');
|
|
}, 200);
|
|
}
|
|
$timeout.cancel(this.durationTimeout);
|
|
this.isShown = false;
|
|
};
|
|
return loader;
|
|
});
|
|
}
|
|
return $q.when(loaderInstance);
|
|
}
|
|
|
|
function showLoader(options) {
|
|
options || (options = {});
|
|
var delay = options.delay || options.showDelay || 0;
|
|
|
|
//If loading.show() was called previously, cancel it and show with our new options
|
|
$timeout.cancel(loadingShowDelay);
|
|
loadingShowDelay = $timeout(angular.noop, delay);
|
|
|
|
loadingShowDelay.then(getLoader).then(function(loader) {
|
|
return loader.show(options);
|
|
});
|
|
|
|
return {
|
|
hide: deprecated.method(LOADING_HIDE_DEPRECATED, $log.error, hideLoader),
|
|
show: deprecated.method(LOADING_SHOW_DEPRECATED, $log.error, function() {
|
|
showLoader(options);
|
|
}),
|
|
setContent: deprecated.method(LOADING_SET_DEPRECATED, $log.error, function(content) {
|
|
getLoader().then(function(loader) {
|
|
loader.show({ template: content });
|
|
});
|
|
})
|
|
};
|
|
}
|
|
|
|
function hideLoader() {
|
|
$timeout.cancel(loadingShowDelay);
|
|
getLoader().then(function(loader) {
|
|
loader.hide();
|
|
});
|
|
}
|
|
}]);
|