sunrise-cms 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2819a7488e62a5f523822bdca65a69cb2f5fd6e7
4
- data.tar.gz: 0bec11e0682db1e225acc930db2c9d51ebfa4d5f
3
+ metadata.gz: 9c6ea2ab7adf546d9e72100e8431caf3255a1c5c
4
+ data.tar.gz: ca920defde3da27cb13fd1c741685e6fe2989c63
5
5
  SHA512:
6
- metadata.gz: 86b59f3ff4fc1276e849d80e19afe0b5b03d54caf5c414fd2f06d2b4e5ce69900495b4f0e89a2b813cc4a889e6fd6d9a4a457798963fb80632cf68af38931b9c
7
- data.tar.gz: 2bad669260567f6e11729ffeca563e55da0e5270ece2cfec22138a9d34e96e1e923fe893db96382820e2c82112aca271c09ea92795768ddae1d9da8aa56cfee8
6
+ metadata.gz: 7124f8e33257eb9d87fd4f7863ed40985e04ae5e4dcb8d577e5c18c1fae0f8ff504aed4248e642e8a0e74556e0763bce5ff8570ea0e5ad11bda73823fd5086bc
7
+ data.tar.gz: d4ef91faccce0fb23c1f07688d3bb5f2df0abeb455500350bf9d331e5df814a3f597036ad45052d2ac0bc7678191d3ff95c7ebab6561e353fa386bb845ff2c99
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,8 @@
1
+ == 1.0.1
2
+ * Field now supports block
3
+ * Update chosen to 1.1.0
4
+ * Use css name "nochosen" to turn off chosen
5
+
1
6
  == 0.6.6
2
7
  * Allow pre-fill attributes on new form
3
8
  * Add :html option on field config to setup holder
data/README.md CHANGED
@@ -85,7 +85,10 @@ class SunriseProduct < Sunrise::AbstractModel
85
85
  field :title
86
86
  field :price
87
87
  field :total_stock
88
- field :notes
88
+
89
+ field do |form, record|
90
+ form.input :notes, :as => :text, :id => record.id
91
+ end
89
92
 
90
93
  group :sidebar, :holder => :sidebar do
91
94
  field :sale_limit_id, :collection => lambda { SaleLimit.all }, :include_blank => false
File without changes
File without changes
@@ -10,7 +10,6 @@
10
10
  //= require sunrise/jquery.cookie
11
11
  //= require sunrise/jquery-ui-timepicker-addon
12
12
  //= require sunrise/chosen.jquery
13
- //= require sunrise/chosen.overflow.fix
14
13
 
15
14
  //= require sunrise/jquery.editable
16
15
  //= require sunrise/jquery.dialog
@@ -1,17 +1,21 @@
1
- // Chosen, a Select Box Enhancer for jQuery and Protoype
2
- // by Patrick Filler for Harvest, http://getharvest.com
3
- //
4
- // Version 0.9.12
5
- // Full source at https://github.com/harvesthq/chosen
6
- // Copyright (c) 2011 Harvest http://getharvest.com
7
-
8
- // MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md
9
- // This file is generated by `cake build`, do not edit it by hand.
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
+
10
13
  (function() {
11
- var SelectParser;
14
+ var $, AbstractChosen, Chosen, 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; };
12
17
 
13
18
  SelectParser = (function() {
14
-
15
19
  function SelectParser() {
16
20
  this.options_index = 0;
17
21
  this.parsed = [];
@@ -31,7 +35,7 @@
31
35
  this.parsed.push({
32
36
  array_index: group_position,
33
37
  group: true,
34
- label: group.label,
38
+ label: this.escapeExpression(group.label),
35
39
  children: 0,
36
40
  disabled: group.disabled
37
41
  });
@@ -73,6 +77,27 @@
73
77
  }
74
78
  };
75
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
+
76
101
  return SelectParser;
77
102
 
78
103
  })();
@@ -88,33 +113,19 @@
88
113
  return parser.parsed;
89
114
  };
90
115
 
91
- this.SelectParser = SelectParser;
92
-
93
- }).call(this);
94
-
95
- /*
96
- Chosen source: generate output using 'cake build'
97
- Copyright (c) 2011 by Harvest
98
- */
99
-
100
-
101
- (function() {
102
- var AbstractChosen, root;
103
-
104
- root = this;
105
-
106
116
  AbstractChosen = (function() {
107
-
108
117
  function AbstractChosen(form_field, options) {
109
118
  this.form_field = form_field;
110
119
  this.options = options != null ? options : {};
120
+ if (!AbstractChosen.browser_is_supported()) {
121
+ return;
122
+ }
111
123
  this.is_multiple = this.form_field.multiple;
112
124
  this.set_default_text();
113
125
  this.set_default_values();
114
126
  this.setup();
115
127
  this.set_up_html();
116
128
  this.register_observers();
117
- this.finish_setup();
118
129
  }
119
130
 
120
131
  AbstractChosen.prototype.set_default_values = function() {
@@ -129,27 +140,28 @@ Copyright (c) 2011 by Harvest
129
140
  this.mouse_on_container = false;
130
141
  this.results_showing = false;
131
142
  this.result_highlighted = null;
132
- this.result_single_selected = null;
133
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;
134
144
  this.disable_search_threshold = this.options.disable_search_threshold || 0;
135
145
  this.disable_search = this.options.disable_search || false;
136
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;
137
148
  this.search_contains = this.options.search_contains || false;
138
- this.choices = 0;
139
- this.single_backstroke_delete = this.options.single_backstroke_delete || false;
149
+ this.single_backstroke_delete = this.options.single_backstroke_delete != null ? this.options.single_backstroke_delete : true;
140
150
  this.max_selected_options = this.options.max_selected_options || Infinity;
141
- return this.inherit_select_classes = this.options.inherit_select_classes || false;
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;
142
154
  };
143
155
 
144
156
  AbstractChosen.prototype.set_default_text = function() {
145
157
  if (this.form_field.getAttribute("data-placeholder")) {
146
158
  this.default_text = this.form_field.getAttribute("data-placeholder");
147
159
  } else if (this.is_multiple) {
148
- this.default_text = this.options.placeholder_text_multiple || this.options.placeholder_text || "Select Some Options";
160
+ this.default_text = this.options.placeholder_text_multiple || this.options.placeholder_text || AbstractChosen.default_multiple_text;
149
161
  } else {
150
- this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || "Select an Option";
162
+ this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || AbstractChosen.default_single_text;
151
163
  }
152
- return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || "No results match";
164
+ return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || AbstractChosen.default_no_result_text;
153
165
  };
154
166
 
155
167
  AbstractChosen.prototype.mouse_enter = function() {
@@ -185,25 +197,72 @@ Copyright (c) 2011 by Harvest
185
197
  }
186
198
  };
187
199
 
188
- AbstractChosen.prototype.result_add_option = function(option) {
189
- var classes, style;
190
- if (!option.disabled) {
191
- option.dom_id = this.container_id + "_o_" + option.array_index;
192
- classes = option.selected && this.is_multiple ? [] : ["active-result"];
193
- if (option.selected) {
194
- classes.push("result-selected");
195
- }
196
- if (option.group_array_index != null) {
197
- classes.push("group-option");
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);
198
210
  }
199
- if (option.classes !== "") {
200
- classes.push(option.classes);
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
+ }
201
217
  }
202
- style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : "";
203
- return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"' + style + '>' + option.html + '</li>';
204
- } else {
205
- return "";
206
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);
207
266
  };
208
267
 
209
268
  AbstractChosen.prototype.results_update_field = function() {
@@ -212,8 +271,25 @@ Copyright (c) 2011 by Harvest
212
271
  this.results_reset_cleanup();
213
272
  }
214
273
  this.result_clear_highlight();
215
- this.result_single_selected = null;
216
- return this.results_build();
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;
217
293
  };
