forked from urish/angular-spinner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-spinner.js
76 lines (63 loc) · 1.79 KB
/
angular-spinner.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
/**
* angular-spinner version 0.3.1
* License: MIT.
* Copyright (C) 2013, 2014, Uri Shaked and contributors.
*/
(function(window, angular, undefined) {
'use strict';
angular.module('angularSpinner', [])
.factory('usSpinnerService', ['$rootScope', function ($rootScope) {
var config = {};
config.spin = function (key) {
$rootScope.$broadcast('us-spinner:spin', key);
};
config.stop = function (key) {
$rootScope.$broadcast('us-spinner:stop', key);
};
return config;
}])
.directive('usSpinner', ['$window', function ($window) {
return {
scope: true,
controller: ['$scope', '$element', '$attrs', function ($scope, $element, $attrs) {
$scope.spinner = null;
$scope.key = angular.isDefined($attrs.spinnerKey) ? $attrs.spinnerKey : false;
$scope.startActive = angular.isDefined($attrs.spinnerStartActive) ?
$attrs.spinnerStartActive : !($scope.key);
$scope.spin = function () {
if ($scope.spinner) {
$scope.spinner.spin($element[0]);
}
};
$scope.stop = function () {
if ($scope.spinner) {
$scope.spinner.stop();
}
};
}],
link: function (scope, element, attr) {
scope.$watch(attr.usSpinner, function (options) {
scope.stop();
scope.spinner = new $window.Spinner(options);
if (!scope.key || scope.startActive) {
scope.spinner.spin(element[0]);
}
}, true);
scope.$on('us-spinner:spin', function (event, key) {
if(key === scope.key){
scope.spin();
}
});
scope.$on('us-spinner:stop', function (event, key) {
if(key === scope.key){
scope.stop();
}
});
scope.$on('$destroy', function () {
scope.stop();
scope.spinner = null;
});
}
};
}]);
})(window, window.angular);