angular-multi-check.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * angular-multi-check
  3. * http://schlogen.github.io/angular-multi-check/
  4. * Version: 1.0 - 2014-24-11
  5. * License: Apache
  6. */
  7. angular.module('angular-multi-check', [])
  8. .directive('multiCheckGroup', function() {
  9. return {
  10. scope: {},
  11. controller: function($scope) {
  12. this.getElements = function() {
  13. var dataMultiCheck = Array.prototype.slice.call($scope.element[0].querySelectorAll('[data-multi-check]'), 0);
  14. var multiCheck = Array.prototype.slice.call($scope.element[0].querySelectorAll('[multi-check]'), 0);
  15. return multiCheck.concat(dataMultiCheck);
  16. };
  17. this.lastChecked = null;
  18. },
  19. link: function(scope, element) {
  20. scope.element = element;
  21. }
  22. };
  23. })
  24. .directive('multiCheck', function() {
  25. return {
  26. require: ['^ngModel', '^multiCheckGroup'],
  27. restrict: 'A',
  28. link: function(scope, el, attrs, controllers) {
  29. var groupCtrl = controllers[1];
  30. el.bind('click', function(event) {
  31. var last = groupCtrl.lastChecked;
  32. if (last && event.shiftKey) {
  33. var chkboxes = groupCtrl.getElements(),
  34. start = chkboxes.indexOf(event.target),
  35. end = chkboxes.indexOf(last),
  36. checked = last.checked;
  37. angular.forEach(chkboxes.slice(Math.min(start, end), Math.max(start, end) + 1), function(box) {
  38. var model = angular.element(box).data('$ngModelController');
  39. model.$setViewValue(checked);
  40. model.$render();
  41. });
  42. }
  43. groupCtrl.lastChecked = event.target;
  44. });
  45. }
  46. };
  47. });