-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathngHelperBusy.js
More file actions
133 lines (106 loc) · 3.47 KB
/
ngHelperBusy.js
File metadata and controls
133 lines (106 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict';
/**
* The module contains everything we need to handle the busy indicator logic
*/
var ngHelperBusy = angular.module('ngHelperBusy', []);
/**
* @ngdoc directive
* @name ngHelperBusy.directive:ngHelperBusy
* @description
* # ngHelperBusy
*/
ngHelperBusy.directive('ngHelperBusy',['$busy', function ($busy) {
return {
template: '<div id="ngHelperBusyLayer"><div class="wrapper"><div class="loader"></div><div class="message">{{busyMessage}}</div></div></div>',
restrict: 'CEA',
scope: true,
controller: 'NgHelperBusyCtrl'
};
}]);
/**
* @ngdoc controller
* @name ngHelperBusy.controller:ngHelperBusy
* @description
* # ngHelperBusy
*/
ngHelperBusy.controller('NgHelperBusyCtrl', ['$scope', '$rootScope', '$busy', function($scope, $rootScope, $busy) {
var defaultMessage = "Please stay tuned...";
$scope.busyMessage = defaultMessage;
$rootScope.$on('$busy.setMessage', function() {
$scope.busyMessage = $busy.getBusyMessage();
});
$rootScope.$on('$busy.resetMessage', function() {
$scope.busyMessage = defaultMessage;
})
if( $busy.busyOnLoading ){
$scope.busyMessage = $busy.getBusyMessage();
$busy.beBusy();
}
}]);
/**
* @ngdoc service
* @name ngHelperBusy.service:ngHelperBusy
* @description
* # ngHelperBusy
*/
ngHelperBusy.service('$busy', [ '$q', '$rootScope', function($q, $rootScope) {
var self = this;
var currentMessage = null;
self.busyOnLoading = false;
self.getBusyMessage = function() {
return currentMessage;
};
self.setMessage = function(message) {
currentMessage = message;
$rootScope.$emit("$busy.setMessage");
};
self.resetMessage = function() {
currentMessage = null;
$rootScope.$emit("$busy.resetMessage");
};
self.during = function(promise) {
// generate a promise
var deferred = $q.defer();
// make us busy
self.beBusy();
// wait until the given promise is done
$q.when(promise).then(function(data) {
// make us free
self.beFree();
// just call the then method of the original promise
deferred.resolve(data);
}, function(reason) {
self.beFree();
deferred.reject(reason)
}, function(update) {
deferred.notify(update)
});
// return our promise
return deferred.promise;
};
self.beBusy = function() {
// bring up the busy layer
var busyElement = document.getElementById("ngHelperBusyLayer");
if (busyElement !== null && busyElement !== undefined) {
busyElement.classList.add("busy");
}
// lock the body
var bodyElement = document.getElementsByTagName("body")[0];
if (bodyElement !== null && bodyElement !== undefined) {
bodyElement.classList.add("ngHelperBusyLayerNoScroll");
}
};
self.beFree = function() {
// hide the busy layer when done
var busyElement = document.getElementById("ngHelperBusyLayer");
var bodyElement = document.getElementsByTagName("body")[0];
if (busyElement !== null && busyElement !== undefined) {
busyElement.classList.remove("busy");
}
if (bodyElement !== null && bodyElement !== undefined) {
bodyElement.classList.remove("ngHelperBusyLayerNoScroll");
}
// reset the message
self.resetMessage();
}
}]);