Merge "Ignore height as well as width for framed images."
[lhc/web/wiklou.git] / resources / lib / 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.14
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 if (!AbstractChosen.browser_is_supported()) {
112 return;
113 }
114 this.is_multiple = this.form_field.multiple;
115 this.set_default_text();
116 this.set_default_values();
117 this.setup();
118 this.set_up_html();
119 this.register_observers();
120 this.finish_setup();
121 }
122
123 AbstractChosen.prototype.set_default_values = function() {
124 var _this = this;
125 this.click_test_action = function(evt) {
126 return _this.test_active_click(evt);
127 };
128 this.activate_action = function(evt) {
129 return _this.activate_field(evt);
130 };
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;
141 this.choices = 0;
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;
145 };
146
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;
152 } else {
153 this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || AbstractChosen.default_single_text;
154 }
155 return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || AbstractChosen.default_no_result_text;
156 };
157
158 AbstractChosen.prototype.mouse_enter = function() {
159 return this.mouse_on_container = true;
160 };
161
162 AbstractChosen.prototype.mouse_leave = function() {
163 return this.mouse_on_container = false;
164 };
165
166 AbstractChosen.prototype.input_focus = function(evt) {
167 var _this = this;
168 if (this.is_multiple) {
169 if (!this.active_field) {
170 return setTimeout((function() {
171 return _this.container_mousedown();
172 }), 50);
173 }
174 } else {
175 if (!this.active_field) {
176 return this.activate_field();
177 }
178 }
179 };
180
181 AbstractChosen.prototype.input_blur = function(evt) {
182 var _this = this;
183 if (!this.mouse_on_container) {
184 this.active_field = false;
185 return setTimeout((function() {
186 return _this.blur_test();
187 }), 100);
188 }
189 };
190
191 AbstractChosen.prototype.result_add_option = function(option) {
192 var classes, style;
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");
198 }
199 if (option.group_array_index != null) {
200 classes.push("group-option");
201 }
202 if (option.classes !== "") {
203 classes.push(option.classes);
204 }
205 style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : "";
206 return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"' + style + '>' + option.html + '</li>';
207 } else {
208 return "";
209 }
210 };
211
212 AbstractChosen.prototype.results_update_field = function() {
213 this.set_default_text();
214 if (!this.is_multiple) {
215 this.results_reset_cleanup();
216 }
217 this.result_clear_highlight();
218 this.result_single_selected = null;
219 return this.results_build();
220 };
221
222 AbstractChosen.prototype.results_toggle = function() {
223 if (this.results_showing) {
224 return this.results_hide();
225 } else {
226 return this.results_show();
227 }
228 };
229
230 AbstractChosen.prototype.results_search = function(evt) {
231 if (this.results_showing) {
232 return this.winnow_results();
233 } else {
234 return this.results_show();
235 }
236 };
237
238 AbstractChosen.prototype.choices_click = function(evt) {
239 evt.preventDefault();
240 if (!this.results_showing) {
241 return this.results_show();
242 }
243 };
244
245 AbstractChosen.prototype.keyup_checker = function(evt) {
246 var stroke, _ref;
247 stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
248 this.search_field_scale();
249 switch (stroke) {
250 case 8:
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();
256 }
257 break;
258 case 13:
259 evt.preventDefault();
260 if (this.results_showing) {
261 return this.result_select(evt);
262 }
263 break;
264 case 27:
265 if (this.results_showing) {
266 this.results_hide();
267 }
268 return true;
269 case 9:
270 case 38:
271 case 40:
272 case 16:
273 case 91:
274 case 17:
275 break;
276 default:
277 return this.results_search();
278 }
279 };
280
281 AbstractChosen.prototype.generate_field_id = function() {
282 var new_id;
283 new_id = this.generate_random_id();
284 this.form_field.id = new_id;
285 return new_id;
286 };
287
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);
293 };
294
295 AbstractChosen.prototype.container_width = function() {
296 var width;
297 if (this.options.width != null) {
298 return this.options.width;
299 }
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();
301 return width + "px";
302 };
303
304 AbstractChosen.browser_is_supported = function() {
305 var _ref;
306 if (window.navigator.appName === "Microsoft Internet Explorer") {
307 return (null !== (_ref = document.documentMode) && _ref >= 8);
308 }
309 return true;
310 };
311
312 AbstractChosen.default_multiple_text = "Select Some Options";
313
314 AbstractChosen.default_single_text = "Select an Option";
315
316 AbstractChosen.default_no_result_text = "No results match";
317
318 return AbstractChosen;
319
320 })();
321
322 root.AbstractChosen = AbstractChosen;
323
324 }).call(this);
325
326 /*
327 Chosen source: generate output using 'cake build'
328 Copyright (c) 2011 by Harvest
329 */
330
331
332 (function() {
333 var $, Chosen, root,
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; };
336
337 root = this;
338
339 $ = jQuery;
340
341 $.fn.extend({
342 chosen: function(options) {
343 if (!AbstractChosen.browser_is_supported()) {
344 return this;
345 }
346 return this.each(function(input_field) {
347 var $this;
348 $this = $(this);
349 if (!$this.hasClass("chzn-done")) {
350 return $this.data('chosen', new Chosen(this, options));
351 }
352 });
353 }
354 });
355
356 Chosen = (function(_super) {
357
358 __extends(Chosen, _super);
359
360 function Chosen() {
361 return Chosen.__super__.constructor.apply(this, arguments);
362 }
363
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");
368 };
369
370 Chosen.prototype.finish_setup = function() {
371 return this.form_field_jq.addClass("chzn-done");
372 };
373
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);
382 }
383 if (this.is_rtl) {
384 container_classes.push("chzn-rtl");
385 }
386 container_props = {
387 'id': this.container_id,
388 'class': container_classes.join(' '),
389 'style': "width: " + (this.container_width()) + ";",
390 'title': this.form_field.title
391 };
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>');
395 } else {
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>');
397 }
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();
407 } else {
408 this.search_container = this.container.find('div.chzn-search').first();
409 this.selected_item = this.container.find('.chzn-single').first();
410 }
411 this.results_build();
412 this.set_tab_index();
413 this.set_label_behavior();
414 return this.form_field_jq.trigger("liszt:ready", {
415 chosen: this
416 });
417 };
418
419 Chosen.prototype.register_observers = function() {
420 var _this = this;
421 this.container.mousedown(function(evt) {
422 _this.container_mousedown(evt);
423 });
424 this.container.mouseup(function(evt) {
425 _this.container_mouseup(evt);
426 });
427 this.container.mouseenter(function(evt) {
428 _this.mouse_enter(evt);
429 });
430 this.container.mouseleave(function(evt) {
431 _this.mouse_leave(evt);
432 });
433 this.search_results.mouseup(function(evt) {
434 _this.search_results_mouseup(evt);
435 });
436 this.search_results.mouseover(function(evt) {
437 _this.search_results_mouseover(evt);
438 });
439 this.search_results.mouseout(function(evt) {
440 _this.search_results_mouseout(evt);
441 });
442 this.search_results.bind('mousewheel DOMMouseScroll', function(evt) {
443 _this.search_results_mousewheel(evt);
444 });
445 this.form_field_jq.bind("liszt:updated", function(evt) {
446 _this.results_update_field(evt);
447 });
448 this.form_field_jq.bind("liszt:activate", function(evt) {
449 _this.activate_field(evt);
450 });
451 this.form_field_jq.bind("liszt:open", function(evt) {
452 _this.container_mousedown(evt);
453 });
454 this.search_field.blur(function(evt) {
455 _this.input_blur(evt);
456 });
457 this.search_field.keyup(function(evt) {
458 _this.keyup_checker(evt);
459 });
460 this.search_field.keydown(function(evt) {
461 _this.keydown_checker(evt);
462 });
463 this.search_field.focus(function(evt) {
464 _this.input_focus(evt);
465 });
466 if (this.is_multiple) {
467 return this.search_choices.click(function(evt) {
468 _this.choices_click(evt);
469 });
470 } else {
471 return this.container.click(function(evt) {
472 evt.preventDefault();
473 });
474 }
475 };
476
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);
484 }
485 return this.close_field();
486 } else {
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);
491 }
492 }
493 };
494
495 Chosen.prototype.container_mousedown = function(evt) {
496 if (!this.is_disabled) {
497 if (evt && evt.type === "mousedown" && !this.results_showing) {
498 evt.preventDefault();
499 }
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("");
504 }
505 $(document).click(this.click_test_action);
506 this.results_show();
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();
510 }
511 return this.activate_field();
512 }
513 }
514 };
515
516 Chosen.prototype.container_mouseup = function(evt) {
517 if (evt.target.nodeName === "ABBR" && !this.is_disabled) {
518 return this.results_reset(evt);
519 }
520 };
521
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);
525 if (delta != null) {
526 evt.preventDefault();
527 if (evt.type === 'DOMMouseScroll') {
528 delta = delta * 40;
529 }
530 return this.search_results.scrollTop(delta + this.search_results.scrollTop());
531 }
532 };
533
534 Chosen.prototype.blur_test = function(evt) {
535 if (!this.active_field && this.container.hasClass("chzn-container-active")) {
536 return this.close_field();
537 }
538 };
539
540 Chosen.prototype.close_field = function() {
541 $(document).unbind("click", this.click_test_action);
542 this.active_field = false;
543 this.results_hide();
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();
549 };
550
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();
556 };
557
558 Chosen.prototype.test_active_click = function(evt) {
559 if ($(evt.target).parents('#' + this.container_id).length) {
560 return this.active_field = true;
561 } else {
562 return this.close_field();
563 }
564 };
565
566 Chosen.prototype.results_build = function() {
567 var content, data, _i, _len, _ref;
568 this.parsing = true;
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();
572 this.choices = 0;
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");
577 } else {
578 this.container.removeClass("chzn-container-single-nosearch");
579 }
580 }
581 content = '';
582 _ref = this.results_data;
583 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
584 data = _ref[_i];
585 if (data.group) {
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();
595 }
596 }
597 }
598 }
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;
604 };
605
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>';
610 } else {
611 return "";
612 }
613 };
614
615 Chosen.prototype.result_do_highlight = function(el) {
616 var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
617 if (el.length) {
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);
630 }
631 }
632 };
633
634 Chosen.prototype.result_clear_highlight = function() {
635 if (this.result_highlight) {
636 this.result_highlight.removeClass("highlighted");
637 }
638 return this.result_highlight = null;
639 };
640
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", {
646 chosen: this
647 });
648 return false;
649 }
650 this.container.addClass("chzn-with-drop");
651 this.form_field_jq.trigger("liszt:showing_dropdown", {
652 chosen: this
653 });
654 this.results_showing = true;
655 this.search_field.focus();
656 this.search_field.val(this.search_field.val());
657 return this.winnow_results();
658 };
659
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", {
664 chosen: this
665 });
666 return this.results_showing = false;
667 };
668
669 Chosen.prototype.set_tab_index = function(el) {
670 var ti;
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);
675 }
676 };
677
678 Chosen.prototype.set_label_behavior = function() {
679 var _this = this;
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 + "]");
683 }
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);
688 } else {
689 return _this.activate_field();
690 }
691 });
692 }
693 };
694
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");
699 } else {
700 this.search_field.val("");
701 return this.search_field.removeClass("default");
702 }
703 };
704
705 Chosen.prototype.search_results_mouseup = function(evt) {
706 var target;
707 target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
708 if (target.length) {
709 this.result_highlight = target;
710 this.result_select(evt);
711 return this.search_field.focus();
712 }
713 };
714
715 Chosen.prototype.search_results_mouseover = function(evt) {
716 var target;
717 target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
718 if (target) {
719 return this.result_do_highlight(target);
720 }
721 };
722
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();
726 }
727 };
728
729 Chosen.prototype.choice_build = function(item) {
730 var choice_id, html, link,
731 _this = this;
732 if (this.is_multiple && this.max_selected_options <= this.choices) {
733 this.form_field_jq.trigger("liszt:maxselected", {
734 chosen: this
735 });
736 return false;
737 }
738 choice_id = this.container_id + "_c_" + item.array_index;
739 this.choices += 1;
740 if (item.disabled) {
741 html = '<li class="search-choice search-choice-disabled" id="' + choice_id + '"><span>' + item.html + '</span></li>';
742 } else {
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>';
744 }
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);
749 });
750 };
751
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));
757 }
758 };
759
760 Chosen.prototype.choice_destroy = function(link) {
761 if (this.result_deselect(link.attr("rel"))) {
762 this.choices -= 1;
763 this.show_search_field_default();
764 if (this.is_multiple && this.choices > 0 && this.search_field.val().length < 1) {
765 this.results_hide();
766 }
767 link.parents('li').first().remove();
768 return this.search_field_scale();
769 }
770 };
771
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");
777 }
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();
783 }
784 };
785
786 Chosen.prototype.results_reset_cleanup = function() {
787 this.current_selectedIndex = this.form_field.selectedIndex;
788 return this.selected_item.find("abbr").remove();
789 };
790
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);
799 } else {
800 this.search_results.find(".result-selected").removeClass("result-selected");
801 this.result_single_selected = high;
802 this.selected_item.removeClass("chzn-default");
803 }
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);
811 } else {
812 this.selected_item.find("span").first().text(item.text);
813 if (this.allow_single_deselect) {
814 this.single_deselect_control_build();
815 }
816 }
817 if (!((evt.metaKey || evt.ctrlKey) && this.is_multiple)) {
818 this.results_hide();
819 }
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
824 });
825 }
826 this.current_selectedIndex = this.form_field.selectedIndex;
827 return this.search_field_scale();
828 }
829 };
830
831 Chosen.prototype.result_activate = function(el) {
832 return el.addClass("active-result");
833 };
834
835 Chosen.prototype.result_deactivate = function(el) {
836 return el.removeClass("active-result");
837 };
838
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
851 });
852 this.search_field_scale();
853 return true;
854 } else {
855 return false;
856 }
857 };
858
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>");
862 }
863 };
864
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();
868 results = 0;
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++) {
875 option = _ref[_i];
876 if (!option.disabled && !option.empty) {
877 if (option.group) {
878 $('#' + option.dom_id).css('display', 'none');
879 } else if (!(this.is_multiple && option.selected)) {
880 found = false;
881 result_id = option.dom_id;
882 result = $("#" + result_id);
883 if (regex.test(option.html)) {
884 found = true;
885 results += 1;
886 } else if (this.enable_split_word_search && (option.html.indexOf(" ") >= 0 || option.html.indexOf("[") === 0)) {
887 parts = option.html.replace(/\[|\]/g, "").split(" ");
888 if (parts.length) {
889 for (_j = 0, _len1 = parts.length; _j < _len1; _j++) {
890 part = parts[_j];
891 if (regex.test(part)) {
892 found = true;
893 results += 1;
894 }
895 }
896 }
897 }
898 if (found) {
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);
903 } else {
904 text = option.html;
905 }
906 result.html(text);
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');
910 }
911 } else {
912 if (this.result_highlight && result_id === this.result_highlight.attr('id')) {
913 this.result_clear_highlight();
914 }
915 this.result_deactivate(result);
916 }
917 }
918 }
919 }
920 if (results < 1 && searchText.length) {
921 return this.no_results(searchText);
922 } else {
923 return this.winnow_results_set_highlight();
924 }
925 };
926
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");
931 _results = [];
932 for (_i = 0, _len = lis.length; _i < _len; _i++) {
933 li = lis[_i];
934 li = $(li);
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));
939 } else {
940 _results.push(void 0);
941 }
942 }
943 return _results;
944 };
945
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);
953 }
954 }
955 };
956
957 Chosen.prototype.no_results = function(terms) {
958 var no_results_html;
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);
962 };
963
964 Chosen.prototype.no_results_clear = function() {
965 return this.search_results.find(".no-results").remove();
966 };
967
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();
972 if (first_active) {
973 this.result_do_highlight($(first_active));
974 }
975 } else if (this.results_showing) {
976 next_sib = this.result_highlight.nextAll("li.active-result").first();
977 if (next_sib) {
978 this.result_do_highlight(next_sib);
979 }
980 }
981 if (!this.results_showing) {
982 return this.results_show();
983 }
984 };
985
986 Chosen.prototype.keyup_arrow = function() {
987 var prev_sibs;
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());
994 } else {
995 if (this.choices > 0) {
996 this.results_hide();
997 }
998 return this.result_clear_highlight();
999 }
1000 }
1001 };
1002
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();
1008 } else {
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();
1014 } else {
1015 return this.pending_backstroke.addClass("search-choice-focus");
1016 }
1017 }
1018 }
1019 };
1020
1021 Chosen.prototype.clear_backstroke = function() {
1022 if (this.pending_backstroke) {
1023 this.pending_backstroke.removeClass("search-choice-focus");
1024 }
1025 return this.pending_backstroke = null;
1026 };
1027
1028 Chosen.prototype.keydown_checker = function(evt) {
1029 var stroke, _ref;
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();
1034 }
1035 switch (stroke) {
1036 case 8:
1037 this.backstroke_length = this.search_field.val().length;
1038 break;
1039 case 9:
1040 if (this.results_showing && !this.is_multiple) {
1041 this.result_select(evt);
1042 }
1043 this.mouse_on_container = false;
1044 break;
1045 case 13:
1046 evt.preventDefault();
1047 break;
1048 case 38:
1049 evt.preventDefault();
1050 this.keyup_arrow();
1051 break;
1052 case 40:
1053 this.keydown_arrow();
1054 break;
1055 }
1056 };
1057
1058 Chosen.prototype.search_field_scale = function() {
1059 var div, h, style, style_block, styles, w, _i, _len;
1060 if (this.is_multiple) {
1061 h = 0;
1062 w = 0;
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++) {
1066 style = styles[_i];
1067 style_block += style + ":" + this.search_field.css(style) + ";";
1068 }
1069 div = $('<div />', {
1070 'style': style_block
1071 });
1072 div.text(this.search_field.val());
1073 $('body').append(div);
1074 w = div.width() + 25;
1075 div.remove();
1076 if (!this.f_width) {
1077 this.f_width = this.container.outerWidth();
1078 }
1079 if (w > this.f_width - 10) {
1080 w = this.f_width - 10;
1081 }
1082 return this.search_field.css({
1083 'width': w + 'px'
1084 });
1085 }
1086 };
1087
1088 Chosen.prototype.generate_random_id = function() {
1089 var string;
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();
1093 }
1094 return string;
1095 };
1096
1097 return Chosen;
1098
1099 })(AbstractChosen);
1100
1101 root.Chosen = Chosen;
1102
1103 }).call(this);