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