asselectable.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define("/Plugin/asselectable", ["exports", "jquery"], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(exports, require("jquery"));
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(mod.exports, global.jQuery);
  11. global.PluginAsselectable = mod.exports;
  12. }
  13. })(this, function (_exports, _jquery) {
  14. "use strict";
  15. Object.defineProperty(_exports, "__esModule", {
  16. value: true
  17. });
  18. _exports.default = void 0;
  19. _jquery = babelHelpers.interopRequireDefault(_jquery);
  20. var pluginName = 'asSelectable';
  21. var defaults = {
  22. allSelector: '.selectable-all',
  23. itemSelector: '.selectable-item',
  24. rowSelector: 'tr',
  25. rowSelectable: false,
  26. rowActiveClass: 'active',
  27. onChange: null
  28. };
  29. var asSelectable =
  30. /*#__PURE__*/
  31. function () {
  32. function asSelectable(element, options) {
  33. babelHelpers.classCallCheck(this, asSelectable);
  34. this.element = element;
  35. this.$element = (0, _jquery.default)(element);
  36. this.options = _jquery.default.extend({}, defaults, options, this.$element.data());
  37. this.init();
  38. }
  39. babelHelpers.createClass(asSelectable, [{
  40. key: "init",
  41. value: function init() {
  42. var self = this;
  43. var options = this.options;
  44. self.$element.on('change', options.allSelector, function () {
  45. var value = (0, _jquery.default)(this).prop('checked');
  46. self.getItems().each(function () {
  47. var $one = (0, _jquery.default)(this);
  48. $one.prop('checked', value).trigger('change', [true]);
  49. self.selectRow($one, value);
  50. });
  51. });
  52. self.$element.on('click', options.itemSelector, function (e) {
  53. var $one = (0, _jquery.default)(this);
  54. var value = $one.prop('checked');
  55. self.selectRow($one, value);
  56. e.stopPropagation();
  57. });
  58. self.$element.on('change', options.itemSelector, function () {
  59. var $all = self.$element.find(options.allSelector);
  60. var $row = self.getItems();
  61. var total = $row.length;
  62. var checked = self.getSelected().length;
  63. if (total === checked) {
  64. $all.prop('checked', true);
  65. } else {
  66. $all.prop('checked', false);
  67. }
  68. self._trigger('change', checked);
  69. if (typeof options.callback === 'function') {
  70. options.callback.call(this);
  71. }
  72. });
  73. if (options.rowSelectable) {
  74. self.$element.on('click', options.rowSelector, function (e) {
  75. if (e.target.type !== 'checkbox' && e.target.type !== 'button' && e.target.tagName.toLowerCase() !== 'a' && !(0, _jquery.default)(e.target).parent('div.checkbox-custom').length) {
  76. var $checkbox = (0, _jquery.default)(options.itemSelector, this);
  77. var value = $checkbox.prop('checked');
  78. $checkbox.prop('checked', !value);
  79. self.selectRow($checkbox, !value);
  80. }
  81. });
  82. }
  83. }
  84. }, {
  85. key: "selectRow",
  86. value: function selectRow(item, value) {
  87. if (value) {
  88. item.parents(this.options.rowSelector).addClass(this.options.rowActiveClass);
  89. } else {
  90. item.parents(this.options.rowSelector).removeClass(this.options.rowActiveClass);
  91. }
  92. }
  93. }, {
  94. key: "getItems",
  95. value: function getItems() {
  96. return this.$element.find(this.options.itemSelector);
  97. }
  98. }, {
  99. key: "getSelected",
  100. value: function getSelected() {
  101. return this.getItems().filter(':checked');
  102. }
  103. }, {
  104. key: "_trigger",
  105. value: function _trigger(eventType) {
  106. var method_arguments = Array.prototype.slice.call(arguments, 1);
  107. var data = [this].concat(method_arguments); // event
  108. this.$element.trigger("".concat(pluginName, "::").concat(eventType), data); // callback
  109. eventType = eventType.replace(/\b\w+\b/g, function (word) {
  110. return word.substring(0, 1).toUpperCase() + word.substring(1);
  111. });
  112. var onFunction = "on".concat(eventType);
  113. if (typeof this.options[onFunction] === 'function') {
  114. this.options[onFunction].apply(this, method_arguments);
  115. }
  116. }
  117. }], [{
  118. key: "_jQueryInterface",
  119. value: function _jQueryInterface(options) {
  120. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  121. args[_key - 1] = arguments[_key];
  122. }
  123. if (typeof options === 'string') {
  124. var method = options;
  125. if (/^\_/.test(method)) {
  126. return false;
  127. } else if (/^(get)/.test(method)) {
  128. var api = this.first().data(pluginName);
  129. if (api && typeof api[method] === 'function') {
  130. return api[method].apply(api, args);
  131. }
  132. } else {
  133. return this.each(function () {
  134. var api = _jquery.default.data(this, pluginName);
  135. if (api && typeof api[method] === 'function') {
  136. api[method].apply(api, args);
  137. }
  138. });
  139. }
  140. } else {
  141. return this.each(function () {
  142. if (!_jquery.default.data(this, pluginName)) {
  143. _jquery.default.data(this, pluginName, new asSelectable(this, options));
  144. }
  145. });
  146. }
  147. }
  148. }]);
  149. return asSelectable;
  150. }();
  151. _jquery.default.fn[pluginName] = asSelectable._jQueryInterface;
  152. _jquery.default.fn[pluginName].constructor = asSelectable;
  153. _jquery.default.fn[pluginName].noConflict = function () {
  154. _jquery.default.fn[pluginName] = window.JQUERY_NO_CONFLICT;
  155. return asSelectable._jQueryInterface;
  156. };
  157. var _default = asSelectable;
  158. _exports.default = _default;
  159. });