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