selectyc 0.0.11 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/selectyc.js CHANGED
@@ -49,18 +49,19 @@ class l {
49
49
  class h {
50
50
  #t = [];
51
51
  #c;
52
- #f = [];
52
+ #m = [];
53
53
  #d;
54
- #m;
54
+ #g;
55
55
  #s;
56
56
  #e;
57
57
  #i;
58
58
  #a;
59
+ #u;
59
60
  #n = !1;
60
- #u = !1;
61
61
  #p = !1;
62
- #C = "";
63
- #b = '<svg class="w-5 h-5 text-gray-900 dark:text-gray-400" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m19 9-7 7-7-7"/></svg>';
62
+ #f = !1;
63
+ #b = "";
64
+ #S = '<svg class="w-5 h-5 text-gray-900 dark:text-gray-400" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m19 9-7 7-7-7"/></svg>';
64
65
  constructor(t) {
65
66
  if (window.selects === void 0 && (window.selects = []), window.selects[t] !== void 0)
66
67
  return window.selects[t];
@@ -124,56 +125,56 @@ class h {
124
125
  const t = this.#t;
125
126
  this.#t = [], this.#r(t);
126
127
  }
127
- #S() {
128
- this.#x(), this.#s.addEventListener("click", (t) => {
128
+ #x() {
129
+ this.#E(), this.#s.addEventListener("click", (t) => {
129
130
  if (t.target.classList.contains("remove-tag")) {
130
- this.#R(t.target.getAttribute("data-value")), this.onUnselect !== void 0 && this.onUnselect(t.target.getAttribute("data-value"), this), this.#n && this.#h(), this.#g();
131
+ this.#A(t.target.getAttribute("data-value")), this.onUnselect !== void 0 && this.onUnselect(t.target.getAttribute("data-value"), this), this.#n && this.#h(), this.#v();
131
132
  return;
132
133
  }
133
- this.#n ? this.#h() : this.#g();
134
+ this.#n ? this.#h() : this.#v();
134
135
  }), document.addEventListener("click", (t) => {
135
136
  !this.#d.contains(t.target) && !this.#e.contains(t.target) && !t.target.classList.contains("remove-tag") && this.#h();
136
137
  }), window.onresize = () => {
137
138
  this.#h();
138
- }, this.isMultiple && this.#v();
139
+ }, this.isMultiple && this.#C();
139
140
  }
140
141
  // Draw functions
141
- #x() {
142
- this.#O();
142
+ #E() {
143
+ this.#I();
143
144
  const t = document.createElement("div");
144
145
  t.className = "main-div-selectyc", t.style.width = this.width, t.id = `selectyc_main_div_${this.id}`;
145
146
  const e = document.createElement("div");
146
147
  e.className = "custom-multiselect";
147
148
  let s = document.createElement("div");
148
- s.className = "selected-items round" + this.bodyRound, s.tabIndex = 0, s.classList.add(this.boxOverflow === "horizontal" ? "horizontal-overflow" : "vertical-overflow"), this.isMultiple || (s = this.#E(s)), e.appendChild(s), e.appendChild(this.#L()), t.appendChild(e);
149
+ s.className = "selected-items round" + this.bodyRound, s.tabIndex = 0, s.classList.add(this.boxOverflow === "horizontal" ? "horizontal-overflow" : "vertical-overflow"), this.isMultiple || (s = this.#D(s)), e.appendChild(s), e.appendChild(this.#N()), t.appendChild(e);
149
150
  const i = document.getElementById(this.id), a = document.createElement("input");
150
151
  a.name = i.name, a.id = i.id, this.rootClassName !== "" && (a.className = i.className), a.style.display = "none";
151
152
  let n = i.parentNode;
152
- t.appendChild(a), n.replaceChild(t, i), this.#c = a, this.#m = s, this.#d = e, this.#s = this.#d.querySelector(".selected-items"), !this.isMultiple && this.#t.length > 0 && (this.#c.value = this.#t[0].value);
153
+ t.appendChild(a), n.replaceChild(t, i), this.#c = a, this.#g = s, this.#d = e, this.#s = this.#d.querySelector(".selected-items"), !this.isMultiple && this.#t.length > 0 && (this.#c.value = this.#t[0].value);
153
154
  }
154
- #E(t) {
155
+ #D(t) {
155
156
  const e = document.createElement("input");
156
157
  e.className = "custom-single-select", e.id = `selectyc_text_box_${this.id}`, e.placeholder = this.singleLabel, e.value = this.#t.length === 0 ? null : this.#t[0].text, e.readOnly = !0;
157
158
  const s = document.createElement("div");
158
- return s.innerHTML = this.#b, this.#u && t.appendChild(this.#D()), t.appendChild(e), t.appendChild(s), this.#a = e, t;
159
+ return s.innerHTML = this.#S, this.#p && t.appendChild(this.#L()), t.appendChild(e), t.appendChild(s), this.#a = e, t;
159
160
  }
