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,228 @@
1
+/**
2
+* Tom Select v2.2.2
3
+* Licensed under the Apache License, Version 2.0 (the "License");
4
+*/
5
+
6
+/**
7
+ * Converts a scalar to its best string representation
8
+ * for hash keys and HTML attribute values.
9
+ *
10
+ * Transformations:
11
+ *   'str'     -> 'str'
12
+ *   null      -> ''
13
+ *   undefined -> ''
14
+ *   true      -> '1'
15
+ *   false     -> '0'
16
+ *   0         -> '0'
17
+ *   1         -> '1'
18
+ *
19
+ */
20
+const hash_key = value => {
21
+  if (typeof value === 'undefined' || value === null) return null;
22
+  return get_hash(value);
23
+};
24
+const get_hash = value => {
25
+  if (typeof value === 'boolean') return value ? '1' : '0';
26
+  return value + '';
27
+};
28
+/**
29
+ * Prevent default
30
+ *
31
+ */
32
+
33
+const preventDefault = (evt, stop = false) => {
34
+  if (evt) {
35
+    evt.preventDefault();
36
+
37
+    if (stop) {
38
+      evt.stopPropagation();
39
+    }
40
+  }
41
+};
42
+
43
+/*! @orchidjs/unicode-variants | https://github.com/orchidjs/unicode-variants | Apache License (v2) */
44
+const accent_pat = '[\u0300-\u036F\u{b7}\u{2be}\u{2bc}]';
45
+/** @type {TUnicodeMap} */
46
+
47
+const latin_convert = {};
48
+/** @type {TUnicodeMap} */
49
+
50
+const latin_condensed = {
51
+  '/': '⁄∕',
52
+  '0': '߀',
53
+  "a": "ⱥɐɑ",
54
+  "aa": "ꜳ",
55
+  "ae": "æǽǣ",
56
+  "ao": "ꜵ",
57
+  "au": "ꜷ",
58
+  "av": "ꜹꜻ",
59
+  "ay": "ꜽ",
60
+  "b": "ƀɓƃ",
61
+  "c": "ꜿƈȼↄ",
62
+  "d": "đɗɖᴅƌꮷԁɦ",
63
+  "e": "ɛǝᴇɇ",
64
+  "f": "ꝼƒ",
65
+  "g": "ǥɠꞡᵹꝿɢ",
66
+  "h": "ħⱨⱶɥ",
67
+  "i": "ɨı",
68
+  "j": "ɉȷ",
69
+  "k": "ƙⱪꝁꝃꝅꞣ",
70
+  "l": "łƚɫⱡꝉꝇꞁɭ",
71
+  "m": "ɱɯϻ",
72
+  "n": "ꞥƞɲꞑᴎлԉ",
73
+  "o": "øǿɔɵꝋꝍᴑ",
74
+  "oe": "œ",
75
+  "oi": "ƣ",
76
+  "oo": "ꝏ",
77
+  "ou": "ȣ",
78
+  "p": "ƥᵽꝑꝓꝕρ",
79
+  "q": "ꝗꝙɋ",
80
+  "r": "ɍɽꝛꞧꞃ",
81
+  "s": "ßȿꞩꞅʂ",
82
+  "t": "ŧƭʈⱦꞇ",
83
+  "th": "þ",
84
+  "tz": "ꜩ",
85
+  "u": "ʉ",
86
+  "v": "ʋꝟʌ",
87
+  "vy": "ꝡ",
88
+  "w": "ⱳ",
89
+  "y": "ƴɏỿ",
90
+  "z": "ƶȥɀⱬꝣ",
91
+  "hv": "ƕ"
92
+};
93
+
94
+for (let latin in latin_condensed) {
95
+  let unicode = latin_condensed[latin] || '';
96
+
97
+  for (let i = 0; i < unicode.length; i++) {
98
+    let char = unicode.substring(i, i + 1);
99
+    latin_convert[char] = latin;
100
+  }
101
+}
102
+
103
+new RegExp(Object.keys(latin_convert).join('|') + '|' + accent_pat, 'gu');
104
+
105
+/**
106
+ * Return a dom element from either a dom query string, jQuery object, a dom element or html string
107
+ * https://stackoverflow.com/questions/494143/creating-a-new-dom-element-from-an-html-string-using-built-in-dom-methods-or-pro/35385518#35385518
108
+ *
109
+ * param query should be {}
110
+ */
111
+
112
+const getDom = query => {
113
+  if (query.jquery) {
114
+    return query[0];
115
+  }
116
+
117
+  if (query instanceof HTMLElement) {
118
+    return query;
119
+  }
120
+
121
+  if (isHtmlString(query)) {
122
+    var tpl = document.createElement('template');
123
+    tpl.innerHTML = query.trim(); // Never return a text node of whitespace as the result
124
+
125
+    return tpl.content.firstChild;
126
+  }
127
+
128
+  return document.querySelector(query);
129
+};
130
+const isHtmlString = arg => {
131
+  if (typeof arg === 'string' && arg.indexOf('<') > -1) {
132
+    return true;
133
+  }
134
+
135
+  return false;
136
+};
137
+
138
+/**
139
+ * Plugin: "restore_on_backspace" (Tom Select)
140
+ * Copyright (c) contributors
141
+ *
142
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
143
+ * file except in compliance with the License. You may obtain a copy of the License at:
144
+ * http://www.apache.org/licenses/LICENSE-2.0
145
+ *
146
+ * Unless required by applicable law or agreed to in writing, software distributed under
147
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
148
+ * ANY KIND, either express or implied. See the License for the specific language
149
+ * governing permissions and limitations under the License.
150
+ *
151
+ */
152
+function plugin () {
153
+  var self = this;
154
+  var orig_onOptionSelect = self.onOptionSelect;
155
+  self.settings.hideSelected = false; // update the checkbox for an option
156
+
157
+  var UpdateCheckbox = function UpdateCheckbox(option) {
158
+    setTimeout(() => {
159
+      var checkbox = option.querySelector('input');
160
+
161
+      if (checkbox instanceof HTMLInputElement) {
162
+        if (option.classList.contains('selected')) {
163
+          checkbox.checked = true;
164
+        } else {
165
+          checkbox.checked = false;
166
+        }
167
+      }
168
+    }, 1);
169
+  }; // add checkbox to option template
170
+
171
+
172
+  self.hook('after', 'setupTemplates', () => {
173
+    var orig_render_option = self.settings.render.option;
174
+
175
+    self.settings.render.option = (data, escape_html) => {
176
+      var rendered = getDom(orig_render_option.call(self, data, escape_html));
177
+      var checkbox = document.createElement('input');
178
+      checkbox.addEventListener('click', function (evt) {
179
+        preventDefault(evt);
180
+      });
181
+      checkbox.type = 'checkbox';
182
+      const hashed = hash_key(data[self.settings.valueField]);
183
+
184
+      if (hashed && self.items.indexOf(hashed) > -1) {
185
+        checkbox.checked = true;
186
+      }
187
+
188
+      rendered.prepend(checkbox);
189
+      return rendered;
190
+    };
191
+  }); // uncheck when item removed
192
+
193
+  self.on('item_remove', value => {
194
+    var option = self.getOption(value);
195
+
196
+    if (option) {
197
+      // if dropdown hasn't been opened yet, the option won't exist
198
+      option.classList.remove('selected'); // selected class won't be removed yet
199
+
200
+      UpdateCheckbox(option);
201
+    }
202
+  }); // check when item added
203
+
204
+  self.on('item_add', value => {
205
+    var option = self.getOption(value);
206
+
207
+    if (option) {
208
+      // if dropdown hasn't been opened yet, the option won't exist
209
+      UpdateCheckbox(option);
210
+    }
211
+  }); // remove items when selected option is clicked
212
+
213
+  self.hook('instead', 'onOptionSelect', (evt, option) => {
214
+    if (option.classList.contains('selected')) {
215
+      option.classList.remove('selected');
216
+      self.removeItem(option.dataset.value);
217
+      self.refreshOptions();
218
+      preventDefault(evt, true);
219
+      return;
220
+    }
221
+
222
+    orig_onOptionSelect.call(self, evt, option);
223
+    UpdateCheckbox(option);
224
+  });
225
+}
226
+
227
+export { plugin as default };
228
+//# sourceMappingURL=plugin.js.map