Project

General

Profile

Download (28.6 KB) Statistics
| Branch: | Tag: | Revision:
1
(function() {
2
  /*
3
  Chosen, a Select Box Enhancer for jQuery and Protoype
4
  by Patrick Filler for Harvest, http://getharvest.com
5
  
6
  Available for use under the MIT License, http://en.wikipedia.org/wiki/MIT_License
7
  
8
  Copyright (c) 2011 by Harvest
9
  */  var Chosen, SelectParser, get_side_border_padding, root;
10
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
11
  root = typeof exports !== "undefined" && exports !== null ? exports : this;
12
  Chosen = (function() {
13
    function Chosen(elmn) {
14
      this.set_default_values();
15
      this.form_field = elmn;
16
      this.is_multiple = this.form_field.multiple;
17
      this.default_text_default = this.form_field.multiple ? "Select Some Options" : "Select an Option";
18
      this.set_up_html();
19
      this.register_observers();
20
    }
21
    Chosen.prototype.set_default_values = function() {
22
      this.click_test_action = __bind(function(evt) {
23
        return this.test_active_click(evt);
24
      }, this);
25
      this.active_field = false;
26
      this.mouse_on_container = false;
27
      this.results_showing = false;
28
      this.result_highlighted = null;
29
      this.result_single_selected = null;
30
      this.choices = 0;
31
      this.single_temp = new Template('<a href="javascript:void(0)" class="chzn-single"><span>#{default}</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><div class="chzn-search"><input type="text" /></div><ul class="chzn-results"></ul></div>');
32
      this.multi_temp = new Template('<ul class="chzn-choices"><li class="search-field"><input type="text" value="#{default}" class="default" style="width:25px;" /></li></ul><div class="chzn-drop" style="left:-9000px;"><ul class="chzn-results"></ul></div>');
33
      this.choice_temp = new Template('<li class="search-choice" id="#{id}"><span>#{choice}</span><a href="javascript:void(0)" class="search-choice-close" rel="#{position}"></a></li>');
34
      return this.no_results_temp = new Template('<li class="no-results">No results match "<span>#{terms}</span>"</li>');
35
    };
36
    Chosen.prototype.set_up_html = function() {
37
      var base_template, container_props, dd_top, dd_width, sf_width;
38
      this.container_id = this.form_field.id + "_chzn";
39
      this.f_width = this.form_field.getStyle("width") ? parseInt(this.form_field.getStyle("width"), 10) : this.form_field.getWidth();
40
      container_props = {
41
        'id': this.container_id,
42
        'class': 'chzn-container',
43
        'style': 'width: ' + this.f_width + 'px'
44
      };
45
      this.default_text = this.form_field.readAttribute('title') ? this.form_field.readAttribute('title') : this.default_text_default;
46
      base_template = this.is_multiple ? new Element('div', container_props).update(this.multi_temp.evaluate({
47
        "default": this.default_text
48
      })) : new Element('div', container_props).update(this.single_temp.evaluate({
49
        "default": this.default_text
50
      }));
51
      this.form_field.hide().insert({
52
        after: base_template
53
      });
54
      this.container = $(this.container_id);
55
      this.container.addClassName("chzn-container-" + (this.is_multiple ? "multi" : "single"));
56
      this.dropdown = this.container.down('div.chzn-drop');
57
      dd_top = this.container.getHeight();
58
      dd_width = this.f_width - get_side_border_padding(this.dropdown);
59
      this.dropdown.setStyle({
60
        "width": dd_width + "px",
61
        "top": dd_top + "px"
62
      });
63
      this.search_field = this.container.down('input');
64
      this.search_results = this.container.down('ul.chzn-results');
65
      this.search_field_scale();
66
      this.search_no_results = this.container.down('li.no-results');
67
      if (this.is_multiple) {
68
        this.search_choices = this.container.down('ul.chzn-choices');
69
        this.search_container = this.container.down('li.search-field');
70
      } else {
71
        this.search_container = this.container.down('div.chzn-search');
72
        this.selected_item = this.container.down('.chzn-single');
73
        sf_width = dd_width - get_side_border_padding(this.search_container) - get_side_border_padding(this.search_field);
74
        this.search_field.setStyle({
75
          "width": sf_width + "px"
76
        });
77
      }
78
      this.results_build();
79
      return this.set_tab_index();
80
    };
81
    Chosen.prototype.register_observers = function() {
82
      this.container.observe("click", __bind(function(evt) {
83
        return this.container_click(evt);
84
      }, this));
85
      this.container.observe("mouseenter", __bind(function(evt) {
86
        return this.mouse_enter(evt);
87
      }, this));
88
      this.container.observe("mouseleave", __bind(function(evt) {
89
        return this.mouse_leave(evt);
90
      }, this));
91
      this.search_results.observe("click", __bind(function(evt) {
92
        return this.search_results_click(evt);
93
      }, this));
94
      this.search_results.observe("mouseover", __bind(function(evt) {
95
        return this.search_results_mouseover(evt);
96
      }, this));
97
      this.search_results.observe("mouseout", __bind(function(evt) {
98
        return this.search_results_mouseout(evt);
99
      }, this));
100
      this.form_field.observe("liszt:updated", __bind(function(evt) {
101
        return this.results_update_field(evt);
102
      }, this));
103
      this.search_field.observe("blur", __bind(function(evt) {
104
        return this.input_blur(evt);
105
      }, this));
106
      this.search_field.observe("keyup", __bind(function(evt) {
107
        return this.keyup_checker(evt);
108
      }, this));
109
      this.search_field.observe("keydown", __bind(function(evt) {
110
        return this.keydown_checker(evt);
111
      }, this));
112
      if (this.is_multiple) {
113
        this.search_choices.observe("click", __bind(function(evt) {
114
          return this.choices_click(evt);
115
        }, this));
116
        return this.search_field.observe("focus", __bind(function(evt) {
117
          return this.input_focus(evt);
118
        }, this));
119
      } else {
120
        return this.selected_item.observe("focus", __bind(function(evt) {
121
          return this.activate_field(evt);
122
        }, this));
123
      }
124
    };
125
    Chosen.prototype.container_click = function(evt) {
126
      if (evt && evt.type === "click") {
127
        evt.stop();
128
      }
129
      if (!this.pending_destroy_click) {
130
        if (!this.active_field) {
131
          if (this.is_multiple) {
132
            this.search_field.clear();
133
          }
134
          document.observe("click", this.click_test_action);
135
          this.results_show();
136
        } else if (!this.is_multiple && evt && (evt.target === this.selected_item || evt.target.up("a.chzn-single"))) {
137
          this.results_toggle();
138
        }
139
        return this.activate_field();
140
      } else {
141
        return this.pending_destroy_click = false;
142
      }
143
    };
144
    Chosen.prototype.mouse_enter = function() {
145
      return this.mouse_on_container = true;
146
    };
147
    Chosen.prototype.mouse_leave = function() {
148
      return this.mouse_on_container = false;
149
    };
150
    Chosen.prototype.input_focus = function(evt) {
151
      if (!this.active_field) {
152
        return setTimeout(this.container_click.bind(this), 50);
153
      }
154
    };
155
    Chosen.prototype.input_blur = function(evt) {
156
      if (!this.mouse_on_container) {
157
        this.active_field = false;
158
        return setTimeout(this.blur_test.bind(this), 100);
159
      }
160
    };
161
    Chosen.prototype.blur_test = function(evt) {
162
      if (!this.active_field && this.container.hasClassName("chzn-container-active")) {
163
        return this.close_field();
164
      }
165
    };
166
    Chosen.prototype.close_field = function() {
167
      document.stopObserving("click", this.click_test_action);
168
      if (!this.is_multiple) {
169
        this.selected_item.tabIndex = this.search_field.tabIndex;
170
        this.search_field.tabIndex = -1;
171
      }
172
      this.active_field = false;
173
      this.results_hide();
174
      this.container.removeClassName("chzn-container-active");
175
      this.winnow_results_clear();
176
      this.clear_backstroke();
177
      this.show_search_field_default();
178
      return this.search_field_scale();
179
    };
180
    Chosen.prototype.activate_field = function() {
181
      if (!this.is_multiple && !this.active_field) {
182
        this.search_field.tabIndex = this.selected_item.tabIndex;
183
        this.selected_item.tabIndex = -1;
184
      }
185
      this.container.addClassName("chzn-container-active");
186
      this.active_field = true;
187
      this.search_field.value = this.search_field.value;
188
      return this.search_field.focus();
189
    };
190
    Chosen.prototype.test_active_click = function(evt) {
191
      if (evt.target.up('#' + this.container.id)) {
192
        return this.active_field = true;
193
      } else {
194
        return this.close_field();
195
      }
196
    };
197
    Chosen.prototype.results_build = function() {
198
      var content, data, startTime, _i, _len, _ref;
199
      startTime = new Date();
200
      this.parsing = true;
201
      this.results_data = SelectParser.select_to_array(this.form_field);
202
      if (this.is_multiple && this.choices > 0) {
203
        this.search_choices.select("li.search-choice").invoke("remove");
204
        this.choices = 0;
205
      } else if (!this.is_multiple) {
206
        this.selected_item.down("span").update(this.default_text);
207
      }
208
      content = '';
209
      _ref = this.results_data;
210
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
211
        data = _ref[_i];
212
        if (data.group) {
213
          content += this.result_add_group(data);
214
        } else if (!data.empty) {
215
          content += this.result_add_option(data);
216
          if (data.selected && this.is_multiple) {
217
            this.choice_build(data);
218
          } else if (data.selected && !this.is_multiple) {
219
            this.selected_item.down("span").update(data.text);
220
          }
221
        }
222
      }
223
      this.show_search_field_default();
224
      this.search_field_scale();
225
      this.search_results.update(content);
226
      return this.parsing = false;
227
    };
228
    Chosen.prototype.result_add_group = function(group) {
229
      if (!group.disabled) {
230
        group.dom_id = this.form_field.id + "chzn_g_" + group.array_index;
231
        return '<li id="' + group.dom_id + '" class="group-result">' + group.label.escapeHTML() + '</li>';
232
      } else {
233
        return "";
234
      }
235
    };
236
    Chosen.prototype.result_add_option = function(option) {
237
      var classes;
238
      if (!option.disabled) {
239
        option.dom_id = this.form_field.id + "chzn_o_" + option.array_index;
240
        classes = option.selected && this.is_multiple ? [] : ["active-result"];
241
        if (option.selected) {
242
          classes.push("result-selected");
243
        }
244
        if (option.group_array_index != null) {
245
          classes.push("group-option");
246
        }
247
        return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + option.text.escapeHTML() + '</li>';
248
      } else {
249
        return "";
250
      }
251
    };
252
    Chosen.prototype.results_update_field = function() {
253
      this.result_clear_highlight();
254
      this.result_single_selected = null;
255
      return this.results_build();
256
    };
257
    Chosen.prototype.result_do_highlight = function(el) {
258
      var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
259
      this.result_clear_highlight();
260
      this.result_highlight = el;
261
      this.result_highlight.addClassName("highlighted");
262
      maxHeight = parseInt(this.search_results.getStyle('maxHeight'), 10);
263
      visible_top = this.search_results.scrollTop;
264
      visible_bottom = maxHeight + visible_top;
265
      high_top = this.result_highlight.positionedOffset().top;
266
      high_bottom = high_top + this.result_highlight.getHeight();
267
      if (high_bottom >= visible_bottom) {
268
        return this.search_results.scrollTop = (high_bottom - maxHeight) > 0 ? high_bottom - maxHeight : 0;
269
      } else if (high_top < visible_top) {
270
        return this.search_results.scrollTop = high_top;
271
      }
272
    };
273
    Chosen.prototype.result_clear_highlight = function() {
274
      if (this.result_highlight) {
275
        this.result_highlight.removeClassName('highlighted');
276
      }
277
      return this.result_highlight = null;
278
    };
279
    Chosen.prototype.results_toggle = function() {
280
      if (this.results_showing) {
281
        return this.results_hide();
282
      } else {
283
        return this.results_show();
284
      }
285
    };
286
    Chosen.prototype.results_show = function() {
287
      var dd_top;
288
      if (!this.is_multiple) {
289
        this.selected_item.addClassName('chzn-single-with-drop');
290
        if (this.result_single_selected) {
291
          this.result_do_highlight(this.result_single_selected);
292
        }
293
      }
294
      dd_top = this.is_multiple ? this.container.getHeight() : this.container.getHeight() - 1;
295
      this.dropdown.setStyle({
296
        "top": dd_top + "px",
297
        "left": 0
298
      });
299
      this.results_showing = true;
300
      this.search_field.focus();
301
      this.search_field.value = this.search_field.value;
302
      return this.winnow_results();
303
    };
304
    Chosen.prototype.results_hide = function() {
305
      if (!this.is_multiple) {
306
        this.selected_item.removeClassName('chzn-single-with-drop');
307
      }
308
      this.result_clear_highlight();
309
      this.dropdown.setStyle({
310
        "left": "-9000px"
311
      });
312
      return this.results_showing = false;
313
    };
314
    Chosen.prototype.set_tab_index = function(el) {
315
      var ti;
316
      if (this.form_field.tabIndex) {
317
        ti = this.form_field.tabIndex;
318
        this.form_field.tabIndex = -1;
319
        if (this.is_multiple) {
320
          return this.search_field.tabIndex = ti;
321
        } else {
322
          this.selected_item.tabIndex = ti;
323
          return this.search_field.tabIndex = -1;
324
        }
325
      }
326
    };
327
    Chosen.prototype.show_search_field_default = function() {
328
      if (this.is_multiple && this.choices < 1 && !this.active_field) {
329
        this.search_field.value = this.default_text;
330
        return this.search_field.addClassName("default");
331
      } else {
332
        this.search_field.value = "";
333
        return this.search_field.removeClassName("default");
334
      }
335
    };
336
    Chosen.prototype.search_results_click = function(evt) {
337
      var target;
338
      target = evt.target.hasClassName("active-result") ? evt.target : evt.target.up(".active-result");
339
      if (target) {
340
        this.result_highlight = target;
341
        return this.result_select();
342
      }
343
    };
344
    Chosen.prototype.search_results_mouseover = function(evt) {
345
      var target;
346
      target = evt.target.hasClassName("active-result") ? evt.target : evt.target.up(".active-result");
347
      if (target) {
348
        return this.result_do_highlight(target);
349
      }
350
    };
351
    Chosen.prototype.search_results_mouseout = function(evt) {
352
      if (evt.target.hasClassName('active-result') || evt.target.up('.active-result')) {
353
        return this.result_clear_highlight();
354
      }
355
    };
356
    Chosen.prototype.choices_click = function(evt) {
357
      evt.preventDefault();
358
      if (this.active_field && !(evt.target.hasClassName('search-choice') || evt.target.up('.search-choice')) && !this.results_showing) {
359
        return this.results_show();
360
      }
361
    };
362
    Chosen.prototype.choice_build = function(item) {
363
      var choice_id, link;
364
      choice_id = this.form_field.id + "_chzn_c_" + item.array_index;
365
      this.choices += 1;
366
      this.search_container.insert({
367
        before: this.choice_temp.evaluate({
368
          "id": choice_id,
369
          "choice": item.text,
370
          "position": item.array_index
371
        })
372
      });
373
      link = $(choice_id).down('a');
374
      return link.observe("click", __bind(function(evt) {
375
        return this.choice_destroy_link_click(evt);
376
      }, this));
377
    };
378
    Chosen.prototype.choice_destroy_link_click = function(evt) {
379
      evt.preventDefault();
380
      this.pending_destroy_click = true;
381
      return this.choice_destroy(evt.target);
382
    };
383
    Chosen.prototype.choice_destroy = function(link) {
384
      this.choices -= 1;
385
      this.show_search_field_default();
386
      if (this.is_multiple && this.choices > 0 && this.search_field.value.length < 1) {
387
        this.results_hide();
388
      }
389
      this.result_deselect(link.readAttribute("rel"));
390
      return link.up('li').remove();
391
    };
392
    Chosen.prototype.result_select = function() {
393
      var high, item, position;
394
      if (this.result_highlight) {
395
        high = this.result_highlight;
396
        this.result_clear_highlight();
397
        high.addClassName("result-selected");
398
        if (this.is_multiple) {
399
          this.result_deactivate(high);
400
        } else {
401
          this.result_single_selected = high;
402
        }
403
        position = high.id.substr(high.id.lastIndexOf("_") + 1);
404
        item = this.results_data[position];
405
        item.selected = true;
406
        this.form_field.options[item.options_index].selected = true;
407
        if (this.is_multiple) {
408
          this.choice_build(item);
409
        } else {
410
          this.selected_item.down("span").update(item.text);
411
        }
412
        this.results_hide();
413
        this.search_field.value = "";
414
        if (typeof Event.simulate === 'function') {
415
          this.form_field.simulate("change");
416
        }
417
        return this.search_field_scale();
418
      }
419
    };
420
    Chosen.prototype.result_activate = function(el) {
421
      return el.addClassName("active-result").show();
422
    };
423
    Chosen.prototype.result_deactivate = function(el) {
424
      return el.removeClassName("active-result").hide();
425
    };
426
    Chosen.prototype.result_deselect = function(pos) {
427
      var result, result_data;
428
      result_data = this.results_data[pos];
429
      result_data.selected = false;
430
      this.form_field.options[result_data.options_index].selected = false;
431
      result = $(this.form_field.id + "chzn_o_" + pos);
432
      result.removeClassName("result-selected").addClassName("active-result").show();
433
      this.result_clear_highlight();
434
      this.winnow_results();
435
      if (typeof Event.simulate === 'function') {
436
        this.form_field.simulate("change");
437
      }
438
      return this.search_field_scale();
439
    };
440
    Chosen.prototype.results_search = function(evt) {
441
      if (this.results_showing) {
442
        return this.winnow_results();
443
      } else {
444
        return this.results_show();
445
      }
446
    };
447
    Chosen.prototype.winnow_results = function() {
448
      var found, option, part, parts, regex, result_id, results, searchText, startTime, startpos, text, zregex, _i, _j, _len, _len2, _ref;
449
      startTime = new Date();
450
      this.no_results_clear();
451
      results = 0;
452
      searchText = this.search_field.value === this.default_text ? "" : this.search_field.value.strip();
453
      regex = new RegExp('^' + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
454
      zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
455
      _ref = this.results_data;
456
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
457
        option = _ref[_i];
458
        if (!option.disabled && !option.empty) {
459
          if (option.group) {
460
            $(option.dom_id).hide();
461
          } else if (!(this.is_multiple && option.selected)) {
462
            found = false;
463
            result_id = option.dom_id;
464
            if (regex.test(option.text)) {
465
              found = true;
466
              results += 1;
467
            } else if (option.text.indexOf(" ") >= 0 || option.text.indexOf("[") === 0) {
468
              parts = option.text.replace(/\[|\]/g, "").split(" ");
469
              if (parts.length) {
470
                for (_j = 0, _len2 = parts.length; _j < _len2; _j++) {
471
                  part = parts[_j];
472
                  if (regex.test(part)) {
473
                    found = true;
474
                    results += 1;
475
                  }
476
                }
477
              }
478
            }
479
            if (found) {
480
              if (searchText.length) {
481
                startpos = option.text.search(zregex);
482
                text = option.text.substr(0, startpos + searchText.length) + '</em>' + option.text.substr(startpos + searchText.length);
483
                text = text.substr(0, startpos) + '<em>' + text.substr(startpos);
484
              } else {
485
                text = option.text;
486
              }
487
              if ($(result_id).innerHTML !== text) {
488
                $(result_id).update(text);
489
              }
490
              this.result_activate($(result_id));
491
              if (option.group_array_index != null) {
492
                $(this.results_data[option.group_array_index].dom_id).show();
493
              }
494
            } else {
495
              if ($(result_id) === this.result_highlight) {
496
                this.result_clear_highlight();
497
              }
498
              this.result_deactivate($(result_id));
499
            }
500
          }
501
        }
502
      }
503
      if (results < 1 && searchText.length) {
504
        return this.no_results(searchText);
505
      } else {
506
        return this.winnow_results_set_highlight();
507
      }
508
    };
509
    Chosen.prototype.winnow_results_clear = function() {
510
      var li, lis, _i, _len, _results;
511
      this.search_field.clear();
512
      lis = this.search_results.select("li");
513
      _results = [];
514
      for (_i = 0, _len = lis.length; _i < _len; _i++) {
515
        li = lis[_i];
516
        _results.push(li.hasClassName("group-result") ? li.show() : !this.is_multiple || !li.hasClassName("result-selected") ? this.result_activate(li) : void 0);
517
      }
518
      return _results;
519
    };
520
    Chosen.prototype.winnow_results_set_highlight = function() {
521
      var do_high;
522
      if (!this.result_highlight) {
523
        do_high = this.search_results.down(".active-result");
524
        if (do_high) {
525
          return this.result_do_highlight(do_high);
526
        }
527
      }
528
    };
529
    Chosen.prototype.no_results = function(terms) {
530
      return this.search_results.insert(this.no_results_temp.evaluate({
531
        "terms": terms.escapeHTML()
532
      }));
533
    };
534
    Chosen.prototype.no_results_clear = function() {
535
      var nr, _results;
536
      nr = null;
537
      _results = [];
538
      while (nr = this.search_results.down(".no-results")) {
539
        _results.push(nr.remove());
540
      }
541
      return _results;
542
    };
543
    Chosen.prototype.keydown_arrow = function() {
544
      var actives, nexts, sibs;
545
      actives = this.search_results.select("li.active-result");
546
      if (actives.length) {
547
        if (!this.result_highlight) {
548
          this.result_do_highlight(actives.first());
549
        } else if (this.results_showing) {
550
          sibs = this.result_highlight.nextSiblings();
551
          nexts = sibs.intersect(actives);
552
          if (nexts.length) {
553
            this.result_do_highlight(nexts.first());
554
          }
555
        }
556
        if (!this.results_showing) {
557
          return this.results_show();
558
        }
559
      }
560
    };
561
    Chosen.prototype.keyup_arrow = function() {
562
      var actives, prevs, sibs;
563
      if (!this.results_showing && !this.is_multiple) {
564
        return this.results_show();
565
      } else if (this.result_highlight) {
566
        sibs = this.result_highlight.previousSiblings();
567
        actives = this.search_results.select("li.active-result");
568
        prevs = sibs.intersect(actives);
569
        if (prevs.length) {
570
          return this.result_do_highlight(prevs.first());
571
        } else {
572
          if (this.choices > 0) {
573
            this.results_hide();
574
          }
575
          return this.result_clear_highlight();
576
        }
577
      }
578
    };
579
    Chosen.prototype.keydown_backstroke = function() {
580
      if (this.pending_backstroke) {
581
        this.choice_destroy(this.pending_backstroke.down("a"));
582
        return this.clear_backstroke();
583
      } else {
584
        this.pending_backstroke = this.search_container.siblings("li.search-choice").last();
585
        return this.pending_backstroke.addClassName("search-choice-focus");
586
      }
587
    };
588
    Chosen.prototype.clear_backstroke = function() {
589
      if (this.pending_backstroke) {
590
        this.pending_backstroke.removeClassName("search-choice-focus");
591
      }
592
      return this.pending_backstroke = null;
593
    };
594
    Chosen.prototype.keyup_checker = function(evt) {
595
      var stroke, _ref;
596
      stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
597
      this.search_field_scale();
598
      switch (stroke) {
599
        case 8:
600
          if (this.is_multiple && this.backstroke_length < 1 && this.choices > 0) {
601
            return this.keydown_backstroke();
602
          } else if (!this.pending_backstroke) {
603
            this.result_clear_highlight();
604
            return this.results_search();
605
          }
606
          break;
607
        case 13:
608
          evt.preventDefault();
609
          if (this.results_showing) {
610
            return this.result_select();
611
          }
612
          break;
613
        case 27:
614
          if (this.results_showing) {
615
            return this.results_hide();
616
          }
617
          break;
618
        case 9:
619
        case 38:
620
        case 40:
621
        case 16:
622
          break;
623
        default:
624
          return this.results_search();
625
      }
626
    };
627
    Chosen.prototype.keydown_checker = function(evt) {
628
      var stroke, _ref;
629
      stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
630
      this.search_field_scale();
631
      if (stroke !== 8 && this.pending_backstroke) {
632
        this.clear_backstroke();
633
      }
634
      switch (stroke) {
635
        case 8:
636
          return this.backstroke_length = this.search_field.value.length;
637
        case 9:
638
          return this.mouse_on_container = false;
639
        case 13:
640
          return evt.preventDefault();
641
        case 38:
642
          evt.preventDefault();
643
          return this.keyup_arrow();
644
        case 40:
645
          return this.keydown_arrow();
646
      }
647
    };
648
    Chosen.prototype.search_field_scale = function() {
649
      var dd_top, div, h, style, style_block, styles, w, _i, _len;
650
      if (this.is_multiple) {
651
        h = 0;
652
        w = 0;
653
        style_block = "position:absolute; left: -1000px; top: -1000px; display:none;";
654
        styles = ['font-size', 'font-style', 'font-weight', 'font-family', 'line-height', 'text-transform', 'letter-spacing'];
655
        for (_i = 0, _len = styles.length; _i < _len; _i++) {
656
          style = styles[_i];
657
          style_block += style + ":" + this.search_field.getStyle(style) + ";";
658
        }
659
        div = new Element('div', {
660
          'style': style_block
661
        }).update(this.search_field.value);
662
        document.body.appendChild(div);
663
        w = Element.measure(div, 'width') + 25;
664
        div.remove();
665
        if (w > this.f_width - 10) {
666
          w = this.f_width - 10;
667
        }
668
        this.search_field.setStyle({
669
          'width': w + 'px'
670
        });
671
        dd_top = this.container.getHeight();
672
        return this.dropdown.setStyle({
673
          "top": dd_top + "px"
674
        });
675
      }
676
    };
677
    return Chosen;
678
  })();
679
  root.Chosen = Chosen;
680
  document.observe('dom:loaded', function(evt) {
681
    var select, selects, _i, _len, _results;
682
    selects = $$(".chzn-select");
683
    _results = [];
684
    for (_i = 0, _len = selects.length; _i < _len; _i++) {
685
      select = selects[_i];
686
      _results.push(new Chosen(select));
687
    }
688
    return _results;
689
  });
690
  get_side_border_padding = function(elmt) {
691
    var layout, side_border_padding;
692
    layout = new Element.Layout(elmt);
693
    return side_border_padding = layout.get("border-left") + layout.get("border-right") + layout.get("padding-left") + layout.get("padding-right");
694
  };
695
  root.get_side_border_padding = get_side_border_padding;
696
  root = typeof exports !== "undefined" && exports !== null ? exports : this;
697
  SelectParser = (function() {
698
    function SelectParser() {
699
      this.options_index = 0;
700
      this.parsed = [];
701
    }
702
    SelectParser.prototype.add_node = function(child) {
703
      if (child.nodeName === "OPTGROUP") {
704
        return this.add_group(child);
705
      } else {
706
        return this.add_option(child);
707
      }
708
    };
709
    SelectParser.prototype.add_group = function(group) {
710
      var group_position, option, _i, _len, _ref, _results;
711
      group_position = this.parsed.length;
712
      this.parsed.push({
713
        array_index: group_position,
714
        group: true,
715
        label: group.label,
716
        children: 0,
717
        disabled: group.disabled
718
      });
719
      _ref = group.childNodes;
720
      _results = [];
721
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
722
        option = _ref[_i];
723
        _results.push(this.add_option(option, group_position, group.disabled));
724
      }
725
      return _results;
726
    };
727
    SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
728
      if (option.nodeName === "OPTION") {
729
        if (option.text !== "") {
730
          if (group_position != null) {
731
            this.parsed[group_position].children += 1;
732
          }
733
          this.parsed.push({
734
            array_index: this.parsed.length,
735
            options_index: this.options_index,
736
            value: option.value,
737
            text: option.text,
738
            selected: option.selected,
739
            disabled: group_disabled === true ? group_disabled : option.disabled,
740
            group_array_index: group_position
741
          });
742
        } else {
743
          this.parsed.push({
744
            array_index: this.parsed.length,
745
            options_index: this.options_index,
746
            empty: true
747
          });
748
        }
749
        return this.options_index += 1;
750
      }
751
    };
752
    return SelectParser;
753
  })();
754
  SelectParser.select_to_array = function(select) {
755
    var child, parser, _i, _len, _ref;
756
    parser = new SelectParser();
757
    _ref = select.childNodes;
758
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
759
      child = _ref[_i];
760
      parser.add_node(child);
761
    }
762
    return parser.parsed;
763
  };
764
  root.SelectParser = SelectParser;
765
}).call(this);
(5-5/6)