You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.6 KiB

  1. (function($K)
  2. {
  3. $K.add('module', 'check', {
  4. init: function(app, context)
  5. {
  6. this.app = app;
  7. // defaults
  8. var defaults = {
  9. target: false,
  10. classname: 'ch'
  11. };
  12. // context
  13. this.context = context;
  14. this.params = context.getParams(defaults);
  15. this.$element = context.getElement();
  16. this.$target = context.getTarget();
  17. },
  18. // events
  19. onclick: function(e, element, type)
  20. {
  21. if (type === 'all')
  22. {
  23. this._toggleAll();
  24. }
  25. },
  26. // public
  27. start: function()
  28. {
  29. this.$checkall = this.$element.find('[data-type=all]');
  30. this.$checkboxes = this.$element.find('.' + this.params.classname);
  31. this.$checkboxes.on('click.kube.check', this._toggle.bind(this));
  32. this._buildChecked();
  33. },
  34. stop: function()
  35. {
  36. this.$checkboxes.off('.kube.check');
  37. this.$target.val('');
  38. },
  39. // private
  40. _buildChecked: function()
  41. {
  42. if (!this.params.target) return;
  43. var arr = this.$target.val().split(',');
  44. this.$checkboxes.each(function(node)
  45. {
  46. if (arr.indexOf(node.value) !== -1)
  47. {
  48. node.checked = true;
  49. }
  50. });
  51. this.$checkall.attr('checked', this._isCheckedAll());
  52. },
  53. _setTarget: function()
  54. {
  55. if (!this.params.target) return;
  56. var arr = [];
  57. this.$checkboxes.each(function(node)
  58. {
  59. if (node.checked)
  60. {
  61. arr.push(node.value);
  62. }
  63. });
  64. var value = (arr.length === 0) ? '' : arr.join(',');
  65. this.$target.val(value);
  66. this.app.broadcast('check.set', this, this.$target);
  67. },
  68. _isCheckedAll: function()
  69. {
  70. var count = 0;
  71. var len = this.$checkboxes.length;
  72. this.$checkboxes.each(function(node)
  73. {
  74. if (node.checked)
  75. {
  76. count++;
  77. }
  78. });
  79. return (len === count);
  80. },
  81. _toggleAll: function(element)
  82. {
  83. var isChecked = this.$checkall.attr('checked');
  84. this.$checkboxes.attr('checked', isChecked);
  85. this._setTarget();
  86. },
  87. _toggle: function()
  88. {
  89. this.$checkall.attr('checked', this._isCheckedAll());
  90. this._setTarget();
  91. }
  92. });
  93. })(Kube);