Skip to content

Commit 05e811b

Browse files
committed
Merge pull request cgross#9 from jcristini/master
Added support to monitor multiple trackers
2 parents 179c030 + 2f9891d commit 05e811b

File tree

4 files changed

+115
-37
lines changed

4 files changed

+115
-37
lines changed

angular-busy.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,33 @@ angular.module('cgBusy').directive('cgBusy',['promiseTracker','$compile','$templ
1010

1111
var options = scope.$eval(attrs.cgBusy);
1212

13-
if (typeof options === 'string'){
13+
if (typeof options === 'string' || angular.isArray(options)) {
1414
options = {tracker:options};
15-
}
15+
}
1616

1717
if (typeof options === 'undefined' || typeof options.tracker === 'undefined'){
1818
throw new Error('Options for cgBusy directive must be provided (tracker option is required).');
19-
}
19+
}
20+
21+
options.tracker = angular.isArray(options.tracker) ? options.tracker : [options.tracker];
2022

21-
if (!scope.$cgBusyTracker){
23+
if (!scope.$cgBusyTracker){
2224
scope.$cgBusyTracker = {};
2325
}
2426

25-
scope.$cgBusyTracker[options.tracker] = promiseTracker(options.tracker);
27+
angular.forEach(options.tracker, function (tracker) {
28+
scope.$cgBusyTracker[tracker] = promiseTracker(tracker);
29+
});
30+
31+
scope.isActive = function() {
32+
var active = false;
33+
angular.forEach(scope.$cgBusyTracker, function (tracker) {
34+
if (tracker.active())
35+
active = true;
36+
});
37+
38+
return active;
39+
};
2640

2741
var position = element.css('position');
2842
if (position === 'static' || position === '' || typeof position === 'undefined'){
@@ -36,7 +50,7 @@ angular.module('cgBusy').directive('cgBusy',['promiseTracker','$compile','$templ
3650
options.backdrop = typeof options.backdrop === 'undefined' ? true : options.backdrop;
3751
var backdrop = options.backdrop ? '<div class="cg-busy cg-busy-backdrop"></div>' : '';
3852

39-
var template = '<div class="cg-busy cg-busy-animation ng-hide" ng-show="$cgBusyTracker[\''+options.tracker+'\'].active()">'+ backdrop + indicatorTemplate+'</div>';
53+
var template = '<div class="cg-busy cg-busy-animation ng-hide" ng-show="isActive()">'+ backdrop + indicatorTemplate+'</div>';
4054
var templateElement = $compile(template)(scope);
4155

4256
angular.element(templateElement.children()[options.backdrop?1:0])
@@ -45,7 +59,6 @@ angular.module('cgBusy').directive('cgBusy',['promiseTracker','$compile','$templ
4559
.css('left',0)
4660
.css('right',0)
4761
.css('bottom',0);
48-
4962
element.append(templateElement);
5063

5164
}).error(function(data){

dist/angular-busy.js

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,33 @@ angular.module('cgBusy').directive('cgBusy',['promiseTracker','$compile','$templ
1010

1111
var options = scope.$eval(attrs.cgBusy);
1212

13-
if (typeof options === 'string'){
13+
if (typeof options === 'string' || angular.isArray(options)) {
1414
options = {tracker:options};
15-
}
15+
}
1616

1717
if (typeof options === 'undefined' || typeof options.tracker === 'undefined'){
1818
throw new Error('Options for cgBusy directive must be provided (tracker option is required).');
19-
}
19+
}
2020

21-
if (!scope.$cgBusyTracker){
21+
options.tracker = angular.isArray(options.tracker) ? options.tracker : [options.tracker];
22+
23+
if (!scope.$cgBusyTracker){
2224
scope.$cgBusyTracker = {};
2325
}
2426

25-
scope.$cgBusyTracker[options.tracker] = promiseTracker(options.tracker);
27+
angular.forEach(options.tracker, function (tracker) {
28+
scope.$cgBusyTracker[tracker] = promiseTracker(tracker);
29+
});
30+
31+
scope.isActive = function() {
32+
var active = false;
33+
angular.forEach(scope.$cgBusyTracker, function (tracker) {
34+
if (tracker.active())
35+
active = true;
36+
});
37+
38+
return active;
39+
};
2640

2741
var position = element.css('position');
2842
if (position === 'static' || position === '' || typeof position === 'undefined'){
@@ -36,7 +50,7 @@ angular.module('cgBusy').directive('cgBusy',['promiseTracker','$compile','$templ
3650
options.backdrop = typeof options.backdrop === 'undefined' ? true : options.backdrop;
3751
var backdrop = options.backdrop ? '<div class="cg-busy cg-busy-backdrop"></div>' : '';
3852

39-
var template = '<div class="cg-busy cg-busy-animation ng-hide" ng-show="$cgBusyTracker[\''+options.tracker+'\'].active()">'+ backdrop + indicatorTemplate+'</div>';
53+
var template = '<div class="cg-busy cg-busy-animation ng-hide" ng-show="isActive()">'+ backdrop + indicatorTemplate+'</div>';
4054
var templateElement = $compile(template)(scope);
4155

4256
angular.element(templateElement.children()[options.backdrop?1:0])
@@ -45,7 +59,6 @@ angular.module('cgBusy').directive('cgBusy',['promiseTracker','$compile','$templ
4559
.css('left',0)
4660
.css('right',0)
4761
.css('bottom',0);
48-
4962
element.append(templateElement);
5063

5164
}).error(function(data){
@@ -60,28 +73,51 @@ angular.module('cgBusy').directive('cgBusy',['promiseTracker','$compile','$templ
6073
angular.module("cgBusy").run(["$templateCache", function($templateCache) {
6174

6275
$templateCache.put("angular-busy.html",
63-
"<div class=\"cg-busy-default-wrapper\">\n" +
64-
"\n" +
65-
" <div class=\"cg-busy-default-sign\">\n" +
66-
"\n" +
67-
" <div class=\"cg-busy-default-spinner\">\n" +
68-
" <div class=\"bar1\"></div>\n" +
69-
" <div class=\"bar2\"></div>\n" +
70-
" <div class=\"bar3\"></div>\n" +
71-
" <div class=\"bar4\"></div>\n" +
72-
" <div class=\"bar5\"></div>\n" +
73-
" <div class=\"bar6\"></div>\n" +
74-
" <div class=\"bar7\"></div>\n" +
75-
" <div class=\"bar8\"></div>\n" +
76-
" <div class=\"bar9\"></div>\n" +
77-
" <div class=\"bar10\"></div>\n" +
78-
" <div class=\"bar11\"></div>\n" +
79-
" <div class=\"bar12\"></div>\n" +
80-
" </div>\n" +
81-
"\n" +
82-
" <div class=\"cg-busy-default-text\">Please Wait...</div>\n" +
83-
"\n" +
84-
" </div>\n" +
76+
"<div class=\"cg-busy-default-wrapper\">\r" +
77+
"\n" +
78+
"\r" +
79+
"\n" +
80+
" <div class=\"cg-busy-default-sign\">\r" +
81+
"\n" +
82+
"\r" +
83+
"\n" +
84+
" <div class=\"cg-busy-default-spinner\">\r" +
85+
"\n" +
86+
" <div class=\"bar1\"></div>\r" +
87+
"\n" +
88+
" <div class=\"bar2\"></div>\r" +
89+
"\n" +
90+
" <div class=\"bar3\"></div>\r" +
91+
"\n" +
92+
" <div class=\"bar4\"></div>\r" +
93+
"\n" +
94+
" <div class=\"bar5\"></div>\r" +
95+
"\n" +
96+
" <div class=\"bar6\"></div>\r" +
97+
"\n" +
98+
" <div class=\"bar7\"></div>\r" +
99+
"\n" +
100+
" <div class=\"bar8\"></div>\r" +
101+
"\n" +
102+
" <div class=\"bar9\"></div>\r" +
103+
"\n" +
104+
" <div class=\"bar10\"></div>\r" +
105+
"\n" +
106+
" <div class=\"bar11\"></div>\r" +
107+
"\n" +
108+
" <div class=\"bar12\"></div>\r" +
109+
"\n" +
110+
" </div>\r" +
111+
"\n" +
112+
"\r" +
113+
"\n" +
114+
" <div class=\"cg-busy-default-text\">Please Wait...</div>\r" +
115+
"\n" +
116+
"\r" +
117+
"\n" +
118+
" </div>\r" +
119+
"\n" +
120+
"\r" +
85121
"\n" +
86122
"</div>"
87123
);

dist/angular-busy.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,35 @@ describe('cgBusy', function() {
3838
expect(this.element.children().css('display')).toBe('none'); //ensure its now invisible as the promise is resolved
3939
});
4040

41+
it('should show the overlay during multiple promises', function() {
42+
43+
this.element = compile('<div cg-busy="[\'my_tracker\',\'my_tracker2\']"></div>')(scope);
44+
angular.element('body').append(this.element);
45+
46+
this.testPromise = q.defer();
47+
_promiseTracker('my_tracker').addPromise(this.testPromise.promise);
48+
49+
this.testPromise2 = q.defer();
50+
_promiseTracker('my_tracker2').addPromise(this.testPromise2.promise);
51+
52+
//httpBackend.flush();
53+
54+
scope.$apply();
55+
56+
expect(this.element.children().length).toBe(1); //ensure element is added
57+
58+
expect(this.element.children().css('display')).toBe('block');//ensure its visible (promise is ongoing)
59+
60+
this.testPromise.resolve();
61+
scope.$apply();
62+
63+
expect(this.element.children().css('display')).toBe('block'); //ensure its still visible (promise is ongoing)
64+
65+
this.testPromise2.resolve();
66+
scope.$apply();
67+
expect(this.element.children().css('display')).toBe('none'); //ensure its now invisible as the promise is resolved
68+
});
69+
4170
it('should load custom templates', function(){
4271

4372
this.element = compile('<div cg-busy="{tracker:\'my_tracker\',template:\'test-custom-template.html\'}"></div>')(scope);

0 commit comments

Comments
 (0)