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