1 // Chosen, a Select Box Enhancer for jQuery and Protoype
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 `cake build`, do not edit it by hand.
13 SelectParser = (function() {
15 function SelectParser() {
16 this.options_index = 0;
20 SelectParser.prototype.add_node = function(child) {
21 if (child.nodeName.toUpperCase() === "OPTGROUP") {
22 return this.add_group(child);
24 return this.add_option(child);
28 SelectParser.prototype.add_group = function(group) {
29 var group_position, option, _i, _len, _ref, _results;
30 group_position = this.parsed.length;
32 array_index: group_position,
36 disabled: group.disabled
38 _ref = group.childNodes;
40 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
42 _results.push(this.add_option(option, group_position, group.disabled));
47 SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
48 if (option.nodeName.toUpperCase() === "OPTION") {
49 if (option.text !== "") {
50 if (group_position != null) {
51 this.parsed[group_position].children += 1;
54 array_index: this.parsed.length,
55 options_index: this.options_index,
58 html: option.innerHTML,
59 selected: option.selected,
60 disabled: group_disabled === true ? group_disabled : option.disabled,
61 group_array_index: group_position,
62 classes: option.className,
63 style: option.style.cssText
67 array_index: this.parsed.length,
68 options_index: this.options_index,
72 return this.options_index += 1;
80 SelectParser.select_to_array = function(select) {
81 var child, parser, _i, _len, _ref;
82 parser = new SelectParser();
83 _ref = select.childNodes;
84 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
86 parser.add_node(child);
91 this.SelectParser = SelectParser;
96 Chosen source: generate output using 'cake build'
97 Copyright (c) 2011 by Harvest
102 var AbstractChosen, root;
106 AbstractChosen = (function() {
108 function AbstractChosen(form_field, options) {
109 this.form_field = form_field;
110 this.options = options != null ? options : {};
111 if (!AbstractChosen.browser_is_supported()) {
114 this.is_multiple = this.form_field.multiple;
115 this.set_default_text();
116 this.set_default_values();
119 this.register_observers();
123 AbstractChosen.prototype.set_default_values = function() {
125 this.click_test_action = function(evt) {
126 return _this.test_active_click(evt);
128 this.activate_action = function(evt) {
129 return _this.activate_field(evt);
131 this.active_field = false;
132 this.mouse_on_container = false;
133 this.results_showing = false;
134 this.result_highlighted = null;
135 this.result_single_selected = null;
136 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;
137 this.disable_search_threshold = this.options.disable_search_threshold || 0;
138 this.disable_search = this.options.disable_search || false;
139 this.enable_split_word_search = this.options.enable_split_word_search != null ? this.options.enable_split_word_search : true;
140 this.search_contains = this.options.search_contains || false;
142 this.single_backstroke_delete = this.options.single_backstroke_delete || false;
143 this.max_selected_options = this.options.max_selected_options || Infinity;
144 return this.inherit_select_classes = this.options.inherit_select_classes || false;
147 AbstractChosen.prototype.set_default_text = function() {
148 if (this.form_field.getAttribute("data-placeholder")) {
149 this.default_text = this.form_field.getAttribute("data-placeholder");
150 } else if (this.is_multiple) {
151 this.default_text = this.options.placeholder_text_multiple || this.options.placeholder_text || AbstractChosen.default_multiple_text;
153 this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || AbstractChosen.default_single_text;
155 return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || AbstractChosen.default_no_result_text;
158 AbstractChosen.prototype.mouse_enter = function() {
159 return this.mouse_on_container = true;
162 AbstractChosen.prototype.mouse_leave = function() {
163 return this.mouse_on_container = false;
166 AbstractChosen.prototype.input_focus = function(evt) {
168 if (this.is_multiple) {
169 if (!this.active_field) {
170 return setTimeout((function() {
171 return _this.container_mousedown();
175 if (!this.active_field) {
176 return this.activate_field();
181 AbstractChosen.prototype.input_blur = function(evt) {
183 if (!this.mouse_on_container) {
184 this.active_field = false;
185 return setTimeout((function() {
186 return _this.blur_test();
191 AbstractChosen.prototype.result_add_option = function(option) {
193 if (!option.disabled) {
194 option.dom_id = this.container_id + "_o_" + option.array_index;
195 classes = option.selected && this.is_multiple ? [] : ["active-result"];
196 if (option.selected) {
197 classes.push("result-selected");
199 if (option.group_array_index != null) {
200 classes.push("group-option");
202 if (option.classes !== "") {
203 classes.push(option.classes);
205 style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : "";
206 return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"' + style + '>' + option.html + '</li>';
212 AbstractChosen.prototype.results_update_field = function() {
213 this.set_default_text();
214 if (!this.is_multiple) {
215 this.results_reset_cleanup();
217 this.result_clear_highlight();
218 this.result_single_selected = null;
219 return this.results_build();
222 AbstractChosen.prototype.results_toggle = function() {
223 if (this.results_showing) {
224 return this.results_hide();
226 return this.results_show();
230 AbstractChosen.prototype.results_search = function(evt) {
231 if (this.results_showing) {
232 return this.winnow_results();
234 return this.results_show();
238 AbstractChosen.prototype.choices_click = function(evt) {
239 evt.preventDefault();
240 if (!this.results_showing) {
241 return this.results_show();
245 AbstractChosen.prototype.keyup_checker = function(evt) {
247 stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
248 this.search_field_scale();
251 if (this.is_multiple && this.backstroke_length < 1 && this.choices > 0) {
252 return this.keydown_backstroke();
253 } else if (!this.pending_backstroke) {
254 this.result_clear_highlight();
255 return this.results_search();
259 evt.preventDefault();
260 if (this.results_showing) {
261 return this.result_select(evt);
265 if (this.results_showing) {
277 return this.results_search();
281 AbstractChosen.prototype.generate_field_id = function() {
283 new_id = this.generate_random_id();
284 this.form_field.id = new_id;
288 AbstractChosen.prototype.generate_random_char = function() {
289 var chars, newchar, rand;
290 chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
291 rand = Math.floor(Math.random() * chars.length);
292 return newchar = chars.substring(rand, rand + 1);
295 AbstractChosen.prototype.container_width = function() {
297 if (this.options.width != null) {
298 return this.options.width;
300 width = window.getComputedStyle != null ? parseFloat(window.getComputedStyle(this.form_field).getPropertyValue('width')) : (typeof jQuery !== "undefined" && jQuery !== null) && (this.form_field_jq != null) ? this.form_field_jq.outerWidth() : this.form_field.getWidth();
304 AbstractChosen.browser_is_supported = function() {
306 if (window.navigator.appName === "Microsoft Internet Explorer") {
307 return (null !== (_ref = document.documentMode) && _ref >= 8);
312 AbstractChosen.default_multiple_text = "Select Some Options";
314 AbstractChosen.default_single_text = "Select an Option";
316 AbstractChosen.default_no_result_text = "No results match";
318 return AbstractChosen;
322 root.AbstractChosen = AbstractChosen;
327 Chosen source: generate output using 'cake build'
328 Copyright (c) 2011 by Harvest
334 __hasProp = {}.hasOwnProperty,
335 __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; };
342 chosen: function(options) {
343 if (!AbstractChosen.browser_is_supported()) {
346 return this.each(function(input_field) {
349 if (!$this.hasClass("chzn-done")) {
350 return $this.data('chosen', new Chosen(this, options));
356 Chosen = (function(_super) {
358 __extends(Chosen, _super);
361 return Chosen.__super__.constructor.apply(this, arguments);
364 Chosen.prototype.setup = function() {
365 this.form_field_jq = $(this.form_field);
366 this.current_selectedIndex = this.form_field.selectedIndex;
367 return this.is_rtl = this.form_field_jq.hasClass("chzn-rtl");
370 Chosen.prototype.finish_setup = function() {
371 return this.form_field_jq.addClass("chzn-done");
374 Chosen.prototype.set_up_html = function() {
375 var container_classes, container_props;
376 this.container_id = this.form_field.id.length ? this.form_field.id.replace(/[^\w]/g, '_') : this.generate_field_id();
377 this.container_id += "_chzn";
378 container_classes = ["chzn-container"];
379 container_classes.push("chzn-container-" + (this.is_multiple ? "multi" : "single"));
380 if (this.inherit_select_classes && this.form_field.className) {
381 container_classes.push(this.form_field.className);
384 container_classes.push("chzn-rtl");
387 'id': this.container_id,
388 'class': container_classes.join(' '),
389 'style': "width: " + (this.container_width()) + ";",
390 'title': this.form_field.title
392 this.container = $("<div />", container_props);
393 if (this.is_multiple) {
394 this.container.html('<ul class="chzn-choices"><li class="search-field"><input type="text" value="' + this.default_text + '" class="default" autocomplete="off" style="width:auto;" /></li></ul><div class="chzn-drop"><ul class="chzn-results"></ul></div>');
396 this.container.html('<a href="javascript:void(0)" class="chzn-single chzn-default" tabindex="-1"><span>' + this.default_text + '</span><div><b></b></div></a><div class="chzn-drop"><div class="chzn-search"><input type="text" autocomplete="off" /></div><ul class="chzn-results"></ul></div>');
398 this.form_field_jq.hide().after(this.container);
399 this.dropdown = this.container.find('div.chzn-drop').first();
400 this.search_field = this.container.find('input').first();
401 this.search_results = this.container.find('ul.chzn-results').first();
402 this.search_field_scale();
403 this.search_no_results = this.container.find('li.no-results').first();
404 if (this.is_multiple) {
405 this.search_choices = this.container.find('ul.chzn-choices').first();
406 this.search_container = this.container.find('li.search-field').first();
408 this.search_container = this.container.find('div.chzn-search').first();
409 this.selected_item = this.container.find('.chzn-single').first();
411 this.results_build();
412 this.set_tab_index();
413 this.set_label_behavior();
414 return this.form_field_jq.trigger("liszt:ready", {
419 Chosen.prototype.register_observers = function() {
421 this.container.mousedown(function(evt) {
422 _this.container_mousedown(evt);
424 this.container.mouseup(function(evt) {
425 _this.container_mouseup(evt);
427 this.container.mouseenter(function(evt) {
428 _this.mouse_enter(evt);
430 this.container.mouseleave(function(evt) {
431 _this.mouse_leave(evt);
433 this.search_results.mouseup(function(evt) {
434 _this.search_results_mouseup(evt);
436 this.search_results.mouseover(function(evt) {
437 _this.search_results_mouseover(evt);
439 this.search_results.mouseout(function(evt) {
440 _this.search_results_mouseout(evt);
442 this.search_results.bind('mousewheel DOMMouseScroll', function(evt) {
443 _this.search_results_mousewheel(evt);
445 this.form_field_jq.bind("liszt:updated", function(evt) {
446 _this.results_update_field(evt);
448 this.form_field_jq.bind("liszt:activate", function(evt) {
449 _this.activate_field(evt);
451 this.form_field_jq.bind("liszt:open", function(evt) {
452 _this.container_mousedown(evt);
454 this.search_field.blur(function(evt) {
455 _this.input_blur(evt);
457 this.search_field.keyup(function(evt) {
458 _this.keyup_checker(evt);
460 this.search_field.keydown(function(evt) {
461 _this.keydown_checker(evt);
463 this.search_field.focus(function(evt) {
464 _this.input_focus(evt);
466 if (this.is_multiple) {
467 return this.search_choices.click(function(evt) {
468 _this.choices_click(evt);
471 return this.container.click(function(evt) {
472 evt.preventDefault();
477 Chosen.prototype.search_field_disabled = function() {
478 this.is_disabled = this.form_field_jq[0].disabled;
479 if (this.is_disabled) {
480 this.container.addClass('chzn-disabled');
481 this.search_field[0].disabled = true;
482 if (!this.is_multiple) {
483 this.selected_item.unbind("focus", this.activate_action);
485 return this.close_field();
487 this.container.removeClass('chzn-disabled');
488 this.search_field[0].disabled = false;
489 if (!this.is_multiple) {
490 return this.selected_item.bind("focus", this.activate_action);
495 Chosen.prototype.container_mousedown = function(evt) {
496 if (!this.is_disabled) {
497 if (evt && evt.type === "mousedown" && !this.results_showing) {
498 evt.preventDefault();
500 if (!((evt != null) && ($(evt.target)).hasClass("search-choice-close"))) {
501 if (!this.active_field) {
502 if (this.is_multiple) {
503 this.search_field.val("");
505 $(document).click(this.click_test_action);
507 } else if (!this.is_multiple && evt && (($(evt.target)[0] === this.selected_item[0]) || $(evt.target).parents("a.chzn-single").length)) {
508 evt.preventDefault();
509 this.results_toggle();
511 return this.activate_field();
516 Chosen.prototype.container_mouseup = function(evt) {
517 if (evt.target.nodeName === "ABBR" && !this.is_disabled) {
518 return this.results_reset(evt);
522 Chosen.prototype.search_results_mousewheel = function(evt) {
523 var delta, _ref, _ref1;
524 delta = -((_ref = evt.originalEvent) != null ? _ref.wheelDelta : void 0) || ((_ref1 = evt.originialEvent) != null ? _ref1.detail : void 0);
526 evt.preventDefault();
527 if (evt.type === 'DOMMouseScroll') {
530 return this.search_results.scrollTop(delta + this.search_results.scrollTop());
534 Chosen.prototype.blur_test = function(evt) {
535 if (!this.active_field && this.container.hasClass("chzn-container-active")) {
536 return this.close_field();
540 Chosen.prototype.close_field = function() {
541 $(document).unbind("click", this.click_test_action);
542 this.active_field = false;
544 this.container.removeClass("chzn-container-active");
545 this.winnow_results_clear();
546 this.clear_backstroke();
547 this.show_search_field_default();
548 return this.search_field_scale();
551 Chosen.prototype.activate_field = function() {
552 this.container.addClass("chzn-container-active");
553 this.active_field = true;
554 this.search_field.val(this.search_field.val());
555 return this.search_field.focus();
558 Chosen.prototype.test_active_click = function(evt) {
559 if ($(evt.target).parents('#' + this.container_id).length) {
560 return this.active_field = true;
562 return this.close_field();
566 Chosen.prototype.results_build = function() {
567 var content, data, _i, _len, _ref;
569 this.results_data = root.SelectParser.select_to_array(this.form_field);
570 if (this.is_multiple && this.choices > 0) {
571 this.search_choices.find("li.search-choice").remove();
573 } else if (!this.is_multiple) {
574 this.selected_item.addClass("chzn-default").find("span").text(this.default_text);
575 if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) {
576 this.container.addClass("chzn-container-single-nosearch");
578 this.container.removeClass("chzn-container-single-nosearch");
582 _ref = this.results_data;
583 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
586 content += this.result_add_group(data);
587 } else if (!data.empty) {
588 content += this.result_add_option(data);
589 if (data.selected && this.is_multiple) {
590 this.choice_build(data);
591 } else if (data.selected && !this.is_multiple) {
592 this.selected_item.removeClass("chzn-default").find("span").text(data.text);
593 if (this.allow_single_deselect) {
594 this.single_deselect_control_build();
599 this.search_field_disabled();
600 this.show_search_field_default();
601 this.search_field_scale();
602 this.search_results.html(content);
603 return this.parsing = false;
606 Chosen.prototype.result_add_group = function(group) {
607 if (!group.disabled) {
608 group.dom_id = this.container_id + "_g_" + group.array_index;
609 return '<li id="' + group.dom_id + '" class="group-result">' + $("<div />").text(group.label).html() + '</li>';
615 Chosen.prototype.result_do_highlight = function(el) {
616 var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
618 this.result_clear_highlight();
619 this.result_highlight = el;
620 this.result_highlight.addClass("highlighted");
621 maxHeight = parseInt(this.search_results.css("maxHeight"), 10);
622 visible_top = this.search_results.scrollTop();
623 visible_bottom = maxHeight + visible_top;
624 high_top = this.result_highlight.position().top + this.search_results.scrollTop();
625 high_bottom = high_top + this.result_highlight.outerHeight();
626 if (high_bottom >= visible_bottom) {
627 return this.search_results.scrollTop((high_bottom - maxHeight) > 0 ? high_bottom - maxHeight : 0);
628 } else if (high_top < visible_top) {
629 return this.search_results.scrollTop(high_top);
634 Chosen.prototype.result_clear_highlight = function() {
635 if (this.result_highlight) {
636 this.result_highlight.removeClass("highlighted");
638 return this.result_highlight = null;
641 Chosen.prototype.results_show = function() {
642 if (this.result_single_selected != null) {
643 this.result_do_highlight(this.result_single_selected);
644 } else if (this.is_multiple && this.max_selected_options <= this.choices) {
645 this.form_field_jq.trigger("liszt:maxselected", {
650 this.container.addClass("chzn-with-drop");
651 this.form_field_jq.trigger("liszt:showing_dropdown", {
654 this.results_showing = true;
655 this.search_field.focus();
656 this.search_field.val(this.search_field.val());
657 return this.winnow_results();
660 Chosen.prototype.results_hide = function() {
661 this.result_clear_highlight();
662 this.container.removeClass("chzn-with-drop");
663 this.form_field_jq.trigger("liszt:hiding_dropdown", {
666 return this.results_showing = false;
669 Chosen.prototype.set_tab_index = function(el) {
671 if (this.form_field_jq.attr("tabindex")) {
672 ti = this.form_field_jq.attr("tabindex");
673 this.form_field_jq.attr("tabindex", -1);
674 return this.search_field.attr("tabindex", ti);
678 Chosen.prototype.set_label_behavior = function() {
680 this.form_field_label = this.form_field_jq.parents("label");
681 if (!this.form_field_label.length && this.form_field.id.length) {
682 this.form_field_label = $("label[for=" + this.form_field.id + "]");
684 if (this.form_field_label.length > 0) {
685 return this.form_field_label.click(function(evt) {
686 if (_this.is_multiple) {
687 return _this.container_mousedown(evt);
689 return _this.activate_field();
695 Chosen.prototype.show_search_field_default = function() {
696 if (this.is_multiple && this.choices < 1 && !this.active_field) {
697 this.search_field.val(this.default_text);
698 return this.search_field.addClass("default");
700 this.search_field.val("");
701 return this.search_field.removeClass("default");
705 Chosen.prototype.search_results_mouseup = function(evt) {
707 target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
709 this.result_highlight = target;
710 this.result_select(evt);
711 return this.search_field.focus();
715 Chosen.prototype.search_results_mouseover = function(evt) {
717 target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
719 return this.result_do_highlight(target);
723 Chosen.prototype.search_results_mouseout = function(evt) {
724 if ($(evt.target).hasClass("active-result" || $(evt.target).parents('.active-result').first())) {
725 return this.result_clear_highlight();
729 Chosen.prototype.choice_build = function(item) {
730 var choice_id, html, link,
732 if (this.is_multiple && this.max_selected_options <= this.choices) {
733 this.form_field_jq.trigger("liszt:maxselected", {
738 choice_id = this.container_id + "_c_" + item.array_index;
741 html = '<li class="search-choice search-choice-disabled" id="' + choice_id + '"><span>' + item.html + '</span></li>';
743 html = '<li class="search-choice" id="' + choice_id + '"><span>' + item.html + '</span><a href="javascript:void(0)" class="search-choice-close" rel="' + item.array_index + '"></a></li>';
745 this.search_container.before(html);
746 link = $('#' + choice_id).find("a").first();
747 return link.click(function(evt) {
748 return _this.choice_destroy_link_click(evt);
752 Chosen.prototype.choice_destroy_link_click = function(evt) {
753 evt.preventDefault();
754 evt.stopPropagation();
755 if (!this.is_disabled) {
756 return this.choice_destroy($(evt.target));
760 Chosen.prototype.choice_destroy = function(link) {
761 if (this.result_deselect(link.attr("rel"))) {
763 this.show_search_field_default();
764 if (this.is_multiple && this.choices > 0 && this.search_field.val().length < 1) {
767 link.parents('li').first().remove();
768 return this.search_field_scale();
772 Chosen.prototype.results_reset = function() {
773 this.form_field.options[0].selected = true;
774 this.selected_item.find("span").text(this.default_text);
775 if (!this.is_multiple) {
776 this.selected_item.addClass("chzn-default");
778 this.show_search_field_default();
779 this.results_reset_cleanup();
780 this.form_field_jq.trigger("change");
781 if (this.active_field) {
782 return this.results_hide();
786 Chosen.prototype.results_reset_cleanup = function() {
787 this.current_selectedIndex = this.form_field.selectedIndex;
788 return this.selected_item.find("abbr").remove();
791 Chosen.prototype.result_select = function(evt) {
792 var high, high_id, item, position;
793 if (this.result_highlight) {
794 high = this.result_highlight;
795 high_id = high.attr("id");
796 this.result_clear_highlight();
797 if (this.is_multiple) {
798 this.result_deactivate(high);
800 this.search_results.find(".result-selected").removeClass("result-selected");
801 this.result_single_selected = high;
802 this.selected_item.removeClass("chzn-default");
804 high.addClass("result-selected");
805 position = high_id.substr(high_id.lastIndexOf("_") + 1);
806 item = this.results_data[position];
807 item.selected = true;
808 this.form_field.options[item.options_index].selected = true;
809 if (this.is_multiple) {
810 this.choice_build(item);
812 this.selected_item.find("span").first().text(item.text);
813 if (this.allow_single_deselect) {
814 this.single_deselect_control_build();
817 if (!((evt.metaKey || evt.ctrlKey) && this.is_multiple)) {
820 this.search_field.val("");
821 if (this.is_multiple || this.form_field.selectedIndex !== this.current_selectedIndex) {
822 this.form_field_jq.trigger("change", {
823 'selected': this.form_field.options[item.options_index].value
826 this.current_selectedIndex = this.form_field.selectedIndex;
827 return this.search_field_scale();
831 Chosen.prototype.result_activate = function(el) {
832 return el.addClass("active-result");
835 Chosen.prototype.result_deactivate = function(el) {
836 return el.removeClass("active-result");
839 Chosen.prototype.result_deselect = function(pos) {
840 var result, result_data;
841 result_data = this.results_data[pos];
842 if (!this.form_field.options[result_data.options_index].disabled) {
843 result_data.selected = false;
844 this.form_field.options[result_data.options_index].selected = false;
845 result = $("#" + this.container_id + "_o_" + pos);
846 result.removeClass("result-selected").addClass("active-result").show();
847 this.result_clear_highlight();
848 this.winnow_results();
849 this.form_field_jq.trigger("change", {
850 deselected: this.form_field.options[result_data.options_index].value
852 this.search_field_scale();
859 Chosen.prototype.single_deselect_control_build = function() {
860 if (this.allow_single_deselect && this.selected_item.find("abbr").length < 1) {
861 return this.selected_item.find("span").first().after("<abbr class=\"search-choice-close\"></abbr>");
865 Chosen.prototype.winnow_results = function() {
866 var found, option, part, parts, regex, regexAnchor, result, result_id, results, searchText, startpos, text, zregex, _i, _j, _len, _len1, _ref;
867 this.no_results_clear();
869 searchText = this.search_field.val() === this.default_text ? "" : $('<div/>').text($.trim(this.search_field.val())).html();
870 regexAnchor = this.search_contains ? "" : "^";
871 regex = new RegExp(regexAnchor + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
872 zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
873 _ref = this.results_data;
874 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
876 if (!option.disabled && !option.empty) {
878 $('#' + option.dom_id).css('display', 'none');
879 } else if (!(this.is_multiple && option.selected)) {
881 result_id = option.dom_id;
882 result = $("#" + result_id);
883 if (regex.test(option.html)) {
886 } else if (this.enable_split_word_search && (option.html.indexOf(" ") >= 0 || option.html.indexOf("[") === 0)) {
887 parts = option.html.replace(/\[|\]/g, "").split(" ");
889 for (_j = 0, _len1 = parts.length; _j < _len1; _j++) {
891 if (regex.test(part)) {
899 if (searchText.length) {
900 startpos = option.html.search(zregex);
901 text = option.html.substr(0, startpos + searchText.length) + '</em>' + option.html.substr(startpos + searchText.length);
902 text = text.substr(0, startpos) + '<em>' + text.substr(startpos);
907 this.result_activate(result);
908 if (option.group_array_index != null) {
909 $("#" + this.results_data[option.group_array_index].dom_id).css('display', 'list-item');
912 if (this.result_highlight && result_id === this.result_highlight.attr('id')) {
913 this.result_clear_highlight();
915 this.result_deactivate(result);
920 if (results < 1 && searchText.length) {
921 return this.no_results(searchText);
923 return this.winnow_results_set_highlight();
927 Chosen.prototype.winnow_results_clear = function() {
928 var li, lis, _i, _len, _results;
929 this.search_field.val("");
930 lis = this.search_results.find("li");
932 for (_i = 0, _len = lis.length; _i < _len; _i++) {
935 if (li.hasClass("group-result")) {
936 _results.push(li.css('display', 'auto'));
937 } else if (!this.is_multiple || !li.hasClass("result-selected")) {
938 _results.push(this.result_activate(li));
940 _results.push(void 0);
946 Chosen.prototype.winnow_results_set_highlight = function() {
947 var do_high, selected_results;
948 if (!this.result_highlight) {
949 selected_results = !this.is_multiple ? this.search_results.find(".result-selected.active-result") : [];
950 do_high = selected_results.length ? selected_results.first() : this.search_results.find(".active-result").first();
951 if (do_high != null) {
952 return this.result_do_highlight(do_high);
957 Chosen.prototype.no_results = function(terms) {
959 no_results_html = $('<li class="no-results">' + this.results_none_found + ' "<span></span>"</li>');
960 no_results_html.find("span").first().html(terms);
961 return this.search_results.append(no_results_html);
964 Chosen.prototype.no_results_clear = function() {
965 return this.search_results.find(".no-results").remove();
968 Chosen.prototype.keydown_arrow = function() {
969 var first_active, next_sib;
970 if (!this.result_highlight) {
971 first_active = this.search_results.find("li.active-result").first();
973 this.result_do_highlight($(first_active));
975 } else if (this.results_showing) {
976 next_sib = this.result_highlight.nextAll("li.active-result").first();
978 this.result_do_highlight(next_sib);
981 if (!this.results_showing) {
982 return this.results_show();
986 Chosen.prototype.keyup_arrow = function() {
988 if (!this.results_showing && !this.is_multiple) {
989 return this.results_show();
990 } else if (this.result_highlight) {
991 prev_sibs = this.result_highlight.prevAll("li.active-result");
992 if (prev_sibs.length) {
993 return this.result_do_highlight(prev_sibs.first());
995 if (this.choices > 0) {
998 return this.result_clear_highlight();
1003 Chosen.prototype.keydown_backstroke = function() {
1004 var next_available_destroy;
1005 if (this.pending_backstroke) {
1006 this.choice_destroy(this.pending_backstroke.find("a").first());
1007 return this.clear_backstroke();
1009 next_available_destroy = this.search_container.siblings("li.search-choice").last();
1010 if (next_available_destroy.length && !next_available_destroy.hasClass("search-choice-disabled")) {
1011 this.pending_backstroke = next_available_destroy;
1012 if (this.single_backstroke_delete) {
1013 return this.keydown_backstroke();
1015 return this.pending_backstroke.addClass("search-choice-focus");
1021 Chosen.prototype.clear_backstroke = function() {
1022 if (this.pending_backstroke) {
1023 this.pending_backstroke.removeClass("search-choice-focus");
1025 return this.pending_backstroke = null;
1028 Chosen.prototype.keydown_checker = function(evt) {
1030 stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
1031 this.search_field_scale();
1032 if (stroke !== 8 && this.pending_backstroke) {
1033 this.clear_backstroke();
1037 this.backstroke_length = this.search_field.val().length;
1040 if (this.results_showing && !this.is_multiple) {
1041 this.result_select(evt);
1043 this.mouse_on_container = false;
1046 evt.preventDefault();
1049 evt.preventDefault();
1053 this.keydown_arrow();
1058 Chosen.prototype.search_field_scale = function() {
1059 var div, h, style, style_block, styles, w, _i, _len;
1060 if (this.is_multiple) {
1063 style_block = "position:absolute; left: -1000px; top: -1000px; display:none;";
1064 styles = ['font-size', 'font-style', 'font-weight', 'font-family', 'line-height', 'text-transform', 'letter-spacing'];
1065 for (_i = 0, _len = styles.length; _i < _len; _i++) {
1067 style_block += style + ":" + this.search_field.css(style) + ";";
1069 div = $('<div />', {
1070 'style': style_block
1072 div.text(this.search_field.val());
1073 $('body').append(div);
1074 w = div.width() + 25;
1076 if (!this.f_width) {
1077 this.f_width = this.container.outerWidth();
1079 if (w > this.f_width - 10) {
1080 w = this.f_width - 10;
1082 return this.search_field.css({
1088 Chosen.prototype.generate_random_id = function() {
1090 string = "sel" + this.generate_random_char() + this.generate_random_char() + this.generate_random_char();
1091 while ($("#" + string).length > 0) {
1092 string += this.generate_random_char();
1101 root.Chosen = Chosen;