Skip to content

Commit 4937d6c

Browse files
Some initial work on this -- rebase me with issue num before merge
1 parent 51cc8eb commit 4937d6c

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

src/loading-bar.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,23 @@ angular.module('chieffancypants.loadingBar', [])
126126
.provider('cfpLoadingBar', function() {
127127

128128
this.includeSpinner = true;
129-
this.parentSelector = 'body';
129+
this.parentSelector = null;
130130

131-
this.$get = ['$document', '$timeout', '$animate', function ($document, $timeout, $animate) {
131+
this.$get = ['$rootElement', '$timeout', '$animate', function ($rootElement, $timeout, $animate) {
132+
var $parent;
132133

133-
var $parentSelector = this.parentSelector,
134-
$parent = $document.find($parentSelector),
135-
loadingBarContainer = angular.element('<div id="loading-bar"><div class="bar"><div class="peg"></div></div></div>'),
134+
if (!this.parentSelector) {
135+
$parent = $rootElement;
136+
} else {
137+
$parent = $rootElement.find(this.parentSelector);
138+
}
139+
140+
// if the parentSelector isn't found, alert the developer instead of
141+
if ($parent.length === 0) {
142+
throw new Error('Could not attach the loading bar to the DOM: selector ("' + this.parentSelector + '") not found');
143+
}
144+
145+
var loadingBarContainer = angular.element('<div id="loading-bar"><div class="bar"><div class="peg"></div></div></div>'),
136146
loadingBar = loadingBarContainer.find('div').eq(0),
137147
spinner = angular.element('<div id="loading-bar-spinner"><div class="spinner-icon"></div></div>');
138148

@@ -170,9 +180,9 @@ angular.module('chieffancypants.loadingBar', [])
170180
loadingBar.css('width', pct);
171181
status = n;
172182

173-
// increment loadingbar to give the illusion that there is always progress
174-
// but make sure to cancel the previous timeouts so we don't have multiple
175-
// incs running at the same time.
183+
// increment loadingbar to give the illusion that there is always
184+
// progress but make sure to cancel the previous timeouts so we don't
185+
// have multiple incs running at the same time.
176186
$timeout.cancel(incTimeout);
177187
incTimeout = $timeout(function() {
178188
_inc();

test/loading-bar-interceptor.coffee

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
isLoadingBarInjected = (doc) ->
2-
injected = false
3-
divs = angular.element(doc).find('div')
4-
for i in divs
5-
if angular.element(i).attr('id') is 'loading-bar'
6-
injected = true
7-
break
8-
return injected
91

102
describe 'loadingBarInterceptor Service', ->
113

12-
$http = $httpBackend = $document = $timeout = result = loadingBar = null
4+
isLoadingBarInjected = () ->
5+
injected = false
6+
divs = $rootElement.find('div')
7+
for i in divs
8+
if angular.element(i).attr('id') is 'loading-bar'
9+
injected = true
10+
break
11+
return injected
12+
13+
$http = $httpBackend = $rootElement = $timeout = result = loadingBar = null
1314
response = {message:'OK'}
1415
endpoint = '/service'
1516

@@ -19,10 +20,10 @@ describe 'loadingBarInterceptor Service', ->
1920
return
2021

2122
result = null
22-
inject (_$http_, _$httpBackend_, _$document_, _$timeout_) ->
23+
inject (_$http_, _$httpBackend_, _$rootElement_, _$timeout_) ->
2324
$http = _$http_
2425
$httpBackend = _$httpBackend_
25-
$document = _$document_
26+
$rootElement = _$rootElement_
2627
$timeout = _$timeout_
2728

2829
beforeEach ->
@@ -35,6 +36,8 @@ describe 'loadingBarInterceptor Service', ->
3536
return this.actual > low && this.actual < high
3637

3738

39+
40+
3841
afterEach ->
3942
$httpBackend.verifyNoOutstandingRequest()
4043
$timeout.verifyNoPendingTasks()
@@ -158,7 +161,7 @@ describe 'loadingBarInterceptor Service', ->
158161

159162
$httpBackend.flush(1)
160163

161-
injected = isLoadingBarInjected $document.find(cfpLoadingBar.parentSelector)
164+
injected = isLoadingBarInjected $rootElement.find(cfpLoadingBar.parentSelector)
162165

163166
expect(injected).toBe true
164167
$httpBackend.flush()
@@ -172,12 +175,12 @@ describe 'loadingBarInterceptor Service', ->
172175
$http.get(endpoint)
173176

174177
$timeout.flush() # loading bar is animated, so flush timeout
175-
expect(isLoadingBarInjected($document.find(cfpLoadingBar.parentSelector))).toBe true
178+
expect(isLoadingBarInjected($rootElement.find(cfpLoadingBar.parentSelector))).toBe true
176179

177180
$httpBackend.flush()
178181
$timeout.flush()
179182

180-
expect(isLoadingBarInjected($document.find(cfpLoadingBar.parentSelector))).toBe false
183+
expect(isLoadingBarInjected($rootElement.find(cfpLoadingBar.parentSelector))).toBe false
181184

182185
it 'should get and set status', inject (cfpLoadingBar) ->
183186
cfpLoadingBar.start()

0 commit comments

Comments
 (0)