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