chosen_assets 1.1.0.0

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