trln-chosen-rails 1.30.0.pre.beta → 1.30.0.pre.beta3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,467 @@
1
+ /*
2
+ * decaffeinate suggestions:
3
+ * DS101: Remove unnecessary use of Array.from
4
+ * DS102: Remove unnecessary code created because of implicit returns
5
+ * DS205: Consider reworking code to avoid use of IIFEs
6
+ * DS206: Consider reworking classes to avoid initClass
7
+ * DS207: Consider shorter variations of null checks
8
+ * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
9
+ */
10
+ class AbstractChosen {
11
+ static initClass() {
12
+
13
+ this.default_multiple_text = "Select Some Options";
14
+ this.default_single_text = "Select an Option";
15
+ this.default_no_result_text = "No results match";
16
+ }
17
+
18
+ constructor(form_field, options) {
19
+ this.label_click_handler = this.label_click_handler.bind(this);
20
+ this.form_field = form_field;
21
+ if (options == null) { options = {}; }
22
+ this.options = options;
23
+ if (!AbstractChosen.browser_is_supported()) { return; }
24
+ this.is_multiple = this.form_field.multiple;
25
+ this.set_default_text();
26
+ this.set_default_values();
27
+
28
+ this.setup();
29
+
30
+ this.set_up_html();
31
+ this.register_observers();
32
+ // instantiation done, fire ready
33
+ this.on_ready();
34
+ }
35
+
36
+ set_default_values() {
37
+ this.click_test_action = evt => this.test_active_click(evt);
38
+ this.activate_action = evt => this.activate_field(evt);
39
+ this.active_field = false;
40
+ this.mouse_on_container = false;
41
+ this.results_showing = false;
42
+ this.result_highlighted = null;
43
+ this.is_rtl = this.options.rtl || /\bchosen-rtl\b/.test(this.form_field.className);
44
+ this.allow_single_deselect = (this.options.allow_single_deselect != null) && (this.form_field.options[0] != null) && (this.form_field.options[0].text === "") ? this.options.allow_single_deselect : false;
45
+ this.disable_search_threshold = this.options.disable_search_threshold || 0;
46
+ this.disable_search = this.options.disable_search || false;
47
+ this.enable_split_word_search = (this.options.enable_split_word_search != null) ? this.options.enable_split_word_search : true;
48
+ this.group_search = (this.options.group_search != null) ? this.options.group_search : true;
49
+ this.search_contains = this.options.search_contains || false;
50
+ this.single_backstroke_delete = (this.options.single_backstroke_delete != null) ? this.options.single_backstroke_delete : true;
51
+ this.max_selected_options = this.options.max_selected_options || Infinity;
52
+ this.inherit_select_classes = this.options.inherit_select_classes || false;
53
+ this.display_selected_options = (this.options.display_selected_options != null) ? this.options.display_selected_options : true;
54
+ this.display_disabled_options = (this.options.display_disabled_options != null) ? this.options.display_disabled_options : true;
55
+ this.include_group_label_in_selected = this.options.include_group_label_in_selected || false;
56
+ this.max_shown_results = this.options.max_shown_results || Number.POSITIVE_INFINITY;
57
+ this.case_sensitive_search = this.options.case_sensitive_search || false;
58
+ return this.hide_results_on_select = (this.options.hide_results_on_select != null) ? this.options.hide_results_on_select : true;
59
+ }
60
+
61
+ set_default_text() {
62
+ if (this.form_field.getAttribute("data-placeholder")) {
63
+ this.default_text = this.form_field.getAttribute("data-placeholder");
64
+ } else if (this.is_multiple) {
65
+ this.default_text = this.options.placeholder_text_multiple || this.options.placeholder_text || AbstractChosen.default_multiple_text;
66
+ } else {
67
+ this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || AbstractChosen.default_single_text;
68
+ }
69
+
70
+ this.default_text = this.escape_html(this.default_text);
71
+
72
+ return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || AbstractChosen.default_no_result_text;
73
+ }
74
+
75
+ choice_label(item) {
76
+ if (this.include_group_label_in_selected && (item.group_label != null)) {
77
+ return `<b class='group-name'>${this.escape_html(item.group_label)}</b>${item.html}`;
78
+ } else {
79
+ return item.html;
80
+ }
81
+ }
82
+
83
+ mouse_enter() { return this.mouse_on_container = true; }
84
+ mouse_leave() { return this.mouse_on_container = false; }
85
+
86
+ input_focus(evt) {
87
+ if (this.is_multiple) {
88
+ if (!this.active_field) { return setTimeout((() => this.container_mousedown()), 50); }
89
+ } else {
90
+ if (!this.active_field) { return this.activate_field(); }
91
+ }
92
+ }
93
+
94
+ input_blur(evt) {
95
+ if (!this.mouse_on_container) {
96
+ this.active_field = false;
97
+ return setTimeout((() => this.blur_test()), 100);
98
+ }
99
+ }
100
+
101
+ label_click_handler(evt) {
102
+ if (this.is_multiple) {
103
+ return this.container_mousedown(evt);
104
+ } else {
105
+ return this.activate_field();
106
+ }
107
+ }
108
+
109
+ results_option_build(options) {
110
+ let content = '';
111
+ let shown_results = 0;
112
+ for (var data of Array.from(this.results_data)) {
113
+ var data_content = '';
114
+ if (data.group) {
115
+ data_content = this.result_add_group(data);
116
+ } else {
117
+ data_content = this.result_add_option(data);
118
+ }
119
+ if (data_content !== '') {
120
+ shown_results++;
121
+ content += data_content;
122
+ }
123
+
124
+ // this select logic pins on an awkward flag
125
+ // we can make it better
126
+ if (options != null ? options.first : undefined) {
127
+ if (data.selected && this.is_multiple) {
128
+ this.choice_build(data);
129
+ } else if (data.selected && !this.is_multiple) {
130
+ this.single_set_selected_text(this.choice_label(data));
131
+ }
132
+ }
133
+
134
+ if (shown_results >= this.max_shown_results) {
135
+ break;
136
+ }
137
+ }
138
+
139
+ return content;
140
+ }
141
+
142
+ result_add_option(option) {
143
+ if (!option.search_match) { return ''; }
144
+ if (!this.include_option_in_results(option)) { return ''; }
145
+
146
+ const classes = [];
147
+ if (!option.disabled && !(option.selected && this.is_multiple)) { classes.push("active-result"); }
148
+ if (option.disabled && !(option.selected && this.is_multiple)) { classes.push("disabled-result"); }
149
+ if (option.selected) { classes.push("result-selected"); }
150
+ if (option.group_array_index != null) { classes.push("group-option"); }
151
+ if (option.classes !== "") { classes.push(option.classes); }
152
+
153
+ const option_el = document.createElement("li");
154
+ option_el.className = classes.join(" ");
155
+ if (option.style) { option_el.style.cssText = option.style; }
156
+ option_el.setAttribute("data-option-array-index", option.array_index);
157
+ option_el.innerHTML = option.highlighted_html || option.html;
158
+ if (option.title) { option_el.title = option.title; }
159
+
160
+ return this.outerHTML(option_el);
161
+ }
162
+
163
+ result_add_group(group) {
164
+ if (!group.search_match && !group.group_match) { return ''; }
165
+ if (!(group.active_options > 0)) { return ''; }
166
+
167
+ const classes = [];
168
+ classes.push("group-result");
169
+ if (group.classes) { classes.push(group.classes); }
170
+
171
+ const group_el = document.createElement("li");
172
+ group_el.className = classes.join(" ");
173
+ group_el.innerHTML = group.highlighted_html || this.escape_html(group.label);
174
+ if (group.title) { group_el.title = group.title; }
175
+
176
+ return this.outerHTML(group_el);
177
+ }
178
+
179
+ results_update_field() {
180
+ this.set_default_text();
181
+ if (!this.is_multiple) { this.results_reset_cleanup(); }
182
+ this.result_clear_highlight();
183
+ this.results_build();
184
+ if (this.results_showing) { return this.winnow_results(); }
185
+ }
186
+
187
+ reset_single_select_options() {
188
+ return (() => {
189
+ const result1 = [];
190
+ for (var result of Array.from(this.results_data)) {
191
+ if (result.selected) { result1.push(result.selected = false); } else {
192
+ result1.push(undefined);
193
+ }
194
+ }
195
+ return result1;
196
+ })();
197
+ }
198
+
199
+ results_toggle() {
200
+ if (this.results_showing) {
201
+ return this.results_hide();
202
+ } else {
203
+ return this.results_show();
204
+ }
205
+ }
206
+
207
+ results_search(evt) {
208
+ if (this.results_showing) {
209
+ return this.winnow_results();
210
+ } else {
211
+ return this.results_show();
212
+ }
213
+ }
214
+
215
+ winnow_results(options) {
216
+ this.no_results_clear();
217
+
218
+ let results = 0;
219
+
220
+ const query = this.get_search_text();
221
+ const escapedQuery = query.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
222
+ const regex = this.get_search_regex(escapedQuery);
223
+
224
+ for (var option of Array.from(this.results_data)) {
225
+
226
+ option.search_match = false;
227
+ var results_group = null;
228
+ var search_match = null;
229
+ option.highlighted_html = '';
230
+
231
+ if (this.include_option_in_results(option)) {
232
+
233
+ if (option.group) {
234
+ option.group_match = false;
235
+ option.active_options = 0;
236
+ }
237
+
238
+ if ((option.group_array_index != null) && this.results_data[option.group_array_index]) {
239
+ results_group = this.results_data[option.group_array_index];
240
+ if ((results_group.active_options === 0) && results_group.search_match) { results += 1; }
241
+ results_group.active_options += 1;
242
+ }
243
+
244
+ var text = option.group ? option.label : option.text;
245
+
246
+ if (!option.group || !!this.group_search) {
247
+ search_match = this.search_string_match(text, regex);
248
+ option.search_match = (search_match != null);
249
+
250
+ if (option.search_match && !option.group) { results += 1; }
251
+
252
+ if (option.search_match) {
253
+ if (query.length) {
254
+ var startpos = search_match.index;
255
+ var prefix = text.slice(0, startpos);
256
+ var fix = text.slice(startpos, startpos + query.length);
257
+ var suffix = text.slice(startpos + query.length);
258
+ option.highlighted_html = `${this.escape_html(prefix)}<em>${this.escape_html(fix)}</em>${this.escape_html(suffix)}`;
259
+ }
260
+
261
+ if (results_group != null) { results_group.group_match = true; }
262
+
263
+ } else if ((option.group_array_index != null) && this.results_data[option.group_array_index].search_match) {
264
+ option.search_match = true;
265
+ }
266
+ }
267
+ }
268
+ }
269
+
270
+ this.result_clear_highlight();
271
+
272
+ if ((results < 1) && query.length) {
273
+ this.update_results_content("");
274
+ return this.no_results(query);
275
+ } else {
276
+ this.update_results_content(this.results_option_build());
277
+ if (!(options != null ? options.skip_highlight : undefined)) { return this.winnow_results_set_highlight(); }
278
+ }
279
+ }
280
+
281
+ get_search_regex(escaped_search_string) {
282
+ let regex_string = this.search_contains ? escaped_search_string : `(^|\\s|\\b)${escaped_search_string}[^\\s]*`;
283
+ if (!this.enable_split_word_search && !this.search_contains) { regex_string = `^${regex_string}`; }
284
+ const regex_flag = this.case_sensitive_search ? "" : "i";
285
+ return new RegExp(regex_string, regex_flag);
286
+ }
287
+
288
+ search_string_match(search_string, regex) {
289
+ const match = regex.exec(search_string);
290
+ if (!this.search_contains && (match != null ? match[1] : undefined)) { match.index += 1; } // make up for lack of lookbehind operator in regex
291
+ return match;
292
+ }
293
+
294
+ choices_count() {
295
+ if (this.selected_option_count != null) { return this.selected_option_count; }
296
+
297
+ this.selected_option_count = 0;
298
+ for (var option of Array.from(this.form_field.options)) {
299
+ if (option.selected) { this.selected_option_count += 1; }
300
+ }
301
+
302
+ return this.selected_option_count;
303
+ }
304
+
305
+ choices_click(evt) {
306
+ evt.preventDefault();
307
+ this.activate_field();
308
+ if (!this.results_showing && !this.is_disabled) { return this.results_show(); }
309
+ }
310
+
311
+ keydown_checker(evt) {
312
+ const stroke = evt.which != null ? evt.which : evt.keyCode;
313
+ this.search_field_scale();
314
+
315
+ if ((stroke !== 8) && this.pending_backstroke) { this.clear_backstroke(); }
316
+
317
+ switch (stroke) {
318
+ case 8: // backspace
319
+ this.backstroke_length = this.get_search_field_value().length;
320
+ break;
321
+ case 9: // tab
322
+ if (this.results_showing && !this.is_multiple) { this.result_select(evt); }
323
+ this.mouse_on_container = false;
324
+ break;
325
+ case 13: // enter
326
+ if (this.results_showing) { evt.preventDefault(); }
327
+ break;
328
+ case 27: // escape
329
+ if (this.results_showing) { evt.preventDefault(); }
330
+ break;
331
+ case 32: // space
332
+ if (this.disable_search) { evt.preventDefault(); }
333
+ break;
334
+ case 38: // up arrow
335
+ evt.preventDefault();
336
+ this.keyup_arrow();
337
+ break;
338
+ case 40: // down arrow
339
+ evt.preventDefault();
340
+ this.keydown_arrow();
341
+ break;
342
+ }
343
+ }
344
+
345
+ keyup_checker(evt) {
346
+ const stroke = evt.which != null ? evt.which : evt.keyCode;
347
+ this.search_field_scale();
348
+
349
+ switch (stroke) {
350
+ case 8: // backspace
351
+ if (this.is_multiple && (this.backstroke_length < 1) && (this.choices_count() > 0)) {
352
+ this.keydown_backstroke();
353
+ } else if (!this.pending_backstroke) {
354
+ this.result_clear_highlight();
355
+ this.results_search();
356
+ }
357
+ break;
358
+ case 13: // enter
359
+ evt.preventDefault();
360
+ if (this.results_showing) { this.result_select(evt); }
361
+ break;
362
+ case 27: // escape
363
+ if (this.results_showing) { this.results_hide(); }
364
+ break;
365
+ case 9: case 16: case 17: case 18: case 38: case 40: case 91:
366
+ // don't do anything on these keys
367
+ default:
368
+ this.results_search();
369
+ break;
370
+ }
371
+ }
372
+
373
+ clipboard_event_checker(evt) {
374
+ if (this.is_disabled) { return; }
375
+ return setTimeout((() => this.results_search()), 50);
376
+ }
377
+
378
+ container_width() {
379
+ if (this.options.width != null) { return this.options.width; } else { return `${this.form_field.offsetWidth}px`; }
380
+ }
381
+
382
+ include_option_in_results(option) {
383
+ if (this.is_multiple && (!this.display_selected_options && option.selected)) { return false; }
384
+ if (!this.display_disabled_options && option.disabled) { return false; }
385
+ if (option.empty) { return false; }
386
+
387
+ return true;
388
+ }
389
+
390
+ search_results_touchstart(evt) {
391
+ this.touch_started = true;
392
+ return this.search_results_mouseover(evt);
393
+ }
394
+
395
+ search_results_touchmove(evt) {
396
+ this.touch_started = false;
397
+ return this.search_results_mouseout(evt);
398
+ }
399
+
400
+ search_results_touchend(evt) {
401
+ if (this.touch_started) { return this.search_results_mouseup(evt); }
402
+ }
403
+
404
+ outerHTML(element) {
405
+ if (element.outerHTML) { return element.outerHTML; }
406
+ const tmp = document.createElement("div");
407
+ tmp.appendChild(element);
408
+ return tmp.innerHTML;
409
+ }
410
+
411
+ get_single_html() {
412
+ return `\
413
+ <a class="chosen-single chosen-default">
414
+ <span>${this.default_text}</span>
415
+ <div><b></b></div>
416
+ </a>
417
+ <div class="chosen-drop">
418
+ <div class="chosen-search">
419
+ <input class="chosen-search-input" type="text" autocomplete="off" />
420
+ </div>
421
+ <ul class="chosen-results"></ul>
422
+ </div>\
423
+ `;
424
+ }
425
+
426
+ get_multi_html() {
427
+ return `\
428
+ <ul class="chosen-choices">
429
+ <li class="search-field">
430
+ <input class="chosen-search-input" type="text" autocomplete="off" value="${this.default_text}" />
431
+ </li>
432
+ </ul>
433
+ <div class="chosen-drop">
434
+ <ul class="chosen-results"></ul>
435
+ </div>\
436
+ `;
437
+ }
438
+
439
+ get_no_results_html(terms) {
440
+ return `\
441
+ <li class="no-results">
442
+ ${this.results_none_found} <span>${this.escape_html(terms)}</span>
443
+ </li>\
444
+ `;
445
+ }
446
+
447
+ // class methods and variables ============================================================
448
+
449
+ static browser_is_supported() {
450
+ if ("Microsoft Internet Explorer" === window.navigator.appName) {
451
+ return document.documentMode >= 8;
452
+ }
453
+ if (/iP(od|hone)/i.test(window.navigator.userAgent) ||
454
+ /IEMobile/i.test(window.navigator.userAgent) ||
455
+ /Windows Phone/i.test(window.navigator.userAgent) ||
456
+ /BlackBerry/i.test(window.navigator.userAgent) ||
457
+ /BB10/i.test(window.navigator.userAgent) ||
458
+ /Android.*Mobile/i.test(window.navigator.userAgent)) {
459
+ return false;
460
+ }
461
+ return true;
462
+ }
463
+ }
464
+ AbstractChosen.initClass();
465
+
466
+
467
+ window.AbstractChosen = AbstractChosen;
@@ -0,0 +1,76 @@
1
+ /*
2
+ * decaffeinate suggestions:
3
+ * DS101: Remove unnecessary use of Array.from
4
+ * DS102: Remove unnecessary code created because of implicit returns
5
+ * DS207: Consider shorter variations of null checks
6
+ * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
7
+ */
8
+ class SelectParser {
9
+
10
+ constructor() {
11
+ this.options_index = 0;
12
+ this.parsed = [];
13
+ }
14
+
15
+ add_node(child) {
16
+ if (child.nodeName.toUpperCase() === "OPTGROUP") {
17
+ return this.add_group(child);
18
+ } else {
19
+ return this.add_option(child);
20
+ }
21
+ }
22
+
23
+ add_group(group) {
24
+ const group_position = this.parsed.length;
25
+ this.parsed.push({
26
+ array_index: group_position,
27
+ group: true,
28
+ label: group.label,
29
+ title: (group.title ? group.title : undefined),
30
+ children: 0,
31
+ disabled: group.disabled,
32
+ classes: group.className
33
+ });
34
+ return Array.from(group.childNodes).map((option) => this.add_option( option, group_position, group.disabled ));
35
+ }
36
+
37
+ add_option(option, group_position, group_disabled) {
38
+ if (option.nodeName.toUpperCase() === "OPTION") {
39
+ if (option.text !== "") {
40
+ if (group_position != null) {
41
+ this.parsed[group_position].children += 1;
42
+ }
43
+ this.parsed.push({
44
+ array_index: this.parsed.length,
45
+ options_index: this.options_index,
46
+ value: option.value,
47
+ text: option.text,
48
+ html: option.innerHTML,
49
+ title: (option.title ? option.title : undefined),
50
+ selected: option.selected,
51
+ disabled: group_disabled === true ? group_disabled : option.disabled,
52
+ group_array_index: group_position,
53
+ group_label: (group_position != null) ? this.parsed[group_position].label : null,
54
+ classes: option.className,
55
+ style: option.style.cssText
56
+ });
57
+ } else {
58
+ this.parsed.push({
59
+ array_index: this.parsed.length,
60
+ options_index: this.options_index,
61
+ empty: true
62
+ });
63
+ }
64
+ return this.options_index += 1;
65
+ }
66
+ }
67
+ }
68
+
69
+ SelectParser.select_to_array = function(select) {
70
+ const parser = new SelectParser();
71
+ for (var child of Array.from(select.childNodes)) { parser.add_node( child ); }
72
+ return parser.parsed;
73
+ };
74
+
75
+
76
+ window.SelectParser = SelectParser;