/[projects]/misc/horsensspejder-web/jquery/chosen_v1.0.0/chosen.proto.js
ViewVC logotype

Contents of /misc/horsensspejder-web/jquery/chosen_v1.0.0/chosen.proto.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2125 - (show annotations) (download) (as text)
Wed Mar 12 19:30:05 2014 UTC (10 years, 3 months ago) by torben
File MIME type: application/javascript
File size: 40895 byte(s)
initial import
1 // Chosen, a Select Box Enhancer for jQuery and Prototype
2 // by Patrick Filler for Harvest, http://getharvest.com
3 //
4 // Version 1.0.0
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 `grunt build`, do not edit it by hand.
10 (function() {
11 var AbstractChosen, SelectParser, _ref,
12 __hasProp = {}.hasOwnProperty,
13 __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; };
14
15 SelectParser = (function() {
16 function SelectParser() {
17 this.options_index = 0;
18 this.parsed = [];
19 }
20
21 SelectParser.prototype.add_node = function(child) {
22 if (child.nodeName.toUpperCase() === "OPTGROUP") {
23 return this.add_group(child);
24 } else {
25 return this.add_option(child);
26 }
27 };
28
29 SelectParser.prototype.add_group = function(group) {
30 var group_position, option, _i, _len, _ref, _results;
31
32 group_position = this.parsed.length;
33 this.parsed.push({
34 array_index: group_position,
35 group: true,
36 label: this.escapeExpression(group.label),
37 children: 0,
38 disabled: group.disabled
39 });
40 _ref = group.childNodes;
41 _results = [];
42 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
43 option = _ref[_i];
44 _results.push(this.add_option(option, group_position, group.disabled));
45 }
46 return _results;
47 };
48
49 SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
50 if (option.nodeName.toUpperCase() === "OPTION") {
51 if (option.text !== "") {
52 if (group_position != null) {
53 this.parsed[group_position].children += 1;
54 }
55 this.parsed.push({
56 array_index: this.parsed.length,
57 options_index: this.options_index,
58 value: option.value,
59 text: option.text,
60 html: option.innerHTML,
61 selected: option.selected,
62 disabled: group_disabled === true ? group_disabled : option.disabled,
63 group_array_index: group_position,
64 classes: option.className,
65 style: option.style.cssText
66 });
67 } else {
68 this.parsed.push({
69 array_index: this.parsed.length,
70 options_index: this.options_index,
71 empty: true
72 });
73 }
74 return this.options_index += 1;
75 }
76 };
77
78 SelectParser.prototype.escapeExpression = function(text) {
79 var map, unsafe_chars;
80
81 if ((text == null) || text === false) {
82 return "";
83 }
84 if (!/[\&\<\>\"\'\`]/.test(text)) {
85 return text;
86 }
87 map = {
88 "<": "&lt;",
89 ">": "&gt;",
90 '"': "&quot;",
91 "'": "&#x27;",
92 "`": "&#x60;"
93 };
94 unsafe_chars = /&(?!\w+;)|[\<\>\"\'\`]/g;
95 return text.replace(unsafe_chars, function(chr) {
96 return map[chr] || "&amp;";
97 });
98 };
99
100 return SelectParser;
101
102 })();
103
104 SelectParser.select_to_array = function(select) {
105 var child, parser, _i, _len, _ref;
106
107 parser = new SelectParser();
108 _ref = select.childNodes;
109 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
110 child = _ref[_i];
111 parser.add_node(child);
112 }
113 return parser.parsed;
114 };
115
116 AbstractChosen = (function() {
117 function AbstractChosen(form_field, options) {
118 this.form_field = form_field;
119 this.options = options != null ? options : {};
120 if (!AbstractChosen.browser_is_supported()) {
121 return;
122 }
123 this.is_multiple = this.form_field.multiple;
124 this.set_default_text();
125 this.set_default_values();
126 this.setup();
127 this.set_up_html();
128 this.register_observers();
129 }
130
131 AbstractChosen.prototype.set_default_values = function() {
132 var _this = this;
133
134 this.click_test_action = function(evt) {
135 return _this.test_active_click(evt);
136 };
137 this.activate_action = function(evt) {
138 return _this.activate_field(evt);
139 };
140 this.active_field = false;
141 this.mouse_on_container = false;
142 this.results_showing = false;
143 this.result_highlighted = null;
144 this.result_single_selected = null;
145 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;
146 this.disable_search_threshold = this.options.disable_search_threshold || 0;
147 this.disable_search = this.options.disable_search || false;
148 this.enable_split_word_search = this.options.enable_split_word_search != null ? this.options.enable_split_word_search : true;
149 this.group_search = this.options.group_search != null ? this.options.group_search : true;
150 this.search_contains = this.options.search_contains || false;
151 this.single_backstroke_delete = this.options.single_backstroke_delete != null ? this.options.single_backstroke_delete : true;
152 this.max_selected_options = this.options.max_selected_options || Infinity;
153 this.inherit_select_classes = this.options.inherit_select_classes || false;
154 this.display_selected_options = this.options.display_selected_options != null ? this.options.display_selected_options : true;
155 return this.display_disabled_options = this.options.display_disabled_options != null ? this.options.display_disabled_options : true;
156 };
157
158 AbstractChosen.prototype.set_default_text = function() {
159 if (this.form_field.getAttribute("data-placeholder")) {
160 this.default_text = this.form_field.getAttribute("data-placeholder");
161 } else if (this.is_multiple) {
162 this.default_text = this.options.placeholder_text_multiple || this.options.placeholder_text || AbstractChosen.default_multiple_text;
163 } else {
164 this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || AbstractChosen.default_single_text;
165 }
166 return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || AbstractChosen.default_no_result_text;
167 };
168
169 AbstractChosen.prototype.mouse_enter = function() {
170 return this.mouse_on_container = true;
171 };
172
173 AbstractChosen.prototype.mouse_leave = function() {
174 return this.mouse_on_container = false;
175 };
176
177 AbstractChosen.prototype.input_focus = function(evt) {
178 var _this = this;
179
180 if (this.is_multiple) {
181 if (!this.active_field) {
182 return setTimeout((function() {
183 return _this.container_mousedown();
184 }), 50);
185 }
186 } else {
187 if (!this.active_field) {
188 return this.activate_field();
189 }
190 }
191 };
192
193 AbstractChosen.prototype.input_blur = function(evt) {
194 var _this = this;
195
196 if (!this.mouse_on_container) {
197 this.active_field = false;
198 return setTimeout((function() {
199 return _this.blur_test();
200 }), 100);
201 }
202 };
203
204 AbstractChosen.prototype.results_option_build = function(options) {
205 var content, data, _i, _len, _ref;
206
207 content = '';
208 _ref = this.results_data;
209 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
210 data = _ref[_i];
211 if (data.group) {
212 content += this.result_add_group(data);
213 } else {
214 content += this.result_add_option(data);
215 }
216 if (options != null ? options.first : void 0) {
217 if (data.selected && this.is_multiple) {
218 this.choice_build(data);
219 } else if (data.selected && !this.is_multiple) {
220 this.single_set_selected_text(data.text);
221 }
222 }
223 }
224 return content;
225 };
226
227 AbstractChosen.prototype.result_add_option = function(option) {
228 var classes, style;
229
230 if (!option.search_match) {
231 return '';
232 }
233 if (!this.include_option_in_results(option)) {
234 return '';
235 }
236 classes = [];
237 if (!option.disabled && !(option.selected && this.is_multiple)) {
238 classes.push("active-result");
239 }
240 if (option.disabled && !(option.selected && this.is_multiple)) {
241 classes.push("disabled-result");
242 }
243 if (option.selected) {
244 classes.push("result-selected");
245 }
246 if (option.group_array_index != null) {
247 classes.push("group-option");
248 }
249 if (option.classes !== "") {
250 classes.push(option.classes);
251 }
252 style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : "";
253 return "<li class=\"" + (classes.join(' ')) + "\"" + style + " data-option-array-index=\"" + option.array_index + "\">" + option.search_text + "</li>";
254 };
255
256 AbstractChosen.prototype.result_add_group = function(group) {
257 if (!(group.search_match || group.group_match)) {
258 return '';
259 }
260 if (!(group.active_options > 0)) {
261 return '';
262 }
263 return "<li class=\"group-result\">" + group.search_text + "</li>";
264 };
265
266 AbstractChosen.prototype.results_update_field = function() {
267 this.set_default_text();
268 if (!this.is_multiple) {
269 this.results_reset_cleanup();
270 }
271 this.result_clear_highlight();
272 this.result_single_selected = null;
273 this.results_build();
274 if (this.results_showing) {
275 return this.winnow_results();
276 }
277 };
278
279 AbstractChosen.prototype.results_toggle = function() {
280 if (this.results_showing) {
281 return this.results_hide();
282 } else {
283 return this.results_show();
284 }
285 };
286
287 AbstractChosen.prototype.results_search = function(evt) {
288 if (this.results_showing) {
289 return this.winnow_results();
290 } else {
291 return this.results_show();
292 }
293 };
294
295 AbstractChosen.prototype.winnow_results = function() {
296 var escapedSearchText, option, regex, regexAnchor, results, results_group, searchText, startpos, text, zregex, _i, _len, _ref;
297
298 this.no_results_clear();
299 results = 0;
300 searchText = this.get_search_text();
301 escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
302 regexAnchor = this.search_contains ? "" : "^";
303 regex = new RegExp(regexAnchor + escapedSearchText, 'i');
304 zregex = new RegExp(escapedSearchText, 'i');
305 _ref = this.results_data;
306 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
307 option = _ref[_i];
308 option.search_match = false;
309 results_group = null;
310 if (this.include_option_in_results(option)) {
311 if (option.group) {
312 option.group_match = false;
313 option.active_options = 0;
314 }
315 if ((option.group_array_index != null) && this.results_data[option.group_array_index]) {
316 results_group = this.results_data[option.group_array_index];
317 if (results_group.active_options === 0 && results_group.search_match) {
318 results += 1;
319 }
320 results_group.active_options += 1;
321 }
322 if (!(option.group && !this.group_search)) {
323 option.search_text = option.group ? option.label : option.html;
324 option.search_match = this.search_string_match(option.search_text, regex);
325 if (option.search_match && !option.group) {
326 results += 1;
327 }
328 if (option.search_match) {
329 if (searchText.length) {
330 startpos = option.search_text.search(zregex);
331 text = option.search_text.substr(0, startpos + searchText.length) + '</em>' + option.search_text.substr(startpos + searchText.length);
332 option.search_text = text.substr(0, startpos) + '<em>' + text.substr(startpos);
333 }
334 if (results_group != null) {
335 results_group.group_match = true;
336 }
337 } else if ((option.group_array_index != null) && this.results_data[option.group_array_index].search_match) {
338 option.search_match = true;
339 }
340 }
341 }
342 }
343 this.result_clear_highlight();
344 if (results < 1 && searchText.length) {
345 this.update_results_content("");
346 return this.no_results(searchText);
347 } else {
348 this.update_results_content(this.results_option_build());
349 return this.winnow_results_set_highlight();
350 }
351 };
352
353 AbstractChosen.prototype.search_string_match = function(search_string, regex) {
354 var part, parts, _i, _len;
355
356 if (regex.test(search_string)) {
357 return true;
358 } else if (this.enable_split_word_search && (search_string.indexOf(" ") >= 0 || search_string.indexOf("[") === 0)) {
359 parts = search_string.replace(/\[|\]/g, "").split(" ");
360 if (parts.length) {
361 for (_i = 0, _len = parts.length; _i < _len; _i++) {
362 part = parts[_i];
363 if (regex.test(part)) {
364 return true;
365 }
366 }
367 }
368 }
369 };
370
371 AbstractChosen.prototype.choices_count = function() {
372 var option, _i, _len, _ref;
373
374 if (this.selected_option_count != null) {
375 return this.selected_option_count;
376 }
377 this.selected_option_count = 0;
378 _ref = this.form_field.options;
379 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
380 option = _ref[_i];
381 if (option.selected) {
382 this.selected_option_count += 1;
383 }
384 }
385 return this.selected_option_count;
386 };
387
388 AbstractChosen.prototype.choices_click = function(evt) {
389 evt.preventDefault();
390 if (!(this.results_showing || this.is_disabled)) {
391 return this.results_show();
392 }
393 };
394
395 AbstractChosen.prototype.keyup_checker = function(evt) {
396 var stroke, _ref;
397
398 stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
399 this.search_field_scale();
400 switch (stroke) {
401 case 8:
402 if (this.is_multiple && this.backstroke_length < 1 && this.choices_count() > 0) {
403 return this.keydown_backstroke();
404 } else if (!this.pending_backstroke) {
405 this.result_clear_highlight();
406 return this.results_search();
407 }
408 break;
409 case 13:
410 evt.preventDefault();
411 if (this.results_showing) {
412 return this.result_select(evt);
413 }
414 break;
415 case 27:
416 if (this.results_showing) {
417 this.results_hide();
418 }
419 return true;
420 case 9:
421 case 38:
422 case 40:
423 case 16:
424 case 91:
425 case 17:
426 break;
427 default:
428 return this.results_search();
429 }
430 };
431
432 AbstractChosen.prototype.container_width = function() {
433 if (this.options.width != null) {
434 return this.options.width;
435 } else {
436 return "" + this.form_field.offsetWidth + "px";
437 }
438 };
439
440 AbstractChosen.prototype.include_option_in_results = function(option) {
441 if (this.is_multiple && (!this.display_selected_options && option.selected)) {
442 return false;
443 }
444 if (!this.display_disabled_options && option.disabled) {
445 return false;
446 }
447 if (option.empty) {
448 return false;
449 }
450 return true;
451 };
452
453 AbstractChosen.browser_is_supported = function() {
454 if (window.navigator.appName === "Microsoft Internet Explorer") {
455 return document.documentMode >= 8;
456 }
457 if (/iP(od|hone)/i.test(window.navigator.userAgent)) {
458 return false;
459 }
460 if (/Android/i.test(window.navigator.userAgent)) {
461 if (/Mobile/i.test(window.navigator.userAgent)) {
462 return false;
463 }
464 }
465 return true;
466 };
467
468 AbstractChosen.default_multiple_text = "Select Some Options";
469
470 AbstractChosen.default_single_text = "Select an Option";
471
472 AbstractChosen.default_no_result_text = "No results match";
473
474 return AbstractChosen;
475
476 })();
477
478 this.Chosen = (function(_super) {
479 __extends(Chosen, _super);
480
481 function Chosen() {
482 _ref = Chosen.__super__.constructor.apply(this, arguments);
483 return _ref;
484 }
485
486 Chosen.prototype.setup = function() {
487 this.current_selectedIndex = this.form_field.selectedIndex;
488 return this.is_rtl = this.form_field.hasClassName("chosen-rtl");
489 };
490
491 Chosen.prototype.set_default_values = function() {
492 Chosen.__super__.set_default_values.call(this);
493 this.single_temp = new Template('<a class="chosen-single chosen-default" tabindex="-1"><span>#{default}</span><div><b></b></div></a><div class="chosen-drop"><div class="chosen-search"><input type="text" autocomplete="off" /></div><ul class="chosen-results"></ul></div>');
494 this.multi_temp = new Template('<ul class="chosen-choices"><li class="search-field"><input type="text" value="#{default}" class="default" autocomplete="off" style="width:25px;" /></li></ul><div class="chosen-drop"><ul class="chosen-results"></ul></div>');
495 return this.no_results_temp = new Template('<li class="no-results">' + this.results_none_found + ' "<span>#{terms}</span>"</li>');
496 };
497
498 Chosen.prototype.set_up_html = function() {
499 var container_classes, container_props;
500
501 container_classes = ["chosen-container"];
502 container_classes.push("chosen-container-" + (this.is_multiple ? "multi" : "single"));
503 if (this.inherit_select_classes && this.form_field.className) {
504 container_classes.push(this.form_field.className);
505 }
506 if (this.is_rtl) {
507 container_classes.push("chosen-rtl");
508 }
509 container_props = {
510 'class': container_classes.join(' '),
511 'style': "width: " + (this.container_width()) + ";",
512 'title': this.form_field.title
513 };
514 if (this.form_field.id.length) {
515 container_props.id = this.form_field.id.replace(/[^\w]/g, '_') + "_chosen";
516 }
517 this.container = this.is_multiple ? new Element('div', container_props).update(this.multi_temp.evaluate({
518 "default": this.default_text
519 })) : new Element('div', container_props).update(this.single_temp.evaluate({
520 "default": this.default_text
521 }));
522 this.form_field.hide().insert({
523 after: this.container
524 });
525 this.dropdown = this.container.down('div.chosen-drop');
526 this.search_field = this.container.down('input');
527 this.search_results = this.container.down('ul.chosen-results');
528 this.search_field_scale();
529 this.search_no_results = this.container.down('li.no-results');
530 if (this.is_multiple) {
531 this.search_choices = this.container.down('ul.chosen-choices');
532 this.search_container = this.container.down('li.search-field');
533 } else {
534 this.search_container = this.container.down('div.chosen-search');
535 this.selected_item = this.container.down('.chosen-single');
536 }
537 this.results_build();
538 this.set_tab_index();
539 this.set_label_behavior();
540 return this.form_field.fire("chosen:ready", {
541 chosen: this
542 });
543 };
544
545 Chosen.prototype.register_observers = function() {
546 var _this = this;
547
548 this.container.observe("mousedown", function(evt) {
549 return _this.container_mousedown(evt);
550 });
551 this.container.observe("mouseup", function(evt) {
552 return _this.container_mouseup(evt);
553 });
554 this.container.observe("mouseenter", function(evt) {
555 return _this.mouse_enter(evt);
556 });
557 this.container.observe("mouseleave", function(evt) {
558 return _this.mouse_leave(evt);
559 });
560 this.search_results.observe("mouseup", function(evt) {
561 return _this.search_results_mouseup(evt);
562 });
563 this.search_results.observe("mouseover", function(evt) {
564 return _this.search_results_mouseover(evt);
565 });
566 this.search_results.observe("mouseout", function(evt) {
567 return _this.search_results_mouseout(evt);
568 });
569 this.search_results.observe("mousewheel", function(evt) {
570 return _this.search_results_mousewheel(evt);
571 });
572 this.search_results.observe("DOMMouseScroll", function(evt) {
573 return _this.search_results_mousewheel(evt);
574 });
575 this.form_field.observe("chosen:updated", function(evt) {
576 return _this.results_update_field(evt);
577 });
578 this.form_field.observe("chosen:activate", function(evt) {
579 return _this.activate_field(evt);
580 });
581 this.form_field.observe("chosen:open", function(evt) {
582 return _this.container_mousedown(evt);
583 });
584 this.search_field.observe("blur", function(evt) {
585 return _this.input_blur(evt);
586 });
587 this.search_field.observe("keyup", function(evt) {
588 return _this.keyup_checker(evt);
589 });
590 this.search_field.observe("keydown", function(evt) {
591 return _this.keydown_checker(evt);
592 });
593 this.search_field.observe("focus", function(evt) {
594 return _this.input_focus(evt);
595 });
596 if (this.is_multiple) {
597 return this.search_choices.observe("click", function(evt) {
598 return _this.choices_click(evt);
599 });
600 } else {
601 return this.container.observe("click", function(evt) {
602 return evt.preventDefault();
603 });
604 }
605 };
606
607 Chosen.prototype.destroy = function() {
608 document.stopObserving("click", this.click_test_action);
609 this.form_field.stopObserving();
610 this.container.stopObserving();
611 this.search_results.stopObserving();
612 this.search_field.stopObserving();
613 if (this.form_field_label != null) {
614 this.form_field_label.stopObserving();
615 }
616 if (this.is_multiple) {
617 this.search_choices.stopObserving();
618 this.container.select(".search-choice-close").each(function(choice) {
619 return choice.stopObserving();
620 });
621 } else {
622 this.selected_item.stopObserving();
623 }
624 if (this.search_field.tabIndex) {
625 this.form_field.tabIndex = this.search_field.tabIndex;
626 }
627 this.container.remove();
628 return this.form_field.show();
629 };
630
631 Chosen.prototype.search_field_disabled = function() {
632 this.is_disabled = this.form_field.disabled;
633 if (this.is_disabled) {
634 this.container.addClassName('chosen-disabled');
635 this.search_field.disabled = true;
636 if (!this.is_multiple) {
637 this.selected_item.stopObserving("focus", this.activate_action);
638 }
639 return this.close_field();
640 } else {
641 this.container.removeClassName('chosen-disabled');
642 this.search_field.disabled = false;
643 if (!this.is_multiple) {
644 return this.selected_item.observe("focus", this.activate_action);
645 }
646 }
647 };
648
649 Chosen.prototype.container_mousedown = function(evt) {
650 if (!this.is_disabled) {
651 if (evt && evt.type === "mousedown" && !this.results_showing) {
652 evt.stop();
653 }
654 if (!((evt != null) && evt.target.hasClassName("search-choice-close"))) {
655 if (!this.active_field) {
656 if (this.is_multiple) {
657 this.search_field.clear();
658 }
659 document.observe("click", this.click_test_action);
660 this.results_show();
661 } else if (!this.is_multiple && evt && (evt.target === this.selected_item || evt.target.up("a.chosen-single"))) {
662 this.results_toggle();
663 }
664 return this.activate_field();
665 }
666 }
667 };
668
669 Chosen.prototype.container_mouseup = function(evt) {
670 if (evt.target.nodeName === "ABBR" && !this.is_disabled) {
671 return this.results_reset(evt);
672 }
673 };
674
675 Chosen.prototype.search_results_mousewheel = function(evt) {
676 var delta;
677
678 delta = -evt.wheelDelta || evt.detail;
679 if (delta != null) {
680 evt.preventDefault();
681 if (evt.type === 'DOMMouseScroll') {
682 delta = delta * 40;
683 }
684 return this.search_results.scrollTop = delta + this.search_results.scrollTop;
685 }
686 };
687
688 Chosen.prototype.blur_test = function(evt) {
689 if (!this.active_field && this.container.hasClassName("chosen-container-active")) {
690 return this.close_field();
691 }
692 };
693
694 Chosen.prototype.close_field = function() {
695 document.stopObserving("click", this.click_test_action);
696 this.active_field = false;
697 this.results_hide();
698 this.container.removeClassName("chosen-container-active");
699 this.clear_backstroke();
700 this.show_search_field_default();
701 return this.search_field_scale();
702 };
703
704 Chosen.prototype.activate_field = function() {
705 this.container.addClassName("chosen-container-active");
706 this.active_field = true;
707 this.search_field.value = this.search_field.value;
708 return this.search_field.focus();
709 };
710
711 Chosen.prototype.test_active_click = function(evt) {
712 if (evt.target.up('.chosen-container') === this.container) {
713 return this.active_field = true;
714 } else {
715 return this.close_field();
716 }
717 };
718
719 Chosen.prototype.results_build = function() {
720 this.parsing = true;
721 this.selected_option_count = null;
722 this.results_data = SelectParser.select_to_array(this.form_field);
723 if (this.is_multiple) {
724 this.search_choices.select("li.search-choice").invoke("remove");
725 } else if (!this.is_multiple) {
726 this.single_set_selected_text();
727 if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) {
728 this.search_field.readOnly = true;
729 this.container.addClassName("chosen-container-single-nosearch");
730 } else {
731 this.search_field.readOnly = false;
732 this.container.removeClassName("chosen-container-single-nosearch");
733 }
734 }
735 this.update_results_content(this.results_option_build({
736 first: true
737 }));
738 this.search_field_disabled();
739 this.show_search_field_default();
740 this.search_field_scale();
741 return this.parsing = false;
742 };
743
744 Chosen.prototype.result_do_highlight = function(el) {
745 var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
746
747 this.result_clear_highlight();
748 this.result_highlight = el;
749 this.result_highlight.addClassName("highlighted");
750 maxHeight = parseInt(this.search_results.getStyle('maxHeight'), 10);
751 visible_top = this.search_results.scrollTop;
752 visible_bottom = maxHeight + visible_top;
753 high_top = this.result_highlight.positionedOffset().top;
754 high_bottom = high_top + this.result_highlight.getHeight();
755 if (high_bottom >= visible_bottom) {
756 return this.search_results.scrollTop = (high_bottom - maxHeight) > 0 ? high_bottom - maxHeight : 0;
757 } else if (high_top < visible_top) {
758 return this.search_results.scrollTop = high_top;
759 }
760 };
761
762 Chosen.prototype.result_clear_highlight = function() {
763 if (this.result_highlight) {
764 this.result_highlight.removeClassName('highlighted');
765 }
766 return this.result_highlight = null;
767 };
768
769 Chosen.prototype.results_show = function() {
770 if (this.is_multiple && this.max_selected_options <= this.choices_count()) {
771 this.form_field.fire("chosen:maxselected", {
772 chosen: this
773 });
774 return false;
775 }
776 this.container.addClassName("chosen-with-drop");
777 this.form_field.fire("chosen:showing_dropdown", {
778 chosen: this
779 });
780 this.results_showing = true;
781 this.search_field.focus();
782 this.search_field.value = this.search_field.value;
783 return this.winnow_results();
784 };
785
786 Chosen.prototype.update_results_content = function(content) {
787 return this.search_results.update(content);
788 };
789
790 Chosen.prototype.results_hide = function() {
791 if (this.results_showing) {
792 this.result_clear_highlight();
793 this.container.removeClassName("chosen-with-drop");
794 this.form_field.fire("chosen:hiding_dropdown", {
795 chosen: this
796 });
797 }
798 return this.results_showing = false;
799 };
800
801 Chosen.prototype.set_tab_index = function(el) {
802 var ti;
803
804 if (this.form_field.tabIndex) {
805 ti = this.form_field.tabIndex;
806 this.form_field.tabIndex = -1;
807 return this.search_field.tabIndex = ti;
808 }
809 };
810
811 Chosen.prototype.set_label_behavior = function() {
812 var _this = this;
813
814 this.form_field_label = this.form_field.up("label");
815 if (this.form_field_label == null) {
816 this.form_field_label = $$("label[for='" + this.form_field.id + "']").first();
817 }
818 if (this.form_field_label != null) {
819 return this.form_field_label.observe("click", function(evt) {
820 if (_this.is_multiple) {
821 return _this.container_mousedown(evt);
822 } else {
823 return _this.activate_field();
824 }
825 });
826 }
827 };
828
829 Chosen.prototype.show_search_field_default = function() {
830 if (this.is_multiple && this.choices_count() < 1 && !this.active_field) {
831 this.search_field.value = this.default_text;
832 return this.search_field.addClassName("default");
833 } else {
834 this.search_field.value = "";
835 return this.search_field.removeClassName("default");
836 }
837 };
838
839 Chosen.prototype.search_results_mouseup = function(evt) {
840 var target;
841
842 target = evt.target.hasClassName("active-result") ? evt.target : evt.target.up(".active-result");
843 if (target) {
844 this.result_highlight = target;
845 this.result_select(evt);
846 return this.search_field.focus();
847 }
848 };
849
850 Chosen.prototype.search_results_mouseover = function(evt) {
851 var target;
852
853 target = evt.target.hasClassName("active-result") ? evt.target : evt.target.up(".active-result");
854 if (target) {
855 return this.result_do_highlight(target);
856 }
857 };
858
859 Chosen.prototype.search_results_mouseout = function(evt) {
860 if (evt.target.hasClassName('active-result') || evt.target.up('.active-result')) {
861 return this.result_clear_highlight();
862 }
863 };
864
865 Chosen.prototype.choice_build = function(item) {
866 var choice, close_link,
867 _this = this;
868
869 choice = new Element('li', {
870 "class": "search-choice"
871 }).update("<span>" + item.html + "</span>");
872 if (item.disabled) {
873 choice.addClassName('search-choice-disabled');
874 } else {
875 close_link = new Element('a', {
876 href: '#',
877 "class": 'search-choice-close',
878 rel: item.array_index
879 });
880 close_link.observe("click", function(evt) {
881 return _this.choice_destroy_link_click(evt);
882 });
883 choice.insert(close_link);
884 }
885 return this.search_container.insert({
886 before: choice
887 });
888 };
889
890 Chosen.prototype.choice_destroy_link_click = function(evt) {
891 evt.preventDefault();
892 evt.stopPropagation();
893 if (!this.is_disabled) {
894 return this.choice_destroy(evt.target);
895 }
896 };
897
898 Chosen.prototype.choice_destroy = function(link) {
899 if (this.result_deselect(link.readAttribute("rel"))) {
900 this.show_search_field_default();
901 if (this.is_multiple && this.choices_count() > 0 && this.search_field.value.length < 1) {
902 this.results_hide();
903 }
904 link.up('li').remove();
905 return this.search_field_scale();
906 }
907 };
908
909 Chosen.prototype.results_reset = function() {
910 this.form_field.options[0].selected = true;
911 this.selected_option_count = null;
912 this.single_set_selected_text();
913 this.show_search_field_default();
914 this.results_reset_cleanup();
915 if (typeof Event.simulate === 'function') {
916 this.form_field.simulate("change");
917 }
918 if (this.active_field) {
919 return this.results_hide();
920 }
921 };
922
923 Chosen.prototype.results_reset_cleanup = function() {
924 var deselect_trigger;
925
926 this.current_selectedIndex = this.form_field.selectedIndex;
927 deselect_trigger = this.selected_item.down("abbr");
928 if (deselect_trigger) {
929 return deselect_trigger.remove();
930 }
931 };
932
933 Chosen.prototype.result_select = function(evt) {
934 var high, item, selected_index;
935
936 if (this.result_highlight) {
937 high = this.result_highlight;
938 this.result_clear_highlight();
939 if (this.is_multiple && this.max_selected_options <= this.choices_count()) {
940 this.form_field.fire("chosen:maxselected", {
941 chosen: this
942 });
943 return false;
944 }
945 if (this.is_multiple) {
946 high.removeClassName("active-result");
947 } else {
948 if (this.result_single_selected) {
949 this.result_single_selected.removeClassName("result-selected");
950 selected_index = this.result_single_selected.getAttribute('data-option-array-index');
951 this.results_data[selected_index].selected = false;
952 }
953 this.result_single_selected = high;
954 }
955 high.addClassName("result-selected");
956 item = this.results_data[high.getAttribute("data-option-array-index")];
957 item.selected = true;
958 this.form_field.options[item.options_index].selected = true;
959 this.selected_option_count = null;
960 if (this.is_multiple) {
961 this.choice_build(item);
962 } else {
963 this.single_set_selected_text(item.text);
964 }
965 if (!((evt.metaKey || evt.ctrlKey) && this.is_multiple)) {
966 this.results_hide();
967 }
968 this.search_field.value = "";
969 if (typeof Event.simulate === 'function' && (this.is_multiple || this.form_field.selectedIndex !== this.current_selectedIndex)) {
970 this.form_field.simulate("change");
971 }
972 this.current_selectedIndex = this.form_field.selectedIndex;
973 return this.search_field_scale();
974 }
975 };
976
977 Chosen.prototype.single_set_selected_text = function(text) {
978 if (text == null) {
979 text = this.default_text;
980 }
981 if (text === this.default_text) {
982 this.selected_item.addClassName("chosen-default");
983 } else {
984 this.single_deselect_control_build();
985 this.selected_item.removeClassName("chosen-default");
986 }
987 return this.selected_item.down("span").update(text);
988 };
989
990 Chosen.prototype.result_deselect = function(pos) {
991 var result_data;
992
993 result_data = this.results_data[pos];
994 if (!this.form_field.options[result_data.options_index].disabled) {
995 result_data.selected = false;
996 this.form_field.options[result_data.options_index].selected = false;
997 this.selected_option_count = null;
998 this.result_clear_highlight();
999 if (this.results_showing) {
1000 this.winnow_results();
1001 }
1002 if (typeof Event.simulate === 'function') {
1003 this.form_field.simulate("change");
1004 }
1005 this.search_field_scale();
1006 return true;
1007 } else {
1008 return false;
1009 }
1010 };
1011
1012 Chosen.prototype.single_deselect_control_build = function() {
1013 if (!this.allow_single_deselect) {
1014 return;
1015 }
1016 if (!this.selected_item.down("abbr")) {
1017 this.selected_item.down("span").insert({
1018 after: "<abbr class=\"search-choice-close\"></abbr>"
1019 });
1020 }
1021 return this.selected_item.addClassName("chosen-single-with-deselect");
1022 };
1023
1024 Chosen.prototype.get_search_text = function() {
1025 if (this.search_field.value === this.default_text) {
1026 return "";
1027 } else {
1028 return this.search_field.value.strip().escapeHTML();
1029 }
1030 };
1031
1032 Chosen.prototype.winnow_results_set_highlight = function() {
1033 var do_high;
1034
1035 if (!this.is_multiple) {
1036 do_high = this.search_results.down(".result-selected.active-result");
1037 }
1038 if (do_high == null) {
1039 do_high = this.search_results.down(".active-result");
1040 }
1041 if (do_high != null) {
1042 return this.result_do_highlight(do_high);
1043 }
1044 };
1045
1046 Chosen.prototype.no_results = function(terms) {
1047 return this.search_results.insert(this.no_results_temp.evaluate({
1048 terms: terms
1049 }));
1050 };
1051
1052 Chosen.prototype.no_results_clear = function() {
1053 var nr, _results;
1054
1055 nr = null;
1056 _results = [];
1057 while (nr = this.search_results.down(".no-results")) {
1058 _results.push(nr.remove());
1059 }
1060 return _results;
1061 };
1062
1063 Chosen.prototype.keydown_arrow = function() {
1064 var next_sib;
1065
1066 if (this.results_showing && this.result_highlight) {
1067 next_sib = this.result_highlight.next('.active-result');
1068 if (next_sib) {
1069 return this.result_do_highlight(next_sib);
1070 }
1071 } else {
1072 return this.results_show();
1073 }
1074 };
1075
1076 Chosen.prototype.keyup_arrow = function() {
1077 var actives, prevs, sibs;
1078
1079 if (!this.results_showing && !this.is_multiple) {
1080 return this.results_show();
1081 } else if (this.result_highlight) {
1082 sibs = this.result_highlight.previousSiblings();
1083 actives = this.search_results.select("li.active-result");
1084 prevs = sibs.intersect(actives);
1085 if (prevs.length) {
1086 return this.result_do_highlight(prevs.first());
1087 } else {
1088 if (this.choices_count() > 0) {
1089 this.results_hide();
1090 }
1091 return this.result_clear_highlight();
1092 }
1093 }
1094 };
1095
1096 Chosen.prototype.keydown_backstroke = function() {
1097 var next_available_destroy;
1098
1099 if (this.pending_backstroke) {
1100 this.choice_destroy(this.pending_backstroke.down("a"));
1101 return this.clear_backstroke();
1102 } else {
1103 next_available_destroy = this.search_container.siblings().last();
1104 if (next_available_destroy && next_available_destroy.hasClassName("search-choice") && !next_available_destroy.hasClassName("search-choice-disabled")) {
1105 this.pending_backstroke = next_available_destroy;
1106 if (this.pending_backstroke) {
1107 this.pending_backstroke.addClassName("search-choice-focus");
1108 }
1109 if (this.single_backstroke_delete) {
1110 return this.keydown_backstroke();
1111 } else {
1112 return this.pending_backstroke.addClassName("search-choice-focus");
1113 }
1114 }
1115 }
1116 };
1117
1118 Chosen.prototype.clear_backstroke = function() {
1119 if (this.pending_backstroke) {
1120 this.pending_backstroke.removeClassName("search-choice-focus");
1121 }
1122 return this.pending_backstroke = null;
1123 };
1124
1125 Chosen.prototype.keydown_checker = function(evt) {
1126 var stroke, _ref1;
1127
1128 stroke = (_ref1 = evt.which) != null ? _ref1 : evt.keyCode;
1129 this.search_field_scale();
1130 if (stroke !== 8 && this.pending_backstroke) {
1131 this.clear_backstroke();
1132 }
1133 switch (stroke) {
1134 case 8:
1135 this.backstroke_length = this.search_field.value.length;
1136 break;
1137 case 9:
1138 if (this.results_showing && !this.is_multiple) {
1139 this.result_select(evt);
1140 }
1141 this.mouse_on_container = false;
1142 break;
1143 case 13:
1144 evt.preventDefault();
1145 break;
1146 case 38:
1147 evt.preventDefault();
1148 this.keyup_arrow();
1149 break;
1150 case 40:
1151 evt.preventDefault();
1152 this.keydown_arrow();
1153 break;
1154 }
1155 };
1156
1157 Chosen.prototype.search_field_scale = function() {
1158 var div, f_width, h, style, style_block, styles, w, _i, _len;
1159
1160 if (this.is_multiple) {
1161 h = 0;
1162 w = 0;
1163 style_block = "position:absolute; left: -1000px; top: -1000px; display:none;";
1164 styles = ['font-size', 'font-style', 'font-weight', 'font-family', 'line-height', 'text-transform', 'letter-spacing'];
1165 for (_i = 0, _len = styles.length; _i < _len; _i++) {
1166 style = styles[_i];
1167 style_block += style + ":" + this.search_field.getStyle(style) + ";";
1168 }
1169 div = new Element('div', {
1170 'style': style_block
1171 }).update(this.search_field.value.escapeHTML());
1172 document.body.appendChild(div);
1173 w = Element.measure(div, 'width') + 25;
1174 div.remove();
1175 f_width = this.container.getWidth();
1176 if (w > f_width - 10) {
1177 w = f_width - 10;
1178 }
1179 return this.search_field.setStyle({
1180 'width': w + 'px'
1181 });
1182 }
1183 };
1184
1185 return Chosen;
1186
1187 })(AbstractChosen);
1188
1189 }).call(this);

  ViewVC Help
Powered by ViewVC 1.1.20