-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from skazi0/lazy-spinner
Lazy spinner directive
- Loading branch information
Showing
8 changed files
with
208 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
assets/app/widgets/suse-lazy-spinner/suse-lazy-spinner.directive.jade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
i.fa.fa-fw.fa-spin.fa-spinner(aria-hidden="true") |
60 changes: 60 additions & 0 deletions
60
assets/app/widgets/suse-lazy-spinner/suse-lazy-spinner.directive.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
(function() { | ||
'use strict'; | ||
|
||
angular | ||
.module('crowbarWidgets') | ||
.directive('suseLazySpinner', suseLazySpinner) | ||
.constant('SUSE_LAZY_SPINNER', { DEFAULT_DELAY: 2000 }); | ||
|
||
suseLazySpinner.$inject = ['$timeout', 'SUSE_LAZY_SPINNER']; | ||
|
||
function suseLazySpinner($timeout, SUSE_LAZY_SPINNER) { | ||
return { | ||
restrict: 'E', | ||
templateUrl: 'app/widgets/suse-lazy-spinner/suse-lazy-spinner.directive.html', | ||
scope: { | ||
active: '=', | ||
visible: '=?', | ||
delay: '@' | ||
}, | ||
link: function(scope, elem) { | ||
// dummy-init optional binding values | ||
scope.visible = scope.visible || false; | ||
|
||
scope.$watch('active', function(newVal) { | ||
newVal ? showSpinner() : hideSpinner(); | ||
}); | ||
|
||
elem.on('$destroy', hideSpinner); | ||
|
||
var timer = null; | ||
|
||
function showSpinner() { | ||
if (timer) { | ||
return; | ||
} | ||
timer = $timeout(function() { | ||
elem.removeClass('hidden'); | ||
scope.visible = true; | ||
}, getDelay()); | ||
} | ||
|
||
function hideSpinner() { | ||
// cancel (possibly) pending timer | ||
if (timer) { | ||
$timeout.cancel(timer); | ||
timer = null; | ||
} | ||
|
||
elem.addClass('hidden'); | ||
scope.visible = false; | ||
} | ||
|
||
function getDelay() { | ||
var delay = parseInt(scope.delay); | ||
return isFinite(delay) ? delay : SUSE_LAZY_SPINNER.DEFAULT_DELAY; | ||
} | ||
} | ||
}; | ||
} | ||
})(); |
129 changes: 129 additions & 0 deletions
129
assets/app/widgets/suse-lazy-spinner/suse-lazy-spinner.directive.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/*global bard $httpBackend assert $rootScope $compile $timeout */ | ||
describe('SUSE Lazy Spinner Directive', function () { | ||
|
||
var directiveElement, | ||
scope; | ||
|
||
beforeEach(function () { | ||
bard.appModule('crowbarApp'); | ||
|
||
bard.inject('$rootScope', '$compile', '$httpBackend', '$timeout'); | ||
|
||
//Mock requests that are expected to be made | ||
$httpBackend.expectGET('app/features/upgrade/i18n/en.json').respond({}); | ||
$httpBackend.flush(); | ||
|
||
scope = $rootScope.$new(); | ||
}); | ||
|
||
function compileDirective(template) { | ||
var element = angular.element(template), | ||
compiledElement = $compile(element)(scope); | ||
scope.$digest(); | ||
|
||
return compiledElement; | ||
} | ||
|
||
// Verify no unexpected http call has been made | ||
bard.verifyNoOutstandingHttpRequests(); | ||
|
||
describe('directive created with default parameters', function () { | ||
beforeEach(function () { | ||
directiveElement = compileDirective('<suse-lazy-spinner active="testActive"></suse-lazy-spinner>'); | ||
}); | ||
|
||
it('should contain an icon with fa-spin class', function () { | ||
var icons = directiveElement.find('i'); | ||
expect(icons.length).toEqual(1); | ||
assert.isTrue(angular.element(icons[0]).hasClass('fa-spin')); | ||
}); | ||
|
||
describe('attached to inactive model', function () { | ||
beforeEach(function () { | ||
scope.testActive = false; | ||
scope.$digest(); | ||
// make sure nothing is scheduled | ||
$timeout.flush(); | ||
}); | ||
|
||
it('should be inactive', function () { | ||
assert.isFalse(directiveElement.isolateScope().active); | ||
}); | ||
|
||
it('should be hidden', function () { | ||
assert.isTrue(directiveElement.hasClass('hidden')); | ||
}); | ||
}); | ||
|
||
describe('attached to active model', function () { | ||
beforeEach(function () { | ||
scope.testActive = true; | ||
scope.$digest(); | ||
}); | ||
|
||
it('should be active', function () { | ||
assert.isTrue(directiveElement.isolateScope().active); | ||
}); | ||
|
||
it('should be hidden before the timeout', function () { | ||
assert.isTrue(directiveElement.hasClass('hidden')); | ||
}); | ||
|
||
it('should not be hidden after the timeout', function () { | ||
$timeout.flush(); | ||
assert.isFalse(directiveElement.hasClass('hidden')); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('directive with custom delay', function () { | ||
beforeEach(function () { | ||
scope.testActive = true; | ||
directiveElement = compileDirective( | ||
'<suse-lazy-spinner active="testActive" delay="100"></suse-lazy-spinner>' | ||
); | ||
}); | ||
|
||
it('should have the delay value in the internal scope', function () { | ||
// NOTE: the value is parsed internally in the directive which is not visible from the outside | ||
expect(directiveElement.isolateScope().delay).toEqual('100'); | ||
}); | ||
}); | ||
|
||
describe('directive with exposed visiblity', function () { | ||
beforeEach(function () { | ||
directiveElement = compileDirective( | ||
'<suse-lazy-spinner active="testActive" visible="spinnerVisible"></suse-lazy-spinner>' | ||
); | ||
}); | ||
|
||
describe('attached to active model', function () { | ||
beforeEach(function () { | ||
scope.testActive = true; | ||
scope.$digest(); | ||
}); | ||
|
||
it('should set visibility to false before timeout', function () { | ||
assert.isFalse(scope.spinnerVisible); | ||
}); | ||
|
||
it('should set visibility to true after timeout', function () { | ||
$timeout.flush(); | ||
assert.isTrue(scope.spinnerVisible); | ||
}); | ||
}); | ||
|
||
describe('attached to inactive model', function () { | ||
beforeEach(function () { | ||
scope.testActive = false; | ||
scope.$digest(); | ||
// make sure nothing is scheduled | ||
$timeout.flush(); | ||
}); | ||
|
||
it('should set visibility to false', function () { | ||
assert.isFalse(scope.spinnerVisible); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* Start= widgets/suse-lazy-spinner/suse-lazy-spinner.less */ | ||
suse-lazy-spinner { | ||
display: inline-block; | ||
} | ||
/* End= widgets/suse-lazy-spinner/suse-lazy-spinner.less */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters