-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathangular-multi-check.js
52 lines (47 loc) · 1.94 KB
/
angular-multi-check.js
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
/*
* angular-multi-check
* http://schlogen.github.io/angular-multi-check/
* Version: 1.0 - 2014-24-11
* License: Apache
*/
angular.module('angular-multi-check', [])
.directive('multiCheckGroup', function() {
return {
scope: {},
controller: ['$scope', function($scope) {
this.getElements = function() {
var dataMultiCheck = Array.prototype.slice.call($scope.element[0].querySelectorAll('[data-multi-check]'), 0);
var multiCheck = Array.prototype.slice.call($scope.element[0].querySelectorAll('[multi-check]'), 0);
return multiCheck.concat(dataMultiCheck);
};
this.lastChecked = null;
}],
link: function(scope, element) {
scope.element = element;
}
};
})
.directive('multiCheck', function() {
return {
require: ['^ngModel', '^multiCheckGroup'],
restrict: 'A',
link: function(scope, el, attrs, controllers) {
var groupCtrl = controllers[1];
el.bind('click', function(event) {
var last = groupCtrl.lastChecked;
if (last && event.shiftKey) {
var chkboxes = groupCtrl.getElements(),
start = chkboxes.indexOf(event.target),
end = chkboxes.indexOf(last),
checked = last.checked;
angular.forEach(chkboxes.slice(Math.min(start, end), Math.max(start, end) + 1), function(box) {
var model = angular.element(box).data('$ngModelController');
model.$setViewValue(checked);
model.$render();
});
}
groupCtrl.lastChecked = event.target;
});
}
};
});