@carry0987/check-box
Advanced tools
Comparing version 2.0.5 to 2.0.6
@@ -8,9 +8,9 @@ interface OnChangeCallback { | ||
interface CheckBoxOption { | ||
checked?: boolean | string | number | Array<string | number>; | ||
checkMark?: string; | ||
checkAll?: string; | ||
onChange?: OnChangeCallback; | ||
onCheckAll?: OnCheckAllCallback; | ||
bindLabel?: boolean; | ||
styles?: object; | ||
checked: boolean | string | number | Array<string | number> | null; | ||
checkMark: string; | ||
checkAll: string | null; | ||
bindLabel: boolean; | ||
styles: object; | ||
onChange: OnChangeCallback; | ||
onCheckAll: OnCheckAllCallback; | ||
} | ||
@@ -41,3 +41,3 @@ interface TotalCheckbox { | ||
private onCheckAllCallback?; | ||
constructor(element: string | HTMLInputElement, option?: CheckBoxOption); | ||
constructor(element: string | HTMLInputElement, option: Partial<CheckBoxOption>); | ||
private init; | ||
@@ -44,0 +44,0 @@ private injectStyles; |
@@ -316,9 +316,9 @@ function reportError(...error) { | ||
const defaults = { | ||
checked: undefined, | ||
checked: null, | ||
checkMark: 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9IiNmZmZmZmYiIHN0cm9rZS13aWR0aD0iMyIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIj48cG9seWxpbmUgcG9pbnRzPSIyMCA2IDkgMTcgNCAxMiI+PC9wb2x5bGluZT48L3N2Zz4=', | ||
checkAll: undefined, | ||
checkAll: null, | ||
bindLabel: true, | ||
onChange: undefined, | ||
onCheckAll: undefined, | ||
styles: {} | ||
styles: {}, | ||
onChange: () => { }, | ||
onCheckAll: () => { }, | ||
}; | ||
@@ -358,6 +358,6 @@ | ||
static instances = []; | ||
static version = '2.0.5'; | ||
static version = '2.0.6'; | ||
static firstLoad = true; | ||
element = null; | ||
options; | ||
options = defaults; | ||
id = 0; | ||
@@ -370,3 +370,3 @@ allElement = []; // Store all elements here which will be used in destroy method | ||
onCheckAllCallback; | ||
constructor(element, option = {}) { | ||
constructor(element, option) { | ||
this.init(element, option, CheckBox.instances.length); | ||
@@ -386,3 +386,3 @@ CheckBox.instances.push(this); | ||
this.element = elements; | ||
this.options = Utils.deepMerge({}, defaults, option); | ||
this.options = Utils.deepMerge({}, this.options, option); | ||
// Inject stylesheet | ||
@@ -476,3 +476,3 @@ this.injectStyles(); | ||
// Retrieve the check all element | ||
if (this.options.checkAll === undefined) | ||
if (this.options.checkAll === null) | ||
return; | ||
@@ -574,3 +574,3 @@ const checkAll = Utils.getElem(this.options.checkAll); | ||
this.element = null; | ||
this.options = {}; | ||
this.options = defaults; | ||
this.allElement = []; | ||
@@ -577,0 +577,0 @@ this.total = { input: [], checked: [], list: [] }; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).CheckBox=t()}(this,(function(){"use strict";function e(...e){console.error(...e)}function t(e){throw new Error(e)}var n=Object.freeze({__proto__:null,reportError:e,throwError:t});function l(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 c="utils-style";const i={from:".utils",to:".utils-"};function s(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 o=n[t],c=t;"object"!=typeof(l=o)||null===l||Array.isArray(l)?e[c]=o:(e[c]&&"object"==typeof e[c]||(e[c]={}),s(e[c],o))}var l;return s(e,...t)}function a(e){c=e}function h(e,t){i.from=e,i.to=t}function r(e,t=null){t=p(t)?"":t;let n=o("style");n.id=c+t,n.textContent="",document.head.append(n);let l=n.sheet;for(let n in e)e.hasOwnProperty(n)&&u(l,n,d(e[n]),t)}function d(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 u(e,t,n,l=null){l=p(l)?"":l;let o=t.replace(i.from,i.to+l);e.insertRule(o+"{"+n+"}",0)}function k(e=null){const t=p(e)?"":e;let n=l("#"+c+t);n&&n.parentNode&&n.parentNode.removeChild(n)}function p(e){return"number"!=typeof e&&(!e||"string"==typeof e&&0===e.length)}function b(e=8){return Math.random().toString(36).substring(2,2+e)}function g(e,t,n){return new CustomEvent(e,{detail:t,...n})}var m=Object.freeze({__proto__:null,addEventListener:function(e,t,n,l){e.addEventListener(t,n,l)},createEvent:g,dispatchEvent:function(n,l=document,o,c){try{if("string"==typeof n){let e=g(n,o,c);return l.dispatchEvent(e)}if(n instanceof Event)return l.dispatchEvent(n);t("Invalid event type")}catch(t){return e("Dispatch Event Error:",t),!1}},removeEventListener:function(e,t,n,l){e.removeEventListener(t,n,l)}});class C{static throwError=n.throwError;static getElem=l;static deepMerge=s;static generateRandom=b;static injectStylesheet=r;static removeStylesheet=k;static setStylesheetId=a;static setReplaceRule=h;static isEmpty=p;static createEvent=m.createEvent;static dispatchEvent=m.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 handleCheckboxTitle(e,t){let n,l=e.title||e.dataset.checkboxTitle||null,o=!1,c=null,i=!1;if(t instanceof HTMLLabelElement){const s=t.htmlFor,a=t.dataset.checkboxFor,h=e.dataset.checkboxId;o=!C.isEmpty(e.id)&&s===e.id,i=!C.isEmpty(e.id)&&a===e.id,C.isEmpty(h)||a!==h||(c=C.isEmpty(e.id)&&C.isEmpty(s)?"check-"+C.generateRandom(6):null,i=!0),(i||o)&&(n=t.cloneNode(!0),l=l||t.textContent,t.parentNode.removeChild(t))}return{title:l,remainLabel:o,randomID:c,labelToRestore:n}}static insertCheckbox(e,t,n,c){let i=C.getTemplate(e),s=o("div");s.innerHTML=i.trim();let a=l(".checkmark",s),h=l("label",s),r=t.cloneNode(!0);return r.withID=!0,n&&(r.id=n,r.withID=!1),!0===c&&(h.htmlFor=r.id),a.addEventListener("click",(e=>{e.preventDefault(),r.click()})),a.parentNode&&a.parentNode.insertBefore(r,a),t.parentNode.replaceChild(s.firstElementChild||s,t),{cloneEle:r,templateNode:s,labelNode:h}}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()})))):n.parentNode.removeChild(n)}static toggleCheckStatus(e,t){t?(e.checked=!0,e.setAttribute("checked","checked")):(e.checked=!1,e.removeAttribute("checked"))}static toggleCheckAll(e,t){let n=l(e);n&&(t&&t.checked&&t.input?C.toggleCheckStatus(n,!1==(t.checked.length!==t.input.length||0===t.checked.length)):C.toggleCheckStatus(n,!1))}static restoreElement(e){if("function"==typeof e.checkBoxChange&&e.removeEventListener("change",e.checkBoxChange),!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)}}C.setStylesheetId("checkbox-style"),C.setReplaceRule(".check-box",".check-box-");const f={checked:void 0,checkMark:"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9IiNmZmZmZmYiIHN0cm9rZS13aWR0aD0iMyIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIj48cG9seWxpbmUgcG9pbnRzPSIyMCA2IDkgMTcgNCAxMiI+PC9wb2x5bGluZT48L3N2Zz4=",checkAll:void 0,bindLabel:!0,onChange:void 0,onCheckAll:void 0,styles:{}};class E{static instances=[];static version="2.0.5";static firstLoad=!0;element=null;options;id=0;allElement=[];total={input:[],checked:[],list:[]};checkAllElement;onChangeCallback;onCheckAllCallback;constructor(e,t={}){this.init(e,t,E.instances.length),E.instances.push(this),1===E.instances.length&&!0===E.firstLoad&&((e,t=!1)=>{!0===t?console.log("Data Type : "+typeof e,"\nValue : "+e):"boolean"!=typeof t?console.log(t):console.log(e)})(`CheckBox is loaded, version: ${E.version}`),E.firstLoad=!1}init(e,t,n){let l=C.getElem(e,"all");return(!l||l.length<1)&&C.throwError("Cannot find elements : "+e),this.id=n,this.element=e,this.options=C.deepMerge({},f,t),this.injectStyles(),this.setupCallbacks(),l.forEach(((e,t)=>this.processCheckbox(e,t))),this.options.checkAll&&this.setupCheckAll(),this}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=C.deepMerge({},this.options.styles,e)),e&&C.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:o,remainLabel:c,randomID:i,labelToRestore:s}=C.handleCheckboxTitle(e,n);l=!!c||l,e.checked?C.toggleCheckStatus(e,!0):this.options.checked&&this.updateCheckboxCheckedStatus(e,t);let{cloneEle:a,labelNode:h}=C.insertCheckbox(this.id.toString(),e,i,c);C.insertCheckboxTitle(o,l,h,a);let r=this.checkBoxChange.bind(this,!0,a);a.addEventListener("change",r),a.checkBoxChange=r,this.allElement.push(a),a.labelToRestore=s}updateCheckboxCheckedStatus(e,t){const n=this.options.checked;"boolean"==typeof n?C.toggleCheckStatus(e,n):"string"==typeof n||"number"==typeof n?e.value!==n.toString()&&t!==Number(n)||C.toggleCheckStatus(e,!0):Array.isArray(n)&&n.includes(e.value)&&C.toggleCheckStatus(e,!0)}setupCheckAll(){if(void 0===this.options.checkAll)return;const e=C.getElem(this.options.checkAll);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:o,labelToRestore:c}=C.handleCheckboxTitle(e,t);n&&t&&"LABEL"===t.tagName&&(n=t.textContent||n,t.parentNode?.removeChild(t));const{cloneEle:i,templateNode:s,labelNode:a}=C.insertCheckbox(this.id.toString(),e,o,l);e.parentNode?.replaceChild(s.firstElementChild||s,e),C.insertCheckboxTitle(n,this.options.bindLabel??!1,a,i);const h=e=>{if(!(e.target instanceof HTMLInputElement))return;const t=e.target.checked;this.allElement.forEach((e=>{C.toggleCheckStatus(e,t)})),this.checkBoxChange(!1),this.onCheckAllCallback&&this.onCheckAllCallback(t)};i.addEventListener("change",h),i.checkAllChange=h,i.labelToRestore=c,this.checkAllElement=i,(!0===this.options.checked||e.checked)&&(C.toggleCheckStatus(i,!0),i.dispatchEvent(new Event("change")))}checkBoxChange(e,t){this.updateTotal(),e&&this.updateCheckAllStatus(),this.onChangeCallback?.(this.total,t),t&&C.toggleCheckStatus(t,t.checked),this.dispatchCheckboxChangeEvent()}updateTotal(){const e=this.total;e.list=[],e.input=[],e.checked=[],this.allElement.forEach((t=>{e.input.push(t),t.checked&&(e.list.push(t.value),e.checked.push(t))}))}updateCheckAllStatus(){if(this.checkAllElement){const e=this.total.checked.length===this.total.input.length;C.toggleCheckStatus(this.checkAllElement,e)}}dispatchCheckboxChangeEvent(){const e=C.createEvent("checkbox-change",{detail:this.total});C.dispatchEvent(e)}destroy(){E.firstLoad=!1,this.allElement.forEach((e=>{C.restoreElement(e)})),this.checkAllElement&&this.checkAllElement.checkAllChange&&(C.toggleCheckAll(this.checkAllElement),C.restoreElement(this.checkAllElement)),this.element=null,this.options={},this.allElement=[],this.total={input:[],checked:[],list:[]},this.checkAllElement=void 0,C.removeStylesheet(this.id.toString());const e=E.instances.indexOf(this);-1!==e&&E.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(;E.instances.length;){E.instances[0].destroy()}}}return E})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).CheckBox=t()}(this,(function(){"use strict";function e(...e){console.error(...e)}function t(e){throw new Error(e)}var n=Object.freeze({__proto__:null,reportError:e,throwError:t});function l(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 c(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 o="utils-style";const i={from:".utils",to:".utils-"};function s(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 c=n[t],o=t;"object"!=typeof(l=c)||null===l||Array.isArray(l)?e[o]=c:(e[o]&&"object"==typeof e[o]||(e[o]={}),s(e[o],c))}var l;return s(e,...t)}function a(e){o=e}function h(e,t){i.from=e,i.to=t}function r(e,t=null){t=p(t)?"":t;let n=c("style");n.id=o+t,n.textContent="",document.head.append(n);let l=n.sheet;for(let n in e)e.hasOwnProperty(n)&&u(l,n,d(e[n]),t)}function d(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 u(e,t,n,l=null){l=p(l)?"":l;let c=t.replace(i.from,i.to+l);e.insertRule(c+"{"+n+"}",0)}function k(e=null){const t=p(e)?"":e;let n=l("#"+o+t);n&&n.parentNode&&n.parentNode.removeChild(n)}function p(e){return"number"!=typeof e&&(!e||"string"==typeof e&&0===e.length)}function b(e=8){return Math.random().toString(36).substring(2,2+e)}function g(e,t,n){return new CustomEvent(e,{detail:t,...n})}var m=Object.freeze({__proto__:null,addEventListener:function(e,t,n,l){e.addEventListener(t,n,l)},createEvent:g,dispatchEvent:function(n,l=document,c,o){try{if("string"==typeof n){let e=g(n,c,o);return l.dispatchEvent(e)}if(n instanceof Event)return l.dispatchEvent(n);t("Invalid event type")}catch(t){return e("Dispatch Event Error:",t),!1}},removeEventListener:function(e,t,n,l){e.removeEventListener(t,n,l)}});class C{static throwError=n.throwError;static getElem=l;static deepMerge=s;static generateRandom=b;static injectStylesheet=r;static removeStylesheet=k;static setStylesheetId=a;static setReplaceRule=h;static isEmpty=p;static createEvent=m.createEvent;static dispatchEvent=m.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 handleCheckboxTitle(e,t){let n,l=e.title||e.dataset.checkboxTitle||null,c=!1,o=null,i=!1;if(t instanceof HTMLLabelElement){const s=t.htmlFor,a=t.dataset.checkboxFor,h=e.dataset.checkboxId;c=!C.isEmpty(e.id)&&s===e.id,i=!C.isEmpty(e.id)&&a===e.id,C.isEmpty(h)||a!==h||(o=C.isEmpty(e.id)&&C.isEmpty(s)?"check-"+C.generateRandom(6):null,i=!0),(i||c)&&(n=t.cloneNode(!0),l=l||t.textContent,t.parentNode.removeChild(t))}return{title:l,remainLabel:c,randomID:o,labelToRestore:n}}static insertCheckbox(e,t,n,o){let i=C.getTemplate(e),s=c("div");s.innerHTML=i.trim();let a=l(".checkmark",s),h=l("label",s),r=t.cloneNode(!0);return r.withID=!0,n&&(r.id=n,r.withID=!1),!0===o&&(h.htmlFor=r.id),a.addEventListener("click",(e=>{e.preventDefault(),r.click()})),a.parentNode&&a.parentNode.insertBefore(r,a),t.parentNode.replaceChild(s.firstElementChild||s,t),{cloneEle:r,templateNode:s,labelNode:h}}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()})))):n.parentNode.removeChild(n)}static toggleCheckStatus(e,t){t?(e.checked=!0,e.setAttribute("checked","checked")):(e.checked=!1,e.removeAttribute("checked"))}static toggleCheckAll(e,t){let n=l(e);n&&(t&&t.checked&&t.input?C.toggleCheckStatus(n,!1==(t.checked.length!==t.input.length||0===t.checked.length)):C.toggleCheckStatus(n,!1))}static restoreElement(e){if("function"==typeof e.checkBoxChange&&e.removeEventListener("change",e.checkBoxChange),!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)}}C.setStylesheetId("checkbox-style"),C.setReplaceRule(".check-box",".check-box-");const f={checked:null,checkMark:"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9IiNmZmZmZmYiIHN0cm9rZS13aWR0aD0iMyIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIj48cG9seWxpbmUgcG9pbnRzPSIyMCA2IDkgMTcgNCAxMiI+PC9wb2x5bGluZT48L3N2Zz4=",checkAll:null,bindLabel:!0,styles:{},onChange:()=>{},onCheckAll:()=>{}};class E{static instances=[];static version="2.0.6";static firstLoad=!0;element=null;options=f;id=0;allElement=[];total={input:[],checked:[],list:[]};checkAllElement;onChangeCallback;onCheckAllCallback;constructor(e,t){this.init(e,t,E.instances.length),E.instances.push(this),1===E.instances.length&&!0===E.firstLoad&&((e,t=!1)=>{!0===t?console.log("Data Type : "+typeof e,"\nValue : "+e):"boolean"!=typeof t?console.log(t):console.log(e)})(`CheckBox is loaded, version: ${E.version}`),E.firstLoad=!1}init(e,t,n){let l=C.getElem(e,"all");return(!l||l.length<1)&&C.throwError("Cannot find elements : "+e),this.id=n,this.element=e,this.options=C.deepMerge({},this.options,t),this.injectStyles(),this.setupCallbacks(),l.forEach(((e,t)=>this.processCheckbox(e,t))),this.options.checkAll&&this.setupCheckAll(),this}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=C.deepMerge({},this.options.styles,e)),e&&C.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:c,remainLabel:o,randomID:i,labelToRestore:s}=C.handleCheckboxTitle(e,n);l=!!o||l,e.checked?C.toggleCheckStatus(e,!0):this.options.checked&&this.updateCheckboxCheckedStatus(e,t);let{cloneEle:a,labelNode:h}=C.insertCheckbox(this.id.toString(),e,i,o);C.insertCheckboxTitle(c,l,h,a);let r=this.checkBoxChange.bind(this,!0,a);a.addEventListener("change",r),a.checkBoxChange=r,this.allElement.push(a),a.labelToRestore=s}updateCheckboxCheckedStatus(e,t){const n=this.options.checked;"boolean"==typeof n?C.toggleCheckStatus(e,n):"string"==typeof n||"number"==typeof n?e.value!==n.toString()&&t!==Number(n)||C.toggleCheckStatus(e,!0):Array.isArray(n)&&n.includes(e.value)&&C.toggleCheckStatus(e,!0)}setupCheckAll(){if(null===this.options.checkAll)return;const e=C.getElem(this.options.checkAll);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:c,labelToRestore:o}=C.handleCheckboxTitle(e,t);n&&t&&"LABEL"===t.tagName&&(n=t.textContent||n,t.parentNode?.removeChild(t));const{cloneEle:i,templateNode:s,labelNode:a}=C.insertCheckbox(this.id.toString(),e,c,l);e.parentNode?.replaceChild(s.firstElementChild||s,e),C.insertCheckboxTitle(n,this.options.bindLabel??!1,a,i);const h=e=>{if(!(e.target instanceof HTMLInputElement))return;const t=e.target.checked;this.allElement.forEach((e=>{C.toggleCheckStatus(e,t)})),this.checkBoxChange(!1),this.onCheckAllCallback&&this.onCheckAllCallback(t)};i.addEventListener("change",h),i.checkAllChange=h,i.labelToRestore=o,this.checkAllElement=i,(!0===this.options.checked||e.checked)&&(C.toggleCheckStatus(i,!0),i.dispatchEvent(new Event("change")))}checkBoxChange(e,t){this.updateTotal(),e&&this.updateCheckAllStatus(),this.onChangeCallback?.(this.total,t),t&&C.toggleCheckStatus(t,t.checked),this.dispatchCheckboxChangeEvent()}updateTotal(){const e=this.total;e.list=[],e.input=[],e.checked=[],this.allElement.forEach((t=>{e.input.push(t),t.checked&&(e.list.push(t.value),e.checked.push(t))}))}updateCheckAllStatus(){if(this.checkAllElement){const e=this.total.checked.length===this.total.input.length;C.toggleCheckStatus(this.checkAllElement,e)}}dispatchCheckboxChangeEvent(){const e=C.createEvent("checkbox-change",{detail:this.total});C.dispatchEvent(e)}destroy(){E.firstLoad=!1,this.allElement.forEach((e=>{C.restoreElement(e)})),this.checkAllElement&&this.checkAllElement.checkAllChange&&(C.toggleCheckAll(this.checkAllElement),C.restoreElement(this.checkAllElement)),this.element=null,this.options=f,this.allElement=[],this.total={input:[],checked:[],list:[]},this.checkAllElement=void 0,C.removeStylesheet(this.id.toString());const e=E.instances.indexOf(this);-1!==e&&E.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(;E.instances.length;){E.instances[0].destroy()}}}return E})); |
{ | ||
"name": "@carry0987/check-box", | ||
"version": "2.0.5", | ||
"version": "2.0.6", | ||
"description": "A library for create and manage checkbox elements", | ||
@@ -5,0 +5,0 @@ "type": "module", |
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
39037