1 // Chosen, a Select Box Enhancer for jQuery and Prototype
2 // by Patrick Filler for Harvest, http://getharvest.com
5 // Full source at https://github.com/harvesthq/chosen
6 // Copyright (c) 2011 Harvest http://getharvest.com
8 // MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md
9 // This file is generated by `grunt build`, do not edit it by hand.
11 var $, AbstractChosen, Chosen, SelectParser, _ref,
12 __hasProp = {}.hasOwnProperty,
13 __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; };
15 SelectParser = (function() {
16 function SelectParser() {
17 this.options_index = 0;
21 SelectParser.prototype.add_node = function(child) {
22 if (child.nodeName.toUpperCase() === "OPTGROUP") {
23 return this.add_group(child);
25 return this.add_option(child);
29 SelectParser.prototype.add_group = function(group) {
30 var group_position, option, _i, _len, _ref, _results;
32 group_position = this.parsed.length;
34 array_index: group_position,
36 label: this.escapeExpression(group.label),
38 disabled: group.disabled
40 _ref = group.childNodes;
42 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
44 _results.push(this.add_option(option, group_position, group.disabled));
49 SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
50 if (option.nodeName.toUpperCase() === "OPTION") {
51 if (option.text !== "") {
52 if (group_position != null) {
53 this.parsed[group_position].children += 1;
56 array_index: this.parsed.length,
57 options_index: this.options_index,
60 html: option.innerHTML,
61 selected: option.selected,
62 disabled: group_disabled === true ? group_disabled : option.disabled,
63 group_array_index: group_position,
64 classes: option.className,
65 style: option.style.cssText
69 array_index: this.parsed.length,
70 options_index: this.options_index,
74 return this.options_index += 1;
78 SelectParser.prototype.escapeExpression = function(text) {
79 var map, unsafe_chars;
81 if ((text == null) || text === false) {
84 if (!/[\&\<\>\"\'\`]/.test(text)) {
94 unsafe_chars = /&(?!\w+;)|[\<\>\"\'\`]/g;
95 return text.replace(unsafe_chars, function(chr) {
96 return map[chr] || "&";
104 SelectParser.select_to_array = function(select) {
105 var child, parser, _i, _len, _ref;
107 parser = new SelectParser();
108 _ref = select.childNodes;
109 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
111 parser.add_node(child);
113 return parser.parsed;
116 AbstractChosen = (function() {
117 function AbstractChosen(form_field, options) {
118 this.form_field = form_field;
119 this.options = options != null ? options : {};
120 if (!AbstractChosen.browser_is_supported()) {
123 this.is_multiple = this.form_field.multiple;
124 this.set_default_text();
125 this.set_default_values();
128 this.register_observers();
131 AbstractChosen.prototype.set_default_values = function() {
134 this.click_test_action = function(evt) {
135 return _this.test_active_click(evt);
137 this.activate_action = function(evt) {
138 return _this.activate_field(evt);
140 this.active_field = false;
141 this.mouse_on_container = false;
142 this.results_showing = false;
143 this.result_highlighted = null;
144 this.result_single_selected = null;
145 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;
146 this.disable_search_threshold = this.options.disable_search_threshold || 0;
147 this.disable_search = this.options.disable_search || false;
148 this.enable_split_word_search = this.options.enable_split_word_search != null ? this.options.enable_split_word_search : true;
149 this.group_search = this.options.group_search != null ? this.options.group_search : true;
150 this.search_contains = this.options.search_contains || false;
151 this.single_backstroke_delete = this.options.single_backstroke_delete != null ? this.options.single_backstroke_delete : true;
152 this.max_selected_options = this.options.max_selected_options || Infinity;
153 this.inherit_select_classes = this.options.inherit_select_classes || false;
154 this.display_selected_options = this.options.display_selected_options != null ? this.options.display_selected_options : true;
155 return this.display_disabled_options = this.options.display_disabled_options != null ? this.options.display_disabled_options : true;
158 AbstractChosen.prototype.set_default_text = function() {
159 if (this.form_field.getAttribute("data-placeholder")) {
160 this.default_text = this.form_field.getAttribute("data-placeholder");
161 } else if (this.is_multiple) {
162 this.default_text = this.options.placeholder_text_multiple || this.options.placeholder_text || AbstractChosen.default_multiple_text;
164 this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || AbstractChosen.default_single_text;
166 return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || AbstractChosen.default_no_result_text;
169 AbstractChosen.prototype.mouse_enter = function() {
170 return this.mouse_on_container = true;
173 AbstractChosen.prototype.mouse_leave = function() {
174 return this.mouse_on_container = false;
177 AbstractChosen.prototype.input_focus = function(evt) {
180 if (this.is_multiple) {
181 if (!this.active_field) {
182 return setTimeout((function() {
183 return _this.container_mousedown();
187 if (!this.active_field) {
188 return this.activate_field();
193 AbstractChosen.prototype.input_blur = function(evt) {
196 if (!this.mouse_on_container) {
197 this.active_field = false;
198 return setTimeout((function() {
199 return _this.blur_test();
204 AbstractChosen.prototype.results_option_build = function(options) {
205 var content, data, _i, _len, _ref;
208 _ref = this.results_data;
209 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
212 content += this.result_add_group(data);
214 content += this.result_add_option(data);
216 if (options != null ? options.first : void 0) {
217 if (data.selected && this.is_multiple) {
218 this.choice_build(data);
219 } else if (data.selected && !this.is_multiple) {
220 this.single_set_selected_text(data.text);
227 AbstractChosen.prototype.result_add_option = function(option) {
230 if (!option.search_match) {
233 if (!this.include_option_in_results(option)) {
237 if (!option.disabled && !(option.selected && this.is_multiple)) {
238 classes.push("active-result");
240 if (option.disabled && !(option.selected && this.is_multiple)) {
241 classes.push("disabled-result");
243 if (option.selected) {
244 classes.push("result-selected");
246 if (option.group_array_index != null) {
247 classes.push("group-option");
249 if (option.classes !== "") {
250 classes.push(option.classes);
252 style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : "";
253 return "<li class=\"" + (classes.join(' ')) + "\"" + style + " data-option-array-index=\"" + option.array_index + "\">" + option.search_text + "</li>";
256 AbstractChosen.prototype.result_add_group = function(group) {
257 if (!(group.search_match || group.group_match)) {
260 if (!(group.active_options > 0)) {
263 return "<li class=\"group-result\">" + group.search_text + "</li>";
266 AbstractChosen.prototype.results_update_field = function() {
267 this.set_default_text();
268 if (!this.is_multiple) {
269 this.results_reset_cleanup();
271 this.result_clear_highlight();
272 this.result_single_selected = null;
273 this.results_build();
274 if (this.results_showing) {
275 return this.winnow_results();
279 AbstractChosen.prototype.results_toggle = function() {
280 if (this.results_showing) {
281 return this.results_hide();
283 return this.results_show();
287 AbstractChosen.prototype.results_search = function(evt) {
288 if (this.results_showing) {
289 return this.winnow_results();
291 return this.results_show();
295 AbstractChosen.prototype.winnow_results = function() {
296 var escapedSearchText, option, regex, regexAnchor, results, results_group, searchText, startpos, text, zregex, _i, _len, _ref;
298 this.no_results_clear();
300 searchText = this.get_search_text();
301 escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
302 regexAnchor = this.search_contains ? "" : "^";
303 regex = new RegExp(regexAnchor + escapedSearchText, 'i');
304 zregex = new RegExp(escapedSearchText, 'i');
305 _ref = this.results_data;
306 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
308 option.search_match = false;
309 results_group = null;
310 if (this.include_option_in_results(option)) {
312 option.group_match = false;
313 option.active_options = 0;
315 if ((option.group_array_index != null) && this.results_data[option.group_array_index]) {
316 results_group = this.results_data[option.group_array_index];
317 if (results_group.active_options === 0 && results_group.search_match) {
320 results_group.active_options += 1;
322 if (!(option.group && !this.group_search)) {
323 option.search_text = option.group ? option.label : option.html;
324 option.search_match = this.search_string_match(option.search_text, regex);
325 if (option.search_match && !option.group) {
328 if (option.search_match) {
329 if (searchText.length) {
330 startpos = option.search_text.search(zregex);
331 text = option.search_text.substr(0, startpos + searchText.length) + '</em>' + option.search_text.substr(startpos + searchText.length);
332 option.search_text = text.substr(0, startpos) + '<em>' + text.substr(startpos);
334 if (results_group != null) {
335 results_group.group_match = true;
337 } else if ((option.group_array_index != null) && this.results_data[option.group_array_index].search_match) {
338 option.search_match = true;
343 this.result_clear_highlight();
344 if (results < 1 && searchText.length) {
345 this.update_results_content("");
346 return this.no_results(searchText);
348 this.update_results_content(this.results_option_build());
349 return this.winnow_results_set_highlight();
353 AbstractChosen.prototype.search_string_match = function(search_string, regex) {
354 var part, parts, _i, _len;
356 if (regex.test(search_string)) {
358 } else if (this.enable_split_word_search && (search_string.indexOf(" ") >= 0 || search_string.indexOf("[") === 0)) {
359 parts = search_string.replace(/\[|\]/g, "").split(" ");
361 for (_i = 0, _len = parts.length; _i < _len; _i++) {
363 if (regex.test(part)) {
371 AbstractChosen.prototype.choices_count = function() {
372 var option, _i, _len, _ref;
374 if (this.selected_option_count != null) {
375 return this.selected_option_count;
377 this.selected_option_count = 0;
378 _ref = this.form_field.options;
379 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
381 if (option.selected) {
382 this.selected_option_count += 1;
385 return this.selected_option_count;
388 AbstractChosen.prototype.choices_click = function(evt) {
389 evt.preventDefault();
390 if (!(this.results_showing || this.is_disabled)) {
391 return this.results_show();
395 AbstractChosen.prototype.keyup_checker = function(evt) {
398 stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
399 this.search_field_scale();
402 if (this.is_multiple && this.backstroke_length < 1 && this.choices_count() > 0) {
403 return this.keydown_backstroke();
404 } else if (!this.pending_backstroke) {
405 this.result_clear_highlight();
406 return this.results_search();
410 evt.preventDefault();
411 if (this.results_showing) {
412 return this.result_select(evt);
416 if (this.results_showing) {
428 return this.results_search();
432 AbstractChosen.prototype.container_width = function() {
433 if (this.options.width != null) {
434 return this.options.width;
436 return "" + this.form_field.offsetWidth + "px";
440 AbstractChosen.prototype.include_option_in_results = function(option) {
441 if (this.is_multiple && (!this.display_selected_options && option.selected)) {
444 if (!this.display_disabled_options && option.disabled) {
453 AbstractChosen.browser_is_supported = function() {
454 if (window.navigator.appName === "Microsoft Internet Explorer") {
455 return document.documentMode >= 8;
457 if (/iP(od|hone)/i.test(window.navigator.userAgent)) {
460 if (/Android/i.test(window.navigator.userAgent)) {
461 if (/Mobile/i.test(window.navigator.userAgent)) {
468 AbstractChosen.default_multiple_text = "Select Some Options";
470 AbstractChosen.default_single_text = "Select an Option";
472 AbstractChosen.default_no_result_text = "No results match";
474 return AbstractChosen;
481 chosen: function(options) {
482 if (!AbstractChosen.browser_is_supported()) {
485 return this.each(function(input_field) {
489 chosen = $this.data('chosen');
490 if (options === 'destroy' && chosen) {
492 } else if (!chosen) {
493 $this.data('chosen', new Chosen(this, options));
499 Chosen = (function(_super) {
500 __extends(Chosen, _super);
503 _ref = Chosen.__super__.constructor.apply(this, arguments);
507 Chosen.prototype.setup = function() {
508 this.form_field_jq = $(this.form_field);
509 this.current_selectedIndex = this.form_field.selectedIndex;
510 return this.is_rtl = this.form_field_jq.hasClass("chosen-rtl");
513 Chosen.prototype.set_up_html = function() {
514 var container_classes, container_props;
516 container_classes = ["chosen-container"];
517 container_classes.push("chosen-container-" + (this.is_multiple ? "multi" : "single"));
518 if (this.inherit_select_classes && this.form_field.className) {
519 container_classes.push(this.form_field.className);
522 container_classes.push("chosen-rtl");
525 'class': container_classes.join(' '),
526 'style': "width: " + (this.container_width()) + ";",
527 'title': this.form_field.title
529 if (this.form_field.id.length) {
530 container_props.id = this.form_field.id.replace(/[^\w]/g, '_') + "_chosen";
532 this.container = $("<div />", container_props);
533 if (this.is_multiple) {
534 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>');
536 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>');
538 this.form_field_jq.hide().after(this.container);
539 this.dropdown = this.container.find('div.chosen-drop').first();
540 this.search_field = this.container.find('input').first();
541 this.search_results = this.container.find('ul.chosen-results').first();
542 this.search_field_scale();
543 this.search_no_results = this.container.find('li.no-results').first();
544 if (this.is_multiple) {
545 this.search_choices = this.container.find('ul.chosen-choices').first();
546 this.search_container = this.container.find('li.search-field').first();
548 this.search_container = this.container.find('div.chosen-search').first();
549 this.selected_item = this.container.find('.chosen-single').first();
551 this.results_build();
552 this.set_tab_index();
553 this.set_label_behavior();
554 return this.form_field_jq.trigger("chosen:ready", {
559 Chosen.prototype.register_observers = function() {
562 this.container.bind('mousedown.chosen', function(evt) {
563 _this.container_mousedown(evt);
565 this.container.bind('mouseup.chosen', function(evt) {
566 _this.container_mouseup(evt);
568 this.container.bind('mouseenter.chosen', function(evt) {
569 _this.mouse_enter(evt);
571 this.container.bind('mouseleave.chosen', function(evt) {
572 _this.mouse_leave(evt);
574 this.search_results.bind('mouseup.chosen', function(evt) {
575 _this.search_results_mouseup(evt);
577 this.search_results.bind('mouseover.chosen', function(evt) {
578 _this.search_results_mouseover(evt);
580 this.search_results.bind('mouseout.chosen', function(evt) {
581 _this.search_results_mouseout(evt);
583 this.search_results.bind('mousewheel.chosen DOMMouseScroll.chosen', function(evt) {
584 _this.search_results_mousewheel(evt);
586 this.form_field_jq.bind("chosen:updated.chosen", function(evt) {
587 _this.results_update_field(evt);
589 this.form_field_jq.bind("chosen:activate.chosen", function(evt) {
590 _this.activate_field(evt);
592 this.form_field_jq.bind("chosen:open.chosen", function(evt) {
593 _this.container_mousedown(evt);
595 this.search_field.bind('blur.chosen', function(evt) {
596 _this.input_blur(evt);
598 this.search_field.bind('keyup.chosen', function(evt) {
599 _this.keyup_checker(evt);
601 this.search_field.bind('keydown.chosen', function(evt) {
602 _this.keydown_checker(evt);
604 this.search_field.bind('focus.chosen', function(evt) {
605 _this.input_focus(evt);
607 if (this.is_multiple) {
608 return this.search_choices.bind('click.chosen', function(evt) {
609 _this.choices_click(evt);
612 return this.container.bind('click.chosen', function(evt) {
613 evt.preventDefault();
618 Chosen.prototype.destroy = function() {
619 $(document).unbind("click.chosen", this.click_test_action);
620 if (this.search_field[0].tabIndex) {
621 this.form_field_jq[0].tabIndex = this.search_field[0].tabIndex;
623 this.container.remove();
624 this.form_field_jq.removeData('chosen');
625 return this.form_field_jq.show();
628 Chosen.prototype.search_field_disabled = function() {
629 this.is_disabled = this.form_field_jq[0].disabled;
630 if (this.is_disabled) {
631 this.container.addClass('chosen-disabled');
632 this.search_field[0].disabled = true;
633 if (!this.is_multiple) {
634 this.selected_item.unbind("focus.chosen", this.activate_action);
636 return this.close_field();
638 this.container.removeClass('chosen-disabled');
639 this.search_field[0].disabled = false;
640 if (!this.is_multiple) {
641 return this.selected_item.bind("focus.chosen", this.activate_action);
646 Chosen.prototype.container_mousedown = function(evt) {
647 if (!this.is_disabled) {
648 if (evt && evt.type === "mousedown" && !this.results_showing) {
649 evt.preventDefault();
651 if (!((evt != null) && ($(evt.target)).hasClass("search-choice-close"))) {
652 if (!this.active_field) {
653 if (this.is_multiple) {
654 this.search_field.val("");
656 $(document).bind('click.chosen', this.click_test_action);
658 } else if (!this.is_multiple && evt && (($(evt.target)[0] === this.selected_item[0]) || $(evt.target).parents("a.chosen-single").length)) {
659 evt.preventDefault();
660 this.results_toggle();
662 return this.activate_field();
667 Chosen.prototype.container_mouseup = function(evt) {
668 if (evt.target.nodeName === "ABBR" && !this.is_disabled) {
669 return this.results_reset(evt);
673 Chosen.prototype.search_results_mousewheel = function(evt) {
674 var delta, _ref1, _ref2;
676 delta = -((_ref1 = evt.originalEvent) != null ? _ref1.wheelDelta : void 0) || ((_ref2 = evt.originialEvent) != null ? _ref2.detail : void 0);
678 evt.preventDefault();
679 if (evt.type === 'DOMMouseScroll') {
682 return this.search_results.scrollTop(delta + this.search_results.scrollTop());
686 Chosen.prototype.blur_test = function(evt) {
687 if (!this.active_field && this.container.hasClass("chosen-container-active")) {
688 return this.close_field();
692 Chosen.prototype.close_field = function() {
693 $(document).unbind("click.chosen", this.click_test_action);
694 this.active_field = false;
696 this.container.removeClass("chosen-container-active");
697 this.clear_backstroke();
698 this.show_search_field_default();
699 return this.search_field_scale();
702 Chosen.prototype.activate_field = function() {
703 this.container.addClass("chosen-container-active");
704 this.active_field = true;
705 this.search_field.val(this.search_field.val());
706 return this.search_field.focus();
709 Chosen.prototype.test_active_click = function(evt) {
710 if (this.container.is($(evt.target).closest('.chosen-container'))) {
711 return this.active_field = true;
713 return this.close_field();
717 Chosen.prototype.results_build = function() {
719 this.selected_option_count = null;
720 this.results_data = SelectParser.select_to_array(this.form_field);
721 if (this.is_multiple) {
722 this.search_choices.find("li.search-choice").remove();
723 } else if (!this.is_multiple) {
724 this.single_set_selected_text();
725 if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) {
726 this.search_field[0].readOnly = true;
727 this.container.addClass("chosen-container-single-nosearch");
729 this.search_field[0].readOnly = false;
730 this.container.removeClass("chosen-container-single-nosearch");
733 this.update_results_content(this.results_option_build({
736 this.search_field_disabled();
737 this.show_search_field_default();
738 this.search_field_scale();
739 return this.parsing = false;
742 Chosen.prototype.result_do_highlight = function(el) {
743 var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
746 this.result_clear_highlight();
747 this.result_highlight = el;
748 this.result_highlight.addClass("highlighted");
749 maxHeight = parseInt(this.search_results.css("maxHeight"), 10);
750 visible_top = this.search_results.scrollTop();
751 visible_bottom = maxHeight + visible_top;
752 high_top = this.result_highlight.position().top + this.search_results.scrollTop();
753 high_bottom = high_top + this.result_highlight.outerHeight();
754 if (high_bottom >= visible_bottom) {
755 return this.search_results.scrollTop((high_bottom - maxHeight) > 0 ? high_bottom - maxHeight : 0);
756 } else if (high_top < visible_top) {
757 return this.search_results.scrollTop(high_top);
762 Chosen.prototype.result_clear_highlight = function() {
763 if (this.result_highlight) {
764 this.result_highlight.removeClass("highlighted");
766 return this.result_highlight = null;
769 Chosen.prototype.results_show = function() {
770 if (this.is_multiple && this.max_selected_options <= this.choices_count()) {
771 this.form_field_jq.trigger("chosen:maxselected", {
776 this.container.addClass("chosen-with-drop");
777 this.form_field_jq.trigger("chosen:showing_dropdown", {
780 this.results_showing = true;
781 this.search_field.focus();
782 this.search_field.val(this.search_field.val());
783 return this.winnow_results();
786 Chosen.prototype.update_results_content = function(content) {
787 return this.search_results.html(content);
790 Chosen.prototype.results_hide = function() {
791 if (this.results_showing) {
792 this.result_clear_highlight();
793 this.container.removeClass("chosen-with-drop");
794 this.form_field_jq.trigger("chosen:hiding_dropdown", {
798 return this.results_showing = false;
801 Chosen.prototype.set_tab_index = function(el) {
804 if (this.form_field.tabIndex) {
805 ti = this.form_field.tabIndex;
806 this.form_field.tabIndex = -1;
807 return this.search_field[0].tabIndex = ti;
811 Chosen.prototype.set_label_behavior = function() {
814 this.form_field_label = this.form_field_jq.parents("label");
815 if (!this.form_field_label.length && this.form_field.id.length) {
816 this.form_field_label = $("label[for='" + this.form_field.id + "']");
818 if (this.form_field_label.length > 0) {
819 return this.form_field_label.bind('click.chosen', function(evt) {
820 if (_this.is_multiple) {
821 return _this.container_mousedown(evt);
823 return _this.activate_field();
829 Chosen.prototype.show_search_field_default = function() {
830 if (this.is_multiple && this.choices_count() < 1 && !this.active_field) {
831 this.search_field.val(this.default_text);
832 return this.search_field.addClass("default");
834 this.search_field.val("");
835 return this.search_field.removeClass("default");
839 Chosen.prototype.search_results_mouseup = function(evt) {
842 target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
844 this.result_highlight = target;
845 this.result_select(evt);
846 return this.search_field.focus();
850 Chosen.prototype.search_results_mouseover = function(evt) {
853 target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
855 return this.result_do_highlight(target);
859 Chosen.prototype.search_results_mouseout = function(evt) {
860 if ($(evt.target).hasClass("active-result" || $(evt.target).parents('.active-result').first())) {
861 return this.result_clear_highlight();
865 Chosen.prototype.choice_build = function(item) {
866 var choice, close_link,
869 choice = $('<li />', {
870 "class": "search-choice"
871 }).html("<span>" + item.html + "</span>");
873 choice.addClass('search-choice-disabled');
875 close_link = $('<a />', {
876 "class": 'search-choice-close',
877 'data-option-array-index': item.array_index
879 close_link.bind('click.chosen', function(evt) {
880 return _this.choice_destroy_link_click(evt);
882 choice.append(close_link);
884 return this.search_container.before(choice);
887 Chosen.prototype.choice_destroy_link_click = function(evt) {
888 evt.preventDefault();
889 evt.stopPropagation();
890 if (!this.is_disabled) {
891 return this.choice_destroy($(evt.target));
895 Chosen.prototype.choice_destroy = function(link) {
896 if (this.result_deselect(link[0].getAttribute("data-option-array-index"))) {
897 this.show_search_field_default();
898 if (this.is_multiple && this.choices_count() > 0 && this.search_field.val().length < 1) {
901 link.parents('li').first().remove();
902 return this.search_field_scale();
906 Chosen.prototype.results_reset = function() {
907 this.form_field.options[0].selected = true;
908 this.selected_option_count = null;
909 this.single_set_selected_text();
910 this.show_search_field_default();
911 this.results_reset_cleanup();
912 this.form_field_jq.trigger("change");
913 if (this.active_field) {
914 return this.results_hide();
918 Chosen.prototype.results_reset_cleanup = function() {
919 this.current_selectedIndex = this.form_field.selectedIndex;
920 return this.selected_item.find("abbr").remove();
923 Chosen.prototype.result_select = function(evt) {
924 var high, item, selected_index;
926 if (this.result_highlight) {
927 high = this.result_highlight;
928 this.result_clear_highlight();
929 if (this.is_multiple && this.max_selected_options <= this.choices_count()) {
930 this.form_field_jq.trigger("chosen:maxselected", {
935 if (this.is_multiple) {
936 high.removeClass("active-result");
938 if (this.result_single_selected) {
939 this.result_single_selected.removeClass("result-selected");
940 selected_index = this.result_single_selected[0].getAttribute('data-option-array-index');
941 this.results_data[selected_index].selected = false;
943 this.result_single_selected = high;
945 high.addClass("result-selected");
946 item = this.results_data[high[0].getAttribute("data-option-array-index")];
947 item.selected = true;
948 this.form_field.options[item.options_index].selected = true;
949 this.selected_option_count = null;
950 if (this.is_multiple) {
951 this.choice_build(item);
953 this.single_set_selected_text(item.text);
955 if (!((evt.metaKey || evt.ctrlKey) && this.is_multiple)) {
958 this.search_field.val("");
959 if (this.is_multiple || this.form_field.selectedIndex !== this.current_selectedIndex) {
960 this.form_field_jq.trigger("change", {
961 'selected': this.form_field.options[item.options_index].value
964 this.current_selectedIndex = this.form_field.selectedIndex;
965 return this.search_field_scale();
969 Chosen.prototype.single_set_selected_text = function(text) {
971 text = this.default_text;
973 if (text === this.default_text) {
974 this.selected_item.addClass("chosen-default");
976 this.single_deselect_control_build();
977 this.selected_item.removeClass("chosen-default");
979 return this.selected_item.find("span").text(text);
982 Chosen.prototype.result_deselect = function(pos) {
985 result_data = this.results_data[pos];
986 if (!this.form_field.options[result_data.options_index].disabled) {
987 result_data.selected = false;
988 this.form_field.options[result_data.options_index].selected = false;
989 this.selected_option_count = null;
990 this.result_clear_highlight();
991 if (this.results_showing) {
992 this.winnow_results();
994 this.form_field_jq.trigger("change", {
995 deselected: this.form_field.options[result_data.options_index].value
997 this.search_field_scale();
1004 Chosen.prototype.single_deselect_control_build = function() {
1005 if (!this.allow_single_deselect) {
1008 if (!this.selected_item.find("abbr").length) {
1009 this.selected_item.find("span").first().after("<abbr class=\"search-choice-close\"></abbr>");
1011 return this.selected_item.addClass("chosen-single-with-deselect");
1014 Chosen.prototype.get_search_text = function() {
1015 if (this.search_field.val() === this.default_text) {
1018 return $('<div/>').text($.trim(this.search_field.val())).html();
1022 Chosen.prototype.winnow_results_set_highlight = function() {
1023 var do_high, selected_results;
1025 selected_results = !this.is_multiple ? this.search_results.find(".result-selected.active-result") : [];
1026 do_high = selected_results.length ? selected_results.first() : this.search_results.find(".active-result").first();
1027 if (do_high != null) {
1028 return this.result_do_highlight(do_high);
1032 Chosen.prototype.no_results = function(terms) {
1033 var no_results_html;
1035 no_results_html = $('<li class="no-results">' + this.results_none_found + ' "<span></span>"</li>');
1036 no_results_html.find("span").first().html(terms);
1037 return this.search_results.append(no_results_html);
1040 Chosen.prototype.no_results_clear = function() {
1041 return this.search_results.find(".no-results").remove();
1044 Chosen.prototype.keydown_arrow = function() {
1047 if (this.results_showing && this.result_highlight) {
1048 next_sib = this.result_highlight.nextAll("li.active-result").first();
1050 return this.result_do_highlight(next_sib);
1053 return this.results_show();
1057 Chosen.prototype.keyup_arrow = function() {
1060 if (!this.results_showing && !this.is_multiple) {
1061 return this.results_show();
1062 } else if (this.result_highlight) {
1063 prev_sibs = this.result_highlight.prevAll("li.active-result");
1064 if (prev_sibs.length) {
1065 return this.result_do_highlight(prev_sibs.first());
1067 if (this.choices_count() > 0) {
1068 this.results_hide();
1070 return this.result_clear_highlight();
1075 Chosen.prototype.keydown_backstroke = function() {
1076 var next_available_destroy;
1078 if (this.pending_backstroke) {
1079 this.choice_destroy(this.pending_backstroke.find("a").first());
1080 return this.clear_backstroke();
1082 next_available_destroy = this.search_container.siblings("li.search-choice").last();
1083 if (next_available_destroy.length && !next_available_destroy.hasClass("search-choice-disabled")) {
1084 this.pending_backstroke = next_available_destroy;
1085 if (this.single_backstroke_delete) {
1086 return this.keydown_backstroke();
1088 return this.pending_backstroke.addClass("search-choice-focus");
1094 Chosen.prototype.clear_backstroke = function() {
1095 if (this.pending_backstroke) {
1096 this.pending_backstroke.removeClass("search-choice-focus");
1098 return this.pending_backstroke = null;
1101 Chosen.prototype.keydown_checker = function(evt) {
1104 stroke = (_ref1 = evt.which) != null ? _ref1 : evt.keyCode;
1105 this.search_field_scale();
1106 if (stroke !== 8 && this.pending_backstroke) {
1107 this.clear_backstroke();
1111 this.backstroke_length = this.search_field.val().length;
1114 if (this.results_showing && !this.is_multiple) {
1115 this.result_select(evt);
1117 this.mouse_on_container = false;
1120 evt.preventDefault();
1123 evt.preventDefault();
1127 evt.preventDefault();
1128 this.keydown_arrow();
1133 Chosen.prototype.search_field_scale = function() {
1134 var div, f_width, h, style, style_block, styles, w, _i, _len;
1136 if (this.is_multiple) {
1139 style_block = "position:absolute; left: -1000px; top: -1000px; display:none;";
1140 styles = ['font-size', 'font-style', 'font-weight', 'font-family', 'line-height', 'text-transform', 'letter-spacing'];
1141 for (_i = 0, _len = styles.length; _i < _len; _i++) {
1143 style_block += style + ":" + this.search_field.css(style) + ";";
1145 div = $('<div />', {
1146 'style': style_block
1148 div.text(this.search_field.val());
1149 $('body').append(div);
1150 w = div.width() + 25;
1152 f_width = this.container.outerWidth();
1153 if (w > f_width - 10) {
1156 return this.search_field.css({