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