PageSourceSearch

https://lsenglish.com.tw/assets/vendor/@windwalker-io/unicorn-next/dist/chunks/field-cascade-select.js

js lsenglish.com.tw collected 2026-10-06 02:26:17 UTC 7,410 bytes, 256 lines download raw bytes

1import { g as getDefaultExportFromCjs } from "./_commonjsHelpers.js";
2import { a6 as prepareAlpineDefer, f as initAlpineComponent, a7 as mergeDeep, K as uid, z as module$1, u as useHttpClient } from "./unicorn.js";
3var alpineComponent = {};
4var hasRequiredAlpineComponent;
5function requireAlpineComponent() {
6  if (hasRequiredAlpineComponent) return alpineComponent;
7  hasRequiredAlpineComponent = 1;
8  Object.defineProperty(alpineComponent, "__esModule", { value: true });
9  class AlpineComponent2 {
10  }
11  alpineComponent.default = AlpineComponent2;
12  return alpineComponent;
13}
14var alpineComponentExports = /* @__PURE__ */ requireAlpineComponent();
15const AlpineComponent = /* @__PURE__ */ getDefaultExportFromCjs(alpineComponentExports);
16var component = {};
17var hasRequiredComponent;
18function requireComponent() {
19  if (hasRequiredComponent) return component;
20  hasRequiredComponent = 1;
21  Object.defineProperty(component, "__esModule", { value: true });
22  function Component2(component2) {
23    return function(...args) {
24      let instance = new component2(...args);
25      const proto = component2.prototype;
26      let data = {};
27      Object.getOwnPropertyNames(proto).forEach(function(key) {
28        if (key === "constructor") {
29          return;
30        }
31        const descriptor = Object.getOwnPropertyDescriptor(proto, key);
32        if (descriptor.value !== void 0) {
33          if (typeof descriptor.value === "function") {
34            data[key] = descriptor.value;
35          }
36        } else if (descriptor.get || descriptor.set) {
37          Object.defineProperty(data, key, { get: descriptor.get, set: descriptor.set });
38        }
39      });
40      return Object.assign(data, instance);
41    };
42  }
43  component.default = Component2;
44  return component;
45}
46var componentExports = /* @__PURE__ */ requireComponent();
47const Component = /* @__PURE__ */ getDefaultExportFromCjs(componentExports);
48var __getOwnPropDesc = /* @__PURE__ */ (() => Object.getOwnPropertyDescriptor)();
49var __decorateClass = (decorators, target, key, kind) => {
50  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
51  for (var i = decorators.length - 1, decorator; i >= 0; i--)
52    if (decorator = decorators[i])
53      result = decorator(result) || result;
54  return result;
55};
56const defaultOptions = {
57  id: "",
58  selected: "",
59  path: [],
60  ignoreSelf: null,
61  placeholder: "- Select -",
62  placeholders: [],
63  ajaxUrl: "",
64  ajaxValueField: "value",
65  source: [],
66  labels: [],
67  labelWidth: "col-md-3",
68  fieldWidth: "col",
69  readonly: false,
70  disabled: false,
71  valueField: "id",
72  textField: "title",
73  horizontal: null,
74  horizontalColWidth: null,
75  defaultValue: "",
76  onSelectInit: () => {
77  },
78  onChange: () => {
79  },
80  onValueInit: () => {
81  }
82};
83let FieldCascadeSelect = class extends AlpineComponent {
84  options;
85  el;
86  canModify = false;
87  lists = [];
88  ajaxUrl = "";
89  values = [];
90  constructor(options = {}) {
91    super();
92    this.options = mergeDeep({}, defaultOptions, options);
93    this.options.id = this.options.id || "cascade-select-" + uid();
94  }
95  init() {
96    this.canModify = !this.options.readonly && !this.options.disabled;
97    this.ajaxUrl = this.options.ajaxUrl;
98    this.values = this.options.path.slice().map(String);
99    let values = this.options.path.slice();
100    if (values.length === 0) {
101      values = [null];
102    } else {
103      values.unshift(null);
104    }
105    let promise = Promise.resolve();
106    let lastValue = null;
107    values.forEach((v, i) => {
108      promise = promise.then(() => {
109        return this.loadItems(v, i).then((list) => {
110          if (list.length > 0) {
111            this.lists.push(list);
112          }
113        });
114      });
115      lastValue = v;
116    });
117    this.el = this.$el;
118    module$1(this.$el, "cascade.select", () => this);
119    this.valueInit(this.$el, lastValue, values);
120  }
121  getLabel(i) {
122    return this.options.labels[i] || `Level ${i + 1}`;
123  }
124  getId(i) {
125    return `${this.options.id}__level-${i}`;
126  }
127  getListValue(i) {
128    return this.values[i] || "";
129  }
130  isSelected(i, item) {
131    return String(this.getListValue(i)) === String(item[this.options.valueField]);
132  }
133  getFinalValue() {
134    const values = this.values.slice();
135    if (values.length === 0) {
136      return this.options.defaultValue;
137    }
138    const v = values.filter((v2) => v2 != null).filter((v2) => v2 !== "").pop();
139    if (v == void 0) {
140      return this.options.defaultValue;
141    }
142    return v;
143  }
144  getLevel() {
145    return this.values.length;
146  }
147  async onChange(i, event) {
148    const el = event.target;
149    this.values[i] = el.value;
150    this.options.onChange(event);
151    event.stopPropagation();
152    const changeEvent = new CustomEvent("change", {
153      detail: {
154        el,
155        component: this,
156        value: el.value,
157        path: this.values
158      }
159    });
160    this.el?.dispatchEvent(changeEvent);
161    if (el.value === "") {
162      this.lists.splice(i + 1);
163      this.values.splice(i + 1);
164      return;
165    }
166    let list = await this.loadItems(el.value, i);
167    this.lists.splice(i + 1);
168    this.values.splice(i + 1);
169    if (list.length > 0) {
170      this.lists.push(list);
171    }
172  }
173  async loadItems(parentId, i) {
174    if (this.ajaxUrl) {
175      const http = await useHttpClient();
176      let res = await http.get(
177        this.ajaxUrl,
178        {
179          params: {
180            [this.options.ajaxValueField]: parentId,
181            self: this.options.ignoreSelf || null
182          }
183        }
184      );
185      return await res.data.data;
186    }
187    if (parentId) {
188      return Promise.resolve(
189        this.handleSourceItems(
190          this.findFromList(this.lists[i - 1] || [], parentId)?.children || []
191        )
192      );
193    }
194    return Promise.resolve(this.handleSourceItems(this.options.source));
195  }
196  valueInit($select, value, path) {
197    const event = new CustomEvent("value.init", {
198      detail: {
199        el: $select,
200        component: this,
201        value,
202        path
203      }
204    });
205    this.options.onSelectInit(event);
206    this.$el.dispatchEvent(event);
207  }
208  selectInit($select) {
209    const event = new CustomEvent("select.init", {
210      detail: {
211        el: $select,
212        component: this
213      }
214    });
215    this.options.onSelectInit(event);
216    this.$el.dispatchEvent(event);
217  }
218  handleSourceItems(items) {
219    return items.map((item) => {
220      return {
221        [this.options.valueField]: item.value[this.options.valueField],
222        [this.options.textField]: item.value[this.options.textField],
223        children: item.children
224      };
225    }).filter((item) => {
226      if (this.options.ignoreSelf) {
227        return item[this.options.valueField] != this.options.ignoreSelf;
228      }
229      return item;
230    });
231  }
232  findFromList(items, value) {
233    const found = items.filter((item) => item[this.options.valueField] == value);
234    return found.shift();
235  }
236  getPlaceholder(i) {
237    if (this.options.placeholders[i]) {
238      return this.options.placeholders[i];
239    }
240    return this.options.placeholder;
241  }
242};
243FieldCascadeSelect = /* @__PURE__ */ __decorateClass([
244  Component
245], FieldCascadeSelect);
246async function init() {
247  await prepareAlpineDefer(() => {
248    Alpine.data("CascadeSelect", (options) => new FieldCascadeSelect(options));
249  });
250  await initAlpineComponent("data-cascade-select");
251}
252const ready = /* @__PURE__ */ init();
253export {
254  ready
255};
256//# sourceMappingURL=field-cascade-select.js.map

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.