218
294
 
219
295
  AbstractChosen.prototype.results_toggle = function() {
@@ -232,13 +308,110 @@ Copyright (c) 2011 by Harvest
232
308
  }
233
309
  };
234
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
+
235
408
  AbstractChosen.prototype.keyup_checker = function(evt) {
236
409
  var stroke, _ref;
237
410
  stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
238
411
  this.search_field_scale();
239
412
  switch (stroke) {
240
413
  case 8:
241
- if (this.is_multiple && this.backstroke_length < 1 && this.choices > 0) {
414
+ if (this.is_multiple && this.backstroke_length < 1 && this.choices_count() > 0) {
242
415
  return this.keydown_backstroke();
243
416
  } else if (!this.pending_backstroke) {
244
417
  this.result_clear_highlight();
@@ -268,233 +441,285 @@ Copyright (c) 2011 by Harvest
268
441
  }
269
442
  };
270
443
 
271
- AbstractChosen.prototype.generate_field_id = function() {
272
- var new_id;
273
- new_id = this.generate_random_id();
274
- this.form_field.id = new_id;
275
- return new_id;
444
+ AbstractChosen.prototype.clipboard_event_checker = function(evt) {
445
+ var _this = this;
446
+ return setTimeout((function() {
447
+ return _this.results_search();
448
+ }), 50);
276
449
  };
277
450
 
278
- AbstractChosen.prototype.generate_random_char = function() {
279
- var chars, newchar, rand;
280
- chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
281
- rand = Math.floor(Math.random() * chars.length);
282
- return newchar = chars.substring(rand, rand + 1);
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
+ }
283
457
  };
284
458
 
285
- return AbstractChosen;
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
+ };
286
471
 
287
- })();
472
+ AbstractChosen.prototype.search_results_touchstart = function(evt) {
473
+ this.touch_started = true;
474
+ return this.search_results_mouseover(evt);
475
+ };
288
476
 
289
- root.AbstractChosen = AbstractChosen;
477
+ AbstractChosen.prototype.search_results_touchmove = function(evt) {
478
+ this.touch_started = false;
479
+ return this.search_results_mouseout(evt);
480
+ };
290
481
 
291
- }).call(this);
482
+ AbstractChosen.prototype.search_results_touchend = function(evt) {
483
+ if (this.touch_started) {
484
+ return this.search_results_mouseup(evt);
485
+ }
486
+ };
292
487
 
293
- /*
294
- Chosen source: generate output using 'cake build'
295
- Copyright (c) 2011 by Harvest
296
- */
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
+ };
297
512
 
513
+ AbstractChosen.default_multiple_text = "Select Some Options";
298
514
 
299
- (function() {
300
- var $, Chosen, get_side_border_padding, root,
301
- __hasProp = {}.hasOwnProperty,
302
- __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; };
515
+ AbstractChosen.default_single_text = "Select an Option";
303
516
 
304
- root = this;
517
+ AbstractChosen.default_no_result_text = "No results match";
518
+
519
+ return AbstractChosen;
520
+
521
+ })();
305
522
 
306
523
  $ = jQuery;
307
524
 
