1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,196 @@ |
1 |
+/** |
|
2 |
+* Tom Select v2.2.2 |
|
3 |
+* Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 |
+*/ |
|
5 |
+ |
|
6 |
+'use strict'; |
|
7 |
+ |
|
8 |
+Object.defineProperty(exports, '__esModule', { value: true }); |
|
9 |
+ |
|
10 |
+/** |
|
11 |
+ * Converts a scalar to its best string representation |
|
12 |
+ * for hash keys and HTML attribute values. |
|
13 |
+ * |
|
14 |
+ * Transformations: |
|
15 |
+ * 'str' -> 'str' |
|
16 |
+ * null -> '' |
|
17 |
+ * undefined -> '' |
|
18 |
+ * true -> '1' |
|
19 |
+ * false -> '0' |
|
20 |
+ * 0 -> '0' |
|
21 |
+ * 1 -> '1' |
|
22 |
+ * |
|
23 |
+ */ |
|
24 |
+const hash_key = value => { |
|
25 |
+ if (typeof value === 'undefined' || value === null) return null; |
|
26 |
+ return get_hash(value); |
|
27 |
+}; |
|
28 |
+const get_hash = value => { |
|
29 |
+ if (typeof value === 'boolean') return value ? '1' : '0'; |
|
30 |
+ return value + ''; |
|
31 |
+}; |
|
32 |
+/** |
|
33 |
+ * Escapes a string for use within HTML. |
|
34 |
+ * |
|
35 |
+ */ |
|
36 |
+ |
|
37 |
+const escape_html = str => { |
|
38 |
+ return (str + '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); |
|
39 |
+}; |
|
40 |
+/** |
|
41 |
+ * Debounce the user provided load function |
|
42 |
+ * |
|
43 |
+ */ |
|
44 |
+ |
|
45 |
+const loadDebounce = (fn, delay) => { |
|
46 |
+ var timeout; |
|
47 |
+ return function (value, callback) { |
|
48 |
+ var self = this; |
|
49 |
+ |
|
50 |
+ if (timeout) { |
|
51 |
+ self.loading = Math.max(self.loading - 1, 0); |
|
52 |
+ clearTimeout(timeout); |
|
53 |
+ } |
|
54 |
+ |
|
55 |
+ timeout = setTimeout(function () { |
|
56 |
+ timeout = null; |
|
57 |
+ self.loadedSearches[value] = true; |
|
58 |
+ fn.call(self, value, callback); |
|
59 |
+ }, delay); |
|
60 |
+ }; |
|
61 |
+}; |
|
62 |
+/** |
|
63 |
+ * Debounce all fired events types listed in `types` |
|
64 |
+ * while executing the provided `fn`. |
|
65 |
+ * |
|
66 |
+ */ |
|
67 |
+ |
|
68 |
+const debounce_events = (self, types, fn) => { |
|
69 |
+ var type; |
|
70 |
+ var trigger = self.trigger; |
|
71 |
+ var event_args = {}; // override trigger method |
|
72 |
+ |
|
73 |
+ self.trigger = function () { |
|
74 |
+ var type = arguments[0]; |
|
75 |
+ |
|
76 |
+ if (types.indexOf(type) !== -1) { |
|
77 |
+ event_args[type] = arguments; |
|
78 |
+ } else { |
|
79 |
+ return trigger.apply(self, arguments); |
|
80 |
+ } |
|
81 |
+ }; // invoke provided function |
|
82 |
+ |
|
83 |
+ |
|
84 |
+ fn.apply(self, []); |
|
85 |
+ self.trigger = trigger; // trigger queued events |
|
86 |
+ |
|
87 |
+ for (type of types) { |
|
88 |
+ if (type in event_args) { |
|
89 |
+ trigger.apply(self, event_args[type]); |
|
90 |
+ } |
|
91 |
+ } |
|
92 |
+}; |
|
93 |
+/** |
|
94 |
+ * Determines the current selection within a text input control. |
|
95 |
+ * Returns an object containing: |
|
96 |
+ * - start |
|
97 |
+ * - length |
|
98 |
+ * |
|
99 |
+ */ |
|
100 |
+ |
|
101 |
+const getSelection = input => { |
|
102 |
+ return { |
|
103 |
+ start: input.selectionStart || 0, |
|
104 |
+ length: (input.selectionEnd || 0) - (input.selectionStart || 0) |
|
105 |
+ }; |
|
106 |
+}; |
|
107 |
+/** |
|
108 |
+ * Prevent default |
|
109 |
+ * |
|
110 |
+ */ |
|
111 |
+ |
|
112 |
+const preventDefault = (evt, stop = false) => { |
|
113 |
+ if (evt) { |
|
114 |
+ evt.preventDefault(); |
|
115 |
+ |
|
116 |
+ if (stop) { |
|
117 |
+ evt.stopPropagation(); |
|
118 |
+ } |
|
119 |
+ } |
|
120 |
+}; |
|
121 |
+/** |
|
122 |
+ * Add event helper |
|
123 |
+ * |
|
124 |
+ */ |
|
125 |
+ |
|
126 |
+const addEvent = (target, type, callback, options) => { |
|
127 |
+ target.addEventListener(type, callback, options); |
|
128 |
+}; |
|
129 |
+/** |
|
130 |
+ * Return true if the requested key is down |
|
131 |
+ * Will return false if more than one control character is pressed ( when [ctrl+shift+a] != [ctrl+a] ) |
|
132 |
+ * The current evt may not always set ( eg calling advanceSelection() ) |
|
133 |
+ * |
|
134 |
+ */ |
|
135 |
+ |
|
136 |
+const isKeyDown = (key_name, evt) => { |
|
137 |
+ if (!evt) { |
|
138 |
+ return false; |
|
139 |
+ } |
|
140 |
+ |
|
141 |
+ if (!evt[key_name]) { |
|
142 |
+ return false; |
|
143 |
+ } |
|
144 |
+ |
|
145 |
+ var count = (evt.altKey ? 1 : 0) + (evt.ctrlKey ? 1 : 0) + (evt.shiftKey ? 1 : 0) + (evt.metaKey ? 1 : 0); |
|
146 |
+ |
|
147 |
+ if (count === 1) { |
|
148 |
+ return true; |
|
149 |
+ } |
|
150 |
+ |
|
151 |
+ return false; |
|
152 |
+}; |
|
153 |
+/** |
|
154 |
+ * Get the id of an element |
|
155 |
+ * If the id attribute is not set, set the attribute with the given id |
|
156 |
+ * |
|
157 |
+ */ |
|
158 |
+ |
|
159 |
+const getId = (el, id) => { |
|
160 |
+ const existing_id = el.getAttribute('id'); |
|
161 |
+ |
|
162 |
+ if (existing_id) { |
|
163 |
+ return existing_id; |
|
164 |
+ } |
|
165 |
+ |
|
166 |
+ el.setAttribute('id', id); |
|
167 |
+ return id; |
|
168 |
+}; |
|
169 |
+/** |
|
170 |
+ * Returns a string with backslashes added before characters that need to be escaped. |
|
171 |
+ */ |
|
172 |
+ |
|
173 |
+const addSlashes = str => { |
|
174 |
+ return str.replace(/[\\"']/g, '\\$&'); |
|
175 |
+}; |
|
176 |
+/** |
|
177 |
+ * |
|
178 |
+ */ |
|
179 |
+ |
|
180 |
+const append = (parent, node) => { |
|
181 |
+ if (node) parent.append(node); |
|
182 |
+}; |
|
183 |
+ |
|
184 |
+exports.addEvent = addEvent; |
|
185 |
+exports.addSlashes = addSlashes; |
|
186 |
+exports.append = append; |
|
187 |
+exports.debounce_events = debounce_events; |
|
188 |
+exports.escape_html = escape_html; |
|
189 |
+exports.getId = getId; |
|
190 |
+exports.getSelection = getSelection; |
|
191 |
+exports.get_hash = get_hash; |
|
192 |
+exports.hash_key = hash_key; |
|
193 |
+exports.isKeyDown = isKeyDown; |
|
194 |
+exports.loadDebounce = loadDebounce; |
|
195 |
+exports.preventDefault = preventDefault; |
|
196 |
+//# sourceMappingURL=utils.js.map |