-
Notifications
You must be signed in to change notification settings - Fork 48
/
angular-loading.js
250 lines (219 loc) · 7.25 KB
/
angular-loading.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* Angular Loading
* @homepage https://github.com/darthwade/angular-loading
* @author Vadym Petrychenko https://github.com/darthwade
* @license The MIT License (http://www.opensource.org/licenses/mit-license.php)
* @copyright 2014 Vadym Petrychenko
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['angular', 'spinjs'], factory);
} else if (typeof exports === 'object') {
// CommonJS
factory(require('angular'), require('spinjs'));
} else {
// Browser globals
factory(window.angular, window.Spinner)
}
}(function (angular, Spinner) {
'use strict';
angular.module('darthwade.loading', [])
.value('loadingOptions', {
active: false, // Defines current loading state
text: 'Loading...', // Display text
className: '', // Custom class, added to directive
overlay: true, // Display overlay
spinner: true, // Display spinner
spinnerOptions: {
lines: 12, // The number of lines to draw
length: 7, // The length of each line
width: 4, // The line thickness
radius: 10, // The radius of the inner circle
rotate: 0, // Rotation offset
corners: 1, // Roundness (0..1)
color: '#000', // #rgb or #rrggbb
direction: 1, // 1: clockwise, -1: counterclockwise
speed: 2, // Rounds per second
trail: 100, // Afterglow percentage
opacity: 1 / 4, // Opacity of the lines
fps: 20, // Frames per second when using setTimeout()
zIndex: 2e9, // Use a high z-index by default
className: 'dw-spinner', // CSS class to assign to the element
top: 'auto', // Center vertically
left: 'auto', // Center horizontally
position: 'relative' // Element position
}
})
.service('$loading', ['$rootScope', 'loadingOptions', function ($rootScope, loadingOptions) {
var self = this;
/**
* Overrides default options
* @param {object} options
*/
self.setDefaultOptions = function (options) {
extend(true, loadingOptions, options);
};
/**
* Activates loading state by key
* @param {string} key
*/
self.start = function (key) {
$rootScope.$evalAsync(function() {
$rootScope.$broadcast('$loadingStart', key);
});
};
/**
* Update loading state by key with loadingOptions object
* @param {string} key
* @param {object} options
*/
self.update = function (key, options) {
$rootScope.$evalAsync(function() {
$rootScope.$broadcast('$loadingUpdate', key, options);
});
};
/**
* Deactivates loading state by key
* @param {string} key
*/
self.finish = function (key) {
$rootScope.$evalAsync(function() {
$rootScope.$broadcast('$loadingFinish', key);
});
};
}])
.directive('dwLoading', ['$rootScope', 'loadingOptions', function ($rootScope, loadingOptions) {
return {
link: function (scope, element, attrs) {
var spinner = null,
key = attrs.dwLoading || false,
options,
container,
body,
spinnerContainer,
text;
/**
* Starts spinner
*/
var start = function () {
if (container) {
container.addClass('dw-loading-active');
}
if (spinner) {
spinner.spin(spinnerContainer[0]);
}
};
/**
* Update spinner, use force to update when loader is already started
*/
var update = function (newOptions, force) {
finish();
options = extend(true, {}, loadingOptions, newOptions);
// Build template
body = angular.element('<div></div>')
.addClass('dw-loading-body');
container = angular.element('<div></div>')
.addClass('dw-loading')
.append(body);
if (options.overlay) {
container.addClass('dw-loading-overlay');
}
if (options.className) {
container.addClass(options.className);
}
if (options.spinner) {
spinnerContainer = angular.element('<div></div>')
.addClass('dw-loading-spinner');
body.append(spinnerContainer);
spinner = new Spinner(options.spinnerOptions);
}
if (options.text) {
text = angular.element('<div></div>')
.addClass('dw-loading-text')
.text(options.text);
body.append(text);
}
element.append(container);
if ( options.active || !key || force) {
start();
}
};
/**
* Stops spinner
*/
var finish = function () {
if (container) {
container.removeClass('dw-loading-active');
}
if (spinner) {
spinner.stop();
}
};
scope.$watch(attrs.dwLoadingOptions, function (newOptions) {
update(newOptions);
}, true);
$rootScope.$on('$loadingStart', function (event, loadKey) {
if (loadKey === key) {
start();
}
});
$rootScope.$on('$loadingUpdate', function (event, loadKey, options) {
if (loadKey === key) {
update(options, true);
}
});
$rootScope.$on('$loadingFinish', function (event, loadKey) {
if (loadKey === key) {
finish();
}
});
scope.$on('$destroy', function () {
finish();
spinner = null;
});
}
};
}]);
/**
* Extends the destination object `dst` by copying all of the properties from the `src` object(s)
* to `dst`. You can specify multiple `src` objects.
*
* @param {Boolean} deep If true, the merge becomes recursive (optional)
* @param {Object} dst Destination object.
* @param {Object} src Source object(s).
* @returns {Object} Reference to `dst`.
*/
function extend(dst) {
var deep = false,
i = 1;
if (typeof dst === 'boolean') {
deep = dst;
dst = arguments[1] || {};
i++;
}
angular.forEach([].slice.call(arguments, i), function (obj) {
var array, clone, copy, key, src;
for (key in obj) {
src = dst[key];
copy = obj[key];
if (dst === copy) {
continue;
}
if (deep && copy && (angular.isObject(copy) ||
(array = angular.isArray(copy)))) {
if (array) {
clone = (src && angular.isArray(src)) ? src : [];
} else {
clone = (src && angular.isObject(src)) ? src : {};
}
dst[key] = extend(deep, clone, copy);
}
else if (copy !== undefined) {
dst[key] = copy;
}
}
});
return dst;
}
}));