stimulus-check-all
Version:
A Stimulus Controller to implement check-all checkbox feature
3 lines (2 loc) • 4.38 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("stimulus")):"function"==typeof define&&define.amd?define(["stimulus"],e):(t=t||self).StimulusCheckAll=e(t.Stimulus)}(this,(function(t){"use strict";function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function r(t){return(r=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function c(t,e){return(c=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function o(t,e){return!e||"object"!=typeof e&&"function"!=typeof e?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):e}function i(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}();return function(){var n,c=r(t);if(e){var i=r(this).constructor;n=Reflect.construct(c,arguments,i)}else n=c.apply(this,arguments);return o(this,n)}}function u(t){let e=!1,n=null;function r(t,e,n,r=!1){e instanceof HTMLInputElement&&(e.indeterminate=r,e.checked!==n&&(e.checked=n,setTimeout(()=>{const n=new CustomEvent("change",{bubbles:!0,cancelable:!1,detail:{relatedTarget:t}});e.dispatchEvent(n)})))}function c(c){const o=c.target;o instanceof Element&&(o.hasAttribute("data-check-all")?function(e){if(e instanceof CustomEvent&&e.detail){const{relatedTarget:t}=e.detail;if(t&&t.hasAttribute("data-check-all-item"))return}const c=e.target;if(!(c instanceof HTMLInputElement))return;n=null;for(const e of t.querySelectorAll("[data-check-all-item]"))r(c,e,c.checked);c.indeterminate=!1,i()}(c):o.hasAttribute("data-check-all-item")&&function(c){if(c instanceof CustomEvent&&c.detail){const{relatedTarget:t}=c.detail;if(t&&(t.hasAttribute("data-check-all")||t.hasAttribute("data-check-all-item")))return}const o=c.target;if(!(o instanceof HTMLInputElement))return;const u=Array.from(t.querySelectorAll("[data-check-all-item]"));if(e&&n){const[t,e]=[u.indexOf(n),u.indexOf(o)].sort();for(const n of u.slice(t,+e+1||9e9))r(o,n,o.checked)}e=!1,n=o;const a=t.querySelector("[data-check-all]");if(a){const t=u.length,e=u.filter(t=>t instanceof HTMLInputElement&&t.checked).length;r(o,a,e===t,t>e&&e>0)}i()}(c))}function o(t){if(!(t.target instanceof Element))return;(t.target instanceof HTMLLabelElement&&t.target.control||t.target).hasAttribute("data-check-all-item")&&(e=t.shiftKey)}function i(){const e=t.querySelector("[data-check-all-count]");if(e){const n=t.querySelectorAll("[data-check-all-item]:checked").length;e.textContent=n.toString()}}return t.addEventListener("mousedown",o),t.addEventListener("change",c),{unsubscribe:()=>{t.removeEventListener("mousedown",o),t.removeEventListener("change",c)}}}var a=function(t){!function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&c(t,e)}(s,t);var r,o,a,l=i(s);function s(){return e(this,s),l.apply(this,arguments)}return r=s,(o=[{key:"connect",value:function(){this._setAttributes(),this._subscription=u(this.element)}},{key:"disconnect",value:function(){this._subscription.unsubscribe(),this._subscription=null}},{key:"_setAttributes",value:function(){this.hasAllTarget&&this.allTarget.setAttribute("data-check-all",""),this.hasCountTarget&&this.countTarget.setAttribute("data-check-all-count",""),this.checkboxes.forEach((function(t){return t.setAttribute("data-check-all-item","")}))}},{key:"checkboxes",get:function(){var t=this.element.querySelectorAll('input[type="checkbox"]:not([data-check-all]):not([data-skip])');return Array.from(t)}},{key:"checked",get:function(){return this.checkboxes.filter((function(t){return t.checked}))}},{key:"unchecked",get:function(){return this.checkboxes.filter((function(t){return!t.checked}))}}])&&n(r.prototype,o),a&&n(r,a),s}(t.Controller);return a.targets=["all","count"],a}));
//# sourceMappingURL=stimulus-check-all.umd.min.js.map