Repository navigation
Expand file tree
/
Copy pathangular1-select-all.js
More file actions
339 lines (282 loc) · 15.1 KB
/
Copy pathangular1-select-all.js
File metadata and controls
339 lines (282 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**@preserve
* angular1-select-all module.
* @version 1.1.0
* @link https://manikantag/github.io/angular1-select-all
* @author Manikanta G
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
; (function() {
'use strict';
/**
* Angular 1.x directive to handle checkbox 'Select All' functionality, with below support:
*
* - Checking/unchecking 'Select All' will make all member checkboxes to check/uncheck, and selected count will be updated.
* - Checking/unchecking any member checkbox will checks/unchecks 'Select All' checkbox appropriately, and selected count will be updated.
*
* <p>Note: Each member's <code>ng-model</code> should use <code>memberModelKey</code> property of
* <code>mgCbSelectAllConfig</code> value object as checkbox model. Default is '<code>__selected</code>'</p>
* @example
* ```html
* <div mg-cb-group>
* <label><input type="checkbox" ng-model="cb.selectAll" mg-cb-select-all="persons" mg-cb-selected-count="cb.selectedCount" > Select All</label>
* <button mg-cb-clear>Clear all</button>
*
* <p ng-repeat="person in persons">
* <label>
* <input type="checkbox" ng-model="person.__selected" mg-cb-member mg-cb-member-index="$index" > {{::person.name}}
* </label>
* </p>
* </div>
* ```
* @author Manikanta G
*/
angular.module('angular1-select-all', [])
.value('mgCbSelectAllConfig', {
memberModelKey: '__selected'
})
/**
* @ngdoc directive
* @name mgCbGroup
* @restrict EA
* @description
* <code>mg-cb-group</code> directive should be used on parent element which wraps
* <code>mg-cb-select-all</code> and all <code>mg-cb-member</code> directives.
* @scope
*/
.directive('mgCbGroup', [ 'mgCbSelectAllConfig', function(mgCbSelectAllConfig) {
return {
scope: {
mgCbGroup: '='
},
restrict: 'AE',
controller: ['$scope', function($scope) {
var callbackFn;
$scope.$on('$destroy', function() {
callbackFn = null;
});
return {
getMemberFilterFn: function () {
if ($scope.mgCbGroup && $scope.mgCbGroup.memberFilterFn) {
return $scope.mgCbGroup.memberFilterFn;
}
},
notify: function(memberElement, memberScope) {
callbackFn && callbackFn(memberElement, memberScope);
},
subscribe: function(cbFn) {
callbackFn = cbFn;
},
getMemberModelKey: function() {
return $scope.mgCbGroup && $scope.mgCbGroup.memberModelKey || mgCbSelectAllConfig.memberModelKey || '__selected';
}
};
}]
};
}])
/**
* @ngdoc directive
* @name mgCbSelectAll
* @restrict EA
* @description
* <code>mg-cb-select-all</code> directive should be used with 'Select All' checkbox.
* This will handle calculating the selected checkbox count and selecting/deselecting
* member checkboxes.
* This directive depends on element's <code>ng-model</code> and <code>mg-cb-group</code> on parent element.
* @scope
* - {Array|Object} mgCbSelectAll Array or Object which is used for all member checkboxes.
* - {String} mgCbSelectedCount Scope property which will be updated with selected member checkbox count.
*/
.directive('mgCbSelectAll', ['$timeout', function($timeout) {
return {
scope: {
mgCbSelectAll: '=', // Array or Object used in ng-repeat for all member checkboxes.
mgCbSelectedCount: '=', // Selected checkbox count.
ngModel: '=' // ng-model is required to restore it's original value from Object (set by mg-cb-member directive) to boolean.
},
restrict: 'AE',
require: ['ngModel', '^mgCbGroup'],
link: function(scope, element, attr, controllers) {
var ngModelCtrl = controllers[0],
mgCbGroupCtrl = controllers[1],
init = true;
var memberModelKey = mgCbGroupCtrl.getMemberModelKey();
var _memberFilterFunction = mgCbGroupCtrl.getMemberFilterFn();
console.debug("mgCbSelectAll: Using '%s' as memberModelKey", memberModelKey);
// Set the selected checkbox count initially.
$timeout(function() {
scope.mgCbSelectedCount = getSelectedCount()[0];
});
mgCbGroupCtrl.subscribe(function(memberElement, memberScope) {
console.debug('mgCbSelectAll: subscribe(): new event', memberElement, memberScope);
// Reset the mgCbSelectAll ng-model value to boolean from Object.
// http://stackoverflow.com/a/31827383/340290
scope.$apply(function() {
// ngModelCtrl.$modelValue = {bymgCbMember: true};
scope.ngModel = { bymgCbMember: true };
});
});
// View value is changed (i.e., checkbox value is changed) --> cascade this change to all member checkboxes.
ngModelCtrl.$parsers.push(function(viewValue) {
if (!init) {
console.debug('mgCbSelectAll: $parsers(): view value changed to %s --> updating all checkboxes', viewValue);
cacscadeSelectAllToAllChecboxes(viewValue);
}
init && (init = false);
return viewValue;
});
// Model value is changed (this could be from controller)
ngModelCtrl.$formatters.push(function(modelValue) {
var selectedCounts;
// If this is the first time this $formatter being invoked (which means this directive
// is just initialised), or if this ng-model is 'true' (which Indicates the 'Select All'
// checkbox is selected initially), then only do the required stuff.
if (modelValue || !init) {
// If the modelValue is an object instead of boolean, this model change is triggered due to
// the member checkbox change (see mg-cb-member/mg-cb-clear directive) --> just re-calculate the selected
// count. Do NOT cascade this change to all member checkboxes (which will cause recursive unchecks).
if (angular.isObject(modelValue) && modelValue.bymgCbMember) {
console.debug('mgCbSelectAll: $formatters(): model value changed to %s by mg-cb-member/mg-cb-clear directive --> re-calculating selected count', modelValue);
selectedCounts = getSelectedCount();
scope.mgCbSelectedCount = selectedCounts[0];
// 'Select All' is checked when the checked count is same as the total members count.
modelValue = scope.mgCbSelectAll.length && scope.mgCbSelectedCount === scope.mgCbSelectAll.length - selectedCounts[1];
// Reset the mgCbSelectAll ng-model value to boolean from Object.
// http://stackoverflow.com/a/31827383/340290
ngModelCtrl.$modelValue = modelValue;
scope.ngModel = modelValue;
} else {
console.debug('mgCbSelectAll: $formatters(): model value changed to %s --> updating all checkboxes', modelValue);
// This model change is not triggered by the member checkbox --> cascade this change to all member checkboxes.
cacscadeSelectAllToAllChecboxes(modelValue);
}
}
init && (init = false);
return modelValue;
});
scope.$watch('mgCbSelectAll.length', function(newLength) {
console.debug('mgCbSelectAll: $watch(): mgCbSelectAll array length changed to: %i', newLength);
// UPDATE: DO NOT DO THIS: This is clearing the selected checkbox too.
// scope.mgCbSelectedCount = getSelectedCount();
// ngModelCtrl.$setViewValue(scope.mgCbSelectAll.length && scope.mgCbSelectedCount === scope.mgCbSelectAll.length);
// Inform the $formatter to recalcuate the selected count. Workaround for the above issue.
scope.ngModel = { bymgCbMember: true };
ngModelCtrl.$render();
});
// Checks/unchecks all the member checkboxes & updates the selected count.
function cacscadeSelectAllToAllChecboxes(isSelectedAll) {
var i,
membersCnt = 0;
for (i = 0; i < scope.mgCbSelectAll.length; i++) {
if (_memberFilterFunction) {
if (!_memberFilterFunction(scope.mgCbSelectAll[i], i)) {
continue;
}
}
scope.mgCbSelectAll[i][memberModelKey] = isSelectedAll;
membersCnt++;
}
scope.mgCbSelectedCount = isSelectedAll ? membersCnt : 0;
}
// Returns currently selected member checkboxes count.
function getSelectedCount() {
var i,
membersCnt = scope.mgCbSelectAll.length,
selectedMembersCount = 0,
filteredOutMembersCount = 0;
for (i = 0; i < membersCnt; i++) {
if (_memberFilterFunction) {
if (!_memberFilterFunction(scope.mgCbSelectAll[i], i)) {
filteredOutMembersCount ++;
continue;
}
}
if (scope.mgCbSelectAll[i][memberModelKey]) {
selectedMembersCount ++;
}
}
return [selectedMembersCount, filteredOutMembersCount];
}
}
};
}])
/**
* @ngdoc directive
* @name mgCbMember
* @restrict EA
* @description
* <code>mg-cb-member</code> directive should be used with each member checkbox.
* <p>This directive depends on element's <code>ng-model</code> and <code>mg-cb-group</code> on parent element.
* This will communicates with <code>mg-cb-group</code> directive, which will
* notify the <code>mg-cb-select-all</code> directive to re-calculate the selected count.</p>
* @scope
* @param {Number} mgCbMemberIndex (Optional) Member index for logging purpose.
*/
.directive('mgCbMember', ['$parse', '$timeout', function($parse, $timeout) {
return {
scope: {},
restrict: 'AE',
require: ['ngModel', '^mgCbGroup'],
link: function(scope, element, attr, controllers) {
var ngModelCtrl = controllers[0],
memberIndex = -1,
mgCbGroupCtrl = controllers[1];
if (attr.mgCbMemberIndex) {
memberIndex = $parse(attr.mgCbMemberIndex)(scope.$parent);
}
// View value is changed (i.e., checkbox value is changed) --> Notify mg-cb-group
// as mg-cb-select-all need to update it's model and re-calculate the selected count.
ngModelCtrl.$parsers.push(function(viewValue) {
console.debug('mgCbMember %i: $parsers(): view value changed to %s --> notifying mg-cb-group', memberIndex, viewValue);
$timeout(function() {
mgCbGroupCtrl.notify('mgCbMember');
});
return viewValue;
});
}
};
}])
/**
* @ngdoc directive
* @name mgCbClear
* @restrict EA
* @description
* <code>mg-cb-clear</code> directive is used to clear all the selected checkboxes.
* @scope
* - {Array|Object} mgCbClear Array or Object which is used for all member checkboxes.
*/
.directive('mgCbClear', [ function() {
return {
scope: {
mgCbMembers: '=mgCbClear' // Array or Object used in ng-repeat for all member checkboxes.
},
restrict: 'AE',
require: ['^mgCbGroup'],
link: function(scope, element, attr, controllers) {
var mgCbGroupCtrl = controllers[0];
var memberModelKey = mgCbGroupCtrl.getMemberModelKey();
var _memberFilterFunction = mgCbGroupCtrl.getMemberFilterFn();
if (!scope.mgCbMembers) {
console.warn('mgCbClear: mg-cb-members collection is not passed. Not initialising the directive.');
return;
}
element.on('click', function() {
var mgCbMembers = scope.mgCbMembers,
membersCnt = mgCbMembers.length,
i;
scope.$apply(function() {
for (i = 0; i < membersCnt; i++) {
if (_memberFilterFunction) {
if (!_memberFilterFunction(mgCbMembers[i], i)) {
continue;
}
}
mgCbMembers[i][memberModelKey] && (mgCbMembers[i][memberModelKey] = false);
}
});
mgCbGroupCtrl.notify('mgCbClear');
});
}
};
}])
;
})();