PluginProbe
Booking Calendar / 11.4.3
Booking Calendar v11.4.3
11.8.4 11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 10.11 All 204 releases
booking / vendors / chosen / chosen.proto.js

chosen.proto.js in Booking Calendar 11.4.3, at vendors/chosen/chosen.proto.js

1,405 lines 47.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /*!
2 Chosen, a Select Box Enhancer for jQuery and Prototype
3 by Patrick Filler for Harvest, http://getharvest.com
4
5 Version 1.8.7
6 Full source at https://github.com/harvesthq/chosen
7 Copyright (c) 2011-2018 Harvest http://getharvest.com
8
9 MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md
10 This file is generated by `grunt build`, do not edit it by hand.
11
12 Docs: https://harvesthq.github.io/chosen/options.html
13 */
14 /**
15 oplugins: customization - I have replaced .choosen- to .chzn- in CSS and JS files.
16 */
17
18 (function() {
19 var AbstractChosen, SelectParser,
20 bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
21 extend = 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; },
22 hasProp = {}.hasOwnProperty;
23
24 SelectParser = (function() {
25 function SelectParser() {
26 this.options_index = 0;
27 this.parsed = [];
28 }
29
30 SelectParser.prototype.add_node = function(child) {
31 if (child.nodeName.toUpperCase() === "OPTGROUP") {
32 return this.add_group(child);
33 } else {
34 return this.add_option(child);
35 }
36 };
37
38 SelectParser.prototype.add_group = function(group) {
39 var group_position, i, len, option, ref, results1;
40 group_position = this.parsed.length;
41 this.parsed.push({
42 array_index: group_position,
43 group: true,
44 label: group.label,
45 title: group.title ? group.title : void 0,
46 children: 0,
47 disabled: group.disabled,
48 classes: group.className
49 });
50 ref = group.childNodes;
51 results1 = [];
52 for (i = 0, len = ref.length; i < len; i++) {
53 option = ref[i];
54 results1.push(this.add_option(option, group_position, group.disabled));
55 }
56 return results1;
57 };
58
59 SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
60 if (option.nodeName.toUpperCase() === "OPTION") {
61 if (option.text !== "") {
62 if (group_position != null) {
63 this.parsed[group_position].children += 1;
64 }
65 this.parsed.push({
66 array_index: this.parsed.length,
67 options_index: this.options_index,
68 value: option.value,
69 text: option.text,
70 html: option.innerHTML,
71 title: option.title ? option.title : void 0,
72 selected: option.selected,
73 disabled: group_disabled === true ? group_disabled : option.disabled,
74 group_array_index: group_position,
75 group_label: group_position != null ? this.parsed[group_position].label : null,
76 classes: option.className,
77 style: option.style.cssText
78 });
79 } else {
80 this.parsed.push({
81 array_index: this.parsed.length,
82 options_index: this.options_index,
83 empty: true
84 });
85 }
86 return this.options_index += 1;
87 }
88 };
89
90 return SelectParser;
91
92 })();
93
94 SelectParser.select_to_array = function(select) {
95 var child, i, len, parser, ref;
96 parser = new SelectParser();
97 ref = select.childNodes;
98 for (i = 0, len = ref.length; i < len; i++) {
99 child = ref[i];
100 parser.add_node(child);
101 }
102 return parser.parsed;
103 };
104
105 AbstractChosen = (function() {
106 function AbstractChosen(form_field, options1) {
107 this.form_field = form_field;
108 this.options = options1 != null ? options1 : {};
109 this.label_click_handler = bind(this.label_click_handler, this);
110 if (!AbstractChosen.browser_is_supported()) {
111 return;
112 }
113 this.is_multiple = this.form_field.multiple;
114 this.set_default_text();
115 this.set_default_values();
116 this.setup();
117 this.set_up_html();
118 this.register_observers();
119 this.on_ready();
120 }
121
122 AbstractChosen.prototype.set_default_values = function() {
123 this.click_test_action = (function(_this) {
124 return function(evt) {
125 return _this.test_active_click(evt);
126 };
127 })(this);
128 this.activate_action = (function(_this) {
129 return function(evt) {
130 return _this.activate_field(evt);
131 };
132 })(this);
133 this.active_field = false;
134 this.mouse_on_container = false;
135 this.results_showing = false;
136 this.result_highlighted = null;
137 this.is_rtl = this.options.rtl || /\bchosen-rtl\b/.test(this.form_field.className);
138 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;
139 this.disable_search_threshold = this.options.disable_search_threshold || 0;
140 this.disable_search = this.options.disable_search || false;
141 this.enable_split_word_search = this.options.enable_split_word_search != null ? this.options.enable_split_word_search : true;
142 this.group_search = this.options.group_search != null ? this.options.group_search : true;
143 this.search_contains = this.options.search_contains || false;
144 this.single_backstroke_delete = this.options.single_backstroke_delete != null ? this.options.single_backstroke_delete : true;
145 this.max_selected_options = this.options.max_selected_options || Infinity;
146 this.inherit_select_classes = this.options.inherit_select_classes || false;
147 this.display_selected_options = this.options.display_selected_options != null ? this.options.display_selected_options : true;
148 this.display_disabled_options = this.options.display_disabled_options != null ? this.options.display_disabled_options : true;
149 this.include_group_label_in_selected = this.options.include_group_label_in_selected || false;
150 this.max_shown_results = this.options.max_shown_results || Number.POSITIVE_INFINITY;
151 this.case_sensitive_search = this.options.case_sensitive_search || false;
152 return this.hide_results_on_select = this.options.hide_results_on_select != null ? this.options.hide_results_on_select : true;
153 };
154
155 AbstractChosen.prototype.set_default_text = function() {
156 if (this.form_field.getAttribute("data-placeholder")) {
157 this.default_text = this.form_field.getAttribute("data-placeholder");
158 } else if (this.is_multiple) {
159 this.default_text = this.options.placeholder_text_multiple || this.options.placeholder_text || AbstractChosen.default_multiple_text;
160 } else {
161 this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || AbstractChosen.default_single_text;
162 }
163 this.default_text = this.escape_html(this.default_text);
164 return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || AbstractChosen.default_no_result_text;
165 };
166
167 AbstractChosen.prototype.choice_label = function(item) {
168 if (this.include_group_label_in_selected && (item.group_label != null)) {
169 return "<b class='group-name'>" + (this.escape_html(item.group_label)) + "</b>" + item.html;
170 } else {
171 return item.html;
172 }
173 };
174
175 AbstractChosen.prototype.mouse_enter = function() {
176 return this.mouse_on_container = true;
177 };
178
179 AbstractChosen.prototype.mouse_leave = function() {
180 return this.mouse_on_container = false;
181 };
182
183 AbstractChosen.prototype.input_focus = function(evt) {
184 if (this.is_multiple) {
185 if (!this.active_field) {
186 return setTimeout(((function(_this) {
187 return function() {
188 return _this.container_mousedown();
189 };
190 })(this)), 50);
191 }
192 } else {
193 if (!this.active_field) {
194 return this.activate_field();
195 }
196 }
197 };
198
199 AbstractChosen.prototype.input_blur = function(evt) {
200 if (!this.mouse_on_container) {
201 this.active_field = false;
202 return setTimeout(((function(_this) {
203 return function() {
204 return _this.blur_test();
205 };
206 })(this)), 100);
207 }
208 };
209
210 AbstractChosen.prototype.label_click_handler = function(evt) {
211 if (this.is_multiple) {
212 return this.container_mousedown(evt);
213 } else {
214 return this.activate_field();
215 }
216 };
217
218 AbstractChosen.prototype.results_option_build = function(options) {
219 var content, data, data_content, i, len, ref, shown_results;
220 content = '';
221 shown_results = 0;
222 ref = this.results_data;
223 for (i = 0, len = ref.length; i < len; i++) {
224 data = ref[i];
225 data_content = '';
226 if (data.group) {
227 data_content = this.result_add_group(data);
228 } else {
229 data_content = this.result_add_option(data);
230 }
231 if (data_content !== '') {
232 shown_results++;
233 content += data_content;
234 }
235 if (options != null ? options.first : void 0) {
236 if (data.selected && this.is_multiple) {
237 this.choice_build(data);
238 } else if (data.selected && !this.is_multiple) {
239 this.single_set_selected_text(this.choice_label(data));
240 }
241 }
242 if (shown_results >= this.max_shown_results) {
243 break;
244 }
245 }
246 return content;
247 };
248
249 AbstractChosen.prototype.result_add_option = function(option) {
250 var classes, option_el;
251 if (!option.search_match) {
252 return '';
253 }
254 if (!this.include_option_in_results(option)) {
255 return '';
256 }
257 classes = [];
258 if (!option.disabled && !(option.selected && this.is_multiple)) {
259 classes.push("active-result");
260 }
261 if (option.disabled && !(option.selected && this.is_multiple)) {
262 classes.push("disabled-result");
263 }
264 if (option.selected) {
265 classes.push("result-selected");
266 }
267 if (option.group_array_index != null) {
268 classes.push("group-option");
269 }
270 if (option.classes !== "") {
271 classes.push(option.classes);
272 }
273 option_el = document.createElement("li");
274 option_el.className = classes.join(" ");
275 if (option.style) {
276 option_el.style.cssText = option.style;
277 }
278 option_el.setAttribute("data-option-array-index", option.array_index);
279 option_el.innerHTML = option.highlighted_html || option.html;
280 if (option.title) {
281 option_el.title = option.title;
282 }
283 return this.outerHTML(option_el);
284 };
285
286 AbstractChosen.prototype.result_add_group = function(group) {
287 var classes, group_el;
288 if (!(group.search_match || group.group_match)) {
289 return '';
290 }
291 if (!(group.active_options > 0)) {
292 return '';
293 }
294 classes = [];
295 classes.push("group-result");
296 if (group.classes) {
297 classes.push(group.classes);
298 }
299 group_el = document.createElement("li");
300 group_el.className = classes.join(" ");
301 group_el.innerHTML = group.highlighted_html || this.escape_html(group.label);
302 if (group.title) {
303 group_el.title = group.title;
304 }
305 return this.outerHTML(group_el);
306 };
307
308 AbstractChosen.prototype.results_update_field = function() {
309 this.set_default_text();
310 if (!this.is_multiple) {
311 this.results_reset_cleanup();
312 }
313 this.result_clear_highlight();
314 this.results_build();
315 if (this.results_showing) {
316 return this.winnow_results();
317 }
318 };
319
320 AbstractChosen.prototype.reset_single_select_options = function() {
321 var i, len, ref, result, results1;
322 ref = this.results_data;
323 results1 = [];
324 for (i = 0, len = ref.length; i < len; i++) {
325 result = ref[i];
326 if (result.selected) {
327 results1.push(result.selected = false);
328 } else {
329 results1.push(void 0);
330 }
331 }
332 return results1;
333 };
334
335 AbstractChosen.prototype.results_toggle = function() {
336 if (this.results_showing) {
337 return this.results_hide();
338 } else {
339 return this.results_show();
340 }
341 };
342
343 AbstractChosen.prototype.results_search = function(evt) {
344 if (this.results_showing) {
345 return this.winnow_results();
346 } else {
347 return this.results_show();
348 }
349 };
350
351 AbstractChosen.prototype.winnow_results = function(options) {
352 var escapedQuery, fix, i, len, option, prefix, query, ref, regex, results, results_group, search_match, startpos, suffix, text;
353 this.no_results_clear();
354 results = 0;
355 query = this.get_search_text();
356 escapedQuery = query.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
357 regex = this.get_search_regex(escapedQuery);
358 ref = this.results_data;
359 for (i = 0, len = ref.length; i < len; i++) {
360 option = ref[i];
361 option.search_match = false;
362 results_group = null;
363 search_match = null;
364 option.highlighted_html = '';
365 if (this.include_option_in_results(option)) {
366 if (option.group) {
367 option.group_match = false;
368 option.active_options = 0;
369 }
370 if ((option.group_array_index != null) && this.results_data[option.group_array_index]) {
371 results_group = this.results_data[option.group_array_index];
372 if (results_group.active_options === 0 && results_group.search_match) {
373 results += 1;
374 }
375 results_group.active_options += 1;
376 }
377 text = option.group ? option.label : option.text;
378 if (!(option.group && !this.group_search)) {
379 search_match = this.search_string_match(text, regex);
380 option.search_match = search_match != null;
381 if (option.search_match && !option.group) {
382 results += 1;
383 }
384 if (option.search_match) {
385 if (query.length) {
386 startpos = search_match.index;
387 prefix = text.slice(0, startpos);
388 fix = text.slice(startpos, startpos + query.length);
389 suffix = text.slice(startpos + query.length);
390 option.highlighted_html = (this.escape_html(prefix)) + "<em>" + (this.escape_html(fix)) + "</em>" + (this.escape_html(suffix));
391 }
392 if (results_group != null) {
393 results_group.group_match = true;
394 }
395 } else if ((option.group_array_index != null) && this.results_data[option.group_array_index].search_match) {
396 option.search_match = true;
397 }
398 }
399 }
400 }
401 this.result_clear_highlight();
402 if (results < 1 && query.length) {
403 this.update_results_content("");
404 return this.no_results(query);
405 } else {
406 this.update_results_content(this.results_option_build());
407 if (!(options != null ? options.skip_highlight : void 0)) {
408 return this.winnow_results_set_highlight();
409 }
410 }
411 };
412
413 AbstractChosen.prototype.get_search_regex = function(escaped_search_string) {
414 var regex_flag, regex_string;
415 regex_string = this.search_contains ? escaped_search_string : "(^|\\s|\\b)" + escaped_search_string + "[^\\s]*";
416 if (!(this.enable_split_word_search || this.search_contains)) {
417 regex_string = "^" + regex_string;
418 }
419 regex_flag = this.case_sensitive_search ? "" : "i";
420 return new RegExp(regex_string, regex_flag);
421 };
422
423 AbstractChosen.prototype.search_string_match = function(search_string, regex) {
424 var match;
425 match = regex.exec(search_string);
426 if (!this.search_contains && (match != null ? match[1] : void 0)) {
427 match.index += 1;
428 }
429 return match;
430 };
431
432 AbstractChosen.prototype.choices_count = function() {
433 var i, len, option, ref;
434 if (this.selected_option_count != null) {
435 return this.selected_option_count;
436 }
437 this.selected_option_count = 0;
438 ref = this.form_field.options;
439 for (i = 0, len = ref.length; i < len; i++) {
440 option = ref[i];
441 if (option.selected) {
442 this.selected_option_count += 1;
443 }
444 }
445 return this.selected_option_count;
446 };
447
448 AbstractChosen.prototype.choices_click = function(evt) {
449 evt.preventDefault();
450 this.activate_field();
451 if (!(this.results_showing || this.is_disabled)) {
452 return this.results_show();
453 }
454 };
455
456 AbstractChosen.prototype.keydown_checker = function(evt) {
457 var ref, stroke;
458 stroke = (ref = evt.which) != null ? ref : evt.keyCode;
459 this.search_field_scale();
460 if (stroke !== 8 && this.pending_backstroke) {
461 this.clear_backstroke();
462 }
463 switch (stroke) {
464 case 8:
465 this.backstroke_length = this.get_search_field_value().length;
466 break;
467 case 9:
468 if (this.results_showing && !this.is_multiple) {
469 this.result_select(evt);
470 }
471 this.mouse_on_container = false;
472 break;
473 case 13:
474 if (this.results_showing) {
475 evt.preventDefault();
476 }
477 break;
478 case 27:
479 if (this.results_showing) {
480 evt.preventDefault();
481 }
482 break;
483 case 32:
484 if (this.disable_search) {
485 evt.preventDefault();
486 }
487 break;
488 case 38:
489 evt.preventDefault();
490 this.keyup_arrow();
491 break;
492 case 40:
493 evt.preventDefault();
494 this.keydown_arrow();
495 break;
496 }
497 };
498
499 AbstractChosen.prototype.keyup_checker = function(evt) {
500 var ref, stroke;
501 stroke = (ref = evt.which) != null ? ref : evt.keyCode;
502 this.search_field_scale();
503 switch (stroke) {
504 case 8:
505 if (this.is_multiple && this.backstroke_length < 1 && this.choices_count() > 0) {
506 this.keydown_backstroke();
507 } else if (!this.pending_backstroke) {
508 this.result_clear_highlight();
509 this.results_search();
510 }
511 break;
512 case 13:
513 evt.preventDefault();
514 if (this.results_showing) {
515 this.result_select(evt);
516 }
517 break;
518 case 27:
519 if (this.results_showing) {
520 this.results_hide();
521 }
522 break;
523 case 9:
524 case 16:
525 case 17:
526 case 18:
527 case 38:
528 case 40:
529 case 91:
530 break;
531 default:
532 this.results_search();
533 break;
534 }
535 };
536
537 AbstractChosen.prototype.clipboard_event_checker = function(evt) {
538 if (this.is_disabled) {
539 return;
540 }
541 return setTimeout(((function(_this) {
542 return function() {
543 return _this.results_search();
544 };
545 })(this)), 50);
546 };
547
548 AbstractChosen.prototype.container_width = function() {
549 if (this.options.width != null) {
550 return this.options.width;
551 } else {
552 return this.form_field.offsetWidth + "px";
553 }
554 };
555
556 AbstractChosen.prototype.include_option_in_results = function(option) {
557 if (this.is_multiple && (!this.display_selected_options && option.selected)) {
558 return false;
559 }
560 if (!this.display_disabled_options && option.disabled) {
561 return false;
562 }
563 if (option.empty) {
564 return false;
565 }
566 return true;
567 };
568
569 AbstractChosen.prototype.search_results_touchstart = function(evt) {
570 this.touch_started = true;
571 return this.search_results_mouseover(evt);
572 };
573
574 AbstractChosen.prototype.search_results_touchmove = function(evt) {
575 this.touch_started = false;
576 return this.search_results_mouseout(evt);
577 };
578
579 AbstractChosen.prototype.search_results_touchend = function(evt) {
580 if (this.touch_started) {
581 return this.search_results_mouseup(evt);
582 }
583 };
584
585 AbstractChosen.prototype.outerHTML = function(element) {
586 var tmp;
587 if (element.outerHTML) {
588 return element.outerHTML;
589 }
590 tmp = document.createElement("div");
591 tmp.appendChild(element);
592 return tmp.innerHTML;
593 };
594
595 AbstractChosen.prototype.get_single_html = function() {
596 return "<a class=\"chosen-single chosen-default\">\n <span>" + this.default_text + "</span>\n <div><b></b></div>\n</a>\n<div class=\"chosen-drop\">\n <div class=\"chosen-search\">\n <input class=\"chosen-search-input\" type=\"text\" autocomplete=\"off\" />\n </div>\n <ul class=\"chosen-results\"></ul>\n</div>";
597 };
598
599 AbstractChosen.prototype.get_multi_html = function() {
600 return "<ul class=\"chosen-choices\">\n <li class=\"search-field\">\n <input class=\"chosen-search-input\" type=\"text\" autocomplete=\"off\" value=\"" + this.default_text + "\" />\n </li>\n</ul>\n<div class=\"chosen-drop\">\n <ul class=\"chosen-results\"></ul>\n</div>";
601 };
602
603 AbstractChosen.prototype.get_no_results_html = function(terms) {
604 return "<li class=\"no-results\">\n " + this.results_none_found + " <span>" + (this.escape_html(terms)) + "</span>\n</li>";
605 };
606
607 AbstractChosen.browser_is_supported = function() {
608 if ("Microsoft Internet Explorer" === window.navigator.appName) {
609 return document.documentMode >= 8;
610 }
611 if (/iP(od|hone)/i.test(window.navigator.userAgent) || /IEMobile/i.test(window.navigator.userAgent) || /Windows Phone/i.test(window.navigator.userAgent) || /BlackBerry/i.test(window.navigator.userAgent) || /BB10/i.test(window.navigator.userAgent) || /Android.*Mobile/i.test(window.navigator.userAgent)) {
612 return false;
613 }
614 return true;
615 };
616
617 AbstractChosen.default_multiple_text = "Select Some Options";
618
619 AbstractChosen.default_single_text = "Select an Option";
620
621 AbstractChosen.default_no_result_text = "No results match";
622
623 return AbstractChosen;
624
625 })();
626
627 this.Chosen = (function(superClass) {
628 var triggerHtmlEvent;
629
630 extend(Chosen, superClass);
631
632 function Chosen() {
633 return Chosen.__super__.constructor.apply(this, arguments);
634 }
635
636 Chosen.prototype.setup = function() {
637 return this.current_selectedIndex = this.form_field.selectedIndex;
638 };
639
640 Chosen.prototype.set_up_html = function() {
641 var container_classes, container_props;
642 container_classes = ["chosen-container"];
643 container_classes.push("chosen-container-" + (this.is_multiple ? "multi" : "single"));
644 if (this.inherit_select_classes && this.form_field.className) {
645 container_classes.push(this.form_field.className);
646 }
647 if (this.is_rtl) {
648 container_classes.push("chosen-rtl");
649 }
650 container_props = {
651 'class': container_classes.join(' '),
652 'title': this.form_field.title
653 };
654 if (this.form_field.id.length) {
655 container_props.id = this.form_field.id.replace(/[^\w]/g, '_') + "_chosen";
656 }
657 this.container = new Element('div', container_props);
658 this.container.setStyle({
659 width: this.container_width()
660 });
661 if (this.is_multiple) {
662 this.container.update(this.get_multi_html());
663 } else {
664 this.container.update(this.get_single_html());
665 }
666 this.form_field.hide().insert({
667 after: this.container
668 });
669 this.dropdown = this.container.down('div.chosen-drop');
670 this.search_field = this.container.down('input');
671 this.search_results = this.container.down('ul.chosen-results');
672 this.search_field_scale();
673 this.search_no_results = this.container.down('li.no-results');
674 if (this.is_multiple) {
675 this.search_choices = this.container.down('ul.chosen-choices');
676 this.search_container = this.container.down('li.search-field');
677 } else {
678 this.search_container = this.container.down('div.chosen-search');
679 this.selected_item = this.container.down('.chosen-single');
680 }
681 this.results_build();
682 this.set_tab_index();
683 return this.set_label_behavior();
684 };
685
686 Chosen.prototype.on_ready = function() {
687 return this.form_field.fire("chosen:ready", {
688 chosen: this
689 });
690 };
691
692 Chosen.prototype.register_observers = function() {
693 this.container.observe("touchstart", (function(_this) {
694 return function(evt) {
695 return _this.container_mousedown(evt);
696 };
697 })(this));
698 this.container.observe("touchend", (function(_this) {
699 return function(evt) {
700 return _this.container_mouseup(evt);
701 };
702 })(this));
703 this.container.observe("mousedown", (function(_this) {
704 return function(evt) {
705 return _this.container_mousedown(evt);
706 };
707 })(this));
708 this.container.observe("mouseup", (function(_this) {
709 return function(evt) {
710 return _this.container_mouseup(evt);
711 };
712 })(this));
713 this.container.observe("mouseenter", (function(_this) {
714 return function(evt) {
715 return _this.mouse_enter(evt);
716 };
717 })(this));
718 this.container.observe("mouseleave", (function(_this) {
719 return function(evt) {
720 return _this.mouse_leave(evt);
721 };
722 })(this));
723 this.search_results.observe("mouseup", (function(_this) {
724 return function(evt) {
725 return _this.search_results_mouseup(evt);
726 };
727 })(this));
728 this.search_results.observe("mouseover", (function(_this) {
729 return function(evt) {
730 return _this.search_results_mouseover(evt);
731 };
732 })(this));
733 this.search_results.observe("mouseout", (function(_this) {
734 return function(evt) {
735 return _this.search_results_mouseout(evt);
736 };
737 })(this));
738 this.search_results.observe("mousewheel", (function(_this) {
739 return function(evt) {
740 return _this.search_results_mousewheel(evt);
741 };
742 })(this));
743 this.search_results.observe("DOMMouseScroll", (function(_this) {
744 return function(evt) {
745 return _this.search_results_mousewheel(evt);
746 };
747 })(this));
748 this.search_results.observe("touchstart", (function(_this) {
749 return function(evt) {
750 return _this.search_results_touchstart(evt);
751 };
752 })(this));
753 this.search_results.observe("touchmove", (function(_this) {
754 return function(evt) {
755 return _this.search_results_touchmove(evt);
756 };
757 })(this));
758 this.search_results.observe("touchend", (function(_this) {
759 return function(evt) {
760 return _this.search_results_touchend(evt);
761 };
762 })(this));
763 this.form_field.observe("chosen:updated", (function(_this) {
764 return function(evt) {
765 return _this.results_update_field(evt);
766 };
767 })(this));
768 this.form_field.observe("chosen:activate", (function(_this) {
769 return function(evt) {
770 return _this.activate_field(evt);
771 };
772 })(this));
773 this.form_field.observe("chosen:open", (function(_this) {
774 return function(evt) {
775 return _this.container_mousedown(evt);
776 };
777 })(this));
778 this.form_field.observe("chosen:close", (function(_this) {
779 return function(evt) {
780 return _this.close_field(evt);
781 };
782 })(this));
783 this.search_field.observe("blur", (function(_this) {
784 return function(evt) {
785 return _this.input_blur(evt);
786 };
787 })(this));
788 this.search_field.observe("keyup", (function(_this) {
789 return function(evt) {
790 return _this.keyup_checker(evt);
791 };
792 })(this));
793 this.search_field.observe("keydown", (function(_this) {
794 return function(evt) {
795 return _this.keydown_checker(evt);
796 };
797 })(this));
798 this.search_field.observe("focus", (function(_this) {
799 return function(evt) {
800 return _this.input_focus(evt);
801 };
802 })(this));
803 this.search_field.observe("cut", (function(_this) {
804 return function(evt) {
805 return _this.clipboard_event_checker(evt);
806 };
807 })(this));
808 this.search_field.observe("paste", (function(_this) {
809 return function(evt) {
810 return _this.clipboard_event_checker(evt);
811 };
812 })(this));
813 if (this.is_multiple) {
814 return this.search_choices.observe("click", (function(_this) {
815 return function(evt) {
816 return _this.choices_click(evt);
817 };
818 })(this));
819 } else {
820 return this.container.observe("click", (function(_this) {
821 return function(evt) {
822 return evt.preventDefault();
823 };
824 })(this));
825 }
826 };
827
828 Chosen.prototype.destroy = function() {
829 var event, i, len, ref;
830 this.container.ownerDocument.stopObserving("click", this.click_test_action);
831 ref = ['chosen:updated', 'chosen:activate', 'chosen:open', 'chosen:close'];
832 for (i = 0, len = ref.length; i < len; i++) {
833 event = ref[i];
834 this.form_field.stopObserving(event);
835 }
836 this.container.stopObserving();
837 this.search_results.stopObserving();
838 this.search_field.stopObserving();
839 if (this.form_field_label != null) {
840 this.form_field_label.stopObserving();
841 }
842 if (this.is_multiple) {
843 this.search_choices.stopObserving();
844 this.container.select(".search-choice-close").each(function(choice) {
845 return choice.stopObserving();
846 });
847 } else {
848 this.selected_item.stopObserving();
849 }
850 if (this.search_field.tabIndex) {
851 this.form_field.tabIndex = this.search_field.tabIndex;
852 }
853 this.container.remove();
854 return this.form_field.show();
855 };
856
857 Chosen.prototype.search_field_disabled = function() {
858 var ref;
859 this.is_disabled = this.form_field.disabled || ((ref = this.form_field.up('fieldset')) != null ? ref.disabled : void 0) || false;
860 if (this.is_disabled) {
861 this.container.addClassName('chosen-disabled');
862 } else {
863 this.container.removeClassName('chosen-disabled');
864 }
865 this.search_field.disabled = this.is_disabled;
866 if (!this.is_multiple) {
867 this.selected_item.stopObserving('focus', this.activate_field);
868 }
869 if (this.is_disabled) {
870 return this.close_field();
871 } else if (!this.is_multiple) {
872 return this.selected_item.observe('focus', this.activate_field);
873 }
874 };
875
876 Chosen.prototype.container_mousedown = function(evt) {
877 var ref;
878 if (this.is_disabled) {
879 return;
880 }
881 if (evt && ((ref = evt.type) === 'mousedown' || ref === 'touchstart') && !this.results_showing) {
882 evt.preventDefault();
883 }
884 if (!((evt != null) && evt.target.hasClassName("search-choice-close"))) {
885 if (!this.active_field) {
886 if (this.is_multiple) {
887 this.search_field.clear();
888 }
889 this.container.ownerDocument.observe("click", this.click_test_action);
890 this.results_show();
891 } else if (!this.is_multiple && evt && (evt.target === this.selected_item || evt.target.up("a.chosen-single"))) {
892 this.results_toggle();
893 }
894 return this.activate_field();
895 }
896 };
897
898 Chosen.prototype.container_mouseup = function(evt) {
899 if (evt.target.nodeName === "ABBR" && !this.is_disabled) {
900 return this.results_reset(evt);
901 }
902 };
903
904 Chosen.prototype.search_results_mousewheel = function(evt) {
905 var delta;
906 delta = evt.deltaY || -evt.wheelDelta || evt.detail;
907 if (delta != null) {
908 evt.preventDefault();
909 if (evt.type === 'DOMMouseScroll') {
910 delta = delta * 40;
911 }
912 return this.search_results.scrollTop = delta + this.search_results.scrollTop;
913 }
914 };
915
916 Chosen.prototype.blur_test = function(evt) {
917 if (!this.active_field && this.container.hasClassName("chosen-container-active")) {
918 return this.close_field();
919 }
920 };
921
922 Chosen.prototype.close_field = function() {
923 this.container.ownerDocument.stopObserving("click", this.click_test_action);
924 this.active_field = false;
925 this.results_hide();
926 this.container.removeClassName("chosen-container-active");
927 this.clear_backstroke();
928 this.show_search_field_default();
929 this.search_field_scale();
930 return this.search_field.trigger( 'blur' ); // FixIn: 8.7.11.12.
931 };
932
933 Chosen.prototype.activate_field = function() {
934 if (this.is_disabled) {
935 return;
936 }
937 this.container.addClassName("chosen-container-active");
938 this.active_field = true;
939 this.search_field.value = this.get_search_field_value();
940 return this.search_field.focus();
941 };
942
943 Chosen.prototype.test_active_click = function(evt) {
944 if (evt.target.up('.chosen-container') === this.container) {
945 return this.active_field = true;
946 } else {
947 return this.close_field();
948 }
949 };
950
951 Chosen.prototype.results_build = function() {
952 this.parsing = true;
953 this.selected_option_count = null;
954 this.results_data = SelectParser.select_to_array(this.form_field);
955 if (this.is_multiple) {
956 this.search_choices.select("li.search-choice").invoke("remove");
957 } else {
958 this.single_set_selected_text();
959 if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) {
960 this.search_field.readOnly = true;
961 this.container.addClassName("chosen-container-single-nosearch");
962 } else {
963 this.search_field.readOnly = false;
964 this.container.removeClassName("chosen-container-single-nosearch");
965 }
966 }
967 this.update_results_content(this.results_option_build({
968 first: true
969 }));
970 this.search_field_disabled();
971 this.show_search_field_default();
972 this.search_field_scale();
973 return this.parsing = false;
974 };
975
976 Chosen.prototype.result_do_highlight = function(el) {
977 var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
978 this.result_clear_highlight();
979 this.result_highlight = el;
980 this.result_highlight.addClassName("highlighted");
981 maxHeight = parseInt(this.search_results.getStyle('maxHeight'), 10);
982 visible_top = this.search_results.scrollTop;
983 visible_bottom = maxHeight + visible_top;
984 high_top = this.result_highlight.positionedOffset().top;
985 high_bottom = high_top + this.result_highlight.getHeight();
986 if (high_bottom >= visible_bottom) {
987 return this.search_results.scrollTop = (high_bottom - maxHeight) > 0 ? high_bottom - maxHeight : 0;
988 } else if (high_top < visible_top) {
989 return this.search_results.scrollTop = high_top;
990 }
991 };
992
993 Chosen.prototype.result_clear_highlight = function() {
994 if (this.result_highlight) {
995 this.result_highlight.removeClassName('highlighted');
996 }
997 return this.result_highlight = null;
998 };
999
1000 Chosen.prototype.results_show = function() {
1001 if (this.is_multiple && this.max_selected_options <= this.choices_count()) {
1002 this.form_field.fire("chosen:maxselected", {
1003 chosen: this
1004 });
1005 return false;
1006 }
1007 this.container.addClassName("chosen-with-drop");
1008 this.results_showing = true;
1009 this.search_field.focus();
1010 this.search_field.value = this.get_search_field_value();
1011 this.winnow_results();
1012 return this.form_field.fire("chosen:showing_dropdown", {
1013 chosen: this
1014 });
1015 };
1016
1017 Chosen.prototype.update_results_content = function(content) {
1018 return this.search_results.update(content);
1019 };
1020
1021 Chosen.prototype.results_hide = function() {
1022 if (this.results_showing) {
1023 this.result_clear_highlight();
1024 this.container.removeClassName("chosen-with-drop");
1025 this.form_field.fire("chosen:hiding_dropdown", {
1026 chosen: this
1027 });
1028 }
1029 return this.results_showing = false;
1030 };
1031
1032 Chosen.prototype.set_tab_index = function(el) {
1033 var ti;
1034 if (this.form_field.tabIndex) {
1035 ti = this.form_field.tabIndex;
1036 this.form_field.tabIndex = -1;
1037 return this.search_field.tabIndex = ti;
1038 }
1039 };
1040
1041 Chosen.prototype.set_label_behavior = function() {
1042 this.form_field_label = this.form_field.up("label");
1043 if (this.form_field_label == null) {
1044 this.form_field_label = $$("label[for='" + this.form_field.id + "']").first();
1045 }
1046 if (this.form_field_label != null) {
1047 return this.form_field_label.observe("click", this.label_click_handler);
1048 }
1049 };
1050
1051 Chosen.prototype.show_search_field_default = function() {
1052 if (this.is_multiple && this.choices_count() < 1 && !this.active_field) {
1053 this.search_field.value = this.default_text;
1054 return this.search_field.addClassName("default");
1055 } else {
1056 this.search_field.value = "";
1057 return this.search_field.removeClassName("default");
1058 }
1059 };
1060
1061 Chosen.prototype.search_results_mouseup = function(evt) {
1062 var target;
1063 target = evt.target.hasClassName("active-result") ? evt.target : evt.target.up(".active-result");
1064 if (target) {
1065 this.result_highlight = target;
1066 this.result_select(evt);
1067 return this.search_field.focus();
1068 }
1069 };
1070
1071 Chosen.prototype.search_results_mouseover = function(evt) {
1072 var target;
1073 target = evt.target.hasClassName("active-result") ? evt.target : evt.target.up(".active-result");
1074 if (target) {
1075 return this.result_do_highlight(target);
1076 }
1077 };
1078
1079 Chosen.prototype.search_results_mouseout = function(evt) {
1080 if (evt.target.hasClassName('active-result') || evt.target.up('.active-result')) {
1081 return this.result_clear_highlight();
1082 }
1083 };
1084
1085 Chosen.prototype.choice_build = function(item) {
1086 var choice, close_link;
1087 choice = new Element('li', {
1088 "class": "search-choice"
1089 }).update("<span>" + (this.choice_label(item)) + "</span>");
1090 if (item.disabled) {
1091 choice.addClassName('search-choice-disabled');
1092 } else {
1093 close_link = new Element('a', {
1094 href: '#',
1095 "class": 'search-choice-close',
1096 rel: item.array_index
1097 });
1098 close_link.observe("click", (function(_this) {
1099 return function(evt) {
1100 return _this.choice_destroy_link_click(evt);
1101 };
1102 })(this));
1103 choice.insert(close_link);
1104 }
1105 return this.search_container.insert({
1106 before: choice
1107 });
1108 };
1109
1110 Chosen.prototype.choice_destroy_link_click = function(evt) {
1111 evt.preventDefault();
1112 evt.stopPropagation();
1113 if (!this.is_disabled) {
1114 return this.choice_destroy(evt.target);
1115 }
1116 };
1117
1118 Chosen.prototype.choice_destroy = function(link) {
1119 if (this.result_deselect(link.readAttribute("rel"))) {
1120 if (this.active_field) {
1121 this.search_field.focus();
1122 } else {
1123 this.show_search_field_default();
1124 }
1125 if (this.is_multiple && this.choices_count() > 0 && this.get_search_field_value().length < 1) {
1126 this.results_hide();
1127 }
1128 link.up('li').remove();
1129 return this.search_field_scale();
1130 }
1131 };
1132
1133 Chosen.prototype.results_reset = function() {
1134 this.reset_single_select_options();
1135 this.form_field.options[0].selected = true;
1136 this.single_set_selected_text();
1137 this.show_search_field_default();
1138 this.results_reset_cleanup();
1139 this.trigger_form_field_change();
1140 if (this.active_field) {
1141 return this.results_hide();
1142 }
1143 };
1144
1145 Chosen.prototype.results_reset_cleanup = function() {
1146 var deselect_trigger;
1147 this.current_selectedIndex = this.form_field.selectedIndex;
1148 deselect_trigger = this.selected_item.down("abbr");
1149 if (deselect_trigger) {
1150 return deselect_trigger.remove();
1151 }
1152 };
1153
1154 Chosen.prototype.result_select = function(evt) {
1155 var high, item;
1156 if (this.result_highlight) {
1157 high = this.result_highlight;
1158 this.result_clear_highlight();
1159 if (this.is_multiple && this.max_selected_options <= this.choices_count()) {
1160 this.form_field.fire("chosen:maxselected", {
1161 chosen: this
1162 });
1163 return false;
1164 }
1165 if (this.is_multiple) {
1166 high.removeClassName("active-result");
1167 } else {
1168 this.reset_single_select_options();
1169 }
1170 high.addClassName("result-selected");
1171 item = this.results_data[high.getAttribute("data-option-array-index")];
1172 item.selected = true;
1173 this.form_field.options[item.options_index].selected = true;
1174 this.selected_option_count = null;
1175 if (this.is_multiple) {
1176 this.choice_build(item);
1177 } else {
1178 this.single_set_selected_text(this.choice_label(item));
1179 }
1180 if (this.is_multiple && (!this.hide_results_on_select || (evt.metaKey || evt.ctrlKey))) {
1181 if (evt.metaKey || evt.ctrlKey) {
1182 this.winnow_results({
1183 skip_highlight: true
1184 });
1185 } else {
1186 this.search_field.value = "";
1187 this.winnow_results();
1188 }
1189 } else {
1190 this.results_hide();
1191 this.show_search_field_default();
1192 }
1193 if (this.is_multiple || this.form_field.selectedIndex !== this.current_selectedIndex) {
1194 this.trigger_form_field_change();
1195 }
1196 this.current_selectedIndex = this.form_field.selectedIndex;
1197 evt.preventDefault();
1198 return this.search_field_scale();
1199 }
1200 };
1201
1202 Chosen.prototype.single_set_selected_text = function(text) {
1203 if (text == null) {
1204 text = this.default_text;
1205 }
1206 if (text === this.default_text) {
1207 this.selected_item.addClassName("chosen-default");
1208 } else {
1209 this.single_deselect_control_build();
1210 this.selected_item.removeClassName("chosen-default");
1211 }
1212 return this.selected_item.down("span").update(text);
1213 };
1214
1215 Chosen.prototype.result_deselect = function(pos) {
1216 var result_data;
1217 result_data = this.results_data[pos];
1218 if (!this.form_field.options[result_data.options_index].disabled) {
1219 result_data.selected = false;
1220 this.form_field.options[result_data.options_index].selected = false;
1221 this.selected_option_count = null;
1222 this.result_clear_highlight();
1223 if (this.results_showing) {
1224 this.winnow_results();
1225 }
1226 this.trigger_form_field_change();
1227 this.search_field_scale();
1228 return true;
1229 } else {
1230 return false;
1231 }
1232 };
1233
1234 Chosen.prototype.single_deselect_control_build = function() {
1235 if (!this.allow_single_deselect) {
1236 return;
1237 }
1238 if (!this.selected_item.down("abbr")) {
1239 this.selected_item.down("span").insert({
1240 after: "<abbr class=\"search-choice-close\"></abbr>"
1241 });
1242 }
1243 return this.selected_item.addClassName("chosen-single-with-deselect");
1244 };
1245
1246 Chosen.prototype.get_search_field_value = function() {
1247 return this.search_field.value;
1248 };
1249
1250 Chosen.prototype.get_search_text = function() {
1251 return this.get_search_field_value().strip();
1252 };
1253
1254 Chosen.prototype.escape_html = function(text) {
1255 return text.escapeHTML();
1256 };
1257
1258 Chosen.prototype.winnow_results_set_highlight = function() {
1259 var do_high;
1260 if (!this.is_multiple) {
1261 do_high = this.search_results.down(".result-selected.active-result");
1262 }
1263 if (do_high == null) {
1264 do_high = this.search_results.down(".active-result");
1265 }
1266 if (do_high != null) {
1267 return this.result_do_highlight(do_high);
1268 }
1269 };
1270
1271 Chosen.prototype.no_results = function(terms) {
1272 this.search_results.insert(this.get_no_results_html(terms));
1273 return this.form_field.fire("chosen:no_results", {
1274 chosen: this
1275 });
1276 };
1277
1278 Chosen.prototype.no_results_clear = function() {
1279 var nr, results1;
1280 nr = null;
1281 results1 = [];
1282 while (nr = this.search_results.down(".no-results")) {
1283 results1.push(nr.remove());
1284 }
1285 return results1;
1286 };
1287
1288 Chosen.prototype.keydown_arrow = function() {
1289 var next_sib;
1290 if (this.results_showing && this.result_highlight) {
1291 next_sib = this.result_highlight.next('.active-result');
1292 if (next_sib) {
1293 return this.result_do_highlight(next_sib);
1294 }
1295 } else {
1296 return this.results_show();
1297 }
1298 };
1299
1300 Chosen.prototype.keyup_arrow = function() {
1301 var actives, prevs, sibs;
1302 if (!this.results_showing && !this.is_multiple) {
1303 return this.results_show();
1304 } else if (this.result_highlight) {
1305 sibs = this.result_highlight.previousSiblings();
1306 actives = this.search_results.select("li.active-result");
1307 prevs = sibs.intersect(actives);
1308 if (prevs.length) {
1309 return this.result_do_highlight(prevs.first());
1310 } else {
1311 if (this.choices_count() > 0) {
1312 this.results_hide();
1313 }
1314 return this.result_clear_highlight();
1315 }
1316 }
1317 };
1318
1319 Chosen.prototype.keydown_backstroke = function() {
1320 var next_available_destroy;
1321 if (this.pending_backstroke) {
1322 this.choice_destroy(this.pending_backstroke.down("a"));
1323 return this.clear_backstroke();
1324 } else {
1325 next_available_destroy = this.search_container.siblings().last();
1326 if (next_available_destroy && next_available_destroy.hasClassName("search-choice") && !next_available_destroy.hasClassName("search-choice-disabled")) {
1327 this.pending_backstroke = next_available_destroy;
1328 if (this.pending_backstroke) {
1329 this.pending_backstroke.addClassName("search-choice-focus");
1330 }
1331 if (this.single_backstroke_delete) {
1332 return this.keydown_backstroke();
1333 } else {
1334 return this.pending_backstroke.addClassName("search-choice-focus");
1335 }
1336 }
1337 }
1338 };
1339
1340 Chosen.prototype.clear_backstroke = function() {
1341 if (this.pending_backstroke) {
1342 this.pending_backstroke.removeClassName("search-choice-focus");
1343 }
1344 return this.pending_backstroke = null;
1345 };
1346
1347 Chosen.prototype.search_field_scale = function() {
1348 var container_width, div, i, len, style, style_block, styles, width;
1349 if (!this.is_multiple) {
1350 return;
1351 }
1352 style_block = {
1353 position: 'absolute',
1354 left: '-1000px',
1355 top: '-1000px',
1356 display: 'none',
1357 whiteSpace: 'pre'
1358 };
1359 styles = ['fontSize', 'fontStyle', 'fontWeight', 'fontFamily', 'lineHeight', 'textTransform', 'letterSpacing'];
1360 for (i = 0, len = styles.length; i < len; i++) {
1361 style = styles[i];
1362 style_block[style] = this.search_field.getStyle(style);
1363 }
1364 div = new Element('div').update(this.escape_html(this.get_search_field_value()));
1365 div.setStyle(style_block);
1366 document.body.appendChild(div);
1367 width = div.measure('width') + 25;
1368 div.remove();
1369 if (container_width = this.container.getWidth()) {
1370 width = Math.min(container_width - 10, width);
1371 }
1372 return this.search_field.setStyle({
1373 width: width + 'px'
1374 });
1375 };
1376
1377 Chosen.prototype.trigger_form_field_change = function() {
1378 triggerHtmlEvent(this.form_field, 'input');
1379 return triggerHtmlEvent(this.form_field, 'change');
1380 };
1381
1382 triggerHtmlEvent = function(element, eventType) {
1383 var error, evt;
1384 if (element.dispatchEvent) {
1385 try {
1386 evt = new Event(eventType, {
1387 bubbles: true,
1388 cancelable: true
1389 });
1390 } catch (error) {
1391 evt = document.createEvent('HTMLEvents');
1392 evt.initEvent(eventType, true, true);
1393 }
1394 return element.dispatchEvent(evt);
1395 } else {
1396 return element.fireEvent("on" + eventType, document.createEventObject());
1397 }
1398 };
1399
1400 return Chosen;
1401
1402 })(AbstractChosen);
1403
1404 }).call(this);
1405