Commit 2b319d0496fb4eee24f78e1817235daf23d52079
Exists in
master
and in
4 other branches
Merge pull request #828 from CedricGatay/feature/chosen_0.9.8_searchContains
Chosen 0.9.8 + Autocomplete contains
Showing
2 changed files
with
226 additions
and
165 deletions
Show diff stats
app/assets/javascripts/application.js
... | ... | @@ -79,3 +79,13 @@ function showDiff(link) { |
79 | 79 | $(link).next('table').show(); |
80 | 80 | $(link).remove(); |
81 | 81 | } |
82 | + | |
83 | +(function($){ | |
84 | + var _chosen = $.fn.chosen; | |
85 | + $.fn.extend({ | |
86 | + chosen: function(options) { | |
87 | + var default_options = {'search_contains' : 'true'}; | |
88 | + $.extend(default_options, options); | |
89 | + return _chosen.apply(this, [default_options]); | |
90 | + }}) | |
91 | +})(jQuery); | ... | ... |
vendor/assets/javascripts/chosen.js
1 | 1 | // Chosen, a Select Box Enhancer for jQuery and Protoype |
2 | 2 | // by Patrick Filler for Harvest, http://getharvest.com |
3 | 3 | // |
4 | -// Version 0.9.5 | |
4 | +// Version 0.9.8 | |
5 | 5 | // Full source at https://github.com/harvesthq/chosen |
6 | 6 | // Copyright (c) 2011 Harvest http://getharvest.com |
7 | 7 | |
... | ... | @@ -9,11 +9,14 @@ |
9 | 9 | // This file is generated by `cake build`, do not edit it by hand. |
10 | 10 | (function() { |
11 | 11 | var SelectParser; |
12 | + | |
12 | 13 | SelectParser = (function() { |
14 | + | |
13 | 15 | function SelectParser() { |
14 | 16 | this.options_index = 0; |
15 | 17 | this.parsed = []; |
16 | 18 | } |
19 | + | |
17 | 20 | SelectParser.prototype.add_node = function(child) { |
18 | 21 | if (child.nodeName === "OPTGROUP") { |
19 | 22 | return this.add_group(child); |
... | ... | @@ -21,6 +24,7 @@ |
21 | 24 | return this.add_option(child); |
22 | 25 | } |
23 | 26 | }; |
27 | + | |
24 | 28 | SelectParser.prototype.add_group = function(group) { |
25 | 29 | var group_position, option, _i, _len, _ref, _results; |
26 | 30 | group_position = this.parsed.length; |
... | ... | @@ -39,12 +43,11 @@ |
39 | 43 | } |
40 | 44 | return _results; |
41 | 45 | }; |
46 | + | |
42 | 47 | SelectParser.prototype.add_option = function(option, group_position, group_disabled) { |
43 | 48 | if (option.nodeName === "OPTION") { |
44 | 49 | if (option.text !== "") { |
45 | - if (group_position != null) { | |
46 | - this.parsed[group_position].children += 1; | |
47 | - } | |
50 | + if (group_position != null) this.parsed[group_position].children += 1; | |
48 | 51 | this.parsed.push({ |
49 | 52 | array_index: this.parsed.length, |
50 | 53 | options_index: this.options_index, |
... | ... | @@ -67,8 +70,11 @@ |
67 | 70 | return this.options_index += 1; |
68 | 71 | } |
69 | 72 | }; |
73 | + | |
70 | 74 | return SelectParser; |
75 | + | |
71 | 76 | })(); |
77 | + | |
72 | 78 | SelectParser.select_to_array = function(select) { |
73 | 79 | var child, parser, _i, _len, _ref; |
74 | 80 | parser = new SelectParser(); |
... | ... | @@ -79,17 +85,23 @@ |
79 | 85 | } |
80 | 86 | return parser.parsed; |
81 | 87 | }; |
88 | + | |
82 | 89 | this.SelectParser = SelectParser; |
90 | + | |
83 | 91 | }).call(this); |
92 | + | |
93 | +/* | |
94 | +Chosen source: generate output using 'cake build' | |
95 | +Copyright (c) 2011 by Harvest | |
96 | +*/ | |
97 | + | |
84 | 98 | (function() { |
85 | - /* | |
86 | - Chosen source: generate output using 'cake build' | |
87 | - Copyright (c) 2011 by Harvest | |
88 | - */ | |
89 | 99 | var AbstractChosen, root; |
90 | - var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | |
100 | + | |
91 | 101 | root = this; |
102 | + | |
92 | 103 | AbstractChosen = (function() { |
104 | + | |
93 | 105 | function AbstractChosen(form_field, options) { |
94 | 106 | this.form_field = form_field; |
95 | 107 | this.options = options != null ? options : {}; |
... | ... | @@ -101,69 +113,75 @@ |
101 | 113 | this.register_observers(); |
102 | 114 | this.finish_setup(); |
103 | 115 | } |
116 | + | |
104 | 117 | AbstractChosen.prototype.set_default_values = function() { |
105 | - this.click_test_action = __bind(function(evt) { | |
106 | - return this.test_active_click(evt); | |
107 | - }, this); | |
108 | - this.activate_action = __bind(function(evt) { | |
109 | - return this.activate_field(evt); | |
110 | - }, this); | |
118 | + var _this = this; | |
119 | + this.click_test_action = function(evt) { | |
120 | + return _this.test_active_click(evt); | |
121 | + }; | |
122 | + this.activate_action = function(evt) { | |
123 | + return _this.activate_field(evt); | |
124 | + }; | |
111 | 125 | this.active_field = false; |
112 | 126 | this.mouse_on_container = false; |
113 | 127 | this.results_showing = false; |
114 | 128 | this.result_highlighted = null; |
115 | 129 | this.result_single_selected = null; |
116 | - this.allow_single_deselect = (this.options.allow_single_deselect != null) && this.form_field.options[0].text === "" ? this.options.allow_single_deselect : false; | |
130 | + 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; | |
117 | 131 | this.disable_search_threshold = this.options.disable_search_threshold || 0; |
132 | + this.search_contains = this.options.search_contains || false; | |
118 | 133 | this.choices = 0; |
119 | 134 | return this.results_none_found = this.options.no_results_text || "No results match"; |
120 | 135 | }; |
136 | + | |
121 | 137 | AbstractChosen.prototype.mouse_enter = function() { |
122 | 138 | return this.mouse_on_container = true; |
123 | 139 | }; |
140 | + | |
124 | 141 | AbstractChosen.prototype.mouse_leave = function() { |
125 | 142 | return this.mouse_on_container = false; |
126 | 143 | }; |
144 | + | |
127 | 145 | AbstractChosen.prototype.input_focus = function(evt) { |
146 | + var _this = this; | |
128 | 147 | if (!this.active_field) { |
129 | - return setTimeout((__bind(function() { | |
130 | - return this.container_mousedown(); | |
131 | - }, this)), 50); | |
148 | + return setTimeout((function() { | |
149 | + return _this.container_mousedown(); | |
150 | + }), 50); | |
132 | 151 | } |
133 | 152 | }; |
153 | + | |
134 | 154 | AbstractChosen.prototype.input_blur = function(evt) { |
155 | + var _this = this; | |
135 | 156 | if (!this.mouse_on_container) { |
136 | 157 | this.active_field = false; |
137 | - return setTimeout((__bind(function() { | |
138 | - return this.blur_test(); | |
139 | - }, this)), 100); | |
158 | + return setTimeout((function() { | |
159 | + return _this.blur_test(); | |
160 | + }), 100); | |
140 | 161 | } |
141 | 162 | }; |
163 | + | |
142 | 164 | AbstractChosen.prototype.result_add_option = function(option) { |
143 | 165 | var classes, style; |
144 | 166 | if (!option.disabled) { |
145 | 167 | option.dom_id = this.container_id + "_o_" + option.array_index; |
146 | 168 | classes = option.selected && this.is_multiple ? [] : ["active-result"]; |
147 | - if (option.selected) { | |
148 | - classes.push("result-selected"); | |
149 | - } | |
150 | - if (option.group_array_index != null) { | |
151 | - classes.push("group-option"); | |
152 | - } | |
153 | - if (option.classes !== "") { | |
154 | - classes.push(option.classes); | |
155 | - } | |
169 | + if (option.selected) classes.push("result-selected"); | |
170 | + if (option.group_array_index != null) classes.push("group-option"); | |
171 | + if (option.classes !== "") classes.push(option.classes); | |
156 | 172 | style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : ""; |
157 | 173 | return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"' + style + '>' + option.html + '</li>'; |
158 | 174 | } else { |
159 | 175 | return ""; |
160 | 176 | } |
161 | 177 | }; |
178 | + | |
162 | 179 | AbstractChosen.prototype.results_update_field = function() { |
163 | 180 | this.result_clear_highlight(); |
164 | 181 | this.result_single_selected = null; |
165 | 182 | return this.results_build(); |
166 | 183 | }; |
184 | + | |
167 | 185 | AbstractChosen.prototype.results_toggle = function() { |
168 | 186 | if (this.results_showing) { |
169 | 187 | return this.results_hide(); |
... | ... | @@ -171,6 +189,7 @@ |
171 | 189 | return this.results_show(); |
172 | 190 | } |
173 | 191 | }; |
192 | + | |
174 | 193 | AbstractChosen.prototype.results_search = function(evt) { |
175 | 194 | if (this.results_showing) { |
176 | 195 | return this.winnow_results(); |
... | ... | @@ -178,6 +197,7 @@ |
178 | 197 | return this.results_show(); |
179 | 198 | } |
180 | 199 | }; |
200 | + | |
181 | 201 | AbstractChosen.prototype.keyup_checker = function(evt) { |
182 | 202 | var stroke, _ref; |
183 | 203 | stroke = (_ref = evt.which) != null ? _ref : evt.keyCode; |
... | ... | @@ -193,15 +213,11 @@ |
193 | 213 | break; |
194 | 214 | case 13: |
195 | 215 | evt.preventDefault(); |
196 | - if (this.results_showing) { | |
197 | - return this.result_select(evt); | |
198 | - } | |
216 | + if (this.results_showing) return this.result_select(evt); | |
199 | 217 | break; |
200 | 218 | case 27: |
201 | - if (this.results_showing) { | |
202 | - return this.results_hide(); | |
203 | - } | |
204 | - break; | |
219 | + if (this.results_showing) this.results_hide(); | |
220 | + return true; | |
205 | 221 | case 9: |
206 | 222 | case 38: |
207 | 223 | case 40: |
... | ... | @@ -213,62 +229,71 @@ |
213 | 229 | return this.results_search(); |
214 | 230 | } |
215 | 231 | }; |
232 | + | |
216 | 233 | AbstractChosen.prototype.generate_field_id = function() { |
217 | 234 | var new_id; |
218 | 235 | new_id = this.generate_random_id(); |
219 | 236 | this.form_field.id = new_id; |
220 | 237 | return new_id; |
221 | 238 | }; |
239 | + | |
222 | 240 | AbstractChosen.prototype.generate_random_char = function() { |
223 | 241 | var chars, newchar, rand; |
224 | 242 | chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ"; |
225 | 243 | rand = Math.floor(Math.random() * chars.length); |
226 | 244 | return newchar = chars.substring(rand, rand + 1); |
227 | 245 | }; |
246 | + | |
228 | 247 | return AbstractChosen; |
248 | + | |
229 | 249 | })(); |
250 | + | |
230 | 251 | root.AbstractChosen = AbstractChosen; |
252 | + | |
231 | 253 | }).call(this); |
254 | + | |
255 | +/* | |
256 | +Chosen source: generate output using 'cake build' | |
257 | +Copyright (c) 2011 by Harvest | |
258 | +*/ | |
259 | + | |
232 | 260 | (function() { |
233 | - /* | |
234 | - Chosen source: generate output using 'cake build' | |
235 | - Copyright (c) 2011 by Harvest | |
236 | - */ | |
237 | - var $, Chosen, get_side_border_padding, root; | |
238 | - var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { | |
239 | - for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } | |
240 | - function ctor() { this.constructor = child; } | |
241 | - ctor.prototype = parent.prototype; | |
242 | - child.prototype = new ctor; | |
243 | - child.__super__ = parent.prototype; | |
244 | - return child; | |
245 | - }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | |
261 | + var $, Chosen, get_side_border_padding, root, | |
262 | + __hasProp = Object.prototype.hasOwnProperty, | |
263 | + __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; }; | |
264 | + | |
246 | 265 | root = this; |
266 | + | |
247 | 267 | $ = jQuery; |
268 | + | |
248 | 269 | $.fn.extend({ |
249 | 270 | chosen: function(options) { |
250 | 271 | if ($.browser.msie && ($.browser.version === "6.0" || $.browser.version === "7.0")) { |
251 | 272 | return this; |
252 | 273 | } |
253 | 274 | return $(this).each(function(input_field) { |
254 | - if (!($(this)).hasClass("chzn-done")) { | |
255 | - return new Chosen(this, options); | |
256 | - } | |
275 | + if (!($(this)).hasClass("chzn-done")) return new Chosen(this, options); | |
257 | 276 | }); |
258 | 277 | } |
259 | 278 | }); |
260 | - Chosen = (function() { | |
261 | - __extends(Chosen, AbstractChosen); | |
279 | + | |
280 | + Chosen = (function(_super) { | |
281 | + | |
282 | + __extends(Chosen, _super); | |
283 | + | |
262 | 284 | function Chosen() { |
263 | 285 | Chosen.__super__.constructor.apply(this, arguments); |
264 | 286 | } |
287 | + | |
265 | 288 | Chosen.prototype.setup = function() { |
266 | 289 | this.form_field_jq = $(this.form_field); |
267 | 290 | return this.is_rtl = this.form_field_jq.hasClass("chzn-rtl"); |
268 | 291 | }; |
292 | + | |
269 | 293 | Chosen.prototype.finish_setup = function() { |
270 | 294 | return this.form_field_jq.addClass("chzn-done"); |
271 | 295 | }; |
296 | + | |
272 | 297 | Chosen.prototype.set_up_html = function() { |
273 | 298 | var container_div, dd_top, dd_width, sf_width; |
274 | 299 | this.container_id = this.form_field.id.length ? this.form_field.id.replace(/(:|\.)/g, '_') : this.generate_field_id(); |
... | ... | @@ -283,7 +308,7 @@ |
283 | 308 | if (this.is_multiple) { |
284 | 309 | 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>'); |
285 | 310 | } else { |
286 | - container_div.html('<a href="javascript:void(0)" class="chzn-single"><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>'); | |
311 | + container_div.html('<a href="javascript:void(0)" class="chzn-single chzn-default"><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>'); | |
287 | 312 | } |
288 | 313 | this.form_field_jq.hide().after(container_div); |
289 | 314 | this.container = $('#' + this.container_id); |
... | ... | @@ -316,49 +341,56 @@ |
316 | 341 | chosen: this |
317 | 342 | }); |
318 | 343 | }; |
344 | + | |
319 | 345 | Chosen.prototype.register_observers = function() { |
320 | - this.container.mousedown(__bind(function(evt) { | |
321 | - return this.container_mousedown(evt); | |
322 | - }, this)); | |
323 | - this.container.mouseup(__bind(function(evt) { | |
324 | - return this.container_mouseup(evt); | |
325 | - }, this)); | |
326 | - this.container.mouseenter(__bind(function(evt) { | |
327 | - return this.mouse_enter(evt); | |
328 | - }, this)); | |
329 | - this.container.mouseleave(__bind(function(evt) { | |
330 | - return this.mouse_leave(evt); | |
331 | - }, this)); | |
332 | - this.search_results.mouseup(__bind(function(evt) { | |
333 | - return this.search_results_mouseup(evt); | |
334 | - }, this)); | |
335 | - this.search_results.mouseover(__bind(function(evt) { | |
336 | - return this.search_results_mouseover(evt); | |
337 | - }, this)); | |
338 | - this.search_results.mouseout(__bind(function(evt) { | |
339 | - return this.search_results_mouseout(evt); | |
340 | - }, this)); | |
341 | - this.form_field_jq.bind("liszt:updated", __bind(function(evt) { | |
342 | - return this.results_update_field(evt); | |
343 | - }, this)); | |
344 | - this.search_field.blur(__bind(function(evt) { | |
345 | - return this.input_blur(evt); | |
346 | - }, this)); | |
347 | - this.search_field.keyup(__bind(function(evt) { | |
348 | - return this.keyup_checker(evt); | |
349 | - }, this)); | |
350 | - this.search_field.keydown(__bind(function(evt) { | |
351 | - return this.keydown_checker(evt); | |
352 | - }, this)); | |
346 | + var _this = this; | |
347 | + this.container.mousedown(function(evt) { | |
348 | + return _this.container_mousedown(evt); | |
349 | + }); | |
350 | + this.container.mouseup(function(evt) { | |
351 | + return _this.container_mouseup(evt); | |
352 | + }); | |
353 | + this.container.mouseenter(function(evt) { | |
354 | + return _this.mouse_enter(evt); | |
355 | + }); | |
356 | + this.container.mouseleave(function(evt) { | |
357 | + return _this.mouse_leave(evt); | |
358 | + }); | |
359 | + this.search_results.mouseup(function(evt) { | |
360 | + return _this.search_results_mouseup(evt); | |
361 | + }); | |
362 | + this.search_results.mouseover(function(evt) { | |
363 | + return _this.search_results_mouseover(evt); | |
364 | + }); | |
365 | + this.search_results.mouseout(function(evt) { | |
366 | + return _this.search_results_mouseout(evt); | |
367 | + }); | |
368 | + this.form_field_jq.bind("liszt:updated", function(evt) { | |
369 | + return _this.results_update_field(evt); | |
370 | + }); | |
371 | + this.search_field.blur(function(evt) { | |
372 | + return _this.input_blur(evt); | |
373 | + }); | |
374 | + this.search_field.keyup(function(evt) { | |
375 | + return _this.keyup_checker(evt); | |
376 | + }); | |
377 | + this.search_field.keydown(function(evt) { | |
378 | + return _this.keydown_checker(evt); | |
379 | + }); | |
353 | 380 | if (this.is_multiple) { |
354 | - this.search_choices.click(__bind(function(evt) { | |
355 | - return this.choices_click(evt); | |
356 | - }, this)); | |
357 | - return this.search_field.focus(__bind(function(evt) { | |
358 | - return this.input_focus(evt); | |
359 | - }, this)); | |
381 | + this.search_choices.click(function(evt) { | |
382 | + return _this.choices_click(evt); | |
383 | + }); | |
384 | + return this.search_field.focus(function(evt) { | |
385 | + return _this.input_focus(evt); | |
386 | + }); | |
387 | + } else { | |
388 | + return this.container.click(function(evt) { | |
389 | + return evt.preventDefault(); | |
390 | + }); | |
360 | 391 | } |
361 | 392 | }; |
393 | + | |
362 | 394 | Chosen.prototype.search_field_disabled = function() { |
363 | 395 | this.is_disabled = this.form_field_jq[0].disabled; |
364 | 396 | if (this.is_disabled) { |
... | ... | @@ -376,21 +408,20 @@ |
376 | 408 | } |
377 | 409 | } |
378 | 410 | }; |
411 | + | |
379 | 412 | Chosen.prototype.container_mousedown = function(evt) { |
380 | 413 | var target_closelink; |
381 | 414 | if (!this.is_disabled) { |
382 | 415 | target_closelink = evt != null ? ($(evt.target)).hasClass("search-choice-close") : false; |
383 | - if (evt && evt.type === "mousedown") { | |
416 | + if (evt && evt.type === "mousedown" && !this.results_showing) { | |
384 | 417 | evt.stopPropagation(); |
385 | 418 | } |
386 | 419 | if (!this.pending_destroy_click && !target_closelink) { |
387 | 420 | if (!this.active_field) { |
388 | - if (this.is_multiple) { | |
389 | - this.search_field.val(""); | |
390 | - } | |
421 | + if (this.is_multiple) this.search_field.val(""); | |
391 | 422 | $(document).click(this.click_test_action); |
392 | 423 | this.results_show(); |
393 | - } else if (!this.is_multiple && evt && ($(evt.target) === this.selected_item || $(evt.target).parents("a.chzn-single").length)) { | |
424 | + } else if (!this.is_multiple && evt && (($(evt.target)[0] === this.selected_item[0]) || $(evt.target).parents("a.chzn-single").length)) { | |
394 | 425 | evt.preventDefault(); |
395 | 426 | this.results_toggle(); |
396 | 427 | } |
... | ... | @@ -400,16 +431,17 @@ |
400 | 431 | } |
401 | 432 | } |
402 | 433 | }; |
434 | + | |
403 | 435 | Chosen.prototype.container_mouseup = function(evt) { |
404 | - if (evt.target.nodeName === "ABBR") { | |
405 | - return this.results_reset(evt); | |
406 | - } | |
436 | + if (evt.target.nodeName === "ABBR") return this.results_reset(evt); | |
407 | 437 | }; |
438 | + | |
408 | 439 | Chosen.prototype.blur_test = function(evt) { |
409 | 440 | if (!this.active_field && this.container.hasClass("chzn-container-active")) { |
410 | 441 | return this.close_field(); |
411 | 442 | } |
412 | 443 | }; |
444 | + | |
413 | 445 | Chosen.prototype.close_field = function() { |
414 | 446 | $(document).unbind("click", this.click_test_action); |
415 | 447 | if (!this.is_multiple) { |
... | ... | @@ -424,6 +456,7 @@ |
424 | 456 | this.show_search_field_default(); |
425 | 457 | return this.search_field_scale(); |
426 | 458 | }; |
459 | + | |
427 | 460 | Chosen.prototype.activate_field = function() { |
428 | 461 | if (!this.is_multiple && !this.active_field) { |
429 | 462 | this.search_field.attr("tabindex", this.selected_item.attr("tabindex")); |
... | ... | @@ -434,6 +467,7 @@ |
434 | 467 | this.search_field.val(this.search_field.val()); |
435 | 468 | return this.search_field.focus(); |
436 | 469 | }; |
470 | + | |
437 | 471 | Chosen.prototype.test_active_click = function(evt) { |
438 | 472 | if ($(evt.target).parents('#' + this.container_id).length) { |
439 | 473 | return this.active_field = true; |
... | ... | @@ -441,9 +475,9 @@ |
441 | 475 | return this.close_field(); |
442 | 476 | } |
443 | 477 | }; |
478 | + | |
444 | 479 | Chosen.prototype.results_build = function() { |
445 | - var content, data, startTime, _i, _len, _ref; | |
446 | - startTime = new Date(); | |
480 | + var content, data, _i, _len, _ref; | |
447 | 481 | this.parsing = true; |
448 | 482 | this.results_data = root.SelectParser.select_to_array(this.form_field); |
449 | 483 | if (this.is_multiple && this.choices > 0) { |
... | ... | @@ -468,10 +502,8 @@ |
468 | 502 | if (data.selected && this.is_multiple) { |
469 | 503 | this.choice_build(data); |
470 | 504 | } else if (data.selected && !this.is_multiple) { |
471 | - this.selected_item.find("span").text(data.text); | |
472 | - if (this.allow_single_deselect) { | |
473 | - this.single_deselect_control_build(); | |
474 | - } | |
505 | + this.selected_item.removeClass("chzn-default").find("span").text(data.text); | |
506 | + if (this.allow_single_deselect) this.single_deselect_control_build(); | |
475 | 507 | } |
476 | 508 | } |
477 | 509 | } |
... | ... | @@ -481,6 +513,7 @@ |
481 | 513 | this.search_results.html(content); |
482 | 514 | return this.parsing = false; |
483 | 515 | }; |
516 | + | |
484 | 517 | Chosen.prototype.result_add_group = function(group) { |
485 | 518 | if (!group.disabled) { |
486 | 519 | group.dom_id = this.container_id + "_g_" + group.array_index; |
... | ... | @@ -489,6 +522,7 @@ |
489 | 522 | return ""; |
490 | 523 | } |
491 | 524 | }; |
525 | + | |
492 | 526 | Chosen.prototype.result_do_highlight = function(el) { |
493 | 527 | var high_bottom, high_top, maxHeight, visible_bottom, visible_top; |
494 | 528 | if (el.length) { |
... | ... | @@ -507,12 +541,12 @@ |
507 | 541 | } |
508 | 542 | } |
509 | 543 | }; |
544 | + | |
510 | 545 | Chosen.prototype.result_clear_highlight = function() { |
511 | - if (this.result_highlight) { | |
512 | - this.result_highlight.removeClass("highlighted"); | |
513 | - } | |
546 | + if (this.result_highlight) this.result_highlight.removeClass("highlighted"); | |
514 | 547 | return this.result_highlight = null; |
515 | 548 | }; |
549 | + | |
516 | 550 | Chosen.prototype.results_show = function() { |
517 | 551 | var dd_top; |
518 | 552 | if (!this.is_multiple) { |
... | ... | @@ -531,6 +565,7 @@ |
531 | 565 | this.search_field.val(this.search_field.val()); |
532 | 566 | return this.winnow_results(); |
533 | 567 | }; |
568 | + | |
534 | 569 | Chosen.prototype.results_hide = function() { |
535 | 570 | if (!this.is_multiple) { |
536 | 571 | this.selected_item.removeClass("chzn-single-with-drop"); |
... | ... | @@ -541,6 +576,7 @@ |
541 | 576 | }); |
542 | 577 | return this.results_showing = false; |
543 | 578 | }; |
579 | + | |
544 | 580 | Chosen.prototype.set_tab_index = function(el) { |
545 | 581 | var ti; |
546 | 582 | if (this.form_field_jq.attr("tabindex")) { |
... | ... | @@ -554,6 +590,7 @@ |
554 | 590 | } |
555 | 591 | } |
556 | 592 | }; |
593 | + | |
557 | 594 | Chosen.prototype.show_search_field_default = function() { |
558 | 595 | if (this.is_multiple && this.choices < 1 && !this.active_field) { |
559 | 596 | this.search_field.val(this.default_text); |
... | ... | @@ -563,6 +600,7 @@ |
563 | 600 | return this.search_field.removeClass("default"); |
564 | 601 | } |
565 | 602 | }; |
603 | + | |
566 | 604 | Chosen.prototype.search_results_mouseup = function(evt) { |
567 | 605 | var target; |
568 | 606 | target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first(); |
... | ... | @@ -571,34 +609,38 @@ |
571 | 609 | return this.result_select(evt); |
572 | 610 | } |
573 | 611 | }; |
612 | + | |
574 | 613 | Chosen.prototype.search_results_mouseover = function(evt) { |
575 | 614 | var target; |
576 | 615 | target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first(); |
577 | - if (target) { | |
578 | - return this.result_do_highlight(target); | |
579 | - } | |
616 | + if (target) return this.result_do_highlight(target); | |
580 | 617 | }; |
618 | + | |
581 | 619 | Chosen.prototype.search_results_mouseout = function(evt) { |
582 | 620 | if ($(evt.target).hasClass("active-result" || $(evt.target).parents('.active-result').first())) { |
583 | 621 | return this.result_clear_highlight(); |
584 | 622 | } |
585 | 623 | }; |
624 | + | |
586 | 625 | Chosen.prototype.choices_click = function(evt) { |
587 | 626 | evt.preventDefault(); |
588 | 627 | if (this.active_field && !($(evt.target).hasClass("search-choice" || $(evt.target).parents('.search-choice').first)) && !this.results_showing) { |
589 | 628 | return this.results_show(); |
590 | 629 | } |
591 | 630 | }; |
631 | + | |
592 | 632 | Chosen.prototype.choice_build = function(item) { |
593 | - var choice_id, link; | |
633 | + var choice_id, link, | |
634 | + _this = this; | |
594 | 635 | choice_id = this.container_id + "_c_" + item.array_index; |
595 | 636 | this.choices += 1; |
596 | 637 | this.search_container.before('<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>'); |
597 | 638 | link = $('#' + choice_id).find("a").first(); |
598 | - return link.click(__bind(function(evt) { | |
599 | - return this.choice_destroy_link_click(evt); | |
600 | - }, this)); | |
639 | + return link.click(function(evt) { | |
640 | + return _this.choice_destroy_link_click(evt); | |
641 | + }); | |
601 | 642 | }; |
643 | + | |
602 | 644 | Chosen.prototype.choice_destroy_link_click = function(evt) { |
603 | 645 | evt.preventDefault(); |
604 | 646 | if (!this.is_disabled) { |
... | ... | @@ -608,6 +650,7 @@ |
608 | 650 | return evt.stopPropagation; |
609 | 651 | } |
610 | 652 | }; |
653 | + | |
611 | 654 | Chosen.prototype.choice_destroy = function(link) { |
612 | 655 | this.choices -= 1; |
613 | 656 | this.show_search_field_default(); |
... | ... | @@ -617,16 +660,17 @@ |
617 | 660 | this.result_deselect(link.attr("rel")); |
618 | 661 | return link.parents('li').first().remove(); |
619 | 662 | }; |
663 | + | |
620 | 664 | Chosen.prototype.results_reset = function(evt) { |
621 | 665 | this.form_field.options[0].selected = true; |
622 | 666 | this.selected_item.find("span").text(this.default_text); |
667 | + if (!this.is_multiple) this.selected_item.addClass("chzn-default"); | |
623 | 668 | this.show_search_field_default(); |
624 | 669 | $(evt.target).remove(); |
625 | 670 | this.form_field_jq.trigger("change"); |
626 | - if (this.active_field) { | |
627 | - return this.results_hide(); | |
628 | - } | |
671 | + if (this.active_field) return this.results_hide(); | |
629 | 672 | }; |
673 | + | |
630 | 674 | Chosen.prototype.result_select = function(evt) { |
631 | 675 | var high, high_id, item, position; |
632 | 676 | if (this.result_highlight) { |
... | ... | @@ -638,6 +682,7 @@ |
638 | 682 | } else { |
639 | 683 | this.search_results.find(".result-selected").removeClass("result-selected"); |
640 | 684 | this.result_single_selected = high; |
685 | + this.selected_item.removeClass("chzn-default"); | |
641 | 686 | } |
642 | 687 | high.addClass("result-selected"); |
643 | 688 | position = high_id.substr(high_id.lastIndexOf("_") + 1); |
... | ... | @@ -648,24 +693,23 @@ |
648 | 693 | this.choice_build(item); |
649 | 694 | } else { |
650 | 695 | this.selected_item.find("span").first().text(item.text); |
651 | - if (this.allow_single_deselect) { | |
652 | - this.single_deselect_control_build(); | |
653 | - } | |
654 | - } | |
655 | - if (!(evt.metaKey && this.is_multiple)) { | |
656 | - this.results_hide(); | |
696 | + if (this.allow_single_deselect) this.single_deselect_control_build(); | |
657 | 697 | } |
698 | + if (!(evt.metaKey && this.is_multiple)) this.results_hide(); | |
658 | 699 | this.search_field.val(""); |
659 | 700 | this.form_field_jq.trigger("change"); |
660 | 701 | return this.search_field_scale(); |
661 | 702 | } |
662 | 703 | }; |
704 | + | |
663 | 705 | Chosen.prototype.result_activate = function(el) { |
664 | 706 | return el.addClass("active-result"); |
665 | 707 | }; |
708 | + | |
666 | 709 | Chosen.prototype.result_deactivate = function(el) { |
667 | 710 | return el.removeClass("active-result"); |
668 | 711 | }; |
712 | + | |
669 | 713 | Chosen.prototype.result_deselect = function(pos) { |
670 | 714 | var result, result_data; |
671 | 715 | result_data = this.results_data[pos]; |
... | ... | @@ -678,28 +722,31 @@ |
678 | 722 | this.form_field_jq.trigger("change"); |
679 | 723 | return this.search_field_scale(); |
680 | 724 | }; |
725 | + | |
681 | 726 | Chosen.prototype.single_deselect_control_build = function() { |
682 | 727 | if (this.allow_single_deselect && this.selected_item.find("abbr").length < 1) { |
683 | 728 | return this.selected_item.find("span").first().after("<abbr class=\"search-choice-close\"></abbr>"); |
684 | 729 | } |
685 | 730 | }; |
731 | + | |
686 | 732 | Chosen.prototype.winnow_results = function() { |
687 | - var found, option, part, parts, regex, result_id, results, searchText, startTime, startpos, text, zregex, _i, _j, _len, _len2, _ref; | |
688 | - startTime = new Date(); | |
733 | + var found, option, part, parts, regex, regexAnchor, result, result_id, results, searchText, startpos, text, zregex, _i, _j, _len, _len2, _ref; | |
689 | 734 | this.no_results_clear(); |
690 | 735 | results = 0; |
691 | 736 | searchText = this.search_field.val() === this.default_text ? "" : $('<div/>').text($.trim(this.search_field.val())).html(); |
692 | - regex = new RegExp('^' + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i'); | |
737 | + regexAnchor = this.search_contains ? "" : "^"; | |
738 | + regex = new RegExp(regexAnchor + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i'); | |
693 | 739 | zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i'); |
694 | 740 | _ref = this.results_data; |
695 | 741 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
696 | 742 | option = _ref[_i]; |
697 | 743 | if (!option.disabled && !option.empty) { |
698 | 744 | if (option.group) { |
699 | - $('#' + option.dom_id).hide(); | |
745 | + $('#' + option.dom_id).css('display', 'none'); | |
700 | 746 | } else if (!(this.is_multiple && option.selected)) { |
701 | 747 | found = false; |
702 | 748 | result_id = option.dom_id; |
749 | + result = $("#" + result_id); | |
703 | 750 | if (regex.test(option.html)) { |
704 | 751 | found = true; |
705 | 752 | results += 1; |
... | ... | @@ -723,18 +770,16 @@ |
723 | 770 | } else { |
724 | 771 | text = option.html; |
725 | 772 | } |
726 | - if ($("#" + result_id).html !== text) { | |
727 | - $("#" + result_id).html(text); | |
728 | - } | |
729 | - this.result_activate($("#" + result_id)); | |
773 | + result.html(text); | |
774 | + this.result_activate(result); | |
730 | 775 | if (option.group_array_index != null) { |
731 | - $("#" + this.results_data[option.group_array_index].dom_id).show(); | |
776 | + $("#" + this.results_data[option.group_array_index].dom_id).css('display', 'list-item'); | |
732 | 777 | } |
733 | 778 | } else { |
734 | 779 | if (this.result_highlight && result_id === this.result_highlight.attr('id')) { |
735 | 780 | this.result_clear_highlight(); |
736 | 781 | } |
737 | - this.result_deactivate($("#" + result_id)); | |
782 | + this.result_deactivate(result); | |
738 | 783 | } |
739 | 784 | } |
740 | 785 | } |
... | ... | @@ -745,6 +790,7 @@ |
745 | 790 | return this.winnow_results_set_highlight(); |
746 | 791 | } |
747 | 792 | }; |
793 | + | |
748 | 794 | Chosen.prototype.winnow_results_clear = function() { |
749 | 795 | var li, lis, _i, _len, _results; |
750 | 796 | this.search_field.val(""); |
... | ... | @@ -753,46 +799,49 @@ |
753 | 799 | for (_i = 0, _len = lis.length; _i < _len; _i++) { |
754 | 800 | li = lis[_i]; |
755 | 801 | li = $(li); |
756 | - _results.push(li.hasClass("group-result") ? li.show() : !this.is_multiple || !li.hasClass("result-selected") ? this.result_activate(li) : void 0); | |
802 | + if (li.hasClass("group-result")) { | |
803 | + _results.push(li.css('display', 'auto')); | |
804 | + } else if (!this.is_multiple || !li.hasClass("result-selected")) { | |
805 | + _results.push(this.result_activate(li)); | |
806 | + } else { | |
807 | + _results.push(void 0); | |
808 | + } | |
757 | 809 | } |
758 | 810 | return _results; |
759 | 811 | }; |
812 | + | |
760 | 813 | Chosen.prototype.winnow_results_set_highlight = function() { |
761 | 814 | var do_high, selected_results; |
762 | 815 | if (!this.result_highlight) { |
763 | 816 | selected_results = !this.is_multiple ? this.search_results.find(".result-selected.active-result") : []; |
764 | 817 | do_high = selected_results.length ? selected_results.first() : this.search_results.find(".active-result").first(); |
765 | - if (do_high != null) { | |
766 | - return this.result_do_highlight(do_high); | |
767 | - } | |
818 | + if (do_high != null) return this.result_do_highlight(do_high); | |
768 | 819 | } |
769 | 820 | }; |
821 | + | |
770 | 822 | Chosen.prototype.no_results = function(terms) { |
771 | 823 | var no_results_html; |
772 | 824 | no_results_html = $('<li class="no-results">' + this.results_none_found + ' "<span></span>"</li>'); |
773 | 825 | no_results_html.find("span").first().html(terms); |
774 | 826 | return this.search_results.append(no_results_html); |
775 | 827 | }; |
828 | + | |
776 | 829 | Chosen.prototype.no_results_clear = function() { |
777 | 830 | return this.search_results.find(".no-results").remove(); |
778 | 831 | }; |
832 | + | |
779 | 833 | Chosen.prototype.keydown_arrow = function() { |
780 | 834 | var first_active, next_sib; |
781 | 835 | if (!this.result_highlight) { |
782 | 836 | first_active = this.search_results.find("li.active-result").first(); |
783 | - if (first_active) { | |
784 | - this.result_do_highlight($(first_active)); | |
785 | - } | |
837 | + if (first_active) this.result_do_highlight($(first_active)); | |
786 | 838 | } else if (this.results_showing) { |
787 | 839 | next_sib = this.result_highlight.nextAll("li.active-result").first(); |
788 | - if (next_sib) { | |
789 | - this.result_do_highlight(next_sib); | |
790 | - } | |
791 | - } | |
792 | - if (!this.results_showing) { | |
793 | - return this.results_show(); | |
840 | + if (next_sib) this.result_do_highlight(next_sib); | |
794 | 841 | } |
842 | + if (!this.results_showing) return this.results_show(); | |
795 | 843 | }; |
844 | + | |
796 | 845 | Chosen.prototype.keyup_arrow = function() { |
797 | 846 | var prev_sibs; |
798 | 847 | if (!this.results_showing && !this.is_multiple) { |
... | ... | @@ -802,13 +851,12 @@ |
802 | 851 | if (prev_sibs.length) { |
803 | 852 | return this.result_do_highlight(prev_sibs.first()); |
804 | 853 | } else { |
805 | - if (this.choices > 0) { | |
806 | - this.results_hide(); | |
807 | - } | |
854 | + if (this.choices > 0) this.results_hide(); | |
808 | 855 | return this.result_clear_highlight(); |
809 | 856 | } |
810 | 857 | } |
811 | 858 | }; |
859 | + | |
812 | 860 | Chosen.prototype.keydown_backstroke = function() { |
813 | 861 | if (this.pending_backstroke) { |
814 | 862 | this.choice_destroy(this.pending_backstroke.find("a").first()); |
... | ... | @@ -818,27 +866,25 @@ |
818 | 866 | return this.pending_backstroke.addClass("search-choice-focus"); |
819 | 867 | } |
820 | 868 | }; |
869 | + | |
821 | 870 | Chosen.prototype.clear_backstroke = function() { |
822 | 871 | if (this.pending_backstroke) { |
823 | 872 | this.pending_backstroke.removeClass("search-choice-focus"); |
824 | 873 | } |
825 | 874 | return this.pending_backstroke = null; |
826 | 875 | }; |
876 | + | |
827 | 877 | Chosen.prototype.keydown_checker = function(evt) { |
828 | 878 | var stroke, _ref; |
829 | 879 | stroke = (_ref = evt.which) != null ? _ref : evt.keyCode; |
830 | 880 | this.search_field_scale(); |
831 | - if (stroke !== 8 && this.pending_backstroke) { | |
832 | - this.clear_backstroke(); | |
833 | - } | |
881 | + if (stroke !== 8 && this.pending_backstroke) this.clear_backstroke(); | |
834 | 882 | switch (stroke) { |
835 | 883 | case 8: |
836 | 884 | this.backstroke_length = this.search_field.val().length; |
837 | 885 | break; |
838 | 886 | case 9: |
839 | - if (this.results_showing && !this.is_multiple) { | |
840 | - this.result_select(evt); | |
841 | - } | |
887 | + if (this.results_showing && !this.is_multiple) this.result_select(evt); | |
842 | 888 | this.mouse_on_container = false; |
843 | 889 | break; |
844 | 890 | case 13: |
... | ... | @@ -853,6 +899,7 @@ |
853 | 899 | break; |
854 | 900 | } |
855 | 901 | }; |
902 | + | |
856 | 903 | Chosen.prototype.search_field_scale = function() { |
857 | 904 | var dd_top, div, h, style, style_block, styles, w, _i, _len; |
858 | 905 | if (this.is_multiple) { |
... | ... | @@ -871,9 +918,7 @@ |
871 | 918 | $('body').append(div); |
872 | 919 | w = div.width() + 25; |
873 | 920 | div.remove(); |
874 | - if (w > this.f_width - 10) { | |
875 | - w = this.f_width - 10; | |
876 | - } | |
921 | + if (w > this.f_width - 10) w = this.f_width - 10; | |
877 | 922 | this.search_field.css({ |
878 | 923 | 'width': w + 'px' |
879 | 924 | }); |
... | ... | @@ -883,6 +928,7 @@ |
883 | 928 | }); |
884 | 929 | } |
885 | 930 | }; |
931 | + | |
886 | 932 | Chosen.prototype.generate_random_id = function() { |
887 | 933 | var string; |
888 | 934 | string = "sel" + this.generate_random_char() + this.generate_random_char() + this.generate_random_char(); |
... | ... | @@ -891,11 +937,16 @@ |
891 | 937 | } |
892 | 938 | return string; |
893 | 939 | }; |
940 | + | |
894 | 941 | return Chosen; |
895 | - })(); | |
942 | + | |
943 | + })(AbstractChosen); | |
944 | + | |
896 | 945 | get_side_border_padding = function(elmt) { |
897 | 946 | var side_border_padding; |
898 | 947 | return side_border_padding = elmt.outerWidth() - elmt.width(); |
899 | 948 | }; |
949 | + | |
900 | 950 | root.get_side_border_padding = get_side_border_padding; |
951 | + | |
901 | 952 | }).call(this); | ... | ... |