chosen-koenpunt-rails 0.0.2 → 0.0.3

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