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 this.is_multiple
= this.form_field
.multiple
;
112 this.set_default_text();
113 this.set_default_values();
116 this.register_observers();
120 AbstractChosen
.prototype.set_default_values = function() {
122 this.click_test_action = function(evt
) {
123 return _this
.test_active_click(evt
);
125 this.activate_action = function(evt
) {
126 return _this
.activate_field(evt
);
128 this.active_field
= false;
129 this.mouse_on_container
= false;
130 this.results_showing
= false;
131 this.result_highlighted
= null;
132 this.result_single_selected
= null;
133 this.allow_single_deselect
= (this.options
.allow_single_deselect
!= null) && (this.form_field
.options
[0] != null) && this.form_field
.options
[0].text
=== "" ? this.options
.allow_single_deselect
: false;
134 this.disable_search_threshold
= this.options
.disable_search_threshold
|| 0;
135 this.disable_search
= this.options
.disable_search
|| false;
136 this.enable_split_word_search
= this.options
.enable_split_word_search
!= null ? this.options
.enable_split_word_search
: true;
137 this.search_contains
= this.options
.search_contains
|| false;
139 this.single_backstroke_delete
= this.options
.single_backstroke_delete
|| false;
140 this.max_selected_options
= this.options
.max_selected_options
|| Infinity
;
141 return this.inherit_select_classes
= this.options
.inherit_select_classes
|| false;
144 AbstractChosen
.prototype.set_default_text = function() {
145 if (this.form_field
.getAttribute("data-placeholder")) {
146 this.default_text
= this.form_field
.getAttribute("data-placeholder");
147 } else if (this.is_multiple
) {
148 this.default_text
= this.options
.placeholder_text_multiple
|| this.options
.placeholder_text
|| "Select Some Options";
150 this.default_text
= this.options
.placeholder_text_single
|| this.options
.placeholder_text
|| "Select an Option";
152 return this.results_none_found
= this.form_field
.getAttribute("data-no_results_text") || this.options
.no_results_text
|| "No results match";
155 AbstractChosen
.prototype.mouse_enter = function() {
156 return this.mouse_on_container
= true;
159 AbstractChosen
.prototype.mouse_leave = function() {
160 return this.mouse_on_container
= false;
163 AbstractChosen
.prototype.input_focus = function(evt
) {
165 if (this.is_multiple
) {
166 if (!this.active_field
) {
167 return setTimeout((function() {
168 return _this
.container_mousedown();
172 if (!this.active_field
) {
173 return this.activate_field();
178 AbstractChosen
.prototype.input_blur = function(evt
) {
180 if (!this.mouse_on_container
) {
181 this.active_field
= false;
182 return setTimeout((function() {
183 return _this
.blur_test();
188 AbstractChosen
.prototype.result_add_option = function(option
) {
190 if (!option
.disabled
) {
191 option
.dom_id
= this.container_id
+ "_o_" + option
.array_index
;
192 classes
= option
.selected
&& this.is_multiple
? [] : ["active-result"];
193 if (option
.selected
) {
194 classes
.push("result-selected");
196 if (option
.group_array_index
!= null) {
197 classes
.push("group-option");
199 if (option
.classes
!== "") {
200 classes
.push(option
.classes
);
202 style
= option
.style
.cssText
!== "" ? " style=\"" + option
.style
+ "\"" : "";
203 return '<li id="' + option
.dom_id
+ '" class="' + classes
.join(' ') + '"' + style
+ '>' + option
.html
+ '</li>';
209 AbstractChosen
.prototype.results_update_field = function() {
210 this.set_default_text();
211 if (!this.is_multiple
) {
212 this.results_reset_cleanup();
214 this.result_clear_highlight();
215 this.result_single_selected
= null;
216 return this.results_build();
219 AbstractChosen
.prototype.results_toggle = function() {
220 if (this.results_showing
) {
221 return this.results_hide();
223 return this.results_show();
227 AbstractChosen
.prototype.results_search = function(evt
) {
228 if (this.results_showing
) {
229 return this.winnow_results();
231 return this.results_show();
235 AbstractChosen
.prototype.keyup_checker = function(evt
) {
237 stroke
= (_ref
= evt
.which
) != null ? _ref
: evt
.keyCode
;
238 this.search_field_scale();
241 if (this.is_multiple
&& this.backstroke_length
< 1 && this.choices
> 0) {
242 return this.keydown_backstroke();
243 } else if (!this.pending_backstroke
) {
244 this.result_clear_highlight();
245 return this.results_search();
249 evt
.preventDefault();
250 if (this.results_showing
) {
251 return this.result_select(evt
);
255 if (this.results_showing
) {
267 return this.results_search();
271 AbstractChosen
.prototype.generate_field_id = function() {
273 new_id
= this.generate_random_id();
274 this.form_field
.id
= new_id
;
278 AbstractChosen
.prototype.generate_random_char = function() {
279 var chars
, newchar
, rand
;
280 chars
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
281 rand
= Math
.floor(Math
.random() * chars
.length
);
282 return newchar
= chars
.substring(rand
, rand
+ 1);
285 return AbstractChosen
;
289 root
.AbstractChosen
= AbstractChosen
;
294 Chosen source: generate output using 'cake build'
295 Copyright (c) 2011 by Harvest
300 var $, Chosen
, get_side_border_padding
, root
,
301 __hasProp
= {}.hasOwnProperty
,
302 __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
; };
309 chosen: function(options
) {
310 var browser
, match
, ua
;
311 ua
= navigator
.userAgent
.toLowerCase();
312 match
= /(msie) ([\w.]+)/.exec(ua
) || [];
314 name
: match
[1] || "",
315 version
: match
[2] || "0"
317 if (browser
.name
=== "msie" && (browser
.version
=== "6.0" || (browser
.version
=== "7.0" && document
.documentMode
=== 7))) {
320 return this.each(function(input_field
) {
323 if (!$this.hasClass("chzn-done")) {
324 return $this.data('chosen', new Chosen(this, options
));
330 Chosen
= (function(_super
) {
332 __extends(Chosen
, _super
);
335 return Chosen
.__super__
.constructor.apply(this, arguments
);
338 Chosen
.prototype.setup = function() {
339 this.form_field_jq
= $(this.form_field
);
340 this.current_value
= this.form_field_jq
.val();
341 return this.is_rtl
= this.form_field_jq
.hasClass("chzn-rtl");
344 Chosen
.prototype.finish_setup = function() {
345 return this.form_field_jq
.addClass("chzn-done");
348 Chosen
.prototype.set_up_html = function() {
349 var container_classes
, container_div
, container_props
, dd_top
, dd_width
, sf_width
;
350 this.container_id
= this.form_field
.id
.length
? this.form_field
.id
.replace(/[^\w]/g, '_') : this.generate_field_id();
351 this.container_id
+= "_chzn";
352 container_classes
= ["chzn-container"];
353 container_classes
.push("chzn-container-" + (this.is_multiple
? "multi" : "single"));
354 if (this.inherit_select_classes
&& this.form_field
.className
) {
355 container_classes
.push(this.form_field
.className
);
358 container_classes
.push("chzn-rtl");
360 this.f_width
= this.form_field_jq
.outerWidth();
362 id
: this.container_id
,
363 "class": container_classes
.join(' '),
364 style
: 'width: ' + this.f_width
+ 'px;',
365 title
: this.form_field
.title
367 container_div
= $("<div />", container_props
);
368 if (this.is_multiple
) {
369 container_div
.html('<ul class="chzn-choices"><li class="search-field"><input type="text" value="' + this.default_text
+ '" class="default" autocomplete="off" style="width:25px;" /></li></ul><div class="chzn-drop" style="left:-9000px;"><ul class="chzn-results"></ul></div>');
371 container_div
.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" style="left:-9000px;"><div class="chzn-search"><input type="text" autocomplete="off" /></div><ul class="chzn-results"></ul></div>');
373 this.form_field_jq
.hide().after(container_div
);
374 this.container
= $('#' + this.container_id
);
375 this.dropdown
= this.container
.find('div.chzn-drop').first();
376 dd_top
= this.container
.height();
377 dd_width
= this.f_width
- get_side_border_padding(this.dropdown
);
379 "width": dd_width
+ "px",
382 this.search_field
= this.container
.find('input').first();
383 this.search_results
= this.container
.find('ul.chzn-results').first();
384 this.search_field_scale();
385 this.search_no_results
= this.container
.find('li.no-results').first();
386 if (this.is_multiple
) {
387 this.search_choices
= this.container
.find('ul.chzn-choices').first();
388 this.search_container
= this.container
.find('li.search-field').first();
390 this.search_container
= this.container
.find('div.chzn-search').first();
391 this.selected_item
= this.container
.find('.chzn-single').first();
392 sf_width
= dd_width
- get_side_border_padding(this.search_container
) - get_side_border_padding(this.search_field
);
393 this.search_field
.css({
394 "width": sf_width
+ "px"
397 this.results_build();
398 this.set_tab_index();
399 return this.form_field_jq
.trigger("liszt:ready", {
404 Chosen
.prototype.register_observers = function() {
406 this.container
.mousedown(function(evt
) {
407 _this
.container_mousedown(evt
);
409 this.container
.mouseup(function(evt
) {
410 _this
.container_mouseup(evt
);
412 this.container
.mouseenter(function(evt
) {
413 _this
.mouse_enter(evt
);
415 this.container
.mouseleave(function(evt
) {
416 _this
.mouse_leave(evt
);
418 this.search_results
.mouseup(function(evt
) {
419 _this
.search_results_mouseup(evt
);
421 this.search_results
.mouseover(function(evt
) {
422 _this
.search_results_mouseover(evt
);
424 this.search_results
.mouseout(function(evt
) {
425 _this
.search_results_mouseout(evt
);
427 this.form_field_jq
.bind("liszt:updated", function(evt
) {
428 _this
.results_update_field(evt
);
430 this.form_field_jq
.bind("liszt:activate", function(evt
) {
431 _this
.activate_field(evt
);
433 this.form_field_jq
.bind("liszt:open", function(evt
) {
434 _this
.container_mousedown(evt
);
436 this.search_field
.blur(function(evt
) {
437 _this
.input_blur(evt
);
439 this.search_field
.keyup(function(evt
) {
440 _this
.keyup_checker(evt
);
442 this.search_field
.keydown(function(evt
) {
443 _this
.keydown_checker(evt
);
445 this.search_field
.focus(function(evt
) {
446 _this
.input_focus(evt
);
448 if (this.is_multiple
) {
449 return this.search_choices
.click(function(evt
) {
450 _this
.choices_click(evt
);
453 return this.container
.click(function(evt
) {
454 evt
.preventDefault();
459 Chosen
.prototype.search_field_disabled = function() {
460 this.is_disabled
= this.form_field_jq
[0].disabled
;
461 if (this.is_disabled
) {
462 this.container
.addClass('chzn-disabled');
463 this.search_field
[0].disabled
= true;
464 if (!this.is_multiple
) {
465 this.selected_item
.unbind("focus", this.activate_action
);
467 return this.close_field();
469 this.container
.removeClass('chzn-disabled');
470 this.search_field
[0].disabled
= false;
471 if (!this.is_multiple
) {
472 return this.selected_item
.bind("focus", this.activate_action
);
477 Chosen
.prototype.container_mousedown = function(evt
) {
478 var target_closelink
;
479 if (!this.is_disabled
) {
480 target_closelink
= evt
!= null ? ($(evt
.target
)).hasClass("search-choice-close") : false;
481 if (evt
&& evt
.type
=== "mousedown" && !this.results_showing
) {
482 evt
.preventDefault();
484 if (!this.pending_destroy_click
&& !target_closelink
) {
485 if (!this.active_field
) {
486 if (this.is_multiple
) {
487 this.search_field
.val("");
489 $(document
).click(this.click_test_action
);
491 } else if (!this.is_multiple
&& evt
&& (($(evt
.target
)[0] === this.selected_item
[0]) || $(evt
.target
).parents("a.chzn-single").length
)) {
492 evt
.preventDefault();
493 this.results_toggle();
495 return this.activate_field();
497 return this.pending_destroy_click
= false;
502 Chosen
.prototype.container_mouseup = function(evt
) {
503 if (evt
.target
.nodeName
=== "ABBR" && !this.is_disabled
) {
504 return this.results_reset(evt
);
508 Chosen
.prototype.blur_test = function(evt
) {
509 if (!this.active_field
&& this.container
.hasClass("chzn-container-active")) {
510 return this.close_field();
514 Chosen
.prototype.close_field = function() {
515 $(document
).unbind("click", this.click_test_action
);
516 this.active_field
= false;
518 this.container
.removeClass("chzn-container-active");
519 this.winnow_results_clear();
520 this.clear_backstroke();
521 this.show_search_field_default();
522 return this.search_field_scale();
525 Chosen
.prototype.activate_field = function() {
526 this.container
.addClass("chzn-container-active");
527 this.active_field
= true;
528 this.search_field
.val(this.search_field
.val());
529 return this.search_field
.focus();
532 Chosen
.prototype.test_active_click = function(evt
) {
533 if ($(evt
.target
).parents('#' + this.container_id
).length
) {
534 return this.active_field
= true;
536 return this.close_field();
540 Chosen
.prototype.results_build = function() {
541 var content
, data
, _i
, _len
, _ref
;
543 this.results_data
= root
.SelectParser
.select_to_array(this.form_field
);
544 if (this.is_multiple
&& this.choices
> 0) {
545 this.search_choices
.find("li.search-choice").remove();
547 } else if (!this.is_multiple
) {
548 this.selected_item
.addClass("chzn-default").find("span").text(this.default_text
);
549 if (this.disable_search
|| this.form_field
.options
.length
<= this.disable_search_threshold
) {
550 this.container
.addClass("chzn-container-single-nosearch");
552 this.container
.removeClass("chzn-container-single-nosearch");
556 _ref
= this.results_data
;
557 for (_i
= 0, _len
= _ref
.length
; _i
< _len
; _i
++) {
560 content
+= this.result_add_group(data
);
561 } else if (!data
.empty
) {
562 content
+= this.result_add_option(data
);
563 if (data
.selected
&& this.is_multiple
) {
564 this.choice_build(data
);
565 } else if (data
.selected
&& !this.is_multiple
) {
566 this.selected_item
.removeClass("chzn-default").find("span").text(data
.text
);
567 if (this.allow_single_deselect
) {
568 this.single_deselect_control_build();
573 this.search_field_disabled();
574 this.show_search_field_default();
575 this.search_field_scale();
576 this.search_results
.html(content
);
577 return this.parsing
= false;
580 Chosen
.prototype.result_add_group = function(group
) {
581 if (!group
.disabled
) {
582 group
.dom_id
= this.container_id
+ "_g_" + group
.array_index
;
583 return '<li id="' + group
.dom_id
+ '" class="group-result">' + $("<div />").text(group
.label
).html() + '</li>';
589 Chosen
.prototype.result_do_highlight = function(el
) {
590 var high_bottom
, high_top
, maxHeight
, visible_bottom
, visible_top
;
592 this.result_clear_highlight();
593 this.result_highlight
= el
;
594 this.result_highlight
.addClass("highlighted");
595 maxHeight
= parseInt(this.search_results
.css("maxHeight"), 10);
596 visible_top
= this.search_results
.scrollTop();
597 visible_bottom
= maxHeight
+ visible_top
;
598 high_top
= this.result_highlight
.position().top
+ this.search_results
.scrollTop();
599 high_bottom
= high_top
+ this.result_highlight
.outerHeight();
600 if (high_bottom
>= visible_bottom
) {
601 return this.search_results
.scrollTop((high_bottom
- maxHeight
) > 0 ? high_bottom
- maxHeight
: 0);
602 } else if (high_top
< visible_top
) {
603 return this.search_results
.scrollTop(high_top
);
608 Chosen
.prototype.result_clear_highlight = function() {
609 if (this.result_highlight
) {
610 this.result_highlight
.removeClass("highlighted");
612 return this.result_highlight
= null;
615 Chosen
.prototype.results_show = function() {
617 if (!this.is_multiple
) {
618 this.selected_item
.addClass("chzn-single-with-drop");
619 if (this.result_single_selected
) {
620 this.result_do_highlight(this.result_single_selected
);
622 } else if (this.max_selected_options
<= this.choices
) {
623 this.form_field_jq
.trigger("liszt:maxselected", {
628 dd_top
= this.is_multiple
? this.container
.height() : this.container
.height() - 1;
629 this.form_field_jq
.trigger("liszt:showing_dropdown", {
633 "top": dd_top
+ "px",
636 this.results_showing
= true;
637 this.search_field
.focus();
638 this.search_field
.val(this.search_field
.val());
639 return this.winnow_results();
642 Chosen
.prototype.results_hide = function() {
643 if (!this.is_multiple
) {
644 this.selected_item
.removeClass("chzn-single-with-drop");
646 this.result_clear_highlight();
647 this.form_field_jq
.trigger("liszt:hiding_dropdown", {
653 return this.results_showing
= false;
656 Chosen
.prototype.set_tab_index = function(el
) {
658 if (this.form_field_jq
.attr("tabindex")) {
659 ti
= this.form_field_jq
.attr("tabindex");
660 this.form_field_jq
.attr("tabindex", -1);
661 return this.search_field
.attr("tabindex", ti
);
665 Chosen
.prototype.show_search_field_default = function() {
666 if (this.is_multiple
&& this.choices
< 1 && !this.active_field
) {
667 this.search_field
.val(this.default_text
);
668 return this.search_field
.addClass("default");
670 this.search_field
.val("");
671 return this.search_field
.removeClass("default");
675 Chosen
.prototype.search_results_mouseup = function(evt
) {
677 target
= $(evt
.target
).hasClass("active-result") ? $(evt
.target
) : $(evt
.target
).parents(".active-result").first();
679 this.result_highlight
= target
;
680 this.result_select(evt
);
681 return this.search_field
.focus();
685 Chosen
.prototype.search_results_mouseover = function(evt
) {
687 target
= $(evt
.target
).hasClass("active-result") ? $(evt
.target
) : $(evt
.target
).parents(".active-result").first();
689 return this.result_do_highlight(target
);
693 Chosen
.prototype.search_results_mouseout = function(evt
) {
694 if ($(evt
.target
).hasClass("active-result" || $(evt
.target
).parents('.active-result').first())) {
695 return this.result_clear_highlight();
699 Chosen
.prototype.choices_click = function(evt
) {
700 evt
.preventDefault();
701 if (this.active_field
&& !($(evt
.target
).hasClass("search-choice" || $(evt
.target
).parents('.search-choice').first
)) && !this.results_showing
) {
702 return this.results_show();
706 Chosen
.prototype.choice_build = function(item
) {
707 var choice_id
, html
, link
,
709 if (this.is_multiple
&& this.max_selected_options
<= this.choices
) {
710 this.form_field_jq
.trigger("liszt:maxselected", {
715 choice_id
= this.container_id
+ "_c_" + item
.array_index
;
718 html
= '<li class="search-choice search-choice-disabled" id="' + choice_id
+ '"><span>' + item
.html
+ '</span></li>';
720 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>';
722 this.search_container
.before(html
);
723 link
= $('#' + choice_id
).find("a").first();
724 return link
.click(function(evt
) {
725 return _this
.choice_destroy_link_click(evt
);
729 Chosen
.prototype.choice_destroy_link_click = function(evt
) {
730 evt
.preventDefault();
731 if (!this.is_disabled
) {
732 this.pending_destroy_click
= true;
733 return this.choice_destroy($(evt
.target
));
735 return evt
.stopPropagation
;
739 Chosen
.prototype.choice_destroy = function(link
) {
740 if (this.result_deselect(link
.attr("rel"))) {
742 this.show_search_field_default();
743 if (this.is_multiple
&& this.choices
> 0 && this.search_field
.val().length
< 1) {
746 link
.parents('li').first().remove();
747 return this.search_field_scale();
751 Chosen
.prototype.results_reset = function() {
752 this.form_field
.options
[0].selected
= true;
753 this.selected_item
.find("span").text(this.default_text
);
754 if (!this.is_multiple
) {
755 this.selected_item
.addClass("chzn-default");
757 this.show_search_field_default();
758 this.results_reset_cleanup();
759 this.form_field_jq
.trigger("change");
760 if (this.active_field
) {
761 return this.results_hide();
765 Chosen
.prototype.results_reset_cleanup = function() {
766 this.current_value
= this.form_field_jq
.val();
767 return this.selected_item
.find("abbr").remove();
770 Chosen
.prototype.result_select = function(evt
) {
771 var high
, high_id
, item
, position
;
772 if (this.result_highlight
) {
773 high
= this.result_highlight
;
774 high_id
= high
.attr("id");
775 this.result_clear_highlight();
776 if (this.is_multiple
) {
777 this.result_deactivate(high
);
779 this.search_results
.find(".result-selected").removeClass("result-selected");
780 this.result_single_selected
= high
;
781 this.selected_item
.removeClass("chzn-default");
783 high
.addClass("result-selected");
784 position
= high_id
.substr(high_id
.lastIndexOf("_") + 1);
785 item
= this.results_data
[position
];
786 item
.selected
= true;
787 this.form_field
.options
[item
.options_index
].selected
= true;
788 if (this.is_multiple
) {
789 this.choice_build(item
);
791 this.selected_item
.find("span").first().text(item
.text
);
792 if (this.allow_single_deselect
) {
793 this.single_deselect_control_build();
796 if (!((evt
.metaKey
|| evt
.ctrlKey
) && this.is_multiple
)) {
799 this.search_field
.val("");
800 if (this.is_multiple
|| this.form_field_jq
.val() !== this.current_value
) {
801 this.form_field_jq
.trigger("change", {
802 'selected': this.form_field
.options
[item
.options_index
].value
805 this.current_value
= this.form_field_jq
.val();
806 return this.search_field_scale();
810 Chosen
.prototype.result_activate = function(el
) {
811 return el
.addClass("active-result");
814 Chosen
.prototype.result_deactivate = function(el
) {
815 return el
.removeClass("active-result");
818 Chosen
.prototype.result_deselect = function(pos
) {
819 var result
, result_data
;
820 result_data
= this.results_data
[pos
];
821 if (!this.form_field
.options
[result_data
.options_index
].disabled
) {
822 result_data
.selected
= false;
823 this.form_field
.options
[result_data
.options_index
].selected
= false;
824 result
= $("#" + this.container_id
+ "_o_" + pos
);
825 result
.removeClass("result-selected").addClass("active-result").show();
826 this.result_clear_highlight();
827 this.winnow_results();
828 this.form_field_jq
.trigger("change", {
829 deselected
: this.form_field
.options
[result_data
.options_index
].value
831 this.search_field_scale();
838 Chosen
.prototype.single_deselect_control_build = function() {
839 if (this.allow_single_deselect
&& this.selected_item
.find("abbr").length
< 1) {
840 return this.selected_item
.find("span").first().after("<abbr class=\"search-choice-close\"></abbr>");
844 Chosen
.prototype.winnow_results = function() {
845 var found
, option
, part
, parts
, regex
, regexAnchor
, result
, result_id
, results
, searchText
, startpos
, text
, zregex
, _i
, _j
, _len
, _len1
, _ref
;
846 this.no_results_clear();
848 searchText
= this.search_field
.val() === this.default_text
? "" : $('<div/>').text($.trim(this.search_field
.val())).html();
849 regexAnchor
= this.search_contains
? "" : "^";
850 regex
= new RegExp(regexAnchor
+ searchText
.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
851 zregex
= new RegExp(searchText
.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
852 _ref
= this.results_data
;
853 for (_i
= 0, _len
= _ref
.length
; _i
< _len
; _i
++) {
855 if (!option
.disabled
&& !option
.empty
) {
857 $('#' + option
.dom_id
).css('display', 'none');
858 } else if (!(this.is_multiple
&& option
.selected
)) {
860 result_id
= option
.dom_id
;
861 result
= $("#" + result_id
);
862 if (regex
.test(option
.html
)) {
865 } else if (this.enable_split_word_search
&& (option
.html
.indexOf(" ") >= 0 || option
.html
.indexOf("[") === 0)) {
866 parts
= option
.html
.replace(/\[|\]/g, "").split(" ");
868 for (_j
= 0, _len1
= parts
.length
; _j
< _len1
; _j
++) {
870 if (regex
.test(part
)) {
878 if (searchText
.length
) {
879 startpos
= option
.html
.search(zregex
);
880 text
= option
.html
.substr(0, startpos
+ searchText
.length
) + '</em>' + option
.html
.substr(startpos
+ searchText
.length
);
881 text
= text
.substr(0, startpos
) + '<em>' + text
.substr(startpos
);
886 this.result_activate(result
);
887 if (option
.group_array_index
!= null) {
888 $("#" + this.results_data
[option
.group_array_index
].dom_id
).css('display', 'list-item');
891 if (this.result_highlight
&& result_id
=== this.result_highlight
.attr('id')) {
892 this.result_clear_highlight();
894 this.result_deactivate(result
);
899 if (results
< 1 && searchText
.length
) {
900 return this.no_results(searchText
);
902 return this.winnow_results_set_highlight();
906 Chosen
.prototype.winnow_results_clear = function() {
907 var li
, lis
, _i
, _len
, _results
;
908 this.search_field
.val("");
909 lis
= this.search_results
.find("li");
911 for (_i
= 0, _len
= lis
.length
; _i
< _len
; _i
++) {
914 if (li
.hasClass("group-result")) {
915 _results
.push(li
.css('display', 'auto'));
916 } else if (!this.is_multiple
|| !li
.hasClass("result-selected")) {
917 _results
.push(this.result_activate(li
));
919 _results
.push(void 0);
925 Chosen
.prototype.winnow_results_set_highlight = function() {
926 var do_high
, selected_results
;
927 if (!this.result_highlight
) {
928 selected_results
= !this.is_multiple
? this.search_results
.find(".result-selected.active-result") : [];
929 do_high
= selected_results
.length
? selected_results
.first() : this.search_results
.find(".active-result").first();
930 if (do_high
!= null) {
931 return this.result_do_highlight(do_high
);
936 Chosen
.prototype.no_results = function(terms
) {
938 no_results_html
= $('<li class="no-results">' + this.results_none_found
+ ' "<span></span>"</li>');
939 no_results_html
.find("span").first().html(terms
);
940 return this.search_results
.append(no_results_html
);
943 Chosen
.prototype.no_results_clear = function() {
944 return this.search_results
.find(".no-results").remove();
947 Chosen
.prototype.keydown_arrow = function() {
948 var first_active
, next_sib
;
949 if (!this.result_highlight
) {
950 first_active
= this.search_results
.find("li.active-result").first();
952 this.result_do_highlight($(first_active
));
954 } else if (this.results_showing
) {
955 next_sib
= this.result_highlight
.nextAll("li.active-result").first();
957 this.result_do_highlight(next_sib
);
960 if (!this.results_showing
) {
961 return this.results_show();
965 Chosen
.prototype.keyup_arrow = function() {
967 if (!this.results_showing
&& !this.is_multiple
) {
968 return this.results_show();
969 } else if (this.result_highlight
) {
970 prev_sibs
= this.result_highlight
.prevAll("li.active-result");
971 if (prev_sibs
.length
) {
972 return this.result_do_highlight(prev_sibs
.first());
974 if (this.choices
> 0) {
977 return this.result_clear_highlight();
982 Chosen
.prototype.keydown_backstroke = function() {
983 var next_available_destroy
;
984 if (this.pending_backstroke
) {
985 this.choice_destroy(this.pending_backstroke
.find("a").first());
986 return this.clear_backstroke();
988 next_available_destroy
= this.search_container
.siblings("li.search-choice").last();
989 if (next_available_destroy
.length
&& !next_available_destroy
.hasClass("search-choice-disabled")) {
990 this.pending_backstroke
= next_available_destroy
;
991 if (this.single_backstroke_delete
) {
992 return this.keydown_backstroke();
994 return this.pending_backstroke
.addClass("search-choice-focus");
1000 Chosen
.prototype.clear_backstroke = function() {
1001 if (this.pending_backstroke
) {
1002 this.pending_backstroke
.removeClass("search-choice-focus");
1004 return this.pending_backstroke
= null;
1007 Chosen
.prototype.keydown_checker = function(evt
) {
1009 stroke
= (_ref
= evt
.which
) != null ? _ref
: evt
.keyCode
;
1010 this.search_field_scale();
1011 if (stroke
!== 8 && this.pending_backstroke
) {
1012 this.clear_backstroke();
1016 this.backstroke_length
= this.search_field
.val().length
;
1019 if (this.results_showing
&& !this.is_multiple
) {
1020 this.result_select(evt
);
1022 this.mouse_on_container
= false;
1025 evt
.preventDefault();
1028 evt
.preventDefault();
1032 this.keydown_arrow();
1037 Chosen
.prototype.search_field_scale = function() {
1038 var dd_top
, div
, h
, style
, style_block
, styles
, w
, _i
, _len
;
1039 if (this.is_multiple
) {
1042 style_block
= "position:absolute; left: -1000px; top: -1000px; display:none;";
1043 styles
= ['font-size', 'font-style', 'font-weight', 'font-family', 'line-height', 'text-transform', 'letter-spacing'];
1044 for (_i
= 0, _len
= styles
.length
; _i
< _len
; _i
++) {
1046 style_block
+= style
+ ":" + this.search_field
.css(style
) + ";";
1048 div
= $('<div />', {
1049 'style': style_block
1051 div
.text(this.search_field
.val());
1052 $('body').append(div
);
1053 w
= div
.width() + 25;
1055 if (w
> this.f_width
- 10) {
1056 w
= this.f_width
- 10;
1058 this.search_field
.css({
1061 dd_top
= this.container
.height();
1062 return this.dropdown
.css({
1063 "top": dd_top
+ "px"
1068 Chosen
.prototype.generate_random_id = function() {
1070 string
= "sel" + this.generate_random_char() + this.generate_random_char() + this.generate_random_char();
1071 while ($("#" + string
).length
> 0) {
1072 string
+= this.generate_random_char();
1081 root
.Chosen
= Chosen
;
1083 get_side_border_padding = function(elmt
) {
1084 var side_border_padding
;
1085 return side_border_padding
= elmt
.outerWidth() - elmt
.width();
1088 root
.get_side_border_padding
= get_side_border_padding
;