308
525
  $.fn.extend({
309
526
  chosen: function(options) {
310
- var browser, match, ua;
311
- ua = navigator.userAgent.toLowerCase();
312
- match = /(msie) ([\w.]+)/.exec(ua) || [];
313
- browser = {
314
- name: match[1] || "",
315
- version: match[2] || "0"
316
- };
317
- if (browser.name === "msie" && (browser.version === "6.0" || (browser.version === "7.0" && document.documentMode === 7))) {
527
+ if (!AbstractChosen.browser_is_supported()) {
318
528
  return this;
319
529
  }
320
530
  return this.each(function(input_field) {
321
- var $this;
531
+ var $this, chosen;
322
532
  $this = $(this);
323
- if (!$this.hasClass("chzn-done")) {
324
- return $this.data('chosen', new Chosen(this, options));
533
+ chosen = $this.data('chosen');
534
+ if (options === 'destroy' && chosen) {
535
+ chosen.destroy();
536
+ } else if (!chosen) {
537
+ $this.data('chosen', new Chosen(this, options));
325
538
  }
326
539
  });
327
540
  }
328
541
  });
329
542
 
330
543
  Chosen = (function(_super) {
331
-
332
544
  __extends(Chosen, _super);
333
545
 
334
546
  function Chosen() {
335
- return Chosen.__super__.constructor.apply(this, arguments);
547
+ _ref = Chosen.__super__.constructor.apply(this, arguments);
548
+ return _ref;
336
549
  }
337
550
 
338
551
  Chosen.prototype.setup = function() {
339
552
  this.form_field_jq = $(this.form_field);
340
- this.current_value = this.form_field_jq.val();
341
- return this.is_rtl = this.form_field_jq.hasClass("chzn-rtl");
342
- };
343
-
344
- Chosen.prototype.finish_setup = function() {
345
- return this.form_field_jq.addClass("chzn-done");
553
+ this.current_selectedIndex = this.form_field.selectedIndex;
554
+ return this.is_rtl = this.form_field_jq.hasClass("chosen-rtl");
346
555
  };
347
556
 
348
557
  Chosen.prototype.set_up_html = function() {
349
- var container_classes, container_div, container_props, dd_top, dd_width, sf_width;
350
- this.container_id = this.form_field.id.length ? this.form_field.id.replace(/[^\w]/g, '_') : this.generate_field_id();
351
- this.container_id += "_chzn";
352
- container_classes = ["chzn-container"];
353
- container_classes.push("chzn-container-" + (this.is_multiple ? "multi" : "single"));
558
+ var container_classes, container_props;
559
+ container_classes = ["chosen-container"];
560
+ container_classes.push("chosen-container-" + (this.is_multiple ? "multi" : "single"));
354
561
  if (this.inherit_select_classes && this.form_field.className) {
355
562
  container_classes.push(this.form_field.className);
356
563
  }
357
564
  if (this.is_rtl) {
358
- container_classes.push("chzn-rtl");
565
+ container_classes.push("chosen-rtl");
359
566
  }
360
- this.f_width = this.form_field_jq.outerWidth();
361
567
  container_props = {
362
- id: this.container_id,
363
- "class": container_classes.join(' '),
364
- style: 'width: ' + this.f_width + 'px;',
365
- title: this.form_field.title
568
+ 'class': container_classes.join(' '),
569
+ 'style': "width: " + (this.container_width()) + ";",
570
+ 'title': this.form_field.title
366
571
  };
367
- container_div = $("<div />", container_props);
572
+ if (this.form_field.id.length) {
573
+ container_props.id = this.form_field.id.replace(/[^\w]/g, '_') + "_chosen";
574
+ }
575
+ this.container = $("<div />", container_props);
368
576
  if (this.is_multiple) {
369
- container_div.html('<ul class="chzn-choices"><li class="search-field"><input type="text" value="' + this.default_text + '" class="default" autocomplete="off" style="width:25px;" /></li></ul><div class="chzn-drop" style="left:-9000px;"><ul class="chzn-results"></ul></div>');
577
+ 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>');
370
578
  } else {
371
- container_div.html('<a href="javascript:void(0)" class="chzn-single chzn-default" tabindex="-1"><span>' + this.default_text + '</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><div class="chzn-search"><input type="text" autocomplete="off" /></div><ul class="chzn-results"></ul></div>');
372
- }
373
- this.form_field_jq.hide().after(container_div);
374
- this.container = $('#' + this.container_id);
375
- this.dropdown = this.container.find('div.chzn-drop').first();
376
- dd_top = this.container.height();
377
- dd_width = this.f_width - get_side_border_padding(this.dropdown);
378
- this.dropdown.css({
379
- "width": dd_width + "px",
380
- "top": dd_top + "px"
381
- });
579
+ 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>');
580
+ }
581
+ this.form_field_jq.hide().after(this.container);
582
+ this.dropdown = this.container.find('div.chosen-drop').first();
382
583
  this.search_field = this.container.find('input').first();
383
- this.search_results = this.container.find('ul.chzn-results').first();
584
+ this.search_results = this.container.find('ul.chosen-results').first();
384
585
  this.search_field_scale();
385
586
  this.search_no_results = this.container.find('li.no-results').first();
386
587
  if (this.is_multiple) {
387
- this.search_choices = this.container.find('ul.chzn-choices').first();
588
+ this.search_choices = this.container.find('ul.chosen-choices').first();
388
589
  this.search_container = this.container.find('li.search-field').first();
389
590
  } else {
390
- this.search_container = this.container.find('div.chzn-search').first();
391
- this.selected_item = this.container.find('.chzn-single').first();
392
- sf_width = dd_width - get_side_border_padding(this.search_container) - get_side_border_padding(this.search_field);
393
- this.search_field.css({
394
- "width": sf_width + "px"
395
- });
591
+ this.search_container = this.container.find('div.chosen-search').first();
592
+ this.selected_item = this.container.find('.chosen-single').first();
396
593
  }
397
594
  this.results_build();
398
595
  this.set_tab_index();
399
- return this.form_field_jq.trigger("liszt:ready", {
596
+ this.set_label_behavior();
597
+ return this.form_field_jq.trigger("chosen:ready", {
400
598
  chosen: this
401
599
  });
402
600
  };
403
601
 
404
602
  Chosen.prototype.register_observers = function() {
405
603
  var _this = this;
406
- this.container.mousedown(function(evt) {
604
+ this.container.bind('mousedown.chosen', function(evt) {
407
605
  _this.container_mousedown(evt);
408
606
  });
409
- this.container.mouseup(function(evt) {
607
+ this.container.bind('mouseup.chosen', function(evt) {
410
608
  _this.container_mouseup(evt);
411
609
  });
412
- this.container.mouseenter(function(evt) {
610
+ this.container.bind('mouseenter.chosen', function(evt) {
413
611
  _this.mouse_enter(evt);
414
612
  });
415
- this.container.mouseleave(function(evt) {
613
+ this.container.bind('mouseleave.chosen', function(evt) {
416
614
  _this.mouse_leave(evt);
417
615
  });
418
- this.search_results.mouseup(function(evt) {
616
+ this.search_results.bind('mouseup.chosen', function(evt) {
419
617
  _this.search_results_mouseup(evt);
420
618
  });
421
- this.search_results.mouseover(function(evt) {
619
+ this.search_results.bind('mouseover.chosen', function(evt) {
422
620
  _this.search_results_mouseover(evt);
423
621
  });
424
- this.search_results.mouseout(function(evt) {
622
+ this.search_results.bind('mouseout.chosen', function(evt) {
425
623
  _this.search_results_mouseout(evt);
426
624
  });
427
- this.form_field_jq.bind("liszt:updated", function(evt) {
625
+ this.search_results.bind('mousewheel.chosen DOMMouseScroll.chosen', function(evt) {
626
+ _this.search_results_mousewheel(evt);
627
+ });
628
+ this.search_results.bind('touchstart.chosen', function(evt) {
629
+ _this.search_results_touchstart(evt);
630
+ });
631
+ this.search_results.bind('touchmove.chosen', function(evt) {
632
+ _this.search_results_touchmove(evt);
633
+ });
634
+ this.search_results.bind('touchend.chosen', function(evt) {
635
+ _this.search_results_touchend(evt);
636
+ });
637
+ this.form_field_jq.bind("chosen:updated.chosen", function(evt) {
428
638
  _this.results_update_field(evt);
429
639
  });
430
- this.form_field_jq.bind("liszt:activate", function(evt) {
640
+ this.form_field_jq.bind("chosen:activate.chosen", function(evt) {
431
641
  _this.activate_field(evt);
432
642
  });
433
- this.form_field_jq.bind("liszt:open", function(evt) {
643
+ this.form_field_jq.bind("chosen:open.chosen", function(evt) {
434
644
  _this.container_mousedown(evt);
435
645
  });
436
- this.search_field.blur(function(evt) {
646
+ this.form_field_jq.bind("chosen:close.chosen", function(evt) {
647
+ _this.input_blur(evt);
648
+ });
649
+ this.search_field.bind('blur.chosen', function(evt) {
437
650
  _this.input_blur(evt);
438
651
  });
439
- this.search_field.keyup(function(evt) {
652
+ this.search_field.bind('keyup.chosen', function(evt) {
440
653
  _this.keyup_checker(evt);
441
654
  });
442
- this.search_field.keydown(function(evt) {
655
+ this.search_field.bind('keydown.chosen', function(evt) {
443
656
  _this.keydown_checker(evt);
444
657
  });
445
- this.search_field.focus(function(evt) {
658
+ this.search_field.bind('focus.chosen', function(evt) {
446
659
  _this.input_focus(evt);
447
660
  });
661
+ this.search_field.bind('cut.chosen', function(evt) {
662
+ _this.clipboard_event_checker(evt);
663
+ });
664
+ this.search_field.bind('paste.chosen', function(evt) {
665
+ _this.clipboard_event_checker(evt);
666
+ });
448
667
  if (this.is_multiple) {
449
- return this.search_choices.click(function(evt) {
668
+ return this.search_choices.bind('click.chosen', function(evt) {
450
669
  _this.choices_click(evt);
451
670
  });
452
671
  } else {
453
- return this.container.click(function(evt) {
672
+ return this.container.bind('click.chosen', function(evt) {
454
673
  evt.preventDefault();
455
674
  });
456
675
  }
457
676
  };
458
677
 
678
+ Chosen.prototype.destroy = function() {
679
+ $(this.container[0].ownerDocument).unbind("click.chosen", this.click_test_action);
680
+ if (this.search_field[0].tabIndex) {
681
+ this.form_field_jq[0].tabIndex = this.search_field[0].tabIndex;
682
+ }
683
+ this.container.remove();
684
+ this.form_field_jq.removeData('chosen');
685
+ return this.form_field_jq.show();
686
+ };
687
+
459
688
  Chosen.prototype.search_field_disabled = function() {
460
689
  this.is_disabled = this.form_field_jq[0].disabled;
461
690
  if (this.is_disabled) {
462
- this.container.addClass('chzn-disabled');
691
+ this.container.addClass('chosen-disabled');
463
692
  this.search_field[0].disabled = true;
464
693
  if (!this.is_multiple) {
465
- this.selected_item.unbind("focus", this.activate_action);
694
+ this.selected_item.unbind("focus.chosen", this.activate_action);
466
695
  }
467
696
  return this.close_field();
468
697
  } else {
469
- this.container.removeClass('chzn-disabled');
698
+ this.container.removeClass('chosen-disabled');
470
699
  this.search_field[0].disabled = false;
471
700
  if (!this.is_multiple) {
472
- return this.selected_item.bind("focus", this.activate_action);
701
+ return this.selected_item.bind("focus.chosen", this.activate_action);
473
702
  }
474
703
  }
475
704
  };
476
705
 
477
706
  Chosen.prototype.container_mousedown = function(evt) {
478
- var target_closelink;
479
707
  if (!this.is_disabled) {
480
- target_closelink = evt != null ? ($(evt.target)).hasClass("search-choice-close") : false;
481
708
  if (evt && evt.type === "mousedown" && !this.results_showing) {
482
709
  evt.preventDefault();
483
710
  }
484
- if (!this.pending_destroy_click && !target_closelink) {
711
+ if (!((evt != null) && ($(evt.target)).hasClass("search-choice-close"))) {
485
712
  if (!this.active_field) {
486
713
  if (this.is_multiple) {
487
714
  this.search_field.val("");
488
715
  }
489
- $(document).click(this.click_test_action);
716
+ $(this.container[0].ownerDocument).bind('click.chosen', this.click_test_action);
490
717
  this.results_show();
491
- } else if (!this.is_multiple && evt && (($(evt.target)[0] === this.selected_item[0]) || $(evt.target).parents("a.chzn-single").length)) {
718
+ } else if (!this.is_multiple && evt && (($(evt.target)[0] === this.selected_item[0]) || $(evt.target).parents("a.chosen-single").length)) {
492
719
  evt.preventDefault();
493
720
  this.results_toggle();
494
721
  }
495
722
  return this.activate_field();
496
- } else {
497
- return this.pending_destroy_click = false;
498
723
  }
499
724
  }
500
725
  };
@@ -505,32 +730,47 @@ Copyright (c) 2011 by Harvest
505
730
  }
506
731
  };
507
732
 
733
+ Chosen.prototype.search_results_mousewheel = function(evt) {
734
+ var delta;
735
+ if (evt.originalEvent) {
736
+ delta = -evt.originalEvent.wheelDelta || evt.originalEvent.detail;
737
+ }
738
+ if (delta != null) {
739
+ evt.preventDefault();
740
+ if (evt.type === 'DOMMouseScroll') {
741
+ delta = delta * 40;
742
+ }
743
+ return this.search_results.scrollTop(delta + this.search_results.scrollTop());
744
+ }
745
+ };
746
+
508
747
  Chosen.prototype.blur_test = function(evt) {
509
- if (!this.active_field && this.container.hasClass("chzn-container-active")) {
748
+ if (!this.active_field && this.container.hasClass("chosen-container-active")) {
510
749
  return this.close_field();
511
750
  }
512
751
  };
513
752
 
514
753
  Chosen.prototype.close_field = function() {
515
- $(document).unbind("click", this.click_test_action);
754
+ $(this.container[0].ownerDocument).unbind("click.chosen", this.click_test_action);
516
755
  this.active_field = false;
517
756
  this.results_hide();
518
- this.container.removeClass("chzn-container-active");
519
- this.winnow_results_clear();
757
+ this.container.removeClass("chosen-container-active");
520
758
  this.clear_backstroke();
521
759
  this.show_search_field_default();
522
760
  return this.search_field_scale();
523
761
  };
524
762
 
525
763
  Chosen.prototype.activate_field = function() {
526
- this.container.addClass("chzn-container-active");
764
+ this.container.addClass("chosen-container-active");
527
765
  this.active_field = true;
528
766
  this.search_field.val(this.search_field.val());
529
767
  return this.search_field.focus();
530
768
  };
531
769
 
532
770
  Chosen.prototype.test_active_click = function(evt) {
533
- if ($(evt.target).parents('#' + this.container_id).length) {
771
+ var active_container;
772
+ active_container = $(evt.target).closest('.chosen-container');
773
+ if (active_container.length && this.container[0] === active_container[0]) {
534
774
  return this.active_field = true;
535
775
  } else {
536
776
  return this.close_field();
@@ -538,54 +778,30 @@ Copyright (c) 2011 by Harvest
538
778
  };
539
779
 
540
780
  Chosen.prototype.results_build = function() {
541
- var content, data, _i, _len, _ref;
542
781
  this.parsing = true;
543
- this.results_data = root.SelectParser.select_to_array(this.form_field);
544
- if (this.is_multiple && this.choices > 0) {
782
+ this.selected_option_count = null;
783
+ this.results_data = SelectParser.select_to_array(this.form_field);
784
+ if (this.is_multiple) {
545
785
  this.search_choices.find("li.search-choice").remove();
546
- this.choices = 0;
547
786
  } else if (!this.is_multiple) {
548
- this.selected_item.addClass("chzn-default").find("span").text(this.default_text);
787
+ this.single_set_selected_text();
549
788
  if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) {
550
- this.container.addClass("chzn-container-single-nosearch");
789
+ this.search_field[0].readOnly = true;
790
+ this.container.addClass("chosen-container-single-nosearch");
551
791
  } else {
552
- this.container.removeClass("chzn-container-single-nosearch");
553
- }
554
- }
555
- content = '';
556
- _ref = this.results_data;
557
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
558
- data = _ref[_i];
559
- if (data.group) {
560
- content += this.result_add_group(data);
561
- } else if (!data.empty) {
562
- content += this.result_add_option(data);
563
- if (data.selected && this.is_multiple) {
564
- this.choice_build(data);
565
- } else if (data.selected && !this.is_multiple) {
566
- this.selected_item.removeClass("chzn-default").find("span").text(data.text);
567
- if (this.allow_single_deselect) {
568
- this.single_deselect_control_build();
569
- }
570
- }
792
+ this.search_field[0].readOnly = false;
793
+ this.container.removeClass("chosen-container-single-nosearch");
571
794
  }
572
795
  }
796
+ this.update_results_content(this.results_option_build({
797
+ first: true
798
+ }));
573
799
  this.search_field_disabled();
574
800
  this.show_search_field_default();
575
801
  this.search_field_scale();
576
- this.search_results.html(content);
577
802
  return this.parsing = false;
578
803
  };
579
804
 
580
- Chosen.prototype.result_add_group = function(group) {
581
- if (!group.disabled) {
582
- group.dom_id = this.container_id + "_g_" + group.array_index;
583
- return '<li id="' + group.dom_id + '" class="group-result">' + $("<div />").text(group.label).html() + '</li>';
584
- } else {
585
- return "";
586
- }
587
- };
588
-
589
805
  Chosen.prototype.result_do_highlight = function(el) {
590
806
  var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
591
807
  if (el.length) {
@@ -613,57 +829,65 @@ Copyright (c) 2011 by Harvest
613
829
  };
614
830
 
615
831
  Chosen.prototype.results_show = function() {
616
- var dd_top;
617
- if (!this.is_multiple) {
618
- this.selected_item.addClass("chzn-single-with-drop");
619
- if (this.result_single_selected) {
620
- this.result_do_highlight(this.result_single_selected);
621
- }
622
- } else if (this.max_selected_options <= this.choices) {
623
- this.form_field_jq.trigger("liszt:maxselected", {
832
+ if (this.is_multiple && this.max_selected_options <= this.choices_count()) {
833
+ this.form_field_jq.trigger("chosen:maxselected", {
624
834
  chosen: this
625
835
  });
626
836
  return false;
627
837
  }
628
- dd_top = this.is_multiple ? this.container.height() : this.container.height() - 1;
629
- this.form_field_jq.trigger("liszt:showing_dropdown", {
630
- chosen: this
631
- });
632
- this.dropdown.css({
633
- "top": dd_top + "px",
634
- "left": 0
635
- });
838
+ this.container.addClass("chosen-with-drop");
636
839
  this.results_showing = true;
637
840
  this.search_field.focus();
638
841
  this.search_field.val(this.search_field.val());
639
- return this.winnow_results();
842
+ this.winnow_results();
843
+ return this.form_field_jq.trigger("chosen:showing_dropdown", {
844
+ chosen: this
845
+ });
846
+ };
847
+
848
+ Chosen.prototype.update_results_content = function(content) {
849
+ return this.search_results.html(content);
640
850
  };
641
851
 
642
852
  Chosen.prototype.results_hide = function() {
643
- if (!this.is_multiple) {
644
- this.selected_item.removeClass("chzn-single-with-drop");
853
+ if (this.results_showing) {
854
+ this.result_clear_highlight();
855
+ this.container.removeClass("chosen-with-drop");
856
+ this.form_field_jq.trigger("chosen:hiding_dropdown", {
857
+ chosen: this
858
+ });
645
859
  }
646
- this.result_clear_highlight();
647
- this.form_field_jq.trigger("liszt:hiding_dropdown", {
648
- chosen: this
649
- });
650
- this.dropdown.css({
651
- "left": "-9000px"
652
- });
653
860
  return this.results_showing = false;
654
861
  };
655
862
 
656
863
  Chosen.prototype.set_tab_index = function(el) {
657
864
  var ti;
658
- if (this.form_field_jq.attr("tabindex")) {
659
- ti = this.form_field_jq.attr("tabindex");
660
- this.form_field_jq.attr("tabindex", -1);
661
- return this.search_field.attr("tabindex", ti);
865
+ if (this.form_field.tabIndex) {
866
+ ti = this.form_field.tabIndex;
867
+ this.form_field.tabIndex = -1;
868
+ return this.search_field[0].tabIndex = ti;
869
+ }
870
+ };
871
+
872
+ Chosen.prototype.set_label_behavior = function() {
873
+ var _this = this;
874
+ this.form_field_label = this.form_field_jq.parents("label");
875
+ if (!this.form_field_label.length && this.form_field.id.length) {
876
+ this.form_field_label = $("label[for='" + this.form_field.id + "']");
877
+ }
878
+ if (this.form_field_label.length > 0) {
879
+ return this.form_field_label.bind('click.chosen', function(evt) {
880
+ if (_this.is_multiple) {
881
+ return _this.container_mousedown(evt);
882
+ } else {
883
+ return _this.activate_field();
884
+ }
885
+ });
662
886
  }
663
887
  };
664
888
 
665
889
  Chosen.prototype.show_search_field_default = function() {
666
- if (this.is_multiple && this.choices < 1 && !this.active_field) {
890
+ if (this.is_multiple && this.choices_count() < 1 && !this.active_field) {
667
891
  this.search_field.val(this.default_text);
668
892
  return this.search_field.addClass("default");
669
893
  } else {
@@ -696,51 +920,39 @@ Copyright (c) 2011 by Harvest
696
920
  }
697
921
  };
698
922
 
699
- Chosen.prototype.choices_click = function(evt) {
700
- evt.preventDefault();
701
- if (this.active_field && !($(evt.target).hasClass("search-choice" || $(evt.target).parents('.search-choice').first)) && !this.results_showing) {
702
- return this.results_show();
703
- }
704
- };
705
-
706
923
  Chosen.prototype.choice_build = function(item) {
707
- var choice_id, html, link,
924
+ var choice, close_link,
708
925
  _this = this;
709
- if (this.is_multiple && this.max_selected_options <= this.choices) {
710
- this.form_field_jq.trigger("liszt:maxselected", {
711
- chosen: this
712
- });
713
- return false;
714
- }
715
- choice_id = this.container_id + "_c_" + item.array_index;
716
- this.choices += 1;
926
+ choice = $('<li />', {
927
+ "class": "search-choice"
928
+ }).html("<span>" + item.html + "</span>");
717
929
  if (item.disabled) {
718
- html = '<li class="search-choice search-choice-disabled" id="' + choice_id + '"><span>' + item.html + '</span></li>';
930
+ choice.addClass('search-choice-disabled');
719
931
  } else {
720
- html = '<li class="search-choice" id="' + choice_id + '"><span>' + item.html + '</span><a href="javascript:void(0)" class="search-choice-close" rel="' + item.array_index + '"></a></li>';
932
+ close_link = $('<a />', {
933
+ "class": 'search-choice-close',
934
+ 'data-option-array-index': item.array_index
935
+ });
936
+ close_link.bind('click.chosen', function(evt) {
937
+ return _this.choice_destroy_link_click(evt);
938
+ });
939
+ choice.append(close_link);
721
940
  }
722
- this.search_container.before(html);
723
- link = $('#' + choice_id).find("a").first();
724
- return link.click(function(evt) {
725
- return _this.choice_destroy_link_click(evt);
726
- });
941
+ return this.search_container.before(choice);
727
942
  };
728
943
 
729
944
  Chosen.prototype.choice_destroy_link_click = function(evt) {
730
945
  evt.preventDefault();
946
+ evt.stopPropagation();
731
947
  if (!this.is_disabled) {
732
- this.pending_destroy_click = true;
733
948
  return this.choice_destroy($(evt.target));
734
- } else {
735
- return evt.stopPropagation;
736
949
  }
737
950
  };
738
951
 
739
952
  Chosen.prototype.choice_destroy = function(link) {
740
- if (this.result_deselect(link.attr("rel"))) {
741
- this.choices -= 1;
953
+ if (this.result_deselect(link[0].getAttribute("data-option-array-index"))) {
742
954
  this.show_search_field_default();
743
- if (this.is_multiple && this.choices > 0 && this.search_field.val().length < 1) {
955
+ if (this.is_multiple && this.choices_count() > 0 && this.search_field.val().length < 1) {
744
956
  this.results_hide();
745
957
  }
746
958
  link.parents('li').first().remove();
@@ -749,11 +961,9 @@ Copyright (c) 2011 by Harvest
749
961
  };
750
962
 
751
963
  Chosen.prototype.results_reset = function() {
964
+ this.reset_single_select_options();
752
965
  this.form_field.options[0].selected = true;
753
- this.selected_item.find("span").text(this.default_text);
754
- if (!this.is_multiple) {
755
- this.selected_item.addClass("chzn-default");
756
- }
966
+ this.single_set_selected_text();
757
967
  this.show_search_field_default();
758
968
  this.results_reset_cleanup();
759
969
  this.form_field_jq.trigger("change");
@@ -763,68 +973,73 @@ Copyright (c) 2011 by Harvest
763
973
  };
764
974
 
765
975
  Chosen.prototype.results_reset_cleanup = function() {
766
- this.current_value = this.form_field_jq.val();
976
+ this.current_selectedIndex = this.form_field.selectedIndex;
767
977
  return this.selected_item.find("abbr").remove();
768
978
  };
769
979
 
770
980
  Chosen.prototype.result_select = function(evt) {
771
- var high, high_id, item, position;
981
+ var high, item;
772
982
  if (this.result_highlight) {
773
983
  high = this.result_highlight;
774
- high_id = high.attr("id");
775
984
  this.result_clear_highlight();
985
+ if (this.is_multiple && this.max_selected_options <= this.choices_count()) {
986
+ this.form_field_jq.trigger("chosen:maxselected", {
987
+ chosen: this
988
+ });
989
+ return false;
990
+ }
776
991
  if (this.is_multiple) {
777
- this.result_deactivate(high);
992
+ high.removeClass("active-result");
778
993
  } else {
779
- this.search_results.find(".result-selected").removeClass("result-selected");
780
- this.result_single_selected = high;
781
- this.selected_item.removeClass("chzn-default");
994
+ this.reset_single_select_options();
782
995
  }
783
- high.addClass("result-selected");
784
- position = high_id.substr(high_id.lastIndexOf("_") + 1);
785
- item = this.results_data[position];
996
+ item = this.results_data[high[0].getAttribute("data-option-array-index")];
786
997
  item.selected = true;
787
998
  this.form_field.options[item.options_index].selected = true;
999
+ this.selected_option_count = null;
788
1000
  if (this.is_multiple) {
789
1001
  this.choice_build(item);
790
1002
  } else {
791
- this.selected_item.find("span").first().text(item.text);
792
- if (this.allow_single_deselect) {
793
- this.single_deselect_control_build();
794
- }
1003
+ this.single_set_selected_text(item.text);
795
1004
  }
796
1005
  if (!((evt.metaKey || evt.ctrlKey) && this.is_multiple)) {
797
1006
  this.results_hide();
798
1007
  }
799
1008
  this.search_field.val("");
800
- if (this.is_multiple || this.form_field_jq.val() !== this.current_value) {
1009
+ if (this.is_multiple || this.form_field.selectedIndex !== this.current_selectedIndex) {
801
1010
  this.form_field_jq.trigger("change", {
802
1011
  'selected': this.form_field.options[item.options_index].value
803
1012
  });
804
1013
  }
805
- this.current_value = this.form_field_jq.val();
1014
+ this.current_selectedIndex = this.form_field.selectedIndex;
806
1015
  return this.search_field_scale();
807
1016
  }
808
1017
  };
809
1018
 
810
- Chosen.prototype.result_activate = function(el) {
811
- return el.addClass("active-result");
812
- };
813
-
814
- Chosen.prototype.result_deactivate = function(el) {
815
- return el.removeClass("active-result");
1019
+ Chosen.prototype.single_set_selected_text = function(text) {
1020
+ if (text == null) {
1021
+ text = this.default_text;
1022
+ }
1023
+ if (text === this.default_text) {
1024
+ this.selected_item.addClass("chosen-default");
1025
+ } else {
1026
+ this.single_deselect_control_build();
1027
+ this.selected_item.removeClass("chosen-default");
1028
+ }
1029
+ return this.selected_item.find("span").text(text);
816
1030
  };
817
1031
 
818
1032
  Chosen.prototype.result_deselect = function(pos) {
819
- var result, result_data;
1033
+ var result_data;
820
1034
  result_data = this.results_data[pos];
821
1035
  if (!this.form_field.options[result_data.options_index].disabled) {
822
1036
  result_data.selected = false;
823
1037
  this.form_field.options[result_data.options_index].selected = false;
824
- result = $("#" + this.container_id + "_o_" + pos);
825
- result.removeClass("result-selected").addClass("active-result").show();
1038
+ this.selected_option_count = null;
826
1039
  this.result_clear_highlight();
827
- this.winnow_results();
1040
+ if (this.results_showing) {
1041
+ this.winnow_results();
1042
+ }
828
1043
  this.form_field_jq.trigger("change", {
829
1044
  deselected: this.form_field.options[result_data.options_index].value
830
1045
  });
@@ -836,100 +1051,29 @@ Copyright (c) 2011 by Harvest
836
1051
  };
837
1052
 
838
1053
  Chosen.prototype.single_deselect_control_build = function() {
839
- if (this.allow_single_deselect && this.selected_item.find("abbr").length < 1) {
840
- return this.selected_item.find("span").first().after("<abbr class=\"search-choice-close\"></abbr>");
841
- }
842
- };
843
-
844
- Chosen.prototype.winnow_results = function() {
845
- var found, option, part, parts, regex, regexAnchor, result, result_id, results, searchText, startpos, text, zregex, _i, _j, _len, _len1, _ref;
846
- this.no_results_clear();
847
- results = 0;
848
- searchText = this.search_field.val() === this.default_text ? "" : $('<div/>').text($.trim(this.search_field.val())).html();
849
- regexAnchor = this.search_contains ? "" : "^";
850
- regex = new RegExp(regexAnchor + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
851
- zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
852
- _ref = this.results_data;
853
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
854
- option = _ref[_i];
855
- if (!option.disabled && !option.empty) {
856
- if (option.group) {
857
- $('#' + option.dom_id).css('display', 'none');
858
- } else if (!(this.is_multiple && option.selected)) {
859
- found = false;
860
- result_id = option.dom_id;
861
- result = $("#" + result_id);
862
- if (regex.test(option.html)) {
863
- found = true;
864
- results += 1;
865
- } else if (this.enable_split_word_search && (option.html.indexOf(" ") >= 0 || option.html.indexOf("[") === 0)) {
866
- parts = option.html.replace(/\[|\]/g, "").split(" ");
867
- if (parts.length) {
868
- for (_j = 0, _len1 = parts.length; _j < _len1; _j++) {
869
- part = parts[_j];
870
- if (regex.test(part)) {
871
- found = true;
872
- results += 1;
873
- }
874
- }
875
- }
876
- }
877
- if (found) {
878
- if (searchText.length) {
879
- startpos = option.html.search(zregex);
880
- text = option.html.substr(0, startpos + searchText.length) + '</em>' + option.html.substr(startpos + searchText.length);
881
- text = text.substr(0, startpos) + '<em>' + text.substr(startpos);
882
- } else {
883
- text = option.html;
884
- }
885
- result.html(text);
886
- this.result_activate(result);
887
- if (option.group_array_index != null) {
888
- $("#" + this.results_data[option.group_array_index].dom_id).css('display', 'list-item');
889
- }
890
- } else {
891
- if (this.result_highlight && result_id === this.result_highlight.attr('id')) {
892
- this.result_clear_highlight();
893
- }
894
- this.result_deactivate(result);
895
- }
896
- }
897
- }
1054
+ if (!this.allow_single_deselect) {
1055
+ return;
898
1056
  }
899
- if (results < 1 && searchText.length) {
900
- return this.no_results(searchText);
901
- } else {
902
- return this.winnow_results_set_highlight();
1057
+ if (!this.selected_item.find("abbr").length) {
1058
+ this.selected_item.find("span").first().after("<abbr class=\"search-choice-close\"></abbr>");
903
1059
  }
1060
+ return this.selected_item.addClass("chosen-single-with-deselect");
904
1061
  };
905
1062
 
906
- Chosen.prototype.winnow_results_clear = function() {
907
- var li, lis, _i, _len, _results;
908
- this.search_field.val("");
909
- lis = this.search_results.find("li");
910
- _results = [];
911
- for (_i = 0, _len = lis.length; _i < _len; _i++) {
912
- li = lis[_i];
913
- li = $(li);
914
- if (li.hasClass("group-result")) {
915
- _results.push(li.css('display', 'auto'));
916
- } else if (!this.is_multiple || !li.hasClass("result-selected")) {
917
- _results.push(this.result_activate(li));
918
- } else {
919
- _results.push(void 0);
920
- }
1063
+ Chosen.prototype.get_search_text = function() {
1064
+ if (this.search_field.val() === this.default_text) {
1065
+ return "";
1066
+ } else {
1067
+ return $('<div/>').text($.trim(this.search_field.val())).html();
921
1068
  }
922
- return _results;
923
1069
  };
924
1070
 
925
1071
  Chosen.prototype.winnow_results_set_highlight = function() {
926
1072
  var do_high, selected_results;
927
- if (!this.result_highlight) {
928
- selected_results = !this.is_multiple ? this.search_results.find(".result-selected.active-result") : [];
929
- do_high = selected_results.length ? selected_results.first() : this.search_results.find(".active-result").first();
930
- if (do_high != null) {
931
- return this.result_do_highlight(do_high);
932
- }
1073
+ selected_results = !this.is_multiple ? this.search_results.find(".result-selected.active-result") : [];
1074
+ do_high = selected_results.length ? selected_results.first() : this.search_results.find(".active-result").first();
1075
+ if (do_high != null) {
1076
+ return this.result_do_highlight(do_high);
933
1077
  }
934
1078
  };
935
1079
 
@@ -937,7 +1081,10 @@ Copyright (c) 2011 by Harvest
937
1081
  var no_results_html;
938
1082
  no_results_html = $('<li class="no-results">' + this.results_none_found + ' "<span></span>"</li>');
939
1083
  no_results_html.find("span").first().html(terms);
940
- return this.search_results.append(no_results_html);
1084
+ this.search_results.append(no_results_html);
1085
+ return this.form_field_jq.trigger("chosen:no_results", {
1086
+ chosen: this
1087
+ });
941
1088
  };
942
1089
 
943
1090
  Chosen.prototype.no_results_clear = function() {
@@ -945,19 +1092,13 @@ Copyright (c) 2011 by Harvest
945
1092
  };
946
1093
 
947
1094
  Chosen.prototype.keydown_arrow = function() {
948
- var first_active, next_sib;
949
- if (!this.result_highlight) {
950
- first_active = this.search_results.find("li.active-result").first();
951
- if (first_active) {
952
- this.result_do_highlight($(first_active));
953
- }
954
- } else if (this.results_showing) {
1095
+ var next_sib;
1096
+ if (this.results_showing && this.result_highlight) {
955
1097
  next_sib = this.result_highlight.nextAll("li.active-result").first();
956
1098
  if (next_sib) {
957
- this.result_do_highlight(next_sib);
1099
+ return this.result_do_highlight(next_sib);
958
1100
  }
959
- }
960
- if (!this.results_showing) {
1101
+ } else {
961
1102
  return this.results_show();
962
1103
  }
963
1104
  };
@@ -971,7 +1112,7 @@ Copyright (c) 2011 by Harvest
971
1112
  if (prev_sibs.length) {
972
1113
  return this.result_do_highlight(prev_sibs.first());
973
1114
  } else {
974
- if (this.choices > 0) {
1115
+ if (this.choices_count() > 0) {
975
1116
  this.results_hide();
976
1117
  }
977
1118
  return this.result_clear_highlight();
@@ -1005,8 +1146,8 @@ Copyright (c) 2011 by Harvest
1005
1146
  };
1006
1147
 
1007
1148
  Chosen.prototype.keydown_checker = function(evt) {
1008
- var stroke, _ref;
1009
- stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
1149
+ var stroke, _ref1;
1150
+ stroke = (_ref1 = evt.which) != null ? _ref1 : evt.keyCode;
1010
1151
  this.search_field_scale();
1011
1152
  if (stroke !== 8 && this.pending_backstroke) {
1012
1153
  this.clear_backstroke();
@@ -1029,13 +1170,14 @@ Copyright (c) 2011 by Harvest
1029
1170
  this.keyup_arrow();
1030
1171
  break;
1031
1172
  case 40:
1173
+ evt.preventDefault();
1032
1174
  this.keydown_arrow();
1033
1175
  break;
1034
1176
  }
1035
1177
  };
1036
1178
 
1037
1179
  Chosen.prototype.search_field_scale = function() {
1038
- var dd_top, div, h, style, style_block, styles, w, _i, _len;
1180
+ var div, f_width, h, style, style_block, styles, w, _i, _len;
1039
1181
  if (this.is_multiple) {
1040
1182
  h = 0;
1041
1183
  w = 0;
@@ -1052,39 +1194,18 @@ Copyright (c) 2011 by Harvest
1052
1194
  $('body').append(div);
1053
1195
  w = div.width() + 25;
1054
1196
  div.remove();
1055
- if (w > this.f_width - 10) {
1056
- w = this.f_width - 10;
1197
+ f_width = this.container.outerWidth();
1198
+ if (w > f_width - 10) {
1199
+ w = f_width - 10;
1057
1200
  }
1058
- this.search_field.css({
1201
+ return this.search_field.css({
1059
1202
  'width': w + 'px'
1060
1203
  });
1061
- dd_top = this.container.height();
1062
- return this.dropdown.css({
1063
- "top": dd_top + "px"
1064
- });
1065
- }
1066
- };
1067
-
1068
- Chosen.prototype.generate_random_id = function() {
1069
- var string;
1070
- string = "sel" + this.generate_random_char() + this.generate_random_char() + this.generate_random_char();
1071
- while ($("#" + string).length > 0) {
1072
- string += this.generate_random_char();
1073
1204
  }
1074
- return string;
1075
1205
  };
1076
1206
 
1077
1207
  return Chosen;
1078
1208
 
1079
1209
  })(AbstractChosen);
1080
1210
 
1081
- root.Chosen = Chosen;
1082
-
1083
- get_side_border_padding = function(elmt) {
1084
- var side_border_padding;
1085
- return side_border_padding = elmt.outerWidth() - elmt.width();
1086
- };
1087
-
1088
- root.get_side_border_padding = get_side_border_padding;
1089
-
1090
1211
  }).call(this);