160
- #D() {
161
+ #L() {
161
162
  const t = document.createElement("input");
162
- return t.type = "text", t.required = this.#u, t.tabIndex = -1, t.style.position = "absolute", t.style.opacity = "0", t.style.pointerEvents = "none", t.style.height = "0", t.style.width = "0", t.style.left = "50%", t.value = this.#t.length === 0 ? "" : this.#t[0].text, t;
163
+ return t.type = "text", t.required = this.#p, t.tabIndex = -1, t.style.position = "absolute", t.style.opacity = "0", t.style.pointerEvents = "none", t.style.height = "0", t.style.width = "0", t.style.left = "50%", t.value = this.#t.length === 0 ? "" : this.#t[0].text, this.#u = t, t;
163
164
  }
164
- #L() {
165
+ #N() {
165
166
  const t = document.createElement("div");
166
167
  t.setAttribute("style", "position:fixed; z-index:999;");
167
168
  const e = document.createElement("div");
168
- e.className = "global-dropdown", this.searchInputActive && t.appendChild(this.#N());
169
+ e.className = "global-dropdown", this.searchInputActive && t.appendChild(this.#O());
169
170
  let s;
170
171
  return e.addEventListener("scroll", (i) => {
171
172
  s && clearTimeout(s), s = setTimeout(() => {
172
- this.#A(i);
173
+ this.#B(i);
173
174
  }, 70);
174
175
  }), t.appendChild(e), this.#e = e, t;
175
176
  }
176
- #N() {
177
+ #O() {
177
178
  const t = document.createElement("input");
178
179
  return t.type = "text", t.id = "selectyc_search_input", t.className = "search-input", t.placeholder = "Buscar...", t.addEventListener("input", () => {
179
180
  this.filter = t.value, this.#e.innerHTML = "", this.filter = t.value, this.#y();
@@ -182,21 +183,21 @@ class h {
182
183
  #l(t) {
183
184
  const e = document.createElement("div");
184
185
  e.className = "select-option", e.setAttribute("data-value", t.id), e.textContent = t.text, this.#e.appendChild(e), e.addEventListener("click", () => {
185
- t.id !== "sin_resultados" && (this.#M(t.text, t.id), this.onSelect !== void 0 && this.onSelect(t.id, this), this.#h());
186
+ t.id !== "sin_resultados" && (this.#T(t.text, t.id), this.onSelect !== void 0 && this.onSelect(t.id, this), this.#h());
186
187
  });
187
188
  }
188
189
  // Functions
189
- #O() {
190
+ #I() {
190
191
  const t = document.getElementById(this.id);
191
- this.name = t.name, this.#u = t.required, this.rootClassName = t.className, Array.from(t.options).forEach((e) => {
192
+ this.name = t.name, this.#p = t.required, this.rootClassName = t.className, Array.from(t.options).forEach((e) => {
192
193
  if (e.outerHTML.includes("selected")) {
193
194
  const s = e.text, i = this.#o(e.value);
194
195
  this.isMultiple ? this.#t.push({ value: i, text: s }) : this.#t[0] = { value: i, text: s };
195
196
  }
196
- this.#f.push({ id: this.#o(e.value), text: e.text });
197
+ this.#m.push({ id: this.#o(e.value), text: e.text });
197
198
  });
198
199
  }
