slim-select 4.0.3 → 4.0.4

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/vue/index.js CHANGED
@@ -334,11 +334,14 @@ var P = class {
334
334
  }
335
335
  this.state = "open", this.handlers.afterOpen && this.handlers.afterOpen(), this.handlers.onOpenReady && this.handlers.onOpenReady();
336
336
  }
337
- async requestClose() {
338
- if (this.state === "closed" || this.state === "closing") return;
337
+ async requestClose(e = {
338
+ source: "api",
339
+ selectionChanged: !1
340
+ }) {
341
+ if (this.state === "closed" || this.state === "closing" || this.handlers.beforeClose && this.handlers.beforeClose(e) === !1) return !1;
339
342
  this.cancelPending();
340
- let e = ++this.generation;
341
- this.handlers.beforeClose && this.handlers.beforeClose(), this.state = "closing", await this.waitForPhase("close", e), e === this.generation && (this.state = "closed", this.handlers.afterClose && this.handlers.afterClose(), this.handlers.onCloseReady && this.handlers.onCloseReady());
343
+ let t = ++this.generation;
344
+ return this.handlers.onClose && this.handlers.onClose(e), this.state = "closing", await this.waitForPhase("close", t), t === this.generation ? (this.state = "closed", this.handlers.afterClose && this.handlers.afterClose(), this.handlers.onCloseReady && this.handlers.onCloseReady(), !0) : !1;
342
345
  }
343
346
  cancelPending() {
344
347
  this.generation++, this.animationAbort?.abort(), this.animationAbort = null, this.pendingTimer &&= (clearTimeout(this.pendingTimer), null);
@@ -597,6 +600,24 @@ var P = class {
597
600
  constructor(e, t, n, r) {
598
601
  this.store = n, this.settings = e, this.classes = t, this.callbacks = r, this.lastSelectedOption = null, this.lastRenderedOptions = [], this.main = this.mainDiv(), this.content = this.contentDiv(), this.updateClassStyles(), this.updateAriaAttributes(), this.settings.contentPosition !== "relative" && (this.content.main.style.top = "-9999px", this.content.main.style.left = "-9999px", this.content.main.style.margin = "0", this.content.main.style.width = "auto"), this.settings.contentLocation && this.settings.contentLocation.appendChild(this.content.main), this.settings.modal !== "off" && !this.settings.alwaysOpen && (this.modalElements = this.createModalElements(), document.body.appendChild(this.modalElements.overlay));
599
602
  }
603
+ requestClose(e, t) {
604
+ this.callbacks.close({
605
+ source: e,
606
+ option: t?.option,
607
+ selectionChanged: t?.selectionChanged ?? !1
608
+ });
609
+ }
610
+ selectionChanged(e, t) {
611
+ if (e.length !== t.length) return !0;
612
+ let n = e.map((e) => e.id).sort(), r = t.map((e) => e.id).sort();
613
+ return n.some((e, t) => e !== r[t]);
614
+ }
615
+ closeOnSingleSelectReclick(e) {
616
+ this.settings.closeOnSelect && this.requestClose("select", {
617
+ option: e,
618
+ selectionChanged: !1
619
+ });
620
+ }
600
621
  addClasses(e, t) {
601
622
  if (!t || t.trim() === "") return;
602
623
  let n = t.split(" ").filter((e) => e.trim() !== "");
@@ -642,13 +663,13 @@ var P = class {
642
663
  this.settings.modalTitle ? (n = document.createElement("div"), this.addClasses(n, this.classes.modalTitle), n.id = `${this.settings.id}-modal-title`, n.textContent = this.settings.modalTitle, t.setAttribute("aria-labelledby", n.id)) : t.setAttribute("aria-label", this.settings.ariaLabel);
643
664
  let r = document.createElement("button");
644
665
  r.type = "button", this.addClasses(r, this.classes.modalClose), r.setAttribute("aria-label", "Close"), r.onclick = (e) => {
645
- e.stopPropagation(), this.callbacks.close();
666
+ e.stopPropagation(), this.requestClose("modal");
646
667
  };
647
668
  let i = document.createElementNS("http://www.w3.org/2000/svg", "svg");
648
669
  i.setAttribute("viewBox", "0 0 100 100");
649
670
  let a = document.createElementNS("http://www.w3.org/2000/svg", "path");
650
671
  return a.setAttribute("d", this.classes.deselectPath), i.appendChild(a), r.appendChild(i), e.onclick = (t) => {
651
- t.target === e && this.callbacks.close();
672
+ t.target === e && this.requestClose("modal");
652
673
  }, n && t.appendChild(n), e.appendChild(t), {
653
674
  overlay: e,
654
675
  dialog: t,
@@ -699,17 +720,17 @@ var P = class {
699
720
  switch (e.key) {
700
721
  case "ArrowUp":
701
722
  case "ArrowDown": return this.callbacks.open(), e.key === "ArrowDown" ? this.highlight("down") : this.highlight("up"), !1;
702
- case "Tab": return this.callbacks.close(), !0;
723
+ case "Tab": return this.requestClose("tab"), !0;
703
724
  case "Enter":
704
725
  case " ":
705
726
  this.callbacks.open();
706
727
  let t = this.content.list.querySelector("." + this.classes.getFirst("highlighted"));
707
728
  return t && t.click(), !1;
708
- case "Escape": return this.callbacks.close(), !1;
729
+ case "Escape": return this.requestClose("escape"), !1;
709
730
  }
710
731
  return e.key.length === 1 && this.callbacks.open(), !0;
711
732
  }, e.onclick = (e) => {
712
- this.settings.disabled || (this.settings.isOpen ? this.callbacks.close() : this.callbacks.open());
733
+ this.settings.disabled || (this.settings.isOpen ? this.requestClose("toggle") : this.callbacks.open());
713
734
  };
714
735
  let t = document.createElement("div");
715
736
  this.addClasses(t, this.classes.values), e.appendChild(t);
@@ -731,7 +752,7 @@ var P = class {
731
752
  let e = this.store.getFirstOption(), t = e ? e.id : "";
732
753
  this.callbacks.setSelected(t, !1);
733
754
  }
734
- this.settings.closeOnSelect && this.callbacks.close(), this.callbacks.afterChange && this.callbacks.afterChange(this.store.getSelectedOptions());
755
+ this.settings.closeOnSelect && this.requestClose("deselect", { selectionChanged: !0 }), this.callbacks.afterChange && this.callbacks.afterChange(this.store.getSelectedOptions());
735
756
  }
736
757
  }, n.onkeydown = (e) => {
737
758
  (e.key === "Enter" || e.key === " ") && (e.preventDefault(), n.click());
@@ -851,7 +872,7 @@ var P = class {
851
872
  if (t instanceof L) for (let n of t.options) n.id && e.push(n.id);
852
873
  t instanceof I && e.push(t.id);
853
874
  }
854
- this.callbacks.setSelected(e, !1), this.settings.closeOnSelect && this.callbacks.close(), this.callbacks.afterChange && this.callbacks.afterChange(i), this.updateDeselectAll(), this.updateMultipleValueDeleteVisibility();
875
+ this.callbacks.setSelected(e, !1), this.settings.closeOnSelect && this.requestClose("deselect", { selectionChanged: !0 }), this.callbacks.afterChange && this.callbacks.afterChange(i), this.updateDeselectAll(), this.updateMultipleValueDeleteVisibility();
855
876
  }
856
877
  };
857
878
  let r = document.createElementNS("http://www.w3.org/2000/svg", "svg");
@@ -929,8 +950,8 @@ var P = class {
929
950
  switch (e.key) {
930
951
  case "ArrowUp":
931
952
  case "ArrowDown": return e.key === "ArrowDown" ? this.highlight("down") : this.highlight("up"), !1;
932
- case "Tab": return this.callbacks.close(), !0;
933
- case "Escape": return this.callbacks.close(), !1;
953
+ case "Tab": return this.requestClose("tab"), !0;
954
+ case "Escape": return this.requestClose("escape"), !1;
934
955
  case "Enter":
935
956
  let t = this.content.list.querySelector("." + this.classes.getFirst("highlighted"));
936
957
  return t ? (t.click(), !1) : this.callbacks.addable ? (n.click(), !1) : !0;
@@ -957,7 +978,7 @@ var P = class {
957
978
  if (this.callbacks.search(""), this.settings.closeOnSelect) {
958
979
  let e = d(this.content.main);
959
980
  setTimeout(() => {
960
- this.callbacks.close();
981
+ this.requestClose("select", { selectionChanged: !0 });
961
982
  }, e);
962
983
  }
963
984
  }, r = this.callbacks.addable(t);
@@ -1240,23 +1261,35 @@ var P = class {
1240
1261
  t.classList.add(e);
1241
1262
  }), e.style && (t.style.cssText = e.style), this.setOptionElementContent(t, e, this.content.search.input.value), e.display || this.addClasses(t, this.classes.hide), e.disabled && this.addClasses(t, this.classes.disabled), e.selected && this.settings.hideSelected && this.addClasses(t, this.classes.hide), e.selected ? (this.addClasses(t, this.classes.selected), t.setAttribute("aria-selected", "true"), this.main.main.setAttribute("aria-activedescendant", t.id)) : (this.removeClasses(t, this.classes.selected), t.setAttribute("aria-selected", "false")), t.addEventListener("click", (t) => {
1242
1263
  t.preventDefault(), t.stopPropagation();
1243
- let n = this.store.getSelected(), r = t.currentTarget, i = String(r.dataset.id), a = t.ctrlKey || t.metaKey;
1244
- if (e.disabled || !this.settings.isMultiple && e.selected && !this.settings.allowDeselect || e.selected && e.mandatory || this.settings.isMultiple && this.settings.maxSelected <= n.length && !e.selected || this.settings.isMultiple && this.settings.minSelected >= n.length && e.selected && !a) return;
1245
- let o = !1, s = this.store.getSelectedOptions(), c = [];
1264
+ let n = this.store.getSelected(), r = t.currentTarget, i = String(r.dataset.id), a = this.store.getOptionByID(i) ?? e, o = n.includes(i), s = t.ctrlKey || t.metaKey;
1265
+ if (e.disabled) return;
1266
+ if (!this.settings.isMultiple && o && !this.settings.allowDeselect) {
1267
+ this.closeOnSingleSelectReclick(a);
1268
+ return;
1269
+ }
1270
+ if (o && a.mandatory) {
1271
+ this.closeOnSingleSelectReclick(a);
1272
+ return;
1273
+ }
1274
+ if (this.settings.isMultiple && this.settings.maxSelected <= n.length && !o || this.settings.isMultiple && this.settings.minSelected >= n.length && o && !s) return;
1275
+ let c = !1, l = this.store.getSelectedOptions(), u = [];
1246
1276
  if (this.settings.isMultiple) {
1247
- let n = s.some((e) => e.id === i);
1277
+ let n = l.some((e) => e.id === i);
1248
1278
  if (t.shiftKey && this.lastSelectedOption) {
1249
1279
  let t = this.lastRenderedOptions, n = t.findIndex((e) => e.id === this.lastSelectedOption.id), r = t.findIndex((t) => t.id === e.id);
1250
1280
  if (n >= 0 && r >= 0) {
1251
- let e = Math.min(n, r), i = Math.max(n, r), a = t.slice(e, i + 1).filter((e) => !s.find((t) => t.id === e.id));
1252
- c = s.length + a.length <= this.settings.maxSelected ? s.concat(a) : s;
1253
- } else c = s;
1254
- } else c = n ? s.filter((e) => e.id !== i) : s.concat(e), this.lastSelectedOption = e;
1281
+ let e = Math.min(n, r), i = Math.max(n, r), a = t.slice(e, i + 1).filter((e) => !l.find((t) => t.id === e.id));
1282
+ u = l.length + a.length <= this.settings.maxSelected ? l.concat(a) : l;
1283
+ } else u = l;
1284
+ } else u = n ? l.filter((e) => e.id !== i) : l.concat(a), this.lastSelectedOption = a;
1255
1285
  }
1256
- if (this.settings.isMultiple || (c = e.selected ? [] : [e]), this.callbacks.beforeChange || (o = !0), this.callbacks.beforeChange && (o = this.callbacks.beforeChange(c, s) !== !1), o) {
1257
- this.store.getOptionByID(i) || this.callbacks.addOption(e), this.callbacks.setSelected(c.map((e) => e.id), !1);
1286
+ if (this.settings.isMultiple || (u = o ? [] : [a]), this.callbacks.beforeChange || (c = !0), this.callbacks.beforeChange && (c = this.callbacks.beforeChange(u, l) !== !1), c) {
1287
+ this.store.getOptionByID(i) || this.callbacks.addOption(e), this.callbacks.setSelected(u.map((e) => e.id), !1);
1258
1288
  let n = t.ctrlKey || t.metaKey || t.shiftKey;
1259
- this.settings.closeOnSelect && !(this.settings.isMultiple && n) && this.callbacks.close(), this.callbacks.afterChange && this.callbacks.afterChange(c);
1289
+ this.settings.closeOnSelect && !(this.settings.isMultiple && n) && this.requestClose("select", {
1290
+ option: a,
1291
+ selectionChanged: this.selectionChanged(l, u)
1292
+ }), this.callbacks.afterChange && this.callbacks.afterChange(u);
1260
1293
  }
1261
1294
  }), t;
1262
1295
  }
@@ -1761,7 +1794,6 @@ var W = class {
1761
1794
  let t = [
1762
1795
  "beforeOpen",
1763
1796
  "afterOpen",
1764
- "beforeClose",
1765
1797
  "afterClose"
1766
1798
  ];
1767
1799
  for (let n in e.events) e.events.hasOwnProperty(n) && (t.indexOf(n) === -1 ? this.events[n] = e.events[n] : this.events[n] = C(e.events[n], 100));
@@ -1770,7 +1802,10 @@ var W = class {
1770
1802
  }, this.select.onDisabledChange = (e) => {
1771
1803
  e ? this.disable() : this.enable();
1772
1804
  }, this.select.onLabelClick = () => {
1773
- this.settings.disabled || (this.settings.isOpen ? this.close() : this.open());
1805
+ this.settings.disabled || (this.settings.isOpen ? this.close({
1806
+ source: "toggle",
1807
+ selectionChanged: !1
1808
+ }) : this.open());
1774
1809
  };
1775
1810
  let n = e.data ? e.data : this.select.getData();
1776
1811
  this.store = new R(this.settings.isMultiple ? "multiple" : "single", n), e.data && this.select.updateOptions(this.store.getData());
@@ -1818,7 +1853,8 @@ var W = class {
1818
1853
  }, this.lifecycle = new F({
1819
1854
  beforeOpen: this.events.beforeOpen,
1820
1855
  afterOpen: this.events.afterOpen,
1821
- beforeClose: this.events.beforeClose,
1856
+ beforeClose: (e) => this.events.beforeClose?.(e),
1857
+ onClose: (e) => this.applyClose(e),
1822
1858
  afterClose: () => {
1823
1859
  this.render.clearDirectionClasses(), this.render.finalizeModalClose(), this.events.afterClose && this.events.afterClose();
1824
1860
  },
@@ -1836,7 +1872,10 @@ var W = class {
1836
1872
  !this.settings.isOpen && !this.settings.isFullOpen || this.render.moveContent();
1837
1873
  },
1838
1874
  onVisibilityChange: () => {
1839
- document.hidden && this.close();
1875
+ document.hidden && this.close({
1876
+ source: "api",
1877
+ selectionChanged: !1
1878
+ });
1840
1879
  }
1841
1880
  }), this.render.renderValues(), this.render.renderOptions(this.store.getData(!1));
1842
1881
  let i = this.selectEl.getAttribute("aria-label"), a = this.selectEl.getAttribute("aria-labelledby");
@@ -1891,10 +1930,18 @@ var W = class {
1891
1930
  let e = this.render.content.search.input.value.trim();
1892
1931
  e !== "" && this.search(e);
1893
1932
  }
1894
- close(e = null) {
1895
- !this.settings.isOpen || this.settings.alwaysOpen || (this.lifecycle.cancelPending(), this.render.close(), !this.settings.keepSearch && this.render.content.search.input.value !== "" && (this.sync.flush(), this.search("")), this.render.mainFocus(e), this.settings.isOpen = !1, this.settings.isFullOpen = !1, this.lifecycle.requestClose().then(() => {
1896
- this.settings.isOpen || (this.settings.isOpen = this.lifecycle.isOpen, this.settings.isFullOpen = this.lifecycle.isFullOpen);
1897
- }), this.render.stopPositionTracking());
1933
+ close(e = {
1934
+ source: "api",
1935
+ selectionChanged: !1
1936
+ }) {
1937
+ !this.settings.isOpen || this.settings.alwaysOpen || this.lifecycle.requestClose(e).then((e) => {
1938
+ e && (this.settings.isOpen || (this.settings.isOpen = this.lifecycle.isOpen, this.settings.isFullOpen = this.lifecycle.isFullOpen));
1939
+ });
1940
+ }
1941
+ applyClose(e) {
1942
+ this.render.close(), !this.settings.keepSearch && this.render.content.search.input.value !== "" && (this.sync.flush(), this.search(""));
1943
+ let t = e.source === "outside" || e.source === "toggle";
1944
+ this.render.mainFocus(t ? "click" : null), this.settings.isOpen = !1, this.settings.isFullOpen = !1, this.render.stopPositionTracking();
1898
1945
  }
1899
1946
  search(e) {
1900
1947
  let t = e.trim();
@@ -1956,7 +2003,10 @@ var W = class {
1956
2003
  this.lifecycle.destroy(), this.render.stopPositionTracking(), this.globalEvents.detach({ listenScroll: this.settings.openPosition === "auto" }), this.store.setData([]), this.render.destroy(), this.select.destroy(), delete this.selectEl.slim;
1957
2004
  }
1958
2005
  documentClick(e) {
1959
- this.settings.isOpen && e.target && !S(e.target, this.settings.id) && this.close(e.type);
2006
+ this.settings.isOpen && e.target && !S(e.target, this.settings.id) && this.close({
2007
+ source: "outside",
2008
+ selectionChanged: !1
2009
+ });
1960
2010
  }
1961
2011
  };
1962
2012
  //#endregion