@carry0987/check-box
Advanced tools
Comparing version 2.2.1 to 2.2.2
@@ -402,3 +402,3 @@ const defaults = { | ||
static instances = []; | ||
static version = '2.2.1'; | ||
static version = '2.2.2'; | ||
static firstLoad = true; | ||
@@ -405,0 +405,0 @@ element = null; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).checkBoxjs={})}(this,(function(e){"use strict";const t={checked:null,checkMark:"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9IiNmZmZmZmYiIHN0cm9rZS13aWR0aD0iMyIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIj48cG9seWxpbmUgcG9pbnRzPSIyMCA2IDkgMTcgNCAxMiI+PC9wb2x5bGluZT48L3N2Zz4=",checkAll:null,allowShiftKey:!1,bindLabel:!0,styles:{},onChange:()=>{},onCheckAll:()=>{}};function n(...e){console.error(...e)}function l(e){throw new Error(e)}var i=Object.freeze({__proto__:null,reportError:n,throwError:l});function c(e,t,n){if("string"!=typeof e)return e;let l=document;return null===t&&n?l=n:t&&t instanceof Node&&"querySelector"in t?l=t:n&&n instanceof Node&&"querySelector"in n&&(l=n),"all"===t?l.querySelectorAll(e):l.querySelector(e)}function o(e,t={},n=""){let l=document.createElement(e);for(let e in t)Object.prototype.hasOwnProperty.call(t,e)&&("textContent"===e||"innerText"===e?l.textContent=t[e]:l.setAttribute(e,t[e]));return n&&(l.textContent=n),l}let s="utils-style";const h={from:".utils",to:".utils-"};function a(e){return"object"==typeof e&&null!==e&&!r(e)}function r(e){return Array.isArray(e)}function d(e){return"number"!=typeof e&&("string"==typeof e&&0===e.length||(!(!r(e)||0!==e.length)||(!(!a(e)||0!==Object.keys(e).length)||!e)))}function u(e,...t){if(!t.length)return e;const n=t.shift();if(n)for(const t in n)if(Object.prototype.hasOwnProperty.call(n,t)){const l=n[t],i=t;a(l)||r(l)?(e[i]&&"object"==typeof e[i]||(e[i]=r(l)?[]:{}),u(e[i],l)):e[i]=l}return u(e,...t)}function k(e){s=e}function p(e,t){h.from=e,h.to=t}function g(e,t=null){t=d(t)?"":t;let n=o("style");n.id=s+t,n.textContent="",document.head.append(n);let l=n.sheet;for(let n in e)e.hasOwnProperty(n)&&b(l,n,f(e[n]),t)}function f(e){let t="";for(let[n,l]of Object.entries(e))n=n.replace(/([A-Z])/g,(e=>`-${e[0].toLowerCase()}`)),t+=`${n}:${l};`;return t}function b(e,t,n,l=null){l=d(l)?"":l;let i=t.replace(h.from,h.to+l);e.insertRule(i+"{"+n+"}",0)}function m(e=null){const t=d(e)?"":e;let n=c("#"+s+t);n&&n.parentNode&&n.parentNode.removeChild(n)}function C(e=8){let t="";const n="abcdefghijklmnopqrstuvwxyz0123456789";for(let l=0;l<e;l++){t+=n[Math.floor(36*Math.random())]}return t}function E(e,t,n){return new CustomEvent(e,{detail:t,...n})}var y=Object.freeze({__proto__:null,addEventListener:function(e,t,n,l){e.addEventListener(t,n,l)},createEvent:E,dispatchEvent:function(e,t=document,i,c){try{if("string"==typeof e){let n=E(e,i,c);return t.dispatchEvent(n)}if(e instanceof Event)return t.dispatchEvent(e);l("Invalid event type")}catch(e){return n("Dispatch Event Error:",e),!1}},removeEventListener:function(e,t,n,l){e.removeEventListener(t,n,l)}});class v{static throwError=i.throwError;static getElem=c;static deepMerge=u;static generateRandom=C;static injectStylesheet=g;static removeStylesheet=m;static setStylesheetId=k;static setReplaceRule=p;static isEmpty=d;static createEvent=y.createEvent;static addEventListener=y.addEventListener;static dispatchEvent=y.dispatchEvent;static getTemplate(e){return`\n <div class="checkbox check-box-${e=e.toString()}">\n <span class="checkmark"></span>\n <label class="checkbox-label"></label>\n </div>\n `}static getCheckAllElements(e){if(!e)return[];let t=[];return Array.isArray(e)?e.forEach((e=>{"string"==typeof e?t.push(...v.getElem(e,"all")):e instanceof HTMLInputElement&&t.push(e)})):"string"==typeof e?t.push(...v.getElem(e,"all")):e instanceof HTMLInputElement&&t.push(e),t.filter((e=>null!==e))}static handleCheckboxTitle(e,t){let n,l=e.title||e.dataset.checkboxTitle||null,i=!1,c=null,o=!1;if(t instanceof HTMLLabelElement){const s=t.htmlFor,h=t.dataset.checkboxFor,a=e.dataset.checkboxId;i=!v.isEmpty(e.id)&&s===e.id,o=!v.isEmpty(e.id)&&h===e.id,v.isEmpty(a)||h!==a||(c=v.isEmpty(e.id)&&v.isEmpty(s)?"check-"+v.generateRandom(6):null,o=!0),(o||i)&&(n=t.cloneNode(!0),l=l||t.textContent,t.parentNode.removeChild(t))}return{title:l,remainLabel:i,randomID:c,labelToRestore:n}}static insertCheckbox(e,t,n,l){let i=v.getTemplate(e),s=o("div");s.innerHTML=i.trim();let h=c(".checkmark",s),a=c("label",s),r=t.cloneNode(!0);return r.withID=!0,n&&(r.id=n,r.withID=!1),!0===l&&(a.htmlFor=r.id),h.addEventListener("click",(e=>{e.preventDefault(),r.click(),e.shiftKey&&this.triggerShiftClick(r)})),h.parentNode&&h.parentNode.insertBefore(r,h),t.parentNode.replaceChild(s.firstElementChild||s,t),{cloneEle:r,templateNode:s,labelNode:a}}static insertCheckboxTitle(e,t,n,l){e?(n.textContent=e,!0===t&&(n.classList.add("checkbox-labeled"),n.addEventListener("click",(e=>{e.preventDefault(),l.click(),e.shiftKey&&this.triggerShiftClick(l)})))):n.parentNode.removeChild(n)}static toggleCheckStatus(e,t){t?(e.checked=!0,e.setAttribute("checked","checked")):(e.checked=!1,e.removeAttribute("checked"))}static toggleDisableStatus(e,t){t?(e.disabled=!0,e.setAttribute("disabled","disabled")):(e.disabled=!1,e.removeAttribute("disabled"))}static toggleCheckAll(e,t){0!==e.length&&e.forEach((e=>{if(!t||!t.checked||!t.input)return void v.toggleCheckStatus(e,!1);const n=t.checked.length===t.input.length&&0!==t.checked.length;v.toggleCheckStatus(e,n)}))}static restoreElement(e){if("function"==typeof e.checkBoxChange&&e.removeEventListener("change",e.checkBoxChange),"function"==typeof e.checkAllChange&&e.removeEventListener("change",e.checkAllChange),!1===e.withID&&e.removeAttribute("id"),e.checkBoxChange=void 0,e.removeAttribute("data-checkbox"),e.parentNode){e.parentNode.replaceWith(e)}let t=e.labelToRestore;t&&t.nodeType===Node.ELEMENT_NODE&&e.parentNode?.insertBefore(t,e.nextSibling)}static triggerShiftClick(e){let t=v.createEvent("shift-click",{checkedElement:e});v.dispatchEvent(t,e)}}v.setStylesheetId("checkbox-style"),v.setReplaceRule(".check-box",".check-box-");const x=(e,t=!1)=>{!0===t?console.log("Data Type : "+typeof e,"\nValue : "+e):"boolean"!=typeof t?console.log(t):console.log(e)};class A{static instances=[];static version="2.2.1";static firstLoad=!0;element=null;options=t;id=0;allElement=[];total={input:[],checked:[],list:[]};checkAllElement=[];onChangeCallback;onCheckAllCallback;constructor(e,t){this.init(e,t,A.instances.length),A.instances.push(this),1===A.instances.length&&!0===A.firstLoad&&x(`CheckBox is loaded, version: ${A.version}`),A.firstLoad=!1}init(e,t,n){let l=null;return"string"==typeof e?l=v.getElem(e,"all"):e instanceof NodeList||e instanceof Array?l=e:e instanceof HTMLInputElement&&(l=[e]),l?(this.id=n,this.element=e,this.options=v.deepMerge({},this.options,t),this.injectStyles(),this.setupCallbacks(),l.forEach(((e,t)=>this.processCheckbox(e,t))),this.updateTotal(),this.options.checkAll&&this.processCheckAll(),this):v.throwError("Cannot find elements : "+e)}injectStyles(){let e={};this.options?.checkMark&&(e={".check-box input[type=checkbox] + .checkmark:after":{"background-image":"url("+this.options.checkMark+")"}}),this.options?.styles&&Object.keys(this.options.styles).length>0&&(e=v.deepMerge({},this.options.styles,e)),e&&v.injectStylesheet(e,this.id.toString())}setupCallbacks(){this.onChange=(e,t)=>{this.options?.onChange&&this.options.onChange(e,t)},this.onCheckAll=e=>{this.options?.onCheckAll&&this.options.onCheckAll(e)}}processCheckbox(e,t){if("checkbox"!==e.type)return;if(e.hasAttribute("data-checkbox"))return;e.setAttribute("data-checkbox","true");let n=e.nextElementSibling,l=this.options.bindLabel??!1,{title:i,remainLabel:c,randomID:o,labelToRestore:s}=v.handleCheckboxTitle(e,n);l=!!c||l,e.checked?v.toggleCheckStatus(e,!0):this.options.checked&&this.updateCheckboxStatus(e,t);let{cloneEle:h,labelNode:a}=v.insertCheckbox(this.id.toString(),e,o,c);v.insertCheckboxTitle(i,l,a,h);let r=this.checkBoxChange.bind(this,!0,h,void 0);h.addEventListener("change",r),h.checkBoxChange=r,this.options.allowShiftKey&&v.addEventListener(h,"shift-click",this.handleShiftClick),this.allElement.push(h),h.labelToRestore=s}updateCheckboxStatus(e,t){const n=this.options.checked;"boolean"==typeof n?v.toggleCheckStatus(e,n):"string"==typeof n||"number"==typeof n?e.value!==n.toString()&&t!==Number(n)||v.toggleCheckStatus(e,!0):Array.isArray(n)&&n.includes(e.value)&&v.toggleCheckStatus(e,!0)}processCheckAll(){const e=v.getCheckAllElements(this.options.checkAll);0!==e.length&&e.forEach((e=>{if(!e||"checkbox"!==e.type)return;if(e.hasAttribute("data-checkbox"))return;e.setAttribute("data-checkbox","true");const t=e.nextElementSibling;let{title:n,remainLabel:l,randomID:i,labelToRestore:c}=v.handleCheckboxTitle(e,t);n&&t&&"LABEL"===t.tagName&&(n=t.textContent||n,t.parentNode?.removeChild(t));const{cloneEle:o,templateNode:s,labelNode:h}=v.insertCheckbox(this.id.toString(),e,i,l);e.parentNode?.replaceChild(s.firstElementChild||s,e),v.insertCheckboxTitle(n,this.options.bindLabel??!1,h,o),o.addEventListener("change",this.checkAllChange),o.checkAllChange=this.checkAllChange,o.labelToRestore=c,this.checkAllElement.push(o),0===this.total.input.length&&v.toggleDisableStatus(o,!0),(!0===this.options.checked||e.checked)&&(v.toggleCheckStatus(o,!0),o.dispatchEvent(new Event("change")))}))}checkBoxChange(e,t,n){this.updateTotal(),e&&this.updateCheckAllStatus(),this.onChangeCallback?.(this.total,t),t&&v.toggleCheckStatus(t,n??t.checked),this.dispatchCheckboxChangeEvent()}checkAllChange=e=>{if(!(e.target instanceof HTMLInputElement))return;const t=e.target.checked;this.allElement.forEach((e=>{v.toggleCheckStatus(e,t)})),this.updateTotal(),this.updateCheckAllStatus(),this.checkBoxChange(!1),this.onCheckAllCallback&&this.onCheckAllCallback(t)};updateTotal(){this.total.input=[...this.allElement];const e=this.allElement.filter((e=>e.checked));this.total.checked=this.total.checked.filter((e=>e.checked));const t=e.filter((e=>!this.total.checked.includes(e)));this.total.checked.push(...t),this.total.list=this.total.checked.map((e=>e.value))}updateCheckAllStatus(){if(this.checkAllElement.length>0){const e=this.total.checked.length===this.total.input.length;this.checkAllElement.forEach((t=>{v.toggleCheckStatus(t,e)}))}}dispatchCheckboxChangeEvent(){const e=v.createEvent("checkbox-change",{detail:this.total});v.dispatchEvent(e)}handleShiftClick=e=>{const t=e.detail.checkedElement,n=this.getLastChecked(!0);if(!t||!n)return;let l=this.allElement.indexOf(n),i=this.allElement.indexOf(t);if(n&&-1!==l&&-1!==i){x("Shift-click event detected");const e=Math.min(l,i),n=Math.max(l,i);for(let l=e;l<=n;l++){const e=t.checked,n=this.allElement[l];v.toggleCheckStatus(n,e)}this.updateTotal(),this.updateCheckAllStatus(),this.dispatchCheckboxChangeEvent()}};getLastChecked(e=!1){const t=this.total.checked;let n=e?2:1;return 0===t.length?null:1===t.length?t[0]:t[t.length-n]}destroy(){A.firstLoad=!1,this.allElement.forEach((e=>{v.restoreElement(e)})),this.checkAllElement.length>0&&this.checkAllElement.forEach((e=>{e.checkAllChange&&(v.toggleCheckAll(this.checkAllElement),v.restoreElement(e))})),this.element=null,this.options=t,this.allElement=[],this.total={input:[],checked:[],list:[]},this.checkAllElement=[],v.removeStylesheet(this.id.toString());const e=A.instances.indexOf(this);-1!==e&&A.instances.splice(e,1)}set onChange(e){this.onChangeCallback=e}set onCheckAll(e){this.onCheckAllCallback=e}get elements(){return this.allElement}getCheckBox(){return this.total}refresh(){this.element&&this.init(this.element,this.options,this.id)}static destroyAll(){for(;A.instances.length;){A.instances[0].destroy()}}}var S=Object.freeze({__proto__:null}),L=Object.freeze({__proto__:null});e.CheckBox=A,e.CheckBoxInterface=S,e.CheckBoxType=L})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).checkBoxjs={})}(this,(function(e){"use strict";const t={checked:null,checkMark:"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9IiNmZmZmZmYiIHN0cm9rZS13aWR0aD0iMyIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIj48cG9seWxpbmUgcG9pbnRzPSIyMCA2IDkgMTcgNCAxMiI+PC9wb2x5bGluZT48L3N2Zz4=",checkAll:null,allowShiftKey:!1,bindLabel:!0,styles:{},onChange:()=>{},onCheckAll:()=>{}};function n(...e){console.error(...e)}function l(e){throw new Error(e)}var i=Object.freeze({__proto__:null,reportError:n,throwError:l});function c(e,t,n){if("string"!=typeof e)return e;let l=document;return null===t&&n?l=n:t&&t instanceof Node&&"querySelector"in t?l=t:n&&n instanceof Node&&"querySelector"in n&&(l=n),"all"===t?l.querySelectorAll(e):l.querySelector(e)}function o(e,t={},n=""){let l=document.createElement(e);for(let e in t)Object.prototype.hasOwnProperty.call(t,e)&&("textContent"===e||"innerText"===e?l.textContent=t[e]:l.setAttribute(e,t[e]));return n&&(l.textContent=n),l}let s="utils-style";const h={from:".utils",to:".utils-"};function a(e){return"object"==typeof e&&null!==e&&!r(e)}function r(e){return Array.isArray(e)}function d(e){return"number"!=typeof e&&("string"==typeof e&&0===e.length||(!(!r(e)||0!==e.length)||(!(!a(e)||0!==Object.keys(e).length)||!e)))}function u(e,...t){if(!t.length)return e;const n=t.shift();if(n)for(const t in n)if(Object.prototype.hasOwnProperty.call(n,t)){const l=n[t],i=t;a(l)||r(l)?(e[i]&&"object"==typeof e[i]||(e[i]=r(l)?[]:{}),u(e[i],l)):e[i]=l}return u(e,...t)}function k(e){s=e}function p(e,t){h.from=e,h.to=t}function g(e,t=null){t=d(t)?"":t;let n=o("style");n.id=s+t,n.textContent="",document.head.append(n);let l=n.sheet;for(let n in e)e.hasOwnProperty(n)&&b(l,n,f(e[n]),t)}function f(e){let t="";for(let[n,l]of Object.entries(e))n=n.replace(/([A-Z])/g,(e=>`-${e[0].toLowerCase()}`)),t+=`${n}:${l};`;return t}function b(e,t,n,l=null){l=d(l)?"":l;let i=t.replace(h.from,h.to+l);e.insertRule(i+"{"+n+"}",0)}function m(e=null){const t=d(e)?"":e;let n=c("#"+s+t);n&&n.parentNode&&n.parentNode.removeChild(n)}function C(e=8){let t="";const n="abcdefghijklmnopqrstuvwxyz0123456789";for(let l=0;l<e;l++){t+=n[Math.floor(36*Math.random())]}return t}function E(e,t,n){return new CustomEvent(e,{detail:t,...n})}var y=Object.freeze({__proto__:null,addEventListener:function(e,t,n,l){e.addEventListener(t,n,l)},createEvent:E,dispatchEvent:function(e,t=document,i,c){try{if("string"==typeof e){let n=E(e,i,c);return t.dispatchEvent(n)}if(e instanceof Event)return t.dispatchEvent(e);l("Invalid event type")}catch(e){return n("Dispatch Event Error:",e),!1}},removeEventListener:function(e,t,n,l){e.removeEventListener(t,n,l)}});class v{static throwError=i.throwError;static getElem=c;static deepMerge=u;static generateRandom=C;static injectStylesheet=g;static removeStylesheet=m;static setStylesheetId=k;static setReplaceRule=p;static isEmpty=d;static createEvent=y.createEvent;static addEventListener=y.addEventListener;static dispatchEvent=y.dispatchEvent;static getTemplate(e){return`\n <div class="checkbox check-box-${e=e.toString()}">\n <span class="checkmark"></span>\n <label class="checkbox-label"></label>\n </div>\n `}static getCheckAllElements(e){if(!e)return[];let t=[];return Array.isArray(e)?e.forEach((e=>{"string"==typeof e?t.push(...v.getElem(e,"all")):e instanceof HTMLInputElement&&t.push(e)})):"string"==typeof e?t.push(...v.getElem(e,"all")):e instanceof HTMLInputElement&&t.push(e),t.filter((e=>null!==e))}static handleCheckboxTitle(e,t){let n,l=e.title||e.dataset.checkboxTitle||null,i=!1,c=null,o=!1;if(t instanceof HTMLLabelElement){const s=t.htmlFor,h=t.dataset.checkboxFor,a=e.dataset.checkboxId;i=!v.isEmpty(e.id)&&s===e.id,o=!v.isEmpty(e.id)&&h===e.id,v.isEmpty(a)||h!==a||(c=v.isEmpty(e.id)&&v.isEmpty(s)?"check-"+v.generateRandom(6):null,o=!0),(o||i)&&(n=t.cloneNode(!0),l=l||t.textContent,t.parentNode.removeChild(t))}return{title:l,remainLabel:i,randomID:c,labelToRestore:n}}static insertCheckbox(e,t,n,l){let i=v.getTemplate(e),s=o("div");s.innerHTML=i.trim();let h=c(".checkmark",s),a=c("label",s),r=t.cloneNode(!0);return r.withID=!0,n&&(r.id=n,r.withID=!1),!0===l&&(a.htmlFor=r.id),h.addEventListener("click",(e=>{e.preventDefault(),r.click(),e.shiftKey&&this.triggerShiftClick(r)})),h.parentNode&&h.parentNode.insertBefore(r,h),t.parentNode.replaceChild(s.firstElementChild||s,t),{cloneEle:r,templateNode:s,labelNode:a}}static insertCheckboxTitle(e,t,n,l){e?(n.textContent=e,!0===t&&(n.classList.add("checkbox-labeled"),n.addEventListener("click",(e=>{e.preventDefault(),l.click(),e.shiftKey&&this.triggerShiftClick(l)})))):n.parentNode.removeChild(n)}static toggleCheckStatus(e,t){t?(e.checked=!0,e.setAttribute("checked","checked")):(e.checked=!1,e.removeAttribute("checked"))}static toggleDisableStatus(e,t){t?(e.disabled=!0,e.setAttribute("disabled","disabled")):(e.disabled=!1,e.removeAttribute("disabled"))}static toggleCheckAll(e,t){0!==e.length&&e.forEach((e=>{if(!t||!t.checked||!t.input)return void v.toggleCheckStatus(e,!1);const n=t.checked.length===t.input.length&&0!==t.checked.length;v.toggleCheckStatus(e,n)}))}static restoreElement(e){if("function"==typeof e.checkBoxChange&&e.removeEventListener("change",e.checkBoxChange),"function"==typeof e.checkAllChange&&e.removeEventListener("change",e.checkAllChange),!1===e.withID&&e.removeAttribute("id"),e.checkBoxChange=void 0,e.removeAttribute("data-checkbox"),e.parentNode){e.parentNode.replaceWith(e)}let t=e.labelToRestore;t&&t.nodeType===Node.ELEMENT_NODE&&e.parentNode?.insertBefore(t,e.nextSibling)}static triggerShiftClick(e){let t=v.createEvent("shift-click",{checkedElement:e});v.dispatchEvent(t,e)}}v.setStylesheetId("checkbox-style"),v.setReplaceRule(".check-box",".check-box-");const x=(e,t=!1)=>{!0===t?console.log("Data Type : "+typeof e,"\nValue : "+e):"boolean"!=typeof t?console.log(t):console.log(e)};class A{static instances=[];static version="2.2.2";static firstLoad=!0;element=null;options=t;id=0;allElement=[];total={input:[],checked:[],list:[]};checkAllElement=[];onChangeCallback;onCheckAllCallback;constructor(e,t){this.init(e,t,A.instances.length),A.instances.push(this),1===A.instances.length&&!0===A.firstLoad&&x(`CheckBox is loaded, version: ${A.version}`),A.firstLoad=!1}init(e,t,n){let l=null;return"string"==typeof e?l=v.getElem(e,"all"):e instanceof NodeList||e instanceof Array?l=e:e instanceof HTMLInputElement&&(l=[e]),l?(this.id=n,this.element=e,this.options=v.deepMerge({},this.options,t),this.injectStyles(),this.setupCallbacks(),l.forEach(((e,t)=>this.processCheckbox(e,t))),this.updateTotal(),this.options.checkAll&&this.processCheckAll(),this):v.throwError("Cannot find elements : "+e)}injectStyles(){let e={};this.options?.checkMark&&(e={".check-box input[type=checkbox] + .checkmark:after":{"background-image":"url("+this.options.checkMark+")"}}),this.options?.styles&&Object.keys(this.options.styles).length>0&&(e=v.deepMerge({},this.options.styles,e)),e&&v.injectStylesheet(e,this.id.toString())}setupCallbacks(){this.onChange=(e,t)=>{this.options?.onChange&&this.options.onChange(e,t)},this.onCheckAll=e=>{this.options?.onCheckAll&&this.options.onCheckAll(e)}}processCheckbox(e,t){if("checkbox"!==e.type)return;if(e.hasAttribute("data-checkbox"))return;e.setAttribute("data-checkbox","true");let n=e.nextElementSibling,l=this.options.bindLabel??!1,{title:i,remainLabel:c,randomID:o,labelToRestore:s}=v.handleCheckboxTitle(e,n);l=!!c||l,e.checked?v.toggleCheckStatus(e,!0):this.options.checked&&this.updateCheckboxStatus(e,t);let{cloneEle:h,labelNode:a}=v.insertCheckbox(this.id.toString(),e,o,c);v.insertCheckboxTitle(i,l,a,h);let r=this.checkBoxChange.bind(this,!0,h,void 0);h.addEventListener("change",r),h.checkBoxChange=r,this.options.allowShiftKey&&v.addEventListener(h,"shift-click",this.handleShiftClick),this.allElement.push(h),h.labelToRestore=s}updateCheckboxStatus(e,t){const n=this.options.checked;"boolean"==typeof n?v.toggleCheckStatus(e,n):"string"==typeof n||"number"==typeof n?e.value!==n.toString()&&t!==Number(n)||v.toggleCheckStatus(e,!0):Array.isArray(n)&&n.includes(e.value)&&v.toggleCheckStatus(e,!0)}processCheckAll(){const e=v.getCheckAllElements(this.options.checkAll);0!==e.length&&e.forEach((e=>{if(!e||"checkbox"!==e.type)return;if(e.hasAttribute("data-checkbox"))return;e.setAttribute("data-checkbox","true");const t=e.nextElementSibling;let{title:n,remainLabel:l,randomID:i,labelToRestore:c}=v.handleCheckboxTitle(e,t);n&&t&&"LABEL"===t.tagName&&(n=t.textContent||n,t.parentNode?.removeChild(t));const{cloneEle:o,templateNode:s,labelNode:h}=v.insertCheckbox(this.id.toString(),e,i,l);e.parentNode?.replaceChild(s.firstElementChild||s,e),v.insertCheckboxTitle(n,this.options.bindLabel??!1,h,o),o.addEventListener("change",this.checkAllChange),o.checkAllChange=this.checkAllChange,o.labelToRestore=c,this.checkAllElement.push(o),0===this.total.input.length&&v.toggleDisableStatus(o,!0),(!0===this.options.checked||e.checked)&&(v.toggleCheckStatus(o,!0),o.dispatchEvent(new Event("change")))}))}checkBoxChange(e,t,n){this.updateTotal(),e&&this.updateCheckAllStatus(),this.onChangeCallback?.(this.total,t),t&&v.toggleCheckStatus(t,n??t.checked),this.dispatchCheckboxChangeEvent()}checkAllChange=e=>{if(!(e.target instanceof HTMLInputElement))return;const t=e.target.checked;this.allElement.forEach((e=>{v.toggleCheckStatus(e,t)})),this.updateTotal(),this.updateCheckAllStatus(),this.checkBoxChange(!1),this.onCheckAllCallback&&this.onCheckAllCallback(t)};updateTotal(){this.total.input=[...this.allElement];const e=this.allElement.filter((e=>e.checked));this.total.checked=this.total.checked.filter((e=>e.checked));const t=e.filter((e=>!this.total.checked.includes(e)));this.total.checked.push(...t),this.total.list=this.total.checked.map((e=>e.value))}updateCheckAllStatus(){if(this.checkAllElement.length>0){const e=this.total.checked.length===this.total.input.length;this.checkAllElement.forEach((t=>{v.toggleCheckStatus(t,e)}))}}dispatchCheckboxChangeEvent(){const e=v.createEvent("checkbox-change",{detail:this.total});v.dispatchEvent(e)}handleShiftClick=e=>{const t=e.detail.checkedElement,n=this.getLastChecked(!0);if(!t||!n)return;let l=this.allElement.indexOf(n),i=this.allElement.indexOf(t);if(n&&-1!==l&&-1!==i){x("Shift-click event detected");const e=Math.min(l,i),n=Math.max(l,i);for(let l=e;l<=n;l++){const e=t.checked,n=this.allElement[l];v.toggleCheckStatus(n,e)}this.updateTotal(),this.updateCheckAllStatus(),this.dispatchCheckboxChangeEvent()}};getLastChecked(e=!1){const t=this.total.checked;let n=e?2:1;return 0===t.length?null:1===t.length?t[0]:t[t.length-n]}destroy(){A.firstLoad=!1,this.allElement.forEach((e=>{v.restoreElement(e)})),this.checkAllElement.length>0&&this.checkAllElement.forEach((e=>{e.checkAllChange&&(v.toggleCheckAll(this.checkAllElement),v.restoreElement(e))})),this.element=null,this.options=t,this.allElement=[],this.total={input:[],checked:[],list:[]},this.checkAllElement=[],v.removeStylesheet(this.id.toString());const e=A.instances.indexOf(this);-1!==e&&A.instances.splice(e,1)}set onChange(e){this.onChangeCallback=e}set onCheckAll(e){this.onCheckAllCallback=e}get elements(){return this.allElement}getCheckBox(){return this.total}refresh(){this.element&&this.init(this.element,this.options,this.id)}static destroyAll(){for(;A.instances.length;){A.instances[0].destroy()}}}var S=Object.freeze({__proto__:null}),L=Object.freeze({__proto__:null});e.CheckBox=A,e.CheckBoxInterface=S,e.CheckBoxType=L})); |
{ | ||
"name": "@carry0987/check-box", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"description": "A library for create and manage checkbox elements", | ||
@@ -18,3 +18,3 @@ "type": "module", | ||
"files": [ | ||
"dist" | ||
"dist/*" | ||
], | ||
@@ -21,0 +21,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48941