199
- #g() {
200
+ #v() {
200
201
  if (this.#n)
201
202
  return;
202
203
  this.#s.classList.remove(`round${this.bodyRound}`), this.#s.classList.add(`round${this.bodyRound}t`);
@@ -220,17 +221,17 @@ class h {
220
221
  t = { ...t, ...i };
221
222
  } else this.bodyData !== null && (t = { ...t, ...this.bodyData });
222
223
  const e = await l.make(this.RS).addData(t).setMethod("POST").go(), s = Array.isArray(e) ? e : e.data;
223
- return this.data = Array.from(new Map([...this.#f, ...s].map((i) => [i.id, i])).values()), this.data;
224
+ return this.data = Array.from(new Map([...this.#m, ...s].map((i) => [i.id, i])).values()), this.data;
224
225
  }
225
226
  async #y() {
226
- this.#l({ id: "loading", text: "cargando ..." }), this.records = 10 + this.#t.length, this.#p = !1;
227
+ this.#l({ id: "loading", text: "cargando ..." }), this.records = 10 + this.#t.length, this.#f = !1;
227
228
  let t = await this.#w();
228
229
  const e = this.#t.map((s) => s.value);
229
230
  this.#e.lastChild.dataset.value === "loading" && this.#e.removeChild(this.#e.lastChild), t.forEach((s) => {
230
- s.text.toLowerCase().includes(this.#C.toLowerCase()) && !e.includes(s.id) && this.#l(s);
231
+ s.text.toLowerCase().includes(this.#b.toLowerCase()) && !e.includes(s.id) && this.#l(s);
231
232
  }), this.#e.hasChildNodes() || this.#l({ id: "sin_resultados", text: "sin resultados ..." });
232
233
  }
233
- async #I() {
234
+ async #M() {
234
235
  const t = this.#t.map((n) => n.value);
235
236
  let e = this.filter === null ? "" : this.filter, s = this.data.map((n) => n.id);
236
237
  this.records = this.records + 10 + this.#t.length;
@@ -239,23 +240,23 @@ class h {
239
240
  const a = this.#e.lastChild.dataset.value;
240
241
  i.forEach((n) => {
241
242
  n.text.toLowerCase().includes(e.toLowerCase()) && !t.includes(n.id) && !s.includes(n.id) && this.#l(n);
242
- }), this.#p = a === this.#e.lastChild.dataset.value;
243
+ }), this.#f = a === this.#e.lastChild.dataset.value;
243
244
  }
244
- #M(t, e) {
245
+ #T(t, e) {
245
246
  this.#t.find((i) => i.value === e) || (this.isMultiple || (this.#t = []), this.#t.push({ text: t, value: e })), this.#r(e);
246
247
  }
247
248
  #r(t) {
248
- this.onChange !== void 0 && this.onChange(t, this), this.isMultiple ? this.#v() : this.#T();
249
+ this.onChange !== void 0 && this.onChange(t, this), this.isMultiple ? this.#C() : this.#R();
249
250
  }
250
- #T() {
251
- this.#a.value = null, this.#a.value = this.#t.length > 0 ? this.#t[0].text : null, this.#a.placeholder = this.singleLabel, this.#c.value = this.#t.length > 0 ? this.#t[0].value : null;
251
+ #R() {
252
+ this.#a.value = null, this.#u.value = null, this.#a.value = this.#t.length > 0 ? this.#t[0].text : null, this.#u.value = this.#t.length > 0 ? this.#t[0].text : null, this.#a.placeholder = this.singleLabel, this.#c.value = this.#t.length > 0 ? this.#t[0].value : null;
252
253
  const t = this.#a.parentNode, e = t.firstChild;
253
254
  if (e.className === "remove-tag" && e.remove(), !this.clearButton || this.#t.length <= 0)
254
255
  return;
255
256
  const s = document.createElement("span");
256
257
  s.dataset.value = this.#t[0].value, s.innerHTML = "x", s.className = "remove-tag", t.insertBefore(s, t.firstChild);
257
258
  }
258
- #v() {
259
+ #C() {
259
260
  if (this.#s.innerHTML = "", this.#t.length <= 0) {
260
261
  this.#s.appendChild(document.createElement("div"));
261
262
  return;
@@ -265,19 +266,19 @@ class h {
265
266
  s.className = "selected-tag", s.innerHTML = `${t} <span class="remove-tag" data-value="${e}">x</span>`, this.#s.appendChild(s);
266
267
  });
267
268
  }
268
- #R(t) {
269
+ #A(t) {
269
270
  this.#t = this.#t.filter((e) => this.#o(e.value) !== this.#o(t)), this.#r(t);
270
271
  }
271
- #A(t) {
272
+ #B(t) {
272
273
  const { scrollTop: e, scrollHeight: s, clientHeight: i } = this.#e;
273
- t.target.lastChild !== null && (t.target.lastChild.dataset.value === "loading" || this.#p || e + (i + 1) >= s && (this.#l({ id: "loading", text: "cargando ..." }), this.#I()));
274
+ t.target.lastChild !== null && (t.target.lastChild.dataset.value === "loading" || this.#f || e + (i + 1) >= s && (this.#l({ id: "loading", text: "cargando ..." }), this.#M()));
274
275
  }
275
276
  #o(t) {
276
277
  return !isNaN(t) && !isNaN(parseFloat(t)) ? parseInt(t) : t;
277
278
  }
