forked from urish/angular-spinner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-spinner.js
127 lines (103 loc) · 2.88 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
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
/**
* angular-spinner version 0.7.0
* License: MIT.
* Copyright (C) 2013, 2014, 2015, Uri Shaked and contributors.
*/
'format amd';
(function (root) {
'use strict';
function factory(angular, Spinner) {
return angular
.module('angularSpinner', [])
.constant('SpinJSSpinner', Spinner)
.provider('usSpinnerConfig', function () {
var _config = {}, _themes = {};
return {
setDefaults: function (config) {
_config = config || _config;
},
setTheme: function(name, config) {
_themes[name] = config;
},
$get: function () {
return {
config: _config,
themes: _themes
};
}
};
})
.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', ['SpinJSSpinner', 'usSpinnerConfig', function (SpinJSSpinner, usSpinnerConfig) {
return {
scope: true,
link: function (scope, element, attr) {
scope.spinner = null;
scope.key = angular.isDefined(attr.spinnerKey) ? attr.spinnerKey : false;
scope.startActive = angular.isDefined(attr.spinnerStartActive) ?
scope.$eval(attr.spinnerStartActive) : scope.key ?
false : true;
function stopSpinner() {
if (scope.spinner) {
scope.spinner.stop();
}
}
scope.spin = function () {
if (scope.spinner) {
scope.spinner.spin(element[0]);
}
};
scope.stop = function () {
scope.startActive = false;
stopSpinner();
};
scope.$watch(attr.usSpinner, function (options) {
stopSpinner();
// order of precedence: element options, theme, defaults.
options = angular.extend(
usSpinnerConfig.config,
usSpinnerConfig.themes[attr.spinnerTheme],
options);
scope.spinner = new SpinJSSpinner(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;
});
}
};
}]);
}
if ((typeof module === 'object') && module.exports) {
/* CommonJS module */
module.exports = factory(require('angular'), require('spin.js'));
} else if (typeof define === 'function' && define.amd) {
/* AMD module */
define(['angular', 'spin'], factory);
} else {
/* Browser global */
factory(root.angular, root.Spinner);
}
}(this));