Skip to content
This repository was archived by the owner on Apr 10, 2019. It is now read-only.

Commit c388c36

Browse files
committed
Merge pull request #7 from mburgosh/pr-add-class-option
Add optional class while button disabled
2 parents dd0948f + 7470d22 commit c388c36

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ If thats done then just follow those simple steps:
3131
```
3232
<button ng-click="doSomething()" ng-autodisable>Do something</button>
3333
```
34+
#### Loading class
35+
36+
You can optianlly add a list of classes which will be added to the element while this is disabled. This is usefull to add a spinner or something similar.
37+
38+
```
39+
<button ng-click="doSomething()" ng-autodisable-class="class1 class2" ng-autodisable>Do something</button>
40+
```
41+
3442

3543
### Demo
3644
---

src/angular-autodisable.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727

2828
var DISABLED = 'disabled', // Disabled attribute
2929
ATTRNAME = 'ngAutodisable', // The attribute name to which we store the handlers ids
30-
ELEMENT = null;
30+
ELEMENT = null,
31+
LOADING_CLASS = false,
32+
LOADING_CLASS_ATTR = 'ngAutodisableClass';
3133

3234
// Id for the registered handlers.
3335
// Will be incremented in order to make sure that handler is uniquely registered
@@ -88,6 +90,7 @@
8890
}
8991

9092
attrs.$set(DISABLED, value, true);
93+
toggleLoadingClass(ELEMENT);
9194
}
9295

9396
/**
@@ -105,6 +108,18 @@
105108

106109
var element = angular.element(ELEMENT).find('button[type=submit]');
107110
element.attr(DISABLED, !!value);
111+
toggleLoadingClass(element);
112+
}
113+
114+
/**
115+
* Toggles the loading class (if exist) to the element
116+
*
117+
* @param {Element} element element to get the class attached to
118+
*/
119+
function toggleLoadingClass(element) {
120+
if(LOADING_CLASS) {
121+
element.toggleClass(LOADING_CLASS);
122+
}
108123
}
109124

110125
/**
@@ -168,6 +183,9 @@
168183
restrict : 'A',
169184
compile : function(el, attrs) {
170185
ELEMENT = el;
186+
if ( attrs.hasOwnProperty(LOADING_CLASS_ATTR) ) {
187+
LOADING_CLASS = attrs[LOADING_CLASS_ATTR];
188+
}
171189
if( attrs.hasOwnProperty('ngClick') ) {
172190
type = types.click;
173191
} else if ( attrs.hasOwnProperty('ngSubmit') ) {

test/ng-autodisable-submit.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,29 @@ describe('angular autodisable', function() {
133133
}));
134134
});
135135

136+
describe('loading class', function() {
137+
it('should add a class to the button if the promise is not resolved', inject(function($q) {
138+
$rootScope.clickHandler = function() {
139+
var defer = $q.defer();
140+
return defer.promise;
141+
};
142+
var el = compile('<form ng-submit="clickHandler()" ng-autodisable-class="loading" ng-autodisable> <button type="submit"></button> </form>');
143+
el.find('button[type=submit]').click();
144+
expect(el.find('button[type=submit]').hasClass('loading')).toBe(true);
145+
}));
146+
147+
it('should add multiple class to the button if the promise is not resolved', inject(function($q) {
148+
$rootScope.clickHandler = function() {
149+
var defer = $q.defer();
150+
return defer.promise;
151+
};
152+
var el = compile('<form ng-submit="clickHandler()" ng-autodisable-class="firstClass secondClass" ng-autodisable> <button type="submit"></button> </form>');
153+
el.find('button[type=submit]').click();
154+
expect(el.find('button[type=submit]').hasClass('firstClass')).toBe(true);
155+
expect(el.find('button[type=submit]').hasClass('secondClass')).toBe(true);
156+
}));
157+
});
158+
136159
describe('http promise', function() {
137160
it('should disable the button if ngClick returns HTTP promise', inject(function($http, $httpBackend) {
138161
$rootScope.clickHandler = function() {

test/ng-autodisable.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,29 @@ describe('angular autodisable', function() {
133133
}));
134134
});
135135

136+
describe('loading class', function() {
137+
it('should add a class to the button if the promise is not resolved', inject(function($q) {
138+
$rootScope.clickHandler = function() {
139+
var defer = $q.defer();
140+
return defer.promise;
141+
};
142+
var el = compile('<button ng-click="clickHandler()" ng-autodisable-class="loading" ng-autodisable></button>');
143+
el.click();
144+
expect(el.hasClass('loading')).toBe(true);
145+
}));
146+
147+
it('should add multiple class to the button if the promise is not resolved', inject(function($q) {
148+
$rootScope.clickHandler = function() {
149+
var defer = $q.defer();
150+
return defer.promise;
151+
};
152+
var el = compile('<button ng-click="clickHandler()" ng-autodisable-class="firstClass secondClass" ng-autodisable></button>');
153+
el.click();
154+
expect(el.hasClass('firstClass')).toBe(true);
155+
expect(el.hasClass('secondClass')).toBe(true);
156+
}));
157+
});
158+
136159
describe('http promise', function() {
137160
it('should disable the button if ngClick returns HTTP promise', inject(function($http, $httpBackend) {
138161
$rootScope.clickHandler = function() {

0 commit comments

Comments
 (0)