278
279
  async render() {
279
280
  const t = document.getElementById(this.id), e = t === null ? !1 : t.outerHTML.includes("<select");
280
- this.#m === void 0 || e ? this.#S() : this.#r();
281
+ this.#g === void 0 || e ? this.#x() : this.#r();
281
282
  }
282
283
  }
283
284
  export {
@@ -1 +1 @@
1
- (function(h,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(h=typeof globalThis<"u"?globalThis:h||self,l(h.selectyc={}))})(this,function(h){"use strict";class l{constructor(t){this.route=t}static make(t){return new l(t)}setMethod(t){return this.method=t,this}getMethod(){return this.method===void 0?"POST":this.method}setData(t){return this.data=t,this}addData(t){if(this.data===void 0)return this.setData(t);for(const e in t)this.data[e]=t[e];return this}setFun(t){return this.fun=t,this}async go(){try{const t={method:this.getMethod(),headers:{"X-CSRF-TOKEN":window.CSRF_TOKEN,Accept:"application/json"}};this.data!==void 0&&(t.body=this.data,Object.prototype.toString.call(this.data)!=="[object FormData]"&&(t.body=JSON.stringify(this.data),t.headers["Content-Type"]="application/json"));const e=await fetch(this.route,t);if(!e.ok){const s=await e.json();throw new Error(s.message)}return this.fun===void 0?await e.json():await this.fun(e)}catch(t){window.toastNotification.setType(!1).show(t.message)}}}class d{#t=[];#c;#f=[];#r;#m;#s;#e;#i;#a;#n=!1;#u=!1;#p=!1;#b="";#C='<svg class="w-5 h-5 text-gray-900 dark:text-gray-400" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m19 9-7 7-7-7"/></svg>';constructor(t){if(window.selects===void 0&&(window.selects=[]),window.selects[t]!==void 0)return window.selects[t];this.id=t,this.name,this.data=[],this.isMultiple=!1,this.clearButton=!1,this.singleLabel=" ",this.records=10,this.filter=null,this.bodyRound="",this.width="100%",this.searchInputActive=!0,this.bodyData=null,this.boxOverflow="horizontal",window.selects[t]=this}static set(t){return new d(t)}setData(t){return this.data=t,this}setMultiple(t="horizontal"){return this.isMultiple=!0,this.boxOverflow=t,this}setClearButton(){return this.clearButton=!0,this}setSelectedOptions(t){this.#t=t,this.#d(t)}getSelectedOptions(){return this.#t}getSelectedOption(){return this.#t[0]}getOptions(){return this.data}setSingleLabel(t){return this.singleLabel=t,this}setBodyRound(t){return this.bodyRound=`-${t}`,this}setRS(t){return this.RS=t,this}setBodyData(t){return this.bodyData=t,this}desactiveSearchInput(){return this.searchInputActive=!1,this}addOption(t){this.data.push(t)}setWidth(t){return this.width=t,this}setOnSelect(t){return this.onSelect=t,this}setOnUnselect(t){return this.onUnselect=t,this}setOnChange(t){return this.onChange=t,this}cleanSelection(){const t=this.#t;this.#t=[],this.#d(t)}#S(){this.#x(),this.#s.addEventListener("click",t=>{if(t.target.classList.contains("remove-tag")){this.#R(t.target.getAttribute("data-value")),this.onUnselect!==void 0&&this.onUnselect(t.target.getAttribute("data-value"),this),this.#n&&this.#h(),this.#g();return}this.#n?this.#h():this.#g()}),document.addEventListener("click",t=>{!this.#r.contains(t.target)&&!this.#e.contains(t.target)&&!t.target.classList.contains("remove-tag")&&this.#h()}),window.onresize=()=>{this.#h()},this.isMultiple&&this.#v()}#x(){this.#O();const t=document.createElement("div");t.className="main-div-selectyc",t.style.width=this.width,t.id=`selectyc_main_div_${this.id}`;const e=document.createElement("div");e.className="custom-multiselect";let s=document.createElement("div");s.className="selected-items round"+this.bodyRound,s.tabIndex=0,s.classList.add(this.boxOverflow==="horizontal"?"horizontal-overflow":"vertical-overflow"),this.isMultiple||(s=this.#E(s)),e.appendChild(s),e.appendChild(this.#L()),t.appendChild(e);const i=document.getElementById(this.id),a=document.createElement("input");a.name=i.name,a.id=i.id,this.rootClassName!==""&&(a.className=i.className),a.style.display="none";let n=i.parentNode;t.appendChild(a),n.replaceChild(t,i),this.#c=a,this.#m=s,this.#r=e,this.#s=this.#r.querySelector(".selected-items"),!this.isMultiple&&this.#t.length>0&&(this.#c.value=this.#t[0].value)}#E(t){const e=document.createElement("input");e.className="custom-single-select",e.id=`selectyc_text_box_${this.id}`,e.placeholder=this.singleLabel,e.value=this.#t.length===0?null:this.#t[0].text,e.readOnly=!0;const s=document.createElement("div");return s.innerHTML=this.#C,this.#u&&t.appendChild(this.#D()),t.appendChild(e),t.appendChild(s),this.#a=e,t}#D(){const t=document.createElement("input");return t.type="text",t.required=this.#u,t.tabIndex=-1,t.style.position="absolute",t.style.opacity="0",t.style.pointerEvents="none",t.style.height="0",t.style.width="0",t.style.left="50%",t.value=this.#t.length===0?"":this.#t[0].text,t}#L(){const t=document.createElement("div");t.setAttribute("style","position:fixed; z-index:999;");const e=document.createElement("div");e.className="global-dropdown",this.searchInputActive&&t.appendChild(this.#N());let s;return e.addEventListener("scroll",i=>{s&&clearTimeout(s),s=setTimeout(()=>{this.#A(i)},70)}),t.appendChild(e),this.#e=e,t}#N(){const t=document.createElement("input");return t.type="text",t.id="selectyc_search_input",t.className="search-input",t.placeholder="Buscar...",t.addEventListener("input",()=>{this.filter=t.value,this.#e.innerHTML="",this.filter=t.value,this.#w()}),this.#i=t,t}#l(t){const e=document.createElement("div");e.className="select-option",e.setAttribute("data-value",t.id),e.textContent=t.text,this.#e.appendChild(e),e.addEventListener("click",()=>{t.id!=="sin_resultados"&&(this.#I(t.text,t.id),this.onSelect!==void 0&&this.onSelect(t.id,this),this.#h())})}#O(){const t=document.getElementById(this.id);this.name=t.name,this.#u=t.required,this.rootClassName=t.className,Array.from(t.options).forEach(e=>{if(e.outerHTML.includes("selected")){const s=e.text,i=this.#o(e.value);this.isMultiple?this.#t.push({value:i,text:s}):this.#t[0]={value:i,text:s}}this.#f.push({id:this.#o(e.value),text:e.text})})}#g(){if(this.#n)return;this.#s.classList.remove(`round${this.bodyRound}`),this.#s.classList.add(`round${this.bodyRound}t`);const t=this.#r.getBoundingClientRect();if(this.searchInputActive){this.#i.style.display="block",this.#i.style.width=t.width+"px";const e=this.#i.offsetHeight;this.#e.style.top=e+"px"}this.#e.style.width=t.width+"px",this.#e.innerHTML="",this.#e.style.display="block",this.searchInputActive&&this.#i.focus(),this.#w(),this.#n=!0}#h(){this.#n&&(this.#e.innerHTML="",this.#e.style.display="none",this.searchInputActive&&(this.#i.style.display="none"),this.#s.classList.add(`round${this.bodyRound}`),this.#n=!1)}async#y(){if(this.RS===void 0)return this.data;let t={records:this.records,filter:this.filter};if(typeof this.bodyData=="function"){const i=this.bodyData();t={...t,...i}}else this.bodyData!==null&&(t={...t,...this.bodyData});const e=await l.make(this.RS).addData(t).setMethod("POST").go(),s=Array.isArray(e)?e:e.data;return this.data=Array.from(new Map([...this.#f,...s].map(i=>[i.id,i])).values()),this.data}async#w(){this.#l({id:"loading",text:"cargando ..."}),this.records=10+this.#t.length,this.#p=!1;let t=await this.#y();const e=this.#t.map(s=>s.value);this.#e.lastChild.dataset.value==="loading"&&this.#e.removeChild(this.#e.lastChild),t.forEach(s=>{s.text.toLowerCase().includes(this.#b.toLowerCase())&&!e.includes(s.id)&&this.#l(s)}),this.#e.hasChildNodes()||this.#l({id:"sin_resultados",text:"sin resultados ..."})}async#M(){const t=this.#t.map(n=>n.value);let e=this.filter===null?"":this.filter,s=this.data.map(n=>n.id);this.records=this.records+10+this.#t.length;let i=await this.#y();this.#e.lastChild.dataset.value==="loading"&&this.#e.removeChild(this.#e.lastChild);const a=this.#e.lastChild.dataset.value;i.forEach(n=>{n.text.toLowerCase().includes(e.toLowerCase())&&!t.includes(n.id)&&!s.includes(n.id)&&this.#l(n)}),this.#p=a===this.#e.lastChild.dataset.value}#I(t,e){this.#t.find(i=>i.value===e)||(this.isMultiple||(this.#t=[]),this.#t.push({text:t,value:e})),this.#d(e)}#d(t){this.onChange!==void 0&&this.onChange(t,this),this.isMultiple?this.#v():this.#T()}#T(){this.#a.value=null,this.#a.value=this.#t.length>0?this.#t[0].text:null,this.#a.placeholder=this.singleLabel,this.#c.value=this.#t.length>0?this.#t[0].value:null;const t=this.#a.parentNode,e=t.firstChild;if(e.className==="remove-tag"&&e.remove(),!this.clearButton||this.#t.length<=0)return;const s=document.createElement("span");s.dataset.value=this.#t[0].value,s.innerHTML="x",s.className="remove-tag",t.insertBefore(s,t.firstChild)}#v(){if(this.#s.innerHTML="",this.#t.length<=0){this.#s.appendChild(document.createElement("div"));return}this.#t.forEach(({text:t,value:e})=>{const s=document.createElement("div");s.className="selected-tag",s.innerHTML=`${t} <span class="remove-tag" data-value="${e}">x</span>`,this.#s.appendChild(s)})}#R(t){this.#t=this.#t.filter(e=>this.#o(e.value)!==this.#o(t)),this.#d(t)}#A(t){const{scrollTop:e,scrollHeight:s,clientHeight:i}=this.#e;t.target.lastChild!==null&&(t.target.lastChild.dataset.value==="loading"||this.#p||e+(i+1)>=s&&(this.#l({id:"loading",text:"cargando ..."}),this.#M()))}#o(t){return!isNaN(t)&&!isNaN(parseFloat(t))?parseInt(t):t}async render(){const t=document.getElementById(this.id),e=t===null?!1:t.outerHTML.includes("<select");this.#m===void 0||e?this.#S():this.#d()}}h.selectyc=d,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
1
+ (function(h,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(h=typeof globalThis<"u"?globalThis:h||self,l(h.selectyc={}))})(this,function(h){"use strict";class l{constructor(t){this.route=t}static make(t){return new l(t)}setMethod(t){return this.method=t,this}getMethod(){return this.method===void 0?"POST":this.method}setData(t){return this.data=t,this}addData(t){if(this.data===void 0)return this.setData(t);for(const e in t)this.data[e]=t[e];return this}setFun(t){return this.fun=t,this}async go(){try{const t={method:this.getMethod(),headers:{"X-CSRF-TOKEN":window.CSRF_TOKEN,Accept:"application/json"}};this.data!==void 0&&(t.body=this.data,Object.prototype.toString.call(this.data)!=="[object FormData]"&&(t.body=JSON.stringify(this.data),t.headers["Content-Type"]="application/json"));const e=await fetch(this.route,t);if(!e.ok){const s=await e.json();throw new Error(s.message)}return this.fun===void 0?await e.json():await this.fun(e)}catch(t){window.toastNotification.setType(!1).show(t.message)}}}class d{#t=[];#c;#m=[];#r;#g;#s;#e;#i;#a;#u;#n=!1;#p=!1;#f=!1;#C="";#S='<svg class="w-5 h-5 text-gray-900 dark:text-gray-400" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m19 9-7 7-7-7"/></svg>';constructor(t){if(window.selects===void 0&&(window.selects=[]),window.selects[t]!==void 0)return window.selects[t];this.id=t,this.name,this.data=[],this.isMultiple=!1,this.clearButton=!1,this.singleLabel=" ",this.records=10,this.filter=null,this.bodyRound="",this.width="100%",this.searchInputActive=!0,this.bodyData=null,this.boxOverflow="horizontal",window.selects[t]=this}static set(t){return new d(t)}setData(t){return this.data=t,this}setMultiple(t="horizontal"){return this.isMultiple=!0,this.boxOverflow=t,this}setClearButton(){return this.clearButton=!0,this}setSelectedOptions(t){this.#t=t,this.#d(t)}getSelectedOptions(){return this.#t}getSelectedOption(){return this.#t[0]}getOptions(){return this.data}setSingleLabel(t){return this.singleLabel=t,this}setBodyRound(t){return this.bodyRound=`-${t}`,this}setRS(t){return this.RS=t,this}setBodyData(t){return this.bodyData=t,this}desactiveSearchInput(){return this.searchInputActive=!1,this}addOption(t){this.data.push(t)}setWidth(t){return this.width=t,this}setOnSelect(t){return this.onSelect=t,this}setOnUnselect(t){return this.onUnselect=t,this}setOnChange(t){return this.onChange=t,this}cleanSelection(){const t=this.#t;this.#t=[],this.#d(t)}#x(){this.#E(),this.#s.addEventListener("click",t=>{if(t.target.classList.contains("remove-tag")){this.#A(t.target.getAttribute("data-value")),this.onUnselect!==void 0&&this.onUnselect(t.target.getAttribute("data-value"),this),this.#n&&this.#h(),this.#y();return}this.#n?this.#h():this.#y()}),document.addEventListener("click",t=>{!this.#r.contains(t.target)&&!this.#e.contains(t.target)&&!t.target.classList.contains("remove-tag")&&this.#h()}),window.onresize=()=>{this.#h()},this.isMultiple&&this.#b()}#E(){this.#I();const t=document.createElement("div");t.className="main-div-selectyc",t.style.width=this.width,t.id=`selectyc_main_div_${this.id}`;const e=document.createElement("div");e.className="custom-multiselect";let s=document.createElement("div");s.className="selected-items round"+this.bodyRound,s.tabIndex=0,s.classList.add(this.boxOverflow==="horizontal"?"horizontal-overflow":"vertical-overflow"),this.isMultiple||(s=this.#D(s)),e.appendChild(s),e.appendChild(this.#N()),t.appendChild(e);const i=document.getElementById(this.id),a=document.createElement("input");a.name=i.name,a.id=i.id,this.rootClassName!==""&&(a.className=i.className),a.style.display="none";let n=i.parentNode;t.appendChild(a),n.replaceChild(t,i),this.#c=a,this.#g=s,this.#r=e,this.#s=this.#r.querySelector(".selected-items"),!this.isMultiple&&this.#t.length>0&&(this.#c.value=this.#t[0].value)}#D(t){const e=document.createElement("input");e.className="custom-single-select",e.id=`selectyc_text_box_${this.id}`,e.placeholder=this.singleLabel,e.value=this.#t.length===0?null:this.#t[0].text,e.readOnly=!0;const s=document.createElement("div");return s.innerHTML=this.#S,this.#p&&t.appendChild(this.#L()),t.appendChild(e),t.appendChild(s),this.#a=e,t}#L(){const t=document.createElement("input");return t.type="text",t.required=this.#p,t.tabIndex=-1,t.style.position="absolute",t.style.opacity="0",t.style.pointerEvents="none",t.style.height="0",t.style.width="0",t.style.left="50%",t.value=this.#t.length===0?"":this.#t[0].text,this.#u=t,t}#N(){const t=document.createElement("div");t.setAttribute("style","position:fixed; z-index:999;");const e=document.createElement("div");e.className="global-dropdown",this.searchInputActive&&t.appendChild(this.#O());let s;return e.addEventListener("scroll",i=>{s&&clearTimeout(s),s=setTimeout(()=>{this.#B(i)},70)}),t.appendChild(e),this.#e=e,t}#O(){const t=document.createElement("input");return t.type="text",t.id="selectyc_search_input",t.className="search-input",t.placeholder="Buscar...",t.addEventListener("input",()=>{this.filter=t.value,this.#e.innerHTML="",this.filter=t.value,this.#w()}),this.#i=t,t}#l(t){const e=document.createElement("div");e.className="select-option",e.setAttribute("data-value",t.id),e.textContent=t.text,this.#e.appendChild(e),e.addEventListener("click",()=>{t.id!=="sin_resultados"&&(this.#T(t.text,t.id),this.onSelect!==void 0&&this.onSelect(t.id,this),this.#h())})}#I(){const t=document.getElementById(this.id);this.name=t.name,this.#p=t.required,this.rootClassName=t.className,Array.from(t.options).forEach(e=>{if(e.outerHTML.includes("selected")){const s=e.text,i=this.#o(e.value);this.isMultiple?this.#t.push({value:i,text:s}):this.#t[0]={value:i,text:s}}this.#m.push({id:this.#o(e.value),text:e.text})})}#y(){if(this.#n)return;this.#s.classList.remove(`round${this.bodyRound}`),this.#s.classList.add(`round${this.bodyRound}t`);const t=this.#r.getBoundingClientRect();if(this.searchInputActive){this.#i.style.display="block",this.#i.style.width=t.width+"px";const e=this.#i.offsetHeight;this.#e.style.top=e+"px"}this.#e.style.width=t.width+"px",this.#e.innerHTML="",this.#e.style.display="block",this.searchInputActive&&this.#i.focus(),this.#w(),this.#n=!0}#h(){this.#n&&(this.#e.innerHTML="",this.#e.style.display="none",this.searchInputActive&&(this.#i.style.display="none"),this.#s.classList.add(`round${this.bodyRound}`),this.#n=!1)}async#v(){if(this.RS===void 0)return this.data;let t={records:this.records,filter:this.filter};if(typeof this.bodyData=="function"){const i=this.bodyData();t={...t,...i}}else this.bodyData!==null&&(t={...t,...this.bodyData});const e=await l.make(this.RS).addData(t).setMethod("POST").go(),s=Array.isArray(e)?e:e.data;return this.data=Array.from(new Map([...this.#m,...s].map(i=>[i.id,i])).values()),this.data}async#w(){this.#l({id:"loading",text:"cargando ..."}),this.records=10+this.#t.length,this.#f=!1;let t=await this.#v();const e=this.#t.map(s=>s.value);this.#e.lastChild.dataset.value==="loading"&&this.#e.removeChild(this.#e.lastChild),t.forEach(s=>{s.text.toLowerCase().includes(this.#C.toLowerCase())&&!e.includes(s.id)&&this.#l(s)}),this.#e.hasChildNodes()||this.#l({id:"sin_resultados",text:"sin resultados ..."})}async#M(){const t=this.#t.map(n=>n.value);let e=this.filter===null?"":this.filter,s=this.data.map(n=>n.id);this.records=this.records+10+this.#t.length;let i=await this.#v();this.#e.lastChild.dataset.value==="loading"&&this.#e.removeChild(this.#e.lastChild);const a=this.#e.lastChild.dataset.value;i.forEach(n=>{n.text.toLowerCase().includes(e.toLowerCase())&&!t.includes(n.id)&&!s.includes(n.id)&&this.#l(n)}),this.#f=a===this.#e.lastChild.dataset.value}#T(t,e){this.#t.find(i=>i.value===e)||(this.isMultiple||(this.#t=[]),this.#t.push({text:t,value:e})),this.#d(e)}#d(t){this.onChange!==void 0&&this.onChange(t,this),this.isMultiple?this.#b():this.#R()}#R(){this.#a.value=null,this.#u.value=null,this.#a.value=this.#t.length>0?this.#t[0].text:null,this.#u.value=this.#t.length>0?this.#t[0].text:null,this.#a.placeholder=this.singleLabel,this.#c.value=this.#t.length>0?this.#t[0].value:null;const t=this.#a.parentNode,e=t.firstChild;if(e.className==="remove-tag"&&e.remove(),!this.clearButton||this.#t.length<=0)return;const s=document.createElement("span");s.dataset.value=this.#t[0].value,s.innerHTML="x",s.className="remove-tag",t.insertBefore(s,t.firstChild)}#b(){if(this.#s.innerHTML="",this.#t.length<=0){this.#s.appendChild(document.createElement("div"));return}this.#t.forEach(({text:t,value:e})=>{const s=document.createElement("div");s.className="selected-tag",s.innerHTML=`${t} <span class="remove-tag" data-value="${e}">x</span>`,this.#s.appendChild(s)})}#A(t){this.#t=this.#t.filter(e=>this.#o(e.value)!==this.#o(t)),this.#d(t)}#B(t){const{scrollTop:e,scrollHeight:s,clientHeight:i}=this.#e;t.target.lastChild!==null&&(t.target.lastChild.dataset.value==="loading"||this.#f||e+(i+1)>=s&&(this.#l({id:"loading",text:"cargando ..."}),this.#M()))}#o(t){return!isNaN(t)&&!isNaN(parseFloat(t))?parseInt(t):t}async render(){const t=document.getElementById(this.id),e=t===null?!1:t.outerHTML.includes("<select");this.#g===void 0||e?this.#x():this.#d()}}h.selectyc=d,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "selectyc",
3
3
  "description": "select multiple and simple",
4
- "version": "0.0.11",
4
+ "version": "0.0.12",
5
5
  "main": "./dist/selectyc.umd.js",
6
6
  "module": "./dist/selectyc.js",
7
7
  "type": "module",