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