Fibinger Ádám
2020-09-10 f2e0b5357a6f019780773cb87cc5c633b27c88f2
commit | author | age
812087 1 /*!
2 Chosen, a Select Box Enhancer for jQuery and Prototype
3 by Patrick Filler for Harvest, http://getharvest.com
4
5 Version 1.8.7
6 Full source at https://github.com/harvesthq/chosen
7 Copyright (c) 2011-2018 Harvest http://getharvest.com
8
9 MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md
10 This file is generated by `grunt build`, do not edit it by hand.
11 */
12
13 (function() {
14   var AbstractChosen, SelectParser,
15     bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
16     extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
17     hasProp = {}.hasOwnProperty;
18
19   SelectParser = (function() {
20     function SelectParser() {
21       this.options_index = 0;
22       this.parsed = [];
23     }
24
25     SelectParser.prototype.add_node = function(child) {
26       if (child.nodeName.toUpperCase() === "OPTGROUP") {
27         return this.add_group(child);
28       } else {
29         return this.add_option(child);
30       }
31     };
32
33     SelectParser.prototype.add_group = function(group) {
34       var group_position, i, len, option, ref, results1;
35       group_position = this.parsed.length;
36       this.parsed.push({
37         array_index: group_position,
38         group: true,
39         label: group.label,
40         title: group.title ? group.title : void 0,
41         children: 0,
42         disabled: group.disabled,
43         classes: group.className
44       });
45       ref = group.childNodes;
46       results1 = [];
47       for (i = 0, len = ref.length; i < len; i++) {
48         option = ref[i];
49         results1.push(this.add_option(option, group_position, group.disabled));
50       }
51       return results1;
52     };
53
54     SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
55       if (option.nodeName.toUpperCase() === "OPTION") {
56         if (option.text !== "") {
57           if (group_position != null) {
58             this.parsed[group_position].children += 1;
59           }
60           this.parsed.push({
61             array_index: this.parsed.length,
62             options_index: this.options_index,
63             value: option.value,
64             text: option.text,
65             html: option.innerHTML,
66             title: option.title ? option.title : void 0,
67             selected: option.selected,
68             disabled: group_disabled === true ? group_disabled : option.disabled,
69             group_array_index: group_position,
70             group_label: group_position != null ? this.parsed[group_position].label : null,
71             classes: option.className,
72             style: option.style.cssText
73           });
74         } else {
75           this.parsed.push({
76             array_index: this.parsed.length,
77             options_index: this.options_index,
78             empty: true
79           });
80         }
81         return this.options_index += 1;
82       }
83     };
84
85     return SelectParser;
86
87   })();
88
89   SelectParser.select_to_array = function(select) {
90     var child, i, len, parser, ref;
91     parser = new SelectParser();
92     ref = select.childNodes;
93     for (i = 0, len = ref.length; i < len; i++) {
94       child = ref[i];
95       parser.add_node(child);
96     }
97     return parser.parsed;
98   };
99
100   AbstractChosen = (function() {
101     function AbstractChosen(form_field, options1) {
102       this.form_field = form_field;
103       this.options = options1 != null ? options1 : {};
104       this.label_click_handler = bind(this.label_click_handler, this);
105       if (!AbstractChosen.browser_is_supported()) {
106         return;
107       }
108       this.is_multiple = this.form_field.multiple;
109       this.set_default_text();
110       this.set_default_values();
111       this.setup();
112       this.set_up_html();
113       this.register_observers();
114       this.on_ready();
115     }
116
117     AbstractChosen.prototype.set_default_values = function() {
118       this.click_test_action = (function(_this) {
119         return function(evt) {
120           return _this.test_active_click(evt);
121         };
122       })(this);
123       this.activate_action = (function(_this) {
124         return function(evt) {
125           return _this.activate_field(evt);
126         };
127       })(this);
128       this.active_field = false;
129       this.mouse_on_container = false;
130       this.results_showing = false;
131       this.result_highlighted = null;
132       this.is_rtl = this.options.rtl || /\bchosen-rtl\b/.test(this.form_field.className);
133       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;
134       this.disable_search_threshold = this.options.disable_search_threshold || 0;
135       this.disable_search = this.options.disable_search || false;
136       this.enable_split_word_search = this.options.enable_split_word_search != null ? this.options.enable_split_word_search : true;
137       this.group_search = this.options.group_search != null ? this.options.group_search : true;
138       this.search_contains = this.options.search_contains || false;
139       this.single_backstroke_delete = this.options.single_backstroke_delete != null ? this.options.single_backstroke_delete : true;
140       this.max_selected_options = this.options.max_selected_options || Infinity;
141       this.inherit_select_classes = this.options.inherit_select_classes || false;
142       this.display_selected_options = this.options.display_selected_options != null ? this.options.display_selected_options : true;
143       this.display_disabled_options = this.options.display_disabled_options != null ? this.options.display_disabled_options : true;
144       this.include_group_label_in_selected = this.options.include_group_label_in_selected || false;
145       this.max_shown_results = this.options.max_shown_results || Number.POSITIVE_INFINITY;
146       this.case_sensitive_search = this.options.case_sensitive_search || false;
147       return this.hide_results_on_select = this.options.hide_results_on_select != null ? this.options.hide_results_on_select : true;
148     };
149
150     AbstractChosen.prototype.set_default_text = function() {
151       if (this.form_field.getAttribute("data-placeholder")) {
152         this.default_text = this.form_field.getAttribute("data-placeholder");
153       } else if (this.is_multiple) {
154         this.default_text = this.options.placeholder_text_multiple || this.options.placeholder_text || AbstractChosen.default_multiple_text;
155       } else {
156         this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || AbstractChosen.default_single_text;
157       }
158       this.default_text = this.escape_html(this.default_text);
159       return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || AbstractChosen.default_no_result_text;
160     };
161
162     AbstractChosen.prototype.choice_label = function(item) {
163       if (this.include_group_label_in_selected && (item.group_label != null)) {
164         return "<b class='group-name'>" + (this.escape_html(item.group_label)) + "</b>" + item.html;
165       } else {
166         return item.html;
167       }
168     };
169
170     AbstractChosen.prototype.mouse_enter = function() {
171       return this.mouse_on_container = true;
172     };
173
174     AbstractChosen.prototype.mouse_leave = function() {
175       return this.mouse_on_container = false;
176     };
177
178     AbstractChosen.prototype.input_focus = function(evt) {
179       if (this.is_multiple) {
180         if (!this.active_field) {
181           return setTimeout(((function(_this) {
182             return function() {
183               return _this.container_mousedown();
184             };
185           })(this)), 50);
186         }
187       } else {
188         if (!this.active_field) {
189           return this.activate_field();
190         }
191       }
192     };
193
194     AbstractChosen.prototype.input_blur = function(evt) {
195       if (!this.mouse_on_container) {
196         this.active_field = false;
197         return setTimeout(((function(_this) {
198           return function() {
199             return _this.blur_test();
200           };
201         })(this)), 100);
202       }
203     };
204
205     AbstractChosen.prototype.label_click_handler = function(evt) {
206       if (this.is_multiple) {
207         return this.container_mousedown(evt);
208       } else {
209         return this.activate_field();
210       }
211     };
212
213     AbstractChosen.prototype.results_option_build = function(options) {
214       var content, data, data_content, i, len, ref, shown_results;
215       content = '';
216       shown_results = 0;
217       ref = this.results_data;
218       for (i = 0, len = ref.length; i < len; i++) {
219         data = ref[i];
220         data_content = '';
221         if (data.group) {
222           data_content = this.result_add_group(data);
223         } else {
224           data_content = this.result_add_option(data);
225         }
226         if (data_content !== '') {
227           shown_results++;
228           content += data_content;
229         }
230         if (options != null ? options.first : void 0) {
231           if (data.selected && this.is_multiple) {
232             this.choice_build(data);
233           } else if (data.selected && !this.is_multiple) {
234             this.single_set_selected_text(this.choice_label(data));
235           }
236         }
237         if (shown_results >= this.max_shown_results) {
238           break;
239         }
240       }
241       return content;
242     };
243
244     AbstractChosen.prototype.result_add_option = function(option) {
245       var classes, option_el;
246       if (!option.search_match) {
247         return '';
248       }
249       if (!this.include_option_in_results(option)) {
250         return '';
251       }
252       classes = [];
253       if (!option.disabled && !(option.selected && this.is_multiple)) {
254         classes.push("active-result");
255       }
256       if (option.disabled && !(option.selected && this.is_multiple)) {
257         classes.push("disabled-result");
258       }
259       if (option.selected) {
260         classes.push("result-selected");
261       }
262       if (option.group_array_index != null) {
263         classes.push("group-option");
264       }
265       if (option.classes !== "") {
266         classes.push(option.classes);
267       }
268       option_el = document.createElement("li");
269       option_el.className = classes.join(" ");
270       if (option.style) {
271         option_el.style.cssText = option.style;
272       }
273       option_el.setAttribute("data-option-array-index", option.array_index);
274       option_el.innerHTML = option.highlighted_html || option.html;
275       if (option.title) {
276         option_el.title = option.title;
277       }
278       return this.outerHTML(option_el);
279     };
280
281     AbstractChosen.prototype.result_add_group = function(group) {
282       var classes, group_el;
283       if (!(group.search_match || group.group_match)) {
284         return '';
285       }
286       if (!(group.active_options > 0)) {
287         return '';
288       }
289       classes = [];
290       classes.push("group-result");
291       if (group.classes) {
292         classes.push(group.classes);
293       }
294       group_el = document.createElement("li");
295       group_el.className = classes.join(" ");
296       group_el.innerHTML = group.highlighted_html || this.escape_html(group.label);
297       if (group.title) {
298         group_el.title = group.title;
299       }
300       return this.outerHTML(group_el);
301     };
302
303     AbstractChosen.prototype.results_update_field = function() {
304       this.set_default_text();
305       if (!this.is_multiple) {
306         this.results_reset_cleanup();
307       }
308       this.result_clear_highlight();
309       this.results_build();
310       if (this.results_showing) {
311         return this.winnow_results();
312       }
313     };
314
315     AbstractChosen.prototype.reset_single_select_options = function() {
316       var i, len, ref, result, results1;
317       ref = this.results_data;
318       results1 = [];
319       for (i = 0, len = ref.length; i < len; i++) {
320         result = ref[i];
321         if (result.selected) {
322           results1.push(result.selected = false);
323         } else {
324           results1.push(void 0);
325         }
326       }
327       return results1;
328     };
329
330     AbstractChosen.prototype.results_toggle = function() {
331       if (this.results_showing) {
332         return this.results_hide();
333       } else {
334         return this.results_show();
335       }
336     };
337
338     AbstractChosen.prototype.results_search = function(evt) {
339       if (this.results_showing) {
340         return this.winnow_results();
341       } else {
342         return this.results_show();
343       }
344     };
345
346     AbstractChosen.prototype.winnow_results = function(options) {
347       var escapedQuery, fix, i, len, option, prefix, query, ref, regex, results, results_group, search_match, startpos, suffix, text;
348       this.no_results_clear();
349       results = 0;
350       query = this.get_search_text();
351       escapedQuery = query.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
352       regex = this.get_search_regex(escapedQuery);
353       ref = this.results_data;
354       for (i = 0, len = ref.length; i < len; i++) {
355         option = ref[i];
356         option.search_match = false;
357         results_group = null;
358         search_match = null;
359         option.highlighted_html = '';
360         if (this.include_option_in_results(option)) {
361           if (option.group) {
362             option.group_match = false;
363             option.active_options = 0;
364           }
365           if ((option.group_array_index != null) && this.results_data[option.group_array_index]) {
366             results_group = this.results_data[option.group_array_index];
367             if (results_group.active_options === 0 && results_group.search_match) {
368               results += 1;
369             }
370             results_group.active_options += 1;
371           }
372           text = option.group ? option.label : option.text;
373           if (!(option.group && !this.group_search)) {
374             search_match = this.search_string_match(text, regex);
375             option.search_match = search_match != null;
376             if (option.search_match && !option.group) {
377               results += 1;
378             }
379             if (option.search_match) {
380               if (query.length) {
381                 startpos = search_match.index;
382                 prefix = text.slice(0, startpos);
383                 fix = text.slice(startpos, startpos + query.length);
384                 suffix = text.slice(startpos + query.length);
385                 option.highlighted_html = (this.escape_html(prefix)) + "<em>" + (this.escape_html(fix)) + "</em>" + (this.escape_html(suffix));
386               }
387               if (results_group != null) {
388                 results_group.group_match = true;
389               }
390             } else if ((option.group_array_index != null) && this.results_data[option.group_array_index].search_match) {
391               option.search_match = true;
392             }
393           }
394         }
395       }
396       this.result_clear_highlight();
397       if (results < 1 && query.length) {
398         this.update_results_content("");
399         return this.no_results(query);
400       } else {
401         this.update_results_content(this.results_option_build());
402         if (!(options != null ? options.skip_highlight : void 0)) {
403           return this.winnow_results_set_highlight();
404         }
405       }
406     };
407
408     AbstractChosen.prototype.get_search_regex = function(escaped_search_string) {
409       var regex_flag, regex_string;
410       regex_string = this.search_contains ? escaped_search_string : "(^|\\s|\\b)" + escaped_search_string + "[^\\s]*";
411       if (!(this.enable_split_word_search || this.search_contains)) {
412         regex_string = "^" + regex_string;
413       }
414       regex_flag = this.case_sensitive_search ? "" : "i";
415       return new RegExp(regex_string, regex_flag);
416     };
417
418     AbstractChosen.prototype.search_string_match = function(search_string, regex) {
419       var match;
420       match = regex.exec(search_string);
421       if (!this.search_contains && (match != null ? match[1] : void 0)) {
422         match.index += 1;
423       }
424       return match;
425     };
426
427     AbstractChosen.prototype.choices_count = function() {
428       var i, len, option, ref;
429       if (this.selected_option_count != null) {
430         return this.selected_option_count;
431       }
432       this.selected_option_count = 0;
433       ref = this.form_field.options;
434       for (i = 0, len = ref.length; i < len; i++) {
435         option = ref[i];
436         if (option.selected) {
437           this.selected_option_count += 1;
438         }
439       }
440       return this.selected_option_count;
441     };
442
443     AbstractChosen.prototype.choices_click = function(evt) {
444       evt.preventDefault();
445       this.activate_field();
446       if (!(this.results_showing || this.is_disabled)) {
447         return this.results_show();
448       }
449     };
450
451     AbstractChosen.prototype.keydown_checker = function(evt) {
452       var ref, stroke;
453       stroke = (ref = evt.which) != null ? ref : evt.keyCode;
454       this.search_field_scale();
455       if (stroke !== 8 && this.pending_backstroke) {
456         this.clear_backstroke();
457       }
458       switch (stroke) {
459         case 8:
460           this.backstroke_length = this.get_search_field_value().length;
461           break;
462         case 9:
463           if (this.results_showing && !this.is_multiple) {
464             this.result_select(evt);
465           }
466           this.mouse_on_container = false;
467           break;
468         case 13:
469           if (this.results_showing) {
470             evt.preventDefault();
471           }
472           break;
473         case 27:
474           if (this.results_showing) {
475             evt.preventDefault();
476           }
477           break;
478         case 32:
479           if (this.disable_search) {
480             evt.preventDefault();
481           }
482           break;
483         case 38:
484           evt.preventDefault();
485           this.keyup_arrow();
486           break;
487         case 40:
488           evt.preventDefault();
489           this.keydown_arrow();
490           break;
491       }
492     };
493
494     AbstractChosen.prototype.keyup_checker = function(evt) {
495       var ref, stroke;
496       stroke = (ref = evt.which) != null ? ref : evt.keyCode;
497       this.search_field_scale();
498       switch (stroke) {
499         case 8:
500           if (this.is_multiple && this.backstroke_length < 1 && this.choices_count() > 0) {
501             this.keydown_backstroke();
502           } else if (!this.pending_backstroke) {
503             this.result_clear_highlight();
504             this.results_search();
505           }
506           break;
507         case 13:
508           evt.preventDefault();
509           if (this.results_showing) {
510             this.result_select(evt);
511           }
512           break;
513         case 27:
514           if (this.results_showing) {
515             this.results_hide();
516           }
517           break;
518         case 9:
519         case 16:
520         case 17:
521         case 18:
522         case 38:
523         case 40:
524         case 91:
525           break;
526         default:
527           this.results_search();
528           break;
529       }
530     };
531
532     AbstractChosen.prototype.clipboard_event_checker = function(evt) {
533       if (this.is_disabled) {
534         return;
535       }
536       return setTimeout(((function(_this) {
537         return function() {
538           return _this.results_search();
539         };
540       })(this)), 50);
541     };
542
543     AbstractChosen.prototype.container_width = function() {
544       if (this.options.width != null) {
545         return this.options.width;
546       } else {
547         return this.form_field.offsetWidth + "px";
548       }
549     };
550
551     AbstractChosen.prototype.include_option_in_results = function(option) {
552       if (this.is_multiple && (!this.display_selected_options && option.selected)) {
553         return false;
554       }
555       if (!this.display_disabled_options && option.disabled) {
556         return false;
557       }
558       if (option.empty) {
559         return false;
560       }
561       return true;
562     };
563
564     AbstractChosen.prototype.search_results_touchstart = function(evt) {
565       this.touch_started = true;
566       return this.search_results_mouseover(evt);
567     };
568
569     AbstractChosen.prototype.search_results_touchmove = function(evt) {
570       this.touch_started = false;
571       return this.search_results_mouseout(evt);
572     };
573
574     AbstractChosen.prototype.search_results_touchend = function(evt) {
575       if (this.touch_started) {
576         return this.search_results_mouseup(evt);
577       }
578     };
579
580     AbstractChosen.prototype.outerHTML = function(element) {
581       var tmp;
582       if (element.outerHTML) {
583         return element.outerHTML;
584       }
585       tmp = document.createElement("div");
586       tmp.appendChild(element);
587       return tmp.innerHTML;
588     };
589
590     AbstractChosen.prototype.get_single_html = function() {
591       return "<a class=\"chosen-single chosen-default\">\n  <span>" + this.default_text + "</span>\n  <div><b></b></div>\n</a>\n<div class=\"chosen-drop\">\n  <div class=\"chosen-search\">\n    <input class=\"chosen-search-input\" type=\"text\" autocomplete=\"off\" />\n  </div>\n  <ul class=\"chosen-results\"></ul>\n</div>";
592     };
593
594     AbstractChosen.prototype.get_multi_html = function() {
595       return "<ul class=\"chosen-choices\">\n  <li class=\"search-field\">\n    <input class=\"chosen-search-input\" type=\"text\" autocomplete=\"off\" value=\"" + this.default_text + "\" />\n  </li>\n</ul>\n<div class=\"chosen-drop\">\n  <ul class=\"chosen-results\"></ul>\n</div>";
596     };
597
598     AbstractChosen.prototype.get_no_results_html = function(terms) {
599       return "<li class=\"no-results\">\n  " + this.results_none_found + " <span>" + (this.escape_html(terms)) + "</span>\n</li>";
600     };
601
602     AbstractChosen.browser_is_supported = function() {
603       if ("Microsoft Internet Explorer" === window.navigator.appName) {
604         return document.documentMode >= 8;
605       }
606       if (/iP(od|hone)/i.test(window.navigator.userAgent) || /IEMobile/i.test(window.navigator.userAgent) || /Windows Phone/i.test(window.navigator.userAgent) || /BlackBerry/i.test(window.navigator.userAgent) || /BB10/i.test(window.navigator.userAgent) || /Android.*Mobile/i.test(window.navigator.userAgent)) {
607         return false;
608       }
609       return true;
610     };
611
612     AbstractChosen.default_multiple_text = "Select Some Options";
613
614     AbstractChosen.default_single_text = "Select an Option";
615
616     AbstractChosen.default_no_result_text = "No results match";
617
618     return AbstractChosen;
619
620   })();
621
622   this.Chosen = (function(superClass) {
623     var triggerHtmlEvent;
624
625     extend(Chosen, superClass);
626
627     function Chosen() {
628       return Chosen.__super__.constructor.apply(this, arguments);
629     }
630
631     Chosen.prototype.setup = function() {
632       return this.current_selectedIndex = this.form_field.selectedIndex;
633     };
634
635     Chosen.prototype.set_up_html = function() {
636       var container_classes, container_props;
637       container_classes = ["chosen-container"];
638       container_classes.push("chosen-container-" + (this.is_multiple ? "multi" : "single"));
639       if (this.inherit_select_classes && this.form_field.className) {
640         container_classes.push(this.form_field.className);
641       }
642       if (this.is_rtl) {
643         container_classes.push("chosen-rtl");
644       }
645       container_props = {
646         'class': container_classes.join(' '),
647         'title': this.form_field.title
648       };
649       if (this.form_field.id.length) {
650         container_props.id = this.form_field.id.replace(/[^\w]/g, '_') + "_chosen";
651       }
652       this.container = new Element('div', container_props);
653       this.container.setStyle({
654         width: this.container_width()
655       });
656       if (this.is_multiple) {
657         this.container.update(this.get_multi_html());
658       } else {
659         this.container.update(this.get_single_html());
660       }
661       this.form_field.hide().insert({
662         after: this.container
663       });
664       this.dropdown = this.container.down('div.chosen-drop');
665       this.search_field = this.container.down('input');
666       this.search_results = this.container.down('ul.chosen-results');
667       this.search_field_scale();
668       this.search_no_results = this.container.down('li.no-results');
669       if (this.is_multiple) {
670         this.search_choices = this.container.down('ul.chosen-choices');
671         this.search_container = this.container.down('li.search-field');
672       } else {
673         this.search_container = this.container.down('div.chosen-search');
674         this.selected_item = this.container.down('.chosen-single');
675       }
676       this.results_build();
677       this.set_tab_index();
678       return this.set_label_behavior();
679     };
680
681     Chosen.prototype.on_ready = function() {
682       return this.form_field.fire("chosen:ready", {
683         chosen: this
684       });
685     };
686
687     Chosen.prototype.register_observers = function() {
688       this.container.observe("touchstart", (function(_this) {
689         return function(evt) {
690           return _this.container_mousedown(evt);
691         };
692       })(this));
693       this.container.observe("touchend", (function(_this) {
694         return function(evt) {
695           return _this.container_mouseup(evt);
696         };
697       })(this));
698       this.container.observe("mousedown", (function(_this) {
699         return function(evt) {
700           return _this.container_mousedown(evt);
701         };
702       })(this));
703       this.container.observe("mouseup", (function(_this) {
704         return function(evt) {
705           return _this.container_mouseup(evt);
706         };
707       })(this));
708       this.container.observe("mouseenter", (function(_this) {
709         return function(evt) {
710           return _this.mouse_enter(evt);
711         };
712       })(this));
713       this.container.observe("mouseleave", (function(_this) {
714         return function(evt) {
715           return _this.mouse_leave(evt);
716         };
717       })(this));
718       this.search_results.observe("mouseup", (function(_this) {
719         return function(evt) {
720           return _this.search_results_mouseup(evt);
721         };
722       })(this));
723       this.search_results.observe("mouseover", (function(_this) {
724         return function(evt) {
725           return _this.search_results_mouseover(evt);
726         };
727       })(this));
728       this.search_results.observe("mouseout", (function(_this) {
729         return function(evt) {
730           return _this.search_results_mouseout(evt);
731         };
732       })(this));
733       this.search_results.observe("mousewheel", (function(_this) {
734         return function(evt) {
735           return _this.search_results_mousewheel(evt);
736         };
737       })(this));
738       this.search_results.observe("DOMMouseScroll", (function(_this) {
739         return function(evt) {
740           return _this.search_results_mousewheel(evt);
741         };
742       })(this));
743       this.search_results.observe("touchstart", (function(_this) {
744         return function(evt) {
745           return _this.search_results_touchstart(evt);
746         };
747       })(this));
748       this.search_results.observe("touchmove", (function(_this) {
749         return function(evt) {
750           return _this.search_results_touchmove(evt);
751         };
752       })(this));
753       this.search_results.observe("touchend", (function(_this) {
754         return function(evt) {
755           return _this.search_results_touchend(evt);
756         };
757       })(this));
758       this.form_field.observe("chosen:updated", (function(_this) {
759         return function(evt) {
760           return _this.results_update_field(evt);
761         };
762       })(this));
763       this.form_field.observe("chosen:activate", (function(_this) {
764         return function(evt) {
765           return _this.activate_field(evt);
766         };
767       })(this));
768       this.form_field.observe("chosen:open", (function(_this) {
769         return function(evt) {
770           return _this.container_mousedown(evt);
771         };
772       })(this));
773       this.form_field.observe("chosen:close", (function(_this) {
774         return function(evt) {
775           return _this.close_field(evt);
776         };
777       })(this));
778       this.search_field.observe("blur", (function(_this) {
779         return function(evt) {
780           return _this.input_blur(evt);
781         };
782       })(this));
783       this.search_field.observe("keyup", (function(_this) {
784         return function(evt) {
785           return _this.keyup_checker(evt);
786         };
787       })(this));
788       this.search_field.observe("keydown", (function(_this) {
789         return function(evt) {
790           return _this.keydown_checker(evt);
791         };
792       })(this));
793       this.search_field.observe("focus", (function(_this) {
794         return function(evt) {
795           return _this.input_focus(evt);
796         };
797       })(this));
798       this.search_field.observe("cut", (function(_this) {
799         return function(evt) {
800           return _this.clipboard_event_checker(evt);
801         };
802       })(this));
803       this.search_field.observe("paste", (function(_this) {
804         return function(evt) {
805           return _this.clipboard_event_checker(evt);
806         };
807       })(this));
808       if (this.is_multiple) {
809         return this.search_choices.observe("click", (function(_this) {
810           return function(evt) {
811             return _this.choices_click(evt);
812           };
813         })(this));
814       } else {
815         return this.container.observe("click", (function(_this) {
816           return function(evt) {
817             return evt.preventDefault();
818           };
819         })(this));
820       }
821     };
822
823     Chosen.prototype.destroy = function() {
824       var event, i, len, ref;
825       this.container.ownerDocument.stopObserving("click", this.click_test_action);
826       ref = ['chosen:updated', 'chosen:activate', 'chosen:open', 'chosen:close'];
827       for (i = 0, len = ref.length; i < len; i++) {
828         event = ref[i];
829         this.form_field.stopObserving(event);
830       }
831       this.container.stopObserving();
832       this.search_results.stopObserving();
833       this.search_field.stopObserving();
834       if (this.form_field_label != null) {
835         this.form_field_label.stopObserving();
836       }
837       if (this.is_multiple) {
838         this.search_choices.stopObserving();
839         this.container.select(".search-choice-close").each(function(choice) {
840           return choice.stopObserving();
841         });
842       } else {
843         this.selected_item.stopObserving();
844       }
845       if (this.search_field.tabIndex) {
846         this.form_field.tabIndex = this.search_field.tabIndex;
847       }
848       this.container.remove();
849       return this.form_field.show();
850     };
851
852     Chosen.prototype.search_field_disabled = function() {
853       var ref;
854       this.is_disabled = this.form_field.disabled || ((ref = this.form_field.up('fieldset')) != null ? ref.disabled : void 0) || false;
855       if (this.is_disabled) {
856         this.container.addClassName('chosen-disabled');
857       } else {
858         this.container.removeClassName('chosen-disabled');
859       }
860       this.search_field.disabled = this.is_disabled;
861       if (!this.is_multiple) {
862         this.selected_item.stopObserving('focus', this.activate_field);
863       }
864       if (this.is_disabled) {
865         return this.close_field();
866       } else if (!this.is_multiple) {
867         return this.selected_item.observe('focus', this.activate_field);
868       }
869     };
870
871     Chosen.prototype.container_mousedown = function(evt) {
872       var ref;
873       if (this.is_disabled) {
874         return;
875       }
876       if (evt && ((ref = evt.type) === 'mousedown' || ref === 'touchstart') && !this.results_showing) {
877         evt.preventDefault();
878       }
879       if (!((evt != null) && evt.target.hasClassName("search-choice-close"))) {
880         if (!this.active_field) {
881           if (this.is_multiple) {
882             this.search_field.clear();
883           }
884           this.container.ownerDocument.observe("click", this.click_test_action);
885           this.results_show();
886         } else if (!this.is_multiple && evt && (evt.target === this.selected_item || evt.target.up("a.chosen-single"))) {
887           this.results_toggle();
888         }
889         return this.activate_field();
890       }
891     };
892
893     Chosen.prototype.container_mouseup = function(evt) {
894       if (evt.target.nodeName === "ABBR" && !this.is_disabled) {
895         return this.results_reset(evt);
896       }
897     };
898
899     Chosen.prototype.search_results_mousewheel = function(evt) {
900       var delta;
901       delta = evt.deltaY || -evt.wheelDelta || evt.detail;
902       if (delta != null) {
903         evt.preventDefault();
904         if (evt.type === 'DOMMouseScroll') {
905           delta = delta * 40;
906         }
907         return this.search_results.scrollTop = delta + this.search_results.scrollTop;
908       }
909     };
910
911     Chosen.prototype.blur_test = function(evt) {
912       if (!this.active_field && this.container.hasClassName("chosen-container-active")) {
913         return this.close_field();
914       }
915     };
916
917     Chosen.prototype.close_field = function() {
918       this.container.ownerDocument.stopObserving("click", this.click_test_action);
919       this.active_field = false;
920       this.results_hide();
921       this.container.removeClassName("chosen-container-active");
922       this.clear_backstroke();
923       this.show_search_field_default();
924       this.search_field_scale();
925       return this.search_field.blur();
926     };
927
928     Chosen.prototype.activate_field = function() {
929       if (this.is_disabled) {
930         return;
931       }
932       this.container.addClassName("chosen-container-active");
933       this.active_field = true;
934       this.search_field.value = this.get_search_field_value();
935       return this.search_field.focus();
936     };
937
938     Chosen.prototype.test_active_click = function(evt) {
939       if (evt.target.up('.chosen-container') === this.container) {
940         return this.active_field = true;
941       } else {
942         return this.close_field();
943       }
944     };
945
946     Chosen.prototype.results_build = function() {
947       this.parsing = true;
948       this.selected_option_count = null;
949       this.results_data = SelectParser.select_to_array(this.form_field);
950       if (this.is_multiple) {
951         this.search_choices.select("li.search-choice").invoke("remove");
952       } else {
953         this.single_set_selected_text();
954         if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) {
955           this.search_field.readOnly = true;
956           this.container.addClassName("chosen-container-single-nosearch");
957         } else {
958           this.search_field.readOnly = false;
959           this.container.removeClassName("chosen-container-single-nosearch");
960         }
961       }
962       this.update_results_content(this.results_option_build({
963         first: true
964       }));
965       this.search_field_disabled();
966       this.show_search_field_default();
967       this.search_field_scale();
968       return this.parsing = false;
969     };
970
971     Chosen.prototype.result_do_highlight = function(el) {
972       var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
973       this.result_clear_highlight();
974       this.result_highlight = el;
975       this.result_highlight.addClassName("highlighted");
976       maxHeight = parseInt(this.search_results.getStyle('maxHeight'), 10);
977       visible_top = this.search_results.scrollTop;
978       visible_bottom = maxHeight + visible_top;
979       high_top = this.result_highlight.positionedOffset().top;
980       high_bottom = high_top + this.result_highlight.getHeight();
981       if (high_bottom >= visible_bottom) {
982         return this.search_results.scrollTop = (high_bottom - maxHeight) > 0 ? high_bottom - maxHeight : 0;
983       } else if (high_top < visible_top) {
984         return this.search_results.scrollTop = high_top;
985       }
986     };
987
988     Chosen.prototype.result_clear_highlight = function() {
989       if (this.result_highlight) {
990         this.result_highlight.removeClassName('highlighted');
991       }
992       return this.result_highlight = null;
993     };
994
995     Chosen.prototype.results_show = function() {
996       if (this.is_multiple && this.max_selected_options <= this.choices_count()) {
997         this.form_field.fire("chosen:maxselected", {
998           chosen: this
999         });
1000         return false;
1001       }
1002       this.container.addClassName("chosen-with-drop");
1003       this.results_showing = true;
1004       this.search_field.focus();
1005       this.search_field.value = this.get_search_field_value();
1006       this.winnow_results();
1007       return this.form_field.fire("chosen:showing_dropdown", {
1008         chosen: this
1009       });
1010     };
1011
1012     Chosen.prototype.update_results_content = function(content) {
1013       return this.search_results.update(content);
1014     };
1015
1016     Chosen.prototype.results_hide = function() {
1017       if (this.results_showing) {
1018         this.result_clear_highlight();
1019         this.container.removeClassName("chosen-with-drop");
1020         this.form_field.fire("chosen:hiding_dropdown", {
1021           chosen: this
1022         });
1023       }
1024       return this.results_showing = false;
1025     };
1026
1027     Chosen.prototype.set_tab_index = function(el) {
1028       var ti;
1029       if (this.form_field.tabIndex) {
1030         ti = this.form_field.tabIndex;
1031         this.form_field.tabIndex = -1;
1032         return this.search_field.tabIndex = ti;
1033       }
1034     };
1035
1036     Chosen.prototype.set_label_behavior = function() {
1037       this.form_field_label = this.form_field.up("label");
1038       if (this.form_field_label == null) {
1039         this.form_field_label = $$("label[for='" + this.form_field.id + "']").first();
1040       }
1041       if (this.form_field_label != null) {
1042         return this.form_field_label.observe("click", this.label_click_handler);
1043       }
1044     };
1045
1046     Chosen.prototype.show_search_field_default = function() {
1047       if (this.is_multiple && this.choices_count() < 1 && !this.active_field) {
1048         this.search_field.value = this.default_text;
1049         return this.search_field.addClassName("default");
1050       } else {
1051         this.search_field.value = "";
1052         return this.search_field.removeClassName("default");
1053       }
1054     };
1055
1056     Chosen.prototype.search_results_mouseup = function(evt) {
1057       var target;
1058       target = evt.target.hasClassName("active-result") ? evt.target : evt.target.up(".active-result");
1059       if (target) {
1060         this.result_highlight = target;
1061         this.result_select(evt);
1062         return this.search_field.focus();
1063       }
1064     };
1065
1066     Chosen.prototype.search_results_mouseover = function(evt) {
1067       var target;
1068       target = evt.target.hasClassName("active-result") ? evt.target : evt.target.up(".active-result");
1069       if (target) {
1070         return this.result_do_highlight(target);
1071       }
1072     };
1073
1074     Chosen.prototype.search_results_mouseout = function(evt) {
1075       if (evt.target.hasClassName('active-result') || evt.target.up('.active-result')) {
1076         return this.result_clear_highlight();
1077       }
1078     };
1079
1080     Chosen.prototype.choice_build = function(item) {
1081       var choice, close_link;
1082       choice = new Element('li', {
1083         "class": "search-choice"
1084       }).update("<span>" + (this.choice_label(item)) + "</span>");
1085       if (item.disabled) {
1086         choice.addClassName('search-choice-disabled');
1087       } else {
1088         close_link = new Element('a', {
1089           href: '#',
1090           "class": 'search-choice-close',
1091           rel: item.array_index
1092         });
1093         close_link.observe("click", (function(_this) {
1094           return function(evt) {
1095             return _this.choice_destroy_link_click(evt);
1096           };
1097         })(this));
1098         choice.insert(close_link);
1099       }
1100       return this.search_container.insert({
1101         before: choice
1102       });
1103     };
1104
1105     Chosen.prototype.choice_destroy_link_click = function(evt) {
1106       evt.preventDefault();
1107       evt.stopPropagation();
1108       if (!this.is_disabled) {
1109         return this.choice_destroy(evt.target);
1110       }
1111     };
1112
1113     Chosen.prototype.choice_destroy = function(link) {
1114       if (this.result_deselect(link.readAttribute("rel"))) {
1115         if (this.active_field) {
1116           this.search_field.focus();
1117         } else {
1118           this.show_search_field_default();
1119         }
1120         if (this.is_multiple && this.choices_count() > 0 && this.get_search_field_value().length < 1) {
1121           this.results_hide();
1122         }
1123         link.up('li').remove();
1124         return this.search_field_scale();
1125       }
1126     };
1127
1128     Chosen.prototype.results_reset = function() {
1129       this.reset_single_select_options();
1130       this.form_field.options[0].selected = true;
1131       this.single_set_selected_text();
1132       this.show_search_field_default();
1133       this.results_reset_cleanup();
1134       this.trigger_form_field_change();
1135       if (this.active_field) {
1136         return this.results_hide();
1137       }
1138     };
1139
1140     Chosen.prototype.results_reset_cleanup = function() {
1141       var deselect_trigger;
1142       this.current_selectedIndex = this.form_field.selectedIndex;
1143       deselect_trigger = this.selected_item.down("abbr");
1144       if (deselect_trigger) {
1145         return deselect_trigger.remove();
1146       }
1147     };
1148
1149     Chosen.prototype.result_select = function(evt) {
1150       var high, item;
1151       if (this.result_highlight) {
1152         high = this.result_highlight;
1153         this.result_clear_highlight();
1154         if (this.is_multiple && this.max_selected_options <= this.choices_count()) {
1155           this.form_field.fire("chosen:maxselected", {
1156             chosen: this
1157           });
1158           return false;
1159         }
1160         if (this.is_multiple) {
1161           high.removeClassName("active-result");
1162         } else {
1163           this.reset_single_select_options();
1164         }
1165         high.addClassName("result-selected");
1166         item = this.results_data[high.getAttribute("data-option-array-index")];
1167         item.selected = true;
1168         this.form_field.options[item.options_index].selected = true;
1169         this.selected_option_count = null;
1170         if (this.is_multiple) {
1171           this.choice_build(item);
1172         } else {
1173           this.single_set_selected_text(this.choice_label(item));
1174         }
1175         if (this.is_multiple && (!this.hide_results_on_select || (evt.metaKey || evt.ctrlKey))) {
1176           if (evt.metaKey || evt.ctrlKey) {
1177             this.winnow_results({
1178               skip_highlight: true
1179             });
1180           } else {
1181             this.search_field.value = "";
1182             this.winnow_results();
1183           }
1184         } else {
1185           this.results_hide();
1186           this.show_search_field_default();
1187         }
1188         if (this.is_multiple || this.form_field.selectedIndex !== this.current_selectedIndex) {
1189           this.trigger_form_field_change();
1190         }
1191         this.current_selectedIndex = this.form_field.selectedIndex;
1192         evt.preventDefault();
1193         return this.search_field_scale();
1194       }
1195     };
1196
1197     Chosen.prototype.single_set_selected_text = function(text) {
1198       if (text == null) {
1199         text = this.default_text;
1200       }
1201       if (text === this.default_text) {
1202         this.selected_item.addClassName("chosen-default");
1203       } else {
1204         this.single_deselect_control_build();
1205         this.selected_item.removeClassName("chosen-default");
1206       }
1207       return this.selected_item.down("span").update(text);
1208     };
1209
1210     Chosen.prototype.result_deselect = function(pos) {
1211       var result_data;
1212       result_data = this.results_data[pos];
1213       if (!this.form_field.options[result_data.options_index].disabled) {
1214         result_data.selected = false;
1215         this.form_field.options[result_data.options_index].selected = false;
1216         this.selected_option_count = null;
1217         this.result_clear_highlight();
1218         if (this.results_showing) {
1219           this.winnow_results();
1220         }
1221         this.trigger_form_field_change();
1222         this.search_field_scale();
1223         return true;
1224       } else {
1225         return false;
1226       }
1227     };
1228
1229     Chosen.prototype.single_deselect_control_build = function() {
1230       if (!this.allow_single_deselect) {
1231         return;
1232       }
1233       if (!this.selected_item.down("abbr")) {
1234         this.selected_item.down("span").insert({
1235           after: "<abbr class=\"search-choice-close\"></abbr>"
1236         });
1237       }
1238       return this.selected_item.addClassName("chosen-single-with-deselect");
1239     };
1240
1241     Chosen.prototype.get_search_field_value = function() {
1242       return this.search_field.value;
1243     };
1244
1245     Chosen.prototype.get_search_text = function() {
1246       return this.get_search_field_value().strip();
1247     };
1248
1249     Chosen.prototype.escape_html = function(text) {
1250       return text.escapeHTML();
1251     };
1252
1253     Chosen.prototype.winnow_results_set_highlight = function() {
1254       var do_high;
1255       if (!this.is_multiple) {
1256         do_high = this.search_results.down(".result-selected.active-result");
1257       }
1258       if (do_high == null) {
1259         do_high = this.search_results.down(".active-result");
1260       }
1261       if (do_high != null) {
1262         return this.result_do_highlight(do_high);
1263       }
1264     };
1265
1266     Chosen.prototype.no_results = function(terms) {
1267       this.search_results.insert(this.get_no_results_html(terms));
1268       return this.form_field.fire("chosen:no_results", {
1269         chosen: this
1270       });
1271     };
1272
1273     Chosen.prototype.no_results_clear = function() {
1274       var nr, results1;
1275       nr = null;
1276       results1 = [];
1277       while (nr = this.search_results.down(".no-results")) {
1278         results1.push(nr.remove());
1279       }
1280       return results1;
1281     };
1282
1283     Chosen.prototype.keydown_arrow = function() {
1284       var next_sib;
1285       if (this.results_showing && this.result_highlight) {
1286         next_sib = this.result_highlight.next('.active-result');
1287         if (next_sib) {
1288           return this.result_do_highlight(next_sib);
1289         }
1290       } else {
1291         return this.results_show();
1292       }
1293     };
1294
1295     Chosen.prototype.keyup_arrow = function() {
1296       var actives, prevs, sibs;
1297       if (!this.results_showing && !this.is_multiple) {
1298         return this.results_show();
1299       } else if (this.result_highlight) {
1300         sibs = this.result_highlight.previousSiblings();
1301         actives = this.search_results.select("li.active-result");
1302         prevs = sibs.intersect(actives);
1303         if (prevs.length) {
1304           return this.result_do_highlight(prevs.first());
1305         } else {
1306           if (this.choices_count() > 0) {
1307             this.results_hide();
1308           }
1309           return this.result_clear_highlight();
1310         }
1311       }
1312     };
1313
1314     Chosen.prototype.keydown_backstroke = function() {
1315       var next_available_destroy;
1316       if (this.pending_backstroke) {
1317         this.choice_destroy(this.pending_backstroke.down("a"));
1318         return this.clear_backstroke();
1319       } else {
1320         next_available_destroy = this.search_container.siblings().last();
1321         if (next_available_destroy && next_available_destroy.hasClassName("search-choice") && !next_available_destroy.hasClassName("search-choice-disabled")) {
1322           this.pending_backstroke = next_available_destroy;
1323           if (this.pending_backstroke) {
1324             this.pending_backstroke.addClassName("search-choice-focus");
1325           }
1326           if (this.single_backstroke_delete) {
1327             return this.keydown_backstroke();
1328           } else {
1329             return this.pending_backstroke.addClassName("search-choice-focus");
1330           }
1331         }
1332       }
1333     };
1334
1335     Chosen.prototype.clear_backstroke = function() {
1336       if (this.pending_backstroke) {
1337         this.pending_backstroke.removeClassName("search-choice-focus");
1338       }
1339       return this.pending_backstroke = null;
1340     };
1341
1342     Chosen.prototype.search_field_scale = function() {
1343       var container_width, div, i, len, style, style_block, styles, width;
1344       if (!this.is_multiple) {
1345         return;
1346       }
1347       style_block = {
1348         position: 'absolute',
1349         left: '-1000px',
1350         top: '-1000px',
1351         display: 'none',
1352         whiteSpace: 'pre'
1353       };
1354       styles = ['fontSize', 'fontStyle', 'fontWeight', 'fontFamily', 'lineHeight', 'textTransform', 'letterSpacing'];
1355       for (i = 0, len = styles.length; i < len; i++) {
1356         style = styles[i];
1357         style_block[style] = this.search_field.getStyle(style);
1358       }
1359       div = new Element('div').update(this.escape_html(this.get_search_field_value()));
1360       div.setStyle(style_block);
1361       document.body.appendChild(div);
1362       width = div.measure('width') + 25;
1363       div.remove();
1364       if (container_width = this.container.getWidth()) {
1365         width = Math.min(container_width - 10, width);
1366       }
1367       return this.search_field.setStyle({
1368         width: width + 'px'
1369       });
1370     };
1371
1372     Chosen.prototype.trigger_form_field_change = function() {
1373       triggerHtmlEvent(this.form_field, 'input');
1374       return triggerHtmlEvent(this.form_field, 'change');
1375     };
1376
1377     triggerHtmlEvent = function(element, eventType) {
1378       var error, evt;
1379       if (element.dispatchEvent) {
1380         try {
1381           evt = new Event(eventType, {
1382             bubbles: true,
1383             cancelable: true
1384           });
1385         } catch (error) {
1386           evt = document.createEvent('HTMLEvents');
1387           evt.initEvent(eventType, true, true);
1388         }
1389         return element.dispatchEvent(evt);
1390       } else {
1391         return element.fireEvent("on" + eventType, document.createEventObject());
1392       }
1393     };
1394
1395     return Chosen;
1396
1397   })(AbstractChosen);
1398
1399 }).call(this);