Merge "Add chosen support to HTMLForm's multiselect field"
[lhc/web/wiklou.git] / resources / jquery.chosen / chosen.jquery.js
1 // Chosen, a Select Box Enhancer for jQuery and Protoype
2 // by Patrick Filler for Harvest, http://getharvest.com
3 //
4 // Version 0.9.12
5 // Full source at https://github.com/harvesthq/chosen
6 // Copyright (c) 2011 Harvest http://getharvest.com
7
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.
10 (function() {
11 var SelectParser;
12
13 SelectParser = (function() {
14
15 function SelectParser() {
16 this.options_index = 0;
17 this.parsed = [];
18 }
19
20 SelectParser.prototype.add_node = function(child) {
21 if (child.nodeName.toUpperCase() === "OPTGROUP") {
22 return this.add_group(child);
23 } else {
24 return this.add_option(child);
25 }
26 };
27
28 SelectParser.prototype.add_group = function(group) {
29 var group_position, option, _i, _len, _ref, _results;
30 group_position = this.parsed.length;
31 this.parsed.push({
32 array_index: group_position,
33 group: true,
34 label: group.label,
35 children: 0,
36 disabled: group.disabled
37 });
38 _ref = group.childNodes;
39 _results = [];
40 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
41 option = _ref[_i];
42 _results.push(this.add_option(option, group_position, group.disabled));
43 }
44 return _results;
45 };
46
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;
52 }
53 this.parsed.push({
54 array_index: this.parsed.length,
55 options_index: this.options_index,
56 value: option.value,
57 text: option.text,
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
64 });
65 } else {
66 this.parsed.push({
67 array_index: this.parsed.length,
68 options_index: this.options_index,
69 empty: true
70 });
71 }
72 return this.options_index += 1;
73 }
74 };
75
76 return SelectParser;
77
78 })();
79
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++) {
85 child = _ref[_i];
86 parser.add_node(child);
87 }
88 return parser.parsed;
89 };
90
91 this.SelectParser = SelectParser;
92
93 }).call(this);
94
95 /*
96 Chosen source: generate output using 'cake build'
97 Copyright (c) 2011 by Harvest
98 */
99
100
101 (function() {
102 var AbstractChosen, root;
103
104 root = this;
105
106 AbstractChosen = (function() {
107
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();
114 this.setup();
115 this.set_up_html();
116 this.register_observers();
117 this.finish_setup();
118 }
119
120 AbstractChosen.prototype.set_default_values = function() {
121 var _this = this;
122 this.click_test_action = function(evt) {
123 return _this.test_active_click(evt);
124 };
125 this.activate_action = function(evt) {
126 return _this.activate_field(evt);
127 };
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;
138 this.choices = 0;
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;
142 };
143
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";
149 } else {
150 this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || "Select an Option";
151 }
152 return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || "No results match";
153 };
154
155 AbstractChosen.prototype.mouse_enter = function() {
156 return this.mouse_on_container = true;
157 };
158
159 AbstractChosen.prototype.mouse_leave = function() {
160 return this.mouse_on_container = false;
161 };
162
163 AbstractChosen.prototype.input_focus = function(evt) {
164 var _this = this;
165 if (this.is_multiple) {
166 if (!this.active_field) {
167 return setTimeout((function() {
168 return _this.container_mousedown();
169 }), 50);
170 }
171 } else {
172 if (!this.active_field) {
173 return this.activate_field();
174 }
175 }
176 };
177
178 AbstractChosen.prototype.input_blur = function(evt) {
179 var _this = this;
180 if (!this.mouse_on_container) {
181 this.active_field = false;
182 return setTimeout((function() {
183 return _this.blur_test();
184 }), 100);
185 }
186 };
187
188 AbstractChosen.prototype.result_add_option = function(option) {
189 var classes, style;
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");
195 }
196 if (option.group_array_index != null) {
197 classes.push("group-option");
198 }
199 if (option.classes !== "") {
200 classes.push(option.classes);
201 }
202 style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : "";
203 return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"' + style + '>' + option.html + '</li>';
204 } else {
205 return "";
206 }
207 };
208
209 AbstractChosen.prototype.results_update_field = function() {
210 this.set_default_text();
211 if (!this.is_multiple) {
212 this.results_reset_cleanup();
213 }
214 this.result_clear_highlight();
215 this.result_single_selected = null;
216 return this.results_build();
217 };
218
219 AbstractChosen.prototype.results_toggle = function() {
220 if (this.results_showing) {
221 return this.results_hide();
222 } else {
223 return this.results_show();
224 }
225 };
226
227 AbstractChosen.prototype.results_search = function(evt) {
228 if (this.results_showing) {
229 return this.winnow_results();
230 } else {
231 return this.results_show();
232 }
233 };
234
235 AbstractChosen.prototype.keyup_checker = function(evt) {
236 var stroke, _ref;
237 stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
238 this.search_field_scale();
239 switch (stroke) {
240 case 8:
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();
246 }
247 break;
248 case 13:
249 evt.preventDefault();
250 if (this.results_showing) {
251 return this.result_select(evt);
252 }
253 break;
254 case 27:
255 if (this.results_showing) {
256 this.results_hide();
257 }
258 return true;
259 case 9:
260 case 38:
261 case 40:
262 case 16:
263 case 91:
264 case 17:
265 break;
266 default:
267 return this.results_search();
268 }
269 };
270
271 AbstractChosen.prototype.generate_field_id = function() {
272 var new_id;
273 new_id = this.generate_random_id();
274 this.form_field.id = new_id;
275 return new_id;
276 };
277
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);
283 };
284
285 return AbstractChosen;
286
287 })();
288
289 root.AbstractChosen = AbstractChosen;
290
291 }).call(this);
292
293 /*
294 Chosen source: generate output using 'cake build'
295 Copyright (c) 2011 by Harvest
296 */
297
298
299 (function() {
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; };
303
304 root = this;
305
306 $ = jQuery;
307
308 $.fn.extend({
309 chosen: function(options) {
310 var browser, match, ua;
311 ua = navigator.userAgent.toLowerCase();
312 match = /(msie) ([\w.]+)/.exec(ua) || [];
313 browser = {
314 name: match[1] || "",
315 version: match[2] || "0"
316 };
317 if (browser.name === "msie" && (browser.version === "6.0" || (browser.version === "7.0" && document.documentMode === 7))) {
318 return this;
319 }
320 return this.each(function(input_field) {
321 var $this;
322 $this = $(this);
323 if (!$this.hasClass("chzn-done")) {
324 return $this.data('chosen', new Chosen(this, options));
325 }
326 });
327 }
328 });
329
330 Chosen = (function(_super) {
331
332 __extends(Chosen, _super);
333
334 function Chosen() {
335 return Chosen.__super__.constructor.apply(this, arguments);
336 }
337
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");
342 };
343
344 Chosen.prototype.finish_setup = function() {
345 return this.form_field_jq.addClass("chzn-done");
346 };
347
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);
356 }
357 if (this.is_rtl) {
358 container_classes.push("chzn-rtl");
359 }
360 this.f_width = this.form_field_jq.outerWidth();
361 container_props = {
362 id: this.container_id,
363 "class": container_classes.join(' '),
364 style: 'width: ' + this.f_width + 'px;',
365 title: this.form_field.title
366 };
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>');
370 } else {
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>');
372 }
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);
378 this.dropdown.css({
379 "width": dd_width + "px",
380 "top": dd_top + "px"
381 });
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();
389 } else {
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"
395 });
396 }
397 this.results_build();
398 this.set_tab_index();
399 return this.form_field_jq.trigger("liszt:ready", {
400 chosen: this
401 });
402 };
403
404 Chosen.prototype.register_observers = function() {
405 var _this = this;
406 this.container.mousedown(function(evt) {
407 _this.container_mousedown(evt);
408 });
409 this.container.mouseup(function(evt) {
410 _this.container_mouseup(evt);
411 });
412 this.container.mouseenter(function(evt) {
413 _this.mouse_enter(evt);
414 });
415 this.container.mouseleave(function(evt) {
416 _this.mouse_leave(evt);
417 });
418 this.search_results.mouseup(function(evt) {
419 _this.search_results_mouseup(evt);
420 });
421 this.search_results.mouseover(function(evt) {
422 _this.search_results_mouseover(evt);
423 });
424 this.search_results.mouseout(function(evt) {
425 _this.search_results_mouseout(evt);
426 });
427 this.form_field_jq.bind("liszt:updated", function(evt) {
428 _this.results_update_field(evt);
429 });
430 this.form_field_jq.bind("liszt:activate", function(evt) {
431 _this.activate_field(evt);
432 });
433 this.form_field_jq.bind("liszt:open", function(evt) {
434 _this.container_mousedown(evt);
435 });
436 this.search_field.blur(function(evt) {
437 _this.input_blur(evt);
438 });
439 this.search_field.keyup(function(evt) {
440 _this.keyup_checker(evt);
441 });
442 this.search_field.keydown(function(evt) {
443 _this.keydown_checker(evt);
444 });
445 this.search_field.focus(function(evt) {
446 _this.input_focus(evt);
447 });
448 if (this.is_multiple) {
449 return this.search_choices.click(function(evt) {
450 _this.choices_click(evt);
451 });
452 } else {
453 return this.container.click(function(evt) {
454 evt.preventDefault();
455 });
456 }
457 };
458
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);
466 }
467 return this.close_field();
468 } else {
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);
473 }
474 }
475 };
476
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();
483 }
484 if (!this.pending_destroy_click && !target_closelink) {
485 if (!this.active_field) {
486 if (this.is_multiple) {
487 this.search_field.val("");
488 }
489 $(document).click(this.click_test_action);
490 this.results_show();
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();
494 }
495 return this.activate_field();
496 } else {
497 return this.pending_destroy_click = false;
498 }
499 }
500 };
501
502 Chosen.prototype.container_mouseup = function(evt) {
503 if (evt.target.nodeName === "ABBR" && !this.is_disabled) {
504 return this.results_reset(evt);
505 }
506 };
507
508 Chosen.prototype.blur_test = function(evt) {
509 if (!this.active_field && this.container.hasClass("chzn-container-active")) {
510 return this.close_field();
511 }
512 };
513
514 Chosen.prototype.close_field = function() {
515 $(document).unbind("click", this.click_test_action);
516 this.active_field = false;
517 this.results_hide();
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();
523 };
524
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();
530 };
531
532 Chosen.prototype.test_active_click = function(evt) {
533 if ($(evt.target).parents('#' + this.container_id).length) {
534 return this.active_field = true;
535 } else {
536 return this.close_field();
537 }
538 };
539
540 Chosen.prototype.results_build = function() {
541 var content, data, _i, _len, _ref;
542 this.parsing = true;
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();
546 this.choices = 0;
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");
551 } else {
552 this.container.removeClass("chzn-container-single-nosearch");
553 }
554 }
555 content = '';
556 _ref = this.results_data;
557 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
558 data = _ref[_i];
559 if (data.group) {
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();
569 }
570 }
571 }
572 }
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;
578 };
579
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>';
584 } else {
585 return "";
586 }
587 };
588
589 Chosen.prototype.result_do_highlight = function(el) {
590 var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
591 if (el.length) {
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);
604 }
605 }
606 };
607
608 Chosen.prototype.result_clear_highlight = function() {
609 if (this.result_highlight) {
610 this.result_highlight.removeClass("highlighted");
611 }
612 return this.result_highlight = null;
613 };
614
615 Chosen.prototype.results_show = function() {
616 var dd_top;
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);
621 }
622 } else if (this.max_selected_options <= this.choices) {
623 this.form_field_jq.trigger("liszt:maxselected", {
624 chosen: this
625 });
626 return false;
627 }
628 dd_top = this.is_multiple ? this.container.height() : this.container.height() - 1;
629 this.form_field_jq.trigger("liszt:showing_dropdown", {
630 chosen: this
631 });
632 this.dropdown.css({
633 "top": dd_top + "px",
634 "left": 0
635 });
636 this.results_showing = true;
637 this.search_field.focus();
638 this.search_field.val(this.search_field.val());
639 return this.winnow_results();
640 };
641
642 Chosen.prototype.results_hide = function() {
643 if (!this.is_multiple) {
644 this.selected_item.removeClass("chzn-single-with-drop");
645 }
646 this.result_clear_highlight();
647 this.form_field_jq.trigger("liszt:hiding_dropdown", {
648 chosen: this
649 });
650 this.dropdown.css({
651 "left": "-9000px"
652 });
653 return this.results_showing = false;
654 };
655
656 Chosen.prototype.set_tab_index = function(el) {
657 var ti;
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);
662 }
663 };
664
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");
669 } else {
670 this.search_field.val("");
671 return this.search_field.removeClass("default");
672 }
673 };
674
675 Chosen.prototype.search_results_mouseup = function(evt) {
676 var target;
677 target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
678 if (target.length) {
679 this.result_highlight = target;
680 this.result_select(evt);
681 return this.search_field.focus();
682 }
683 };
684
685 Chosen.prototype.search_results_mouseover = function(evt) {
686 var target;
687 target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
688 if (target) {
689 return this.result_do_highlight(target);
690 }
691 };
692
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();
696 }
697 };
698
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();
703 }
704 };
705
706 Chosen.prototype.choice_build = function(item) {
707 var choice_id, html, link,
708 _this = this;
709 if (this.is_multiple && this.max_selected_options <= this.choices) {
710 this.form_field_jq.trigger("liszt:maxselected", {
711 chosen: this
712 });
713 return false;
714 }
715 choice_id = this.container_id + "_c_" + item.array_index;
716 this.choices += 1;
717 if (item.disabled) {
718 html = '<li class="search-choice search-choice-disabled" id="' + choice_id + '"><span>' + item.html + '</span></li>';
719 } else {
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>';
721 }
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);
726 });
727 };
728
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));
734 } else {
735 return evt.stopPropagation;
736 }
737 };
738
739 Chosen.prototype.choice_destroy = function(link) {
740 if (this.result_deselect(link.attr("rel"))) {
741 this.choices -= 1;
742 this.show_search_field_default();
743 if (this.is_multiple && this.choices > 0 && this.search_field.val().length < 1) {
744 this.results_hide();
745 }
746 link.parents('li').first().remove();
747 return this.search_field_scale();
748 }
749 };
750
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");
756 }
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();
762 }
763 };
764
765 Chosen.prototype.results_reset_cleanup = function() {
766 this.current_value = this.form_field_jq.val();
767 return this.selected_item.find("abbr").remove();
768 };
769
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);
778 } else {
779 this.search_results.find(".result-selected").removeClass("result-selected");
780 this.result_single_selected = high;
781 this.selected_item.removeClass("chzn-default");
782 }
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);
790 } else {
791 this.selected_item.find("span").first().text(item.text);
792 if (this.allow_single_deselect) {
793 this.single_deselect_control_build();
794 }
795 }
796 if (!((evt.metaKey || evt.ctrlKey) && this.is_multiple)) {
797 this.results_hide();
798 }
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
803 });
804 }
805 this.current_value = this.form_field_jq.val();
806 return this.search_field_scale();
807 }
808 };
809
810 Chosen.prototype.result_activate = function(el) {
811 return el.addClass("active-result");
812 };
813
814 Chosen.prototype.result_deactivate = function(el) {
815 return el.removeClass("active-result");
816 };
817
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
830 });
831 this.search_field_scale();
832 return true;
833 } else {
834 return false;
835 }
836 };
837
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>");
841 }
842 };
843
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();
847 results = 0;
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++) {
854 option = _ref[_i];
855 if (!option.disabled && !option.empty) {
856 if (option.group) {
857 $('#' + option.dom_id).css('display', 'none');
858 } else if (!(this.is_multiple && option.selected)) {
859 found = false;
860 result_id = option.dom_id;
861 result = $("#" + result_id);
862 if (regex.test(option.html)) {
863 found = true;
864 results += 1;
865 } else if (this.enable_split_word_search && (option.html.indexOf(" ") >= 0 || option.html.indexOf("[") === 0)) {
866 parts = option.html.replace(/\[|\]/g, "").split(" ");
867 if (parts.length) {
868 for (_j = 0, _len1 = parts.length; _j < _len1; _j++) {
869 part = parts[_j];
870 if (regex.test(part)) {
871 found = true;
872 results += 1;
873 }
874 }
875 }
876 }
877 if (found) {
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);
882 } else {
883 text = option.html;
884 }
885 result.html(text);
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');
889 }
890 } else {
891 if (this.result_highlight && result_id === this.result_highlight.attr('id')) {
892 this.result_clear_highlight();
893 }
894 this.result_deactivate(result);
895 }
896 }
897 }
898 }
899 if (results < 1 && searchText.length) {
900 return this.no_results(searchText);
901 } else {
902 return this.winnow_results_set_highlight();
903 }
904 };
905
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");
910 _results = [];
911 for (_i = 0, _len = lis.length; _i < _len; _i++) {
912 li = lis[_i];
913 li = $(li);
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));
918 } else {
919 _results.push(void 0);
920 }
921 }
922 return _results;
923 };
924
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);
932 }
933 }
934 };
935
936 Chosen.prototype.no_results = function(terms) {
937 var no_results_html;
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);
941 };
942
943 Chosen.prototype.no_results_clear = function() {
944 return this.search_results.find(".no-results").remove();
945 };
946
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();
951 if (first_active) {
952 this.result_do_highlight($(first_active));
953 }
954 } else if (this.results_showing) {
955 next_sib = this.result_highlight.nextAll("li.active-result").first();
956 if (next_sib) {
957 this.result_do_highlight(next_sib);
958 }
959 }
960 if (!this.results_showing) {
961 return this.results_show();
962 }
963 };
964
965 Chosen.prototype.keyup_arrow = function() {
966 var prev_sibs;
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());
973 } else {
974 if (this.choices > 0) {
975 this.results_hide();
976 }
977 return this.result_clear_highlight();
978 }
979 }
980 };
981
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();
987 } else {
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();
993 } else {
994 return this.pending_backstroke.addClass("search-choice-focus");
995 }
996 }
997 }
998 };
999
1000 Chosen.prototype.clear_backstroke = function() {
1001 if (this.pending_backstroke) {
1002 this.pending_backstroke.removeClass("search-choice-focus");
1003 }
1004 return this.pending_backstroke = null;
1005 };
1006
1007 Chosen.prototype.keydown_checker = function(evt) {
1008 var stroke, _ref;
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();
1013 }
1014 switch (stroke) {
1015 case 8:
1016 this.backstroke_length = this.search_field.val().length;
1017 break;
1018 case 9:
1019 if (this.results_showing && !this.is_multiple) {
1020 this.result_select(evt);
1021 }
1022 this.mouse_on_container = false;
1023 break;
1024 case 13:
1025 evt.preventDefault();
1026 break;
1027 case 38:
1028 evt.preventDefault();
1029 this.keyup_arrow();
1030 break;
1031 case 40:
1032 this.keydown_arrow();
1033 break;
1034 }
1035 };
1036
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) {
1040 h = 0;
1041 w = 0;
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++) {
1045 style = styles[_i];
1046 style_block += style + ":" + this.search_field.css(style) + ";";
1047 }
1048 div = $('<div />', {
1049 'style': style_block
1050 });
1051 div.text(this.search_field.val());
1052 $('body').append(div);
1053 w = div.width() + 25;
1054 div.remove();
1055 if (w > this.f_width - 10) {
1056 w = this.f_width - 10;
1057 }
1058 this.search_field.css({
1059 'width': w + 'px'
1060 });
1061 dd_top = this.container.height();
1062 return this.dropdown.css({
1063 "top": dd_top + "px"
1064 });
1065 }
1066 };
1067
1068 Chosen.prototype.generate_random_id = function() {
1069 var string;
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();
1073 }
1074 return string;
1075 };
1076
1077 return Chosen;
1078
1079 })(AbstractChosen);
1080
1081 root.Chosen = Chosen;
1082
1083 get_side_border_padding = function(elmt) {
1084 var side_border_padding;
1085 return side_border_padding = elmt.outerWidth() - elmt.width();
1086 };
1087
1088 root.get_side_border_padding = get_side_border_padding;
1089
1090 }).call(this);