Browse code

Add tom select npm package

Benjamin Roth authored on02/02/2023 12:00:30
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,328 @@
1
+/**
2
+* Tom Select v2.2.2
3
+* Licensed under the Apache License, Version 2.0 (the "License");
4
+*/
5
+
6
+/*! @orchidjs/unicode-variants | https://github.com/orchidjs/unicode-variants | Apache License (v2) */
7
+const accent_pat = '[\u0300-\u036F\u{b7}\u{2be}\u{2bc}]';
8
+/** @type {TUnicodeMap} */
9
+
10
+const latin_convert = {};
11
+/** @type {TUnicodeMap} */
12
+
13
+const latin_condensed = {
14
+  '/': '⁄∕',
15
+  '0': '߀',
16
+  "a": "ⱥɐɑ",
17
+  "aa": "ꜳ",
18
+  "ae": "æǽǣ",
19
+  "ao": "ꜵ",
20
+  "au": "ꜷ",
21
+  "av": "ꜹꜻ",
22
+  "ay": "ꜽ",
23
+  "b": "ƀɓƃ",
24
+  "c": "ꜿƈȼↄ",
25
+  "d": "đɗɖᴅƌꮷԁɦ",
26
+  "e": "ɛǝᴇɇ",
27
+  "f": "ꝼƒ",
28
+  "g": "ǥɠꞡᵹꝿɢ",
29
+  "h": "ħⱨⱶɥ",
30
+  "i": "ɨı",
31
+  "j": "ɉȷ",
32
+  "k": "ƙⱪꝁꝃꝅꞣ",
33
+  "l": "łƚɫⱡꝉꝇꞁɭ",
34
+  "m": "ɱɯϻ",
35
+  "n": "ꞥƞɲꞑᴎлԉ",
36
+  "o": "øǿɔɵꝋꝍᴑ",
37
+  "oe": "œ",
38
+  "oi": "ƣ",
39
+  "oo": "ꝏ",
40
+  "ou": "ȣ",
41
+  "p": "ƥᵽꝑꝓꝕρ",
42
+  "q": "ꝗꝙɋ",
43
+  "r": "ɍɽꝛꞧꞃ",
44
+  "s": "ßȿꞩꞅʂ",
45
+  "t": "ŧƭʈⱦꞇ",
46
+  "th": "þ",
47
+  "tz": "ꜩ",
48
+  "u": "ʉ",
49
+  "v": "ʋꝟʌ",
50
+  "vy": "ꝡ",
51
+  "w": "ⱳ",
52
+  "y": "ƴɏỿ",
53
+  "z": "ƶȥɀⱬꝣ",
54
+  "hv": "ƕ"
55
+};
56
+
57
+for (let latin in latin_condensed) {
58
+  let unicode = latin_condensed[latin] || '';
59
+
60
+  for (let i = 0; i < unicode.length; i++) {
61
+    let char = unicode.substring(i, i + 1);
62
+    latin_convert[char] = latin;
63
+  }
64
+}
65
+
66
+new RegExp(Object.keys(latin_convert).join('|') + '|' + accent_pat, 'gu');
67
+
68
+/**
69
+ * Iterates over arrays and hashes.
70
+ *
71
+ * ```
72
+ * iterate(this.items, function(item, id) {
73
+ *    // invoked for each item
74
+ * });
75
+ * ```
76
+ *
77
+ */
78
+
79
+const iterate = (object, callback) => {
80
+  if (Array.isArray(object)) {
81
+    object.forEach(callback);
82
+  } else {
83
+    for (var key in object) {
84
+      if (object.hasOwnProperty(key)) {
85
+        callback(object[key], key);
86
+      }
87
+    }
88
+  }
89
+};
90
+
91
+/**
92
+ * Add css classes
93
+ *
94
+ */
95
+
96
+const addClasses = (elmts, ...classes) => {
97
+  var norm_classes = classesArray(classes);
98
+  elmts = castAsArray(elmts);
99
+  elmts.map(el => {
100
+    norm_classes.map(cls => {
101
+      el.classList.add(cls);
102
+    });
103
+  });
104
+};
105
+/**
106
+ * Return arguments
107
+ *
108
+ */
109
+
110
+const classesArray = args => {
111
+  var classes = [];
112
+  iterate(args, _classes => {
113
+    if (typeof _classes === 'string') {
114
+      _classes = _classes.trim().split(/[\11\12\14\15\40]/);
115
+    }
116
+
117
+    if (Array.isArray(_classes)) {
118
+      classes = classes.concat(_classes);
119
+    }
120
+  });
121
+  return classes.filter(Boolean);
122
+};
123
+/**
124
+ * Create an array from arg if it's not already an array
125
+ *
126
+ */
127
+
128
+const castAsArray = arg => {
129
+  if (!Array.isArray(arg)) {
130
+    arg = [arg];
131
+  }
132
+
133
+  return arg;
134
+};
135
+
136
+/**
137
+ * Plugin: "restore_on_backspace" (Tom Select)
138
+ * Copyright (c) contributors
139
+ *
140
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
141
+ * file except in compliance with the License. You may obtain a copy of the License at:
142
+ * http://www.apache.org/licenses/LICENSE-2.0
143
+ *
144
+ * Unless required by applicable law or agreed to in writing, software distributed under
145
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
146
+ * ANY KIND, either express or implied. See the License for the specific language
147
+ * governing permissions and limitations under the License.
148
+ *
149
+ */
150
+function plugin () {
151
+  const self = this;
152
+  const orig_canLoad = self.canLoad;
153
+  const orig_clearActiveOption = self.clearActiveOption;
154
+  const orig_loadCallback = self.loadCallback;
155
+  var pagination = {};
156
+  var dropdown_content;
157
+  var loading_more = false;
158
+  var load_more_opt;
159
+  var default_values = [];
160
+
161
+  if (!self.settings.shouldLoadMore) {
162
+    // return true if additional results should be loaded
163
+    self.settings.shouldLoadMore = () => {
164
+      const scroll_percent = dropdown_content.clientHeight / (dropdown_content.scrollHeight - dropdown_content.scrollTop);
165
+
166
+      if (scroll_percent > 0.9) {
167
+        return true;
168
+      }
169
+
170
+      if (self.activeOption) {
171
+        var selectable = self.selectable();
172
+        var index = Array.from(selectable).indexOf(self.activeOption);
173
+
174
+        if (index >= selectable.length - 2) {
175
+          return true;
176
+        }
177
+      }
178
+
179
+      return false;
180
+    };
181
+  }
182
+
183
+  if (!self.settings.firstUrl) {
184
+    throw 'virtual_scroll plugin requires a firstUrl() method';
185
+  } // in order for virtual scrolling to work,
186
+  // options need to be ordered the same way they're returned from the remote data source
187
+
188
+
189
+  self.settings.sortField = [{
190
+    field: '$order'
191
+  }, {
192
+    field: '$score'
193
+  }]; // can we load more results for given query?
194
+
195
+  const canLoadMore = query => {
196
+    if (typeof self.settings.maxOptions === 'number' && dropdown_content.children.length >= self.settings.maxOptions) {
197
+      return false;
198
+    }
199
+
200
+    if (query in pagination && pagination[query]) {
201
+      return true;
202
+    }
203
+
204
+    return false;
205
+  };
206
+
207
+  const clearFilter = (option, value) => {
208
+    if (self.items.indexOf(value) >= 0 || default_values.indexOf(value) >= 0) {
209
+      return true;
210
+    }
211
+
212
+    return false;
213
+  }; // set the next url that will be
214
+
215
+
216
+  self.setNextUrl = (value, next_url) => {
217
+    pagination[value] = next_url;
218
+  }; // getUrl() to be used in settings.load()
219
+
220
+
221
+  self.getUrl = query => {
222
+    if (query in pagination) {
223
+      const next_url = pagination[query];
224
+      pagination[query] = false;
225
+      return next_url;
226
+    } // if the user goes back to a previous query
227
+    // we need to load the first page again
228
+
229
+
230
+    pagination = {};
231
+    return self.settings.firstUrl.call(self, query);
232
+  }; // don't clear the active option (and cause unwanted dropdown scroll)
233
+  // while loading more results
234
+
235
+
236
+  self.hook('instead', 'clearActiveOption', () => {
237
+    if (loading_more) {
238
+      return;
239
+    }
240
+
241
+    return orig_clearActiveOption.call(self);
242
+  }); // override the canLoad method
243
+
244
+  self.hook('instead', 'canLoad', query => {
245
+    // first time the query has been seen
246
+    if (!(query in pagination)) {
247
+      return orig_canLoad.call(self, query);
248
+    }
249
+
250
+    return canLoadMore(query);
251
+  }); // wrap the load
252
+
253
+  self.hook('instead', 'loadCallback', (options, optgroups) => {
254
+    if (!loading_more) {
255
+      self.clearOptions(clearFilter);
256
+    } else if (load_more_opt) {
257
+      const first_option = options[0];
258
+
259
+      if (first_option !== undefined) {
260
+        load_more_opt.dataset.value = first_option[self.settings.valueField];
261
+      }
262
+    }
263
+
264
+    orig_loadCallback.call(self, options, optgroups);
265
+    loading_more = false;
266
+  }); // add templates to dropdown
267
+  //	loading_more if we have another url in the queue
268
+  //	no_more_results if we don't have another url in the queue
269
+
270
+  self.hook('after', 'refreshOptions', () => {
271
+    const query = self.lastValue;
272
+    var option;
273
+
274
+    if (canLoadMore(query)) {
275
+      option = self.render('loading_more', {
276
+        query: query
277
+      });
278
+
279
+      if (option) {
280
+        option.setAttribute('data-selectable', ''); // so that navigating dropdown with [down] keypresses can navigate to this node
281
+
282
+        load_more_opt = option;
283
+      }
284
+    } else if (query in pagination && !dropdown_content.querySelector('.no-results')) {
285
+      option = self.render('no_more_results', {
286
+        query: query
287
+      });
288
+    }
289
+
290
+    if (option) {
291
+      addClasses(option, self.settings.optionClass);
292
+      dropdown_content.append(option);
293
+    }
294
+  }); // add scroll listener and default templates
295
+
296
+  self.on('initialize', () => {
297
+    default_values = Object.keys(self.options);
298
+    dropdown_content = self.dropdown_content; // default templates
299
+
300
+    self.settings.render = Object.assign({}, {
301
+      loading_more: () => {
302
+        return `<div class="loading-more-results">Loading more results ... </div>`;
303
+      },
304
+      no_more_results: () => {
305
+        return `<div class="no-more-results">No more results</div>`;
306
+      }
307
+    }, self.settings.render); // watch dropdown content scroll position
308
+
309
+    dropdown_content.addEventListener('scroll', () => {
310
+      if (!self.settings.shouldLoadMore.call(self)) {
311
+        return;
312
+      } // !important: this will get checked again in load() but we still need to check here otherwise loading_more will be set to true
313
+
314
+
315
+      if (!canLoadMore(self.lastValue)) {
316
+        return;
317
+      } // don't call load() too much
318
+
319
+
320
+      if (loading_more) return;
321
+      loading_more = true;
322
+      self.load.call(self, self.lastValue);
323
+    });
324
+  });
325
+}
326
+
327
+export { plugin as default };
328
+//# sourceMappingURL=plugin.js.map