This repository was archived by the owner on Apr 10, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +73
-1
lines changed Expand file tree Collapse file tree 4 files changed +73
-1
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,14 @@ If thats done then just follow those simple steps:
31
31
```
32
32
<button ng-click="doSomething()" ng-autodisable>Do something</button>
33
33
```
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
+
34
42
35
43
### Demo
36
44
---
Original file line number Diff line number Diff line change 27
27
28
28
var DISABLED = 'disabled' , // Disabled attribute
29
29
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' ;
31
33
32
34
// Id for the registered handlers.
33
35
// Will be incremented in order to make sure that handler is uniquely registered
88
90
}
89
91
90
92
attrs . $set ( DISABLED , value , true ) ;
93
+ toggleLoadingClass ( ELEMENT ) ;
91
94
}
92
95
93
96
/**
105
108
106
109
var element = angular . element ( ELEMENT ) . find ( 'button[type=submit]' ) ;
107
110
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
+ }
108
123
}
109
124
110
125
/**
168
183
restrict : 'A' ,
169
184
compile : function ( el , attrs ) {
170
185
ELEMENT = el ;
186
+ if ( attrs . hasOwnProperty ( LOADING_CLASS_ATTR ) ) {
187
+ LOADING_CLASS = attrs [ LOADING_CLASS_ATTR ] ;
188
+ }
171
189
if ( attrs . hasOwnProperty ( 'ngClick' ) ) {
172
190
type = types . click ;
173
191
} else if ( attrs . hasOwnProperty ( 'ngSubmit' ) ) {
Original file line number Diff line number Diff line change @@ -133,6 +133,29 @@ describe('angular autodisable', function() {
133
133
} ) ) ;
134
134
} ) ;
135
135
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
+
136
159
describe ( 'http promise' , function ( ) {
137
160
it ( 'should disable the button if ngClick returns HTTP promise' , inject ( function ( $http , $httpBackend ) {
138
161
$rootScope . clickHandler = function ( ) {
Original file line number Diff line number Diff line change @@ -133,6 +133,29 @@ describe('angular autodisable', function() {
133
133
} ) ) ;
134
134
} ) ;
135
135
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
+
136
159
describe ( 'http promise' , function ( ) {
137
160
it ( 'should disable the button if ngClick returns HTTP promise' , inject ( function ( $http , $httpBackend ) {
138
161
$rootScope . clickHandler = function ( ) {
You can’t perform that action at this time.
0 commit comments