Skip to content

Commit 62c007d

Browse files
committed
Added support to monitor multiple trackers
1 parent 179c030 commit 62c007d

File tree

4 files changed

+109
-29
lines changed

4 files changed

+109
-29
lines changed

angular-busy.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,27 @@ angular.module('cgBusy').directive('cgBusy',['promiseTracker','$compile','$templ
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 = options.tracker.split(",");
2022

2123
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])

dist/angular-busy.js

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,27 @@ angular.module('cgBusy').directive('cgBusy',['promiseTracker','$compile','$templ
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 = options.tracker.split(",");
2022

2123
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])
@@ -60,28 +74,51 @@ angular.module('cgBusy').directive('cgBusy',['promiseTracker','$compile','$templ
6074
angular.module("cgBusy").run(["$templateCache", function($templateCache) {
6175

6276
$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" +
77+
"<div class=\"cg-busy-default-wrapper\">\r" +
78+
"\n" +
79+
"\r" +
80+
"\n" +
81+
" <div class=\"cg-busy-default-sign\">\r" +
82+
"\n" +
83+
"\r" +
84+
"\n" +
85+
" <div class=\"cg-busy-default-spinner\">\r" +
86+
"\n" +
87+
" <div class=\"bar1\"></div>\r" +
88+
"\n" +
89+
" <div class=\"bar2\"></div>\r" +
90+
"\n" +
91+
" <div class=\"bar3\"></div>\r" +
92+
"\n" +
93+
" <div class=\"bar4\"></div>\r" +
94+
"\n" +
95+
" <div class=\"bar5\"></div>\r" +
96+
"\n" +
97+
" <div class=\"bar6\"></div>\r" +
98+
"\n" +
99+
" <div class=\"bar7\"></div>\r" +
100+
"\n" +
101+
" <div class=\"bar8\"></div>\r" +
102+
"\n" +
103+
" <div class=\"bar9\"></div>\r" +
104+
"\n" +
105+
" <div class=\"bar10\"></div>\r" +
106+
"\n" +
107+
" <div class=\"bar11\"></div>\r" +
108+
"\n" +
109+
" <div class=\"bar12\"></div>\r" +
110+
"\n" +
111+
" </div>\r" +
112+
"\n" +
113+
"\r" +
114+
"\n" +
115+
" <div class=\"cg-busy-default-text\">Please Wait...</div>\r" +
116+
"\n" +
117+
"\r" +
118+
"\n" +
119+
" </div>\r" +
120+
"\n" +
121+
"\r" +
85122
"\n" +
86123
"</div>"
87124
);

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)