Skip to content

Commit 1bc88a7

Browse files
committed
Merge pull request urish#31 from aleksih/master
Added configurable default options
2 parents dc0191c + 0480017 commit 1bc88a7

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ You can also pass spinner options, for example:
3737

3838
Possible configuration options are described in the [spin.js homepage](http://fgnass.github.io/spin.js/).
3939

40+
### Configuring default spinner options
41+
42+
You can use `usSpinnerConfigProvider` to configure default options for all spinners globally. Any options passed from a directive still override these.
43+
44+
```js
45+
myapp.config(['usSpinnerConfigProvider', function (usSpinnerConfigProvider) {
46+
usSpinnerConfigProvider.setDefaults({color: 'blue'});
47+
}]);
48+
```
49+
4050
### Using the usSpinnerService to control spinners
4151

4252
```html
@@ -79,7 +89,7 @@ The spinner-key will be used as an identifier (not unique) allowing you to have
7989

8090
### Example
8191

82-
See [online example on Plunker](http://plnkr.co/edit/BGLUYcylbIVJRz6ztbhf?p=preview).
92+
See [online example on Plunker](http://plnkr.co/edit/BGLUYcylbIVJRz6ztbhf?p=preview).
8393

8494
## License
8595

angular-spinner.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@
1212
angular
1313
.module('angularSpinner', [])
1414

15+
.provider('usSpinnerConfig', function () {
16+
var _config = {};
17+
18+
return {
19+
setDefaults: function (config) {
20+
_config = config || _config;
21+
},
22+
$get: function () {
23+
return {
24+
config: _config
25+
};
26+
}
27+
};
28+
})
29+
1530
.factory('usSpinnerService', ['$rootScope', function ($rootScope) {
1631
var config = {};
1732

@@ -26,7 +41,7 @@
2641
return config;
2742
}])
2843

29-
.directive('usSpinner', ['$window', function ($window) {
44+
.directive('usSpinner', ['$window', 'usSpinnerConfig', function ($window, usSpinnerConfig) {
3045
return {
3146
scope: true,
3247
link: function (scope, element, attr) {
@@ -59,6 +74,14 @@
5974

6075
scope.$watch(attr.usSpinner, function (options) {
6176
stopSpinner();
77+
78+
options = options || {};
79+
for (var property in usSpinnerConfig.config) {
80+
if (options[property] === undefined) {
81+
options[property] = usSpinnerConfig.config[property];
82+
}
83+
}
84+
6285
scope.spinner = new SpinnerConstructor(options);
6386
if (!scope.key || scope.startActive) {
6487
scope.spinner.spin(element[0]);

tests.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44

55
'use strict';
66

7+
describe('Provider: usSpinnerConfigProvider', function () {
8+
beforeEach(module('angularSpinner'));
9+
10+
it('should have configurable options', function () {
11+
module(function (usSpinnerConfigProvider) {
12+
usSpinnerConfigProvider.setDefaults({color: 'black'});
13+
});
14+
15+
inject(function (usSpinnerConfig) {
16+
expect(usSpinnerConfig.config.color).toBe('black');
17+
});
18+
});
19+
});
20+
721
describe('Directive: us-spinner', function () {
822
var Spinner;
923

0 commit comments

Comments
 (0)