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,285 @@
1
+/**
2
+* Tom Select v2.2.2
3
+* Licensed under the Apache License, Version 2.0 (the "License");
4
+*/
5
+
6
+const KEY_ESC = 27;
7
+const KEY_TAB = 9;
8
+typeof navigator === 'undefined' ? false : /Mac/.test(navigator.userAgent);
9
+ // ctrl key or apple key for ma
10
+
11
+/*! @orchidjs/unicode-variants | https://github.com/orchidjs/unicode-variants | Apache License (v2) */
12
+const accent_pat = '[\u0300-\u036F\u{b7}\u{2be}\u{2bc}]';
13
+/** @type {TUnicodeMap} */
14
+
15
+const latin_convert = {};
16
+/** @type {TUnicodeMap} */
17
+
18
+const latin_condensed = {
19
+  '/': '⁄∕',
20
+  '0': '߀',
21
+  "a": "ⱥɐɑ",
22
+  "aa": "ꜳ",
23
+  "ae": "æǽǣ",
24
+  "ao": "ꜵ",
25
+  "au": "ꜷ",
26
+  "av": "ꜹꜻ",
27
+  "ay": "ꜽ",
28
+  "b": "ƀɓƃ",
29
+  "c": "ꜿƈȼↄ",
30
+  "d": "đɗɖᴅƌꮷԁɦ",
31
+  "e": "ɛǝᴇɇ",
32
+  "f": "ꝼƒ",
33
+  "g": "ǥɠꞡᵹꝿɢ",
34
+  "h": "ħⱨⱶɥ",
35
+  "i": "ɨı",
36
+  "j": "ɉȷ",
37
+  "k": "ƙⱪꝁꝃꝅꞣ",
38
+  "l": "łƚɫⱡꝉꝇꞁɭ",
39
+  "m": "ɱɯϻ",
40
+  "n": "ꞥƞɲꞑᴎлԉ",
41
+  "o": "øǿɔɵꝋꝍᴑ",
42
+  "oe": "œ",
43
+  "oi": "ƣ",
44
+  "oo": "ꝏ",
45
+  "ou": "ȣ",
46
+  "p": "ƥᵽꝑꝓꝕρ",
47
+  "q": "ꝗꝙɋ",
48
+  "r": "ɍɽꝛꞧꞃ",
49
+  "s": "ßȿꞩꞅʂ",
50
+  "t": "ŧƭʈⱦꞇ",
51
+  "th": "þ",
52
+  "tz": "ꜩ",
53
+  "u": "ʉ",
54
+  "v": "ʋꝟʌ",
55
+  "vy": "ꝡ",
56
+  "w": "ⱳ",
57
+  "y": "ƴɏỿ",
58
+  "z": "ƶȥɀⱬꝣ",
59
+  "hv": "ƕ"
60
+};
61
+
62
+for (let latin in latin_condensed) {
63
+  let unicode = latin_condensed[latin] || '';
64
+
65
+  for (let i = 0; i < unicode.length; i++) {
66
+    let char = unicode.substring(i, i + 1);
67
+    latin_convert[char] = latin;
68
+  }
69
+}
70
+
71
+new RegExp(Object.keys(latin_convert).join('|') + '|' + accent_pat, 'gu');
72
+
73
+/**
74
+ * Iterates over arrays and hashes.
75
+ *
76
+ * ```
77
+ * iterate(this.items, function(item, id) {
78
+ *    // invoked for each item
79
+ * });
80
+ * ```
81
+ *
82
+ */
83
+
84
+const iterate = (object, callback) => {
85
+  if (Array.isArray(object)) {
86
+    object.forEach(callback);
87
+  } else {
88
+    for (var key in object) {
89
+      if (object.hasOwnProperty(key)) {
90
+        callback(object[key], key);
91
+      }
92
+    }
93
+  }
94
+};
95
+
96
+/**
97
+ * Return a dom element from either a dom query string, jQuery object, a dom element or html string
98
+ * https://stackoverflow.com/questions/494143/creating-a-new-dom-element-from-an-html-string-using-built-in-dom-methods-or-pro/35385518#35385518
99
+ *
100
+ * param query should be {}
101
+ */
102
+
103
+const getDom = query => {
104
+  if (query.jquery) {
105
+    return query[0];
106
+  }
107
+
108
+  if (query instanceof HTMLElement) {
109
+    return query;
110
+  }
111
+
112
+  if (isHtmlString(query)) {
113
+    var tpl = document.createElement('template');
114
+    tpl.innerHTML = query.trim(); // Never return a text node of whitespace as the result
115
+
116
+    return tpl.content.firstChild;
117
+  }
118
+
119
+  return document.querySelector(query);
120
+};
121
+const isHtmlString = arg => {
122
+  if (typeof arg === 'string' && arg.indexOf('<') > -1) {
123
+    return true;
124
+  }
125
+
126
+  return false;
127
+};
128
+/**
129
+ * Add css classes
130
+ *
131
+ */
132
+
133
+const addClasses = (elmts, ...classes) => {
134
+  var norm_classes = classesArray(classes);
135
+  elmts = castAsArray(elmts);
136
+  elmts.map(el => {
137
+    norm_classes.map(cls => {
138
+      el.classList.add(cls);
139
+    });
140
+  });
141
+};
142
+/**
143
+ * Return arguments
144
+ *
145
+ */
146
+
147
+const classesArray = args => {
148
+  var classes = [];
149
+  iterate(args, _classes => {
150
+    if (typeof _classes === 'string') {
151
+      _classes = _classes.trim().split(/[\11\12\14\15\40]/);
152
+    }
153
+
154
+    if (Array.isArray(_classes)) {
155
+      classes = classes.concat(_classes);
156
+    }
157
+  });
158
+  return classes.filter(Boolean);
159
+};
160
+/**
161
+ * Create an array from arg if it's not already an array
162
+ *
163
+ */
164
+
165
+const castAsArray = arg => {
166
+  if (!Array.isArray(arg)) {
167
+    arg = [arg];
168
+  }
169
+
170
+  return arg;
171
+};
172
+
173
+/**
174
+ * Converts a scalar to its best string representation
175
+ * for hash keys and HTML attribute values.
176
+ *
177
+ * Transformations:
178
+ *   'str'     -> 'str'
179
+ *   null      -> ''
180
+ *   undefined -> ''
181
+ *   true      -> '1'
182
+ *   false     -> '0'
183
+ *   0         -> '0'
184
+ *   1         -> '1'
185
+ *
186
+ */
187
+/**
188
+ * Prevent default
189
+ *
190
+ */
191
+
192
+const preventDefault = (evt, stop = false) => {
193
+  if (evt) {
194
+    evt.preventDefault();
195
+
196
+    if (stop) {
197
+      evt.stopPropagation();
198
+    }
199
+  }
200
+};
201
+/**
202
+ * Add event helper
203
+ *
204
+ */
205
+
206
+const addEvent = (target, type, callback, options) => {
207
+  target.addEventListener(type, callback, options);
208
+};
209
+
210
+/**
211
+ * Plugin: "dropdown_input" (Tom Select)
212
+ * Copyright (c) contributors
213
+ *
214
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
215
+ * file except in compliance with the License. You may obtain a copy of the License at:
216
+ * http://www.apache.org/licenses/LICENSE-2.0
217
+ *
218
+ * Unless required by applicable law or agreed to in writing, software distributed under
219
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
220
+ * ANY KIND, either express or implied. See the License for the specific language
221
+ * governing permissions and limitations under the License.
222
+ *
223
+ */
224
+function plugin () {
225
+  const self = this;
226
+  self.settings.shouldOpen = true; // make sure the input is shown even if there are no options to display in the dropdown
227
+
228
+  self.hook('before', 'setup', () => {
229
+    self.focus_node = self.control;
230
+    addClasses(self.control_input, 'dropdown-input');
231
+    const div = getDom('<div class="dropdown-input-wrap">');
232
+    div.append(self.control_input);
233
+    self.dropdown.insertBefore(div, self.dropdown.firstChild); // set a placeholder in the select control
234
+
235
+    const placeholder = getDom('<input class="items-placeholder" tabindex="-1" />');
236
+    placeholder.placeholder = self.settings.placeholder || '';
237
+    self.control.append(placeholder);
238
+  });
239
+  self.on('initialize', () => {
240
+    // set tabIndex on control to -1, otherwise [shift+tab] will put focus right back on control_input
241
+    self.control_input.addEventListener('keydown', evt => {
242
+      //addEvent(self.control_input,'keydown' as const,(evt:KeyboardEvent) =>{
243
+      switch (evt.keyCode) {
244
+        case KEY_ESC:
245
+          if (self.isOpen) {
246
+            preventDefault(evt, true);
247
+            self.close();
248
+          }
249
+
250
+          self.clearActiveItems();
251
+          return;
252
+
253
+        case KEY_TAB:
254
+          self.focus_node.tabIndex = -1;
255
+          break;
256
+      }
257
+
258
+      return self.onKeyDown.call(self, evt);
259
+    });
260
+    self.on('blur', () => {
261
+      self.focus_node.tabIndex = self.isDisabled ? -1 : self.tabIndex;
262
+    }); // give the control_input focus when the dropdown is open
263
+
264
+    self.on('dropdown_open', () => {
265
+      self.control_input.focus();
266
+    }); // prevent onBlur from closing when focus is on the control_input
267
+
268
+    const orig_onBlur = self.onBlur;
269
+    self.hook('instead', 'onBlur', evt => {
270
+      if (evt && evt.relatedTarget == self.control_input) return;
271
+      return orig_onBlur.call(self);
272
+    });
273
+    addEvent(self.control_input, 'blur', () => self.onBlur()); // return focus to control to allow further keyboard input
274
+
275
+    self.hook('before', 'close', () => {
276
+      if (!self.isOpen) return;
277
+      self.focus_node.focus({
278
+        preventScroll: true
279
+      });
280
+    });
281
+  });
282
+}
283
+
284
+export { plugin as default };
285
+//# sourceMappingURL=plugin.js.map