| 1 |
class AbstractChosen |
| 2 |
|
| 3 |
constructor: (@form_field, @options={}) -> |
| 4 |
return unless AbstractChosen.browser_is_supported() |
| 5 |
@is_multiple = @form_field.multiple |
| 6 |
this.set_default_text() |
| 7 |
this.set_default_values() |
| 8 |
|
| 9 |
this.setup() |
| 10 |
|
| 11 |
this.set_up_html() |
| 12 |
this.register_observers() |
| 13 |
# instantiation done, fire ready |
| 14 |
this.on_ready() |
| 15 |
|
| 16 |
set_default_values: -> |
| 17 |
@click_test_action = (evt) => this.test_active_click(evt) |
| 18 |
@activate_action = (evt) => this.activate_field(evt) |
| 19 |
@active_field = false |
| 20 |
@mouse_on_container = false |
| 21 |
@results_showing = false |
| 22 |
@result_highlighted = null |
| 23 |
@is_rtl = @options.rtl || /\bchosen-rtl\b/.test(@form_field.className) |
| 24 |
@allow_single_deselect = if @options.allow_single_deselect? and @form_field.options[0]? and @form_field.options[0].text is "" then @options.allow_single_deselect else false |
| 25 |
@disable_search_threshold = @options.disable_search_threshold || 0 |
| 26 |
@disable_search = @options.disable_search || false |
| 27 |
@enable_split_word_search = if @options.enable_split_word_search? then @options.enable_split_word_search else true |
| 28 |
@group_search = if @options.group_search? then @options.group_search else true |
| 29 |
@search_contains = @options.search_contains || false |
| 30 |
@single_backstroke_delete = if @options.single_backstroke_delete? then @options.single_backstroke_delete else true |
| 31 |
@max_selected_options = @options.max_selected_options || Infinity |
| 32 |
@inherit_select_classes = @options.inherit_select_classes || false |
| 33 |
@display_selected_options = if @options.display_selected_options? then @options.display_selected_options else true |
| 34 |
@display_disabled_options = if @options.display_disabled_options? then @options.display_disabled_options else true |
| 35 |
@include_group_label_in_selected = @options.include_group_label_in_selected || false |
| 36 |
@max_shown_results = @options.max_shown_results || Number.POSITIVE_INFINITY |
| 37 |
@case_sensitive_search = @options.case_sensitive_search || false |
| 38 |
@hide_results_on_select = if @options.hide_results_on_select? then @options.hide_results_on_select else true |
| 39 |
|
| 40 |
set_default_text: -> |
| 41 |
if @form_field.getAttribute("data-placeholder") |
| 42 |
@default_text = @form_field.getAttribute("data-placeholder") |
| 43 |
else if @is_multiple |
| 44 |
@default_text = @options.placeholder_text_multiple || @options.placeholder_text || AbstractChosen.default_multiple_text |
| 45 |
else |
| 46 |
@default_text = @options.placeholder_text_single || @options.placeholder_text || AbstractChosen.default_single_text |
| 47 |
|
| 48 |
@default_text = this.escape_html(@default_text) |
| 49 |
|
| 50 |
@results_none_found = @form_field.getAttribute("data-no_results_text") || @options.no_results_text || AbstractChosen.default_no_result_text |
| 51 |
|
| 52 |
choice_label: (item) -> |
| 53 |
if @include_group_label_in_selected and item.group_label? |
| 54 |
"<b class='group-name'>#{this.escape_html(item.group_label)}</b>#{item.html}" |
| 55 |
else |
| 56 |
item.html |
| 57 |
|
| 58 |
mouse_enter: -> @mouse_on_container = true |
| 59 |
mouse_leave: -> @mouse_on_container = false |
| 60 |
|
| 61 |
input_focus: (evt) -> |
| 62 |
if @is_multiple |
| 63 |
setTimeout (=> this.container_mousedown()), 50 unless @active_field |
| 64 |
else |
| 65 |
@activate_field() unless @active_field |
| 66 |
|
| 67 |
input_blur: (evt) -> |
| 68 |
if not @mouse_on_container |
| 69 |
@active_field = false |
| 70 |
setTimeout (=> this.blur_test()), 100 |
| 71 |
|
| 72 |
label_click_handler: (evt) => |
| 73 |
if @is_multiple |
| 74 |
this.container_mousedown(evt) |
| 75 |
else |
| 76 |
this.activate_field() |
| 77 |
|
| 78 |
results_option_build: (options) -> |
| 79 |
content = '' |
| 80 |
shown_results = 0 |
| 81 |
for data in @results_data |
| 82 |
data_content = '' |
| 83 |
if data.group |
| 84 |
data_content = this.result_add_group data |
| 85 |
else |
| 86 |
data_content = this.result_add_option data |
| 87 |
if data_content != '' |
| 88 |
shown_results++ |
| 89 |
content += data_content |
| 90 |
|
| 91 |
# this select logic pins on an awkward flag |
| 92 |
# we can make it better |
| 93 |
if options?.first |
| 94 |
if data.selected and @is_multiple |
| 95 |
this.choice_build data |
| 96 |
else if data.selected and not @is_multiple |
| 97 |
this.single_set_selected_text(this.choice_label(data)) |
| 98 |
|
| 99 |
if shown_results >= @max_shown_results |
| 100 |
break |
| 101 |
|
| 102 |
content |
| 103 |
|
| 104 |
result_add_option: (option) -> |
| 105 |
return '' unless option.search_match |
| 106 |
return '' unless this.include_option_in_results(option) |
| 107 |
|
| 108 |
classes = [] |
| 109 |
classes.push "active-result" if !option.disabled and !(option.selected and @is_multiple) |
| 110 |
classes.push "disabled-result" if option.disabled and !(option.selected and @is_multiple) |
| 111 |
classes.push "result-selected" if option.selected |
| 112 |
classes.push "group-option" if option.group_array_index? |
| 113 |
classes.push option.classes if option.classes != "" |
| 114 |
|
| 115 |
option_el = document.createElement("li") |
| 116 |
option_el.className = classes.join(" ") |
| 117 |
option_el.style.cssText = option.style if option.style |
| 118 |
option_el.setAttribute("data-option-array-index", option.array_index) |
| 119 |
option_el.innerHTML = option.highlighted_html or option.html |
| 120 |
option_el.title = option.title if option.title |
| 121 |
|
| 122 |
this.outerHTML(option_el) |
| 123 |
|
| 124 |
result_add_group: (group) -> |
| 125 |
return '' unless group.search_match || group.group_match |
| 126 |
return '' unless group.active_options > 0 |
| 127 |
|
| 128 |
classes = [] |
| 129 |
classes.push "group-result" |
| 130 |
classes.push group.classes if group.classes |
| 131 |
|
| 132 |
group_el = document.createElement("li") |
| 133 |
group_el.className = classes.join(" ") |
| 134 |
group_el.innerHTML = group.highlighted_html or this.escape_html(group.label) |
| 135 |
group_el.title = group.title if group.title |
| 136 |
|
| 137 |
this.outerHTML(group_el) |
| 138 |
|
| 139 |
results_update_field: -> |
| 140 |
this.set_default_text() |
| 141 |
this.results_reset_cleanup() if not @is_multiple |
| 142 |
this.result_clear_highlight() |
| 143 |
this.results_build() |
| 144 |
this.winnow_results() if @results_showing |
| 145 |
|
| 146 |
reset_single_select_options: () -> |
| 147 |
for result in @results_data |
| 148 |
result.selected = false if result.selected |
| 149 |
|
| 150 |
results_toggle: -> |
| 151 |
if @results_showing |
| 152 |
this.results_hide() |
| 153 |
else |
| 154 |
this.results_show() |
| 155 |
|
| 156 |
results_search: (evt) -> |
| 157 |
if @results_showing |
| 158 |
this.winnow_results() |
| 159 |
else |
| 160 |
this.results_show() |
| 161 |
|
| 162 |
winnow_results: (options) -> |
| 163 |
this.no_results_clear() |
| 164 |
|
| 165 |
results = 0 |
| 166 |
|
| 167 |
query = this.get_search_text() |
| 168 |
escapedQuery = query.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") |
| 169 |
regex = this.get_search_regex(escapedQuery) |
| 170 |
|
| 171 |
for option in @results_data |
| 172 |
|
| 173 |
option.search_match = false |
| 174 |
results_group = null |
| 175 |
search_match = null |
| 176 |
option.highlighted_html = '' |
| 177 |
|
| 178 |
if this.include_option_in_results(option) |
| 179 |
|
| 180 |
if option.group |
| 181 |
option.group_match = false |
| 182 |
option.active_options = 0 |
| 183 |
|
| 184 |
if option.group_array_index? and @results_data[option.group_array_index] |
| 185 |
results_group = @results_data[option.group_array_index] |
| 186 |
results += 1 if results_group.active_options is 0 and results_group.search_match |
| 187 |
results_group.active_options += 1 |
| 188 |
|
| 189 |
text = if option.group then option.label else option.text |
| 190 |
|
| 191 |
unless option.group and not @group_search |
| 192 |
search_match = this.search_string_match(text, regex) |
| 193 |
option.search_match = search_match? |
| 194 |
|
| 195 |
results += 1 if option.search_match and not option.group |
| 196 |
|
| 197 |
if option.search_match |
| 198 |
if query.length |
| 199 |
startpos = search_match.index |
| 200 |
prefix = text.slice(0, startpos) |
| 201 |
fix = text.slice(startpos, startpos + query.length) |
| 202 |
suffix = text.slice(startpos + query.length) |
| 203 |
option.highlighted_html = "#{this.escape_html(prefix)}<em>#{this.escape_html(fix)}</em>#{this.escape_html(suffix)}" |
| 204 |
|
| 205 |
results_group.group_match = true if results_group? |
| 206 |
|
| 207 |
else if option.group_array_index? and @results_data[option.group_array_index].search_match |
| 208 |
option.search_match = true |
| 209 |
|
| 210 |
this.result_clear_highlight() |
| 211 |
|
| 212 |
if results < 1 and query.length |
| 213 |
this.update_results_content "" |
| 214 |
this.no_results query |
| 215 |
else |
| 216 |
this.update_results_content this.results_option_build() |
| 217 |
this.winnow_results_set_highlight() unless options?.skip_highlight |
| 218 |
|
| 219 |
get_search_regex: (escaped_search_string) -> |
| 220 |
regex_string = if @search_contains then escaped_search_string else "(^|\\s|\\b)#{escaped_search_string}[^\\s]*" |
| 221 |
regex_string = "^#{regex_string}" unless @enable_split_word_search or @search_contains |
| 222 |
regex_flag = if @case_sensitive_search then "" else "i" |
| 223 |
new RegExp(regex_string, regex_flag) |
| 224 |
|
| 225 |
search_string_match: (search_string, regex) -> |
| 226 |
match = regex.exec(search_string) |
| 227 |
match.index += 1 if !@search_contains && match?[1] # make up for lack of lookbehind operator in regex |
| 228 |
match |
| 229 |
|
| 230 |
choices_count: -> |
| 231 |
return @selected_option_count if @selected_option_count? |
| 232 |
|
| 233 |
@selected_option_count = 0 |
| 234 |
for option in @form_field.options |
| 235 |
@selected_option_count += 1 if option.selected |
| 236 |
|
| 237 |
return @selected_option_count |
| 238 |
|
| 239 |
choices_click: (evt) -> |
| 240 |
evt.preventDefault() |
| 241 |
this.activate_field() |
| 242 |
this.results_show() unless @results_showing or @is_disabled |
| 243 |
|
| 244 |
keydown_checker: (evt) -> |
| 245 |
stroke = evt.which ? evt.keyCode |
| 246 |
this.search_field_scale() |
| 247 |
|
| 248 |
this.clear_backstroke() if stroke != 8 and @pending_backstroke |
| 249 |
|
| 250 |
switch stroke |
| 251 |
when 8 # backspace |
| 252 |
@backstroke_length = this.get_search_field_value().length |
| 253 |
break |
| 254 |
when 9 # tab |
| 255 |
this.result_select(evt) if @results_showing and not @is_multiple |
| 256 |
@mouse_on_container = false |
| 257 |
break |
| 258 |
when 13 # enter |
| 259 |
evt.preventDefault() if @results_showing |
| 260 |
break |
| 261 |
when 27 # escape |
| 262 |
evt.preventDefault() if @results_showing |
| 263 |
break |
| 264 |
when 32 # space |
| 265 |
evt.preventDefault() if @disable_search |
| 266 |
break |
| 267 |
when 38 # up arrow |
| 268 |
evt.preventDefault() |
| 269 |
this.keyup_arrow() |
| 270 |
break |
| 271 |
when 40 # down arrow |
| 272 |
evt.preventDefault() |
| 273 |
this.keydown_arrow() |
| 274 |
break |
| 275 |
|
| 276 |
keyup_checker: (evt) -> |
| 277 |
stroke = evt.which ? evt.keyCode |
| 278 |
this.search_field_scale() |
| 279 |
|
| 280 |
switch stroke |
| 281 |
when 8 # backspace |
| 282 |
if @is_multiple and @backstroke_length < 1 and this.choices_count() > 0 |
| 283 |
this.keydown_backstroke() |
| 284 |
else if not @pending_backstroke |
| 285 |
this.result_clear_highlight() |
| 286 |
this.results_search() |
| 287 |
break |
| 288 |
when 13 # enter |
| 289 |
evt.preventDefault() |
| 290 |
this.result_select(evt) if this.results_showing |
| 291 |
break |
| 292 |
when 27 # escape |
| 293 |
this.results_hide() if @results_showing |
| 294 |
break |
| 295 |
when 9, 16, 17, 18, 38, 40, 91 |
| 296 |
# don't do anything on these keys |
| 297 |
else |
| 298 |
this.results_search() |
| 299 |
break |
| 300 |
|
| 301 |
clipboard_event_checker: (evt) -> |
| 302 |
return if @is_disabled |
| 303 |
setTimeout (=> this.results_search()), 50 |
| 304 |
|
| 305 |
container_width: -> |
| 306 |
return if @options.width? then @options.width else "#{@form_field.offsetWidth}px" |
| 307 |
|
| 308 |
include_option_in_results: (option) -> |
| 309 |
return false if @is_multiple and (not @display_selected_options and option.selected) |
| 310 |
return false if not @display_disabled_options and option.disabled |
| 311 |
return false if option.empty |
| 312 |
|
| 313 |
return true |
| 314 |
|
| 315 |
search_results_touchstart: (evt) -> |
| 316 |
@touch_started = true |
| 317 |
this.search_results_mouseover(evt) |
| 318 |
|
| 319 |
search_results_touchmove: (evt) -> |
| 320 |
@touch_started = false |
| 321 |
this.search_results_mouseout(evt) |
| 322 |
|
| 323 |
search_results_touchend: (evt) -> |
| 324 |
this.search_results_mouseup(evt) if @touch_started |
| 325 |
|
| 326 |
outerHTML: (element) -> |
| 327 |
return element.outerHTML if element.outerHTML |
| 328 |
tmp = document.createElement("div") |
| 329 |
tmp.appendChild(element) |
| 330 |
tmp.innerHTML |
| 331 |
|
| 332 |
get_single_html: -> |
| 333 |
""" |
| 334 |
<a class="chosen-single chosen-default"> |
| 335 |
<span>#{@default_text}</span> |
| 336 |
<div><b></b></div> |
| 337 |
</a> |
| 338 |
<div class="chosen-drop"> |
| 339 |
<div class="chosen-search"> |
| 340 |
<input class="chosen-search-input" type="text" autocomplete="off" /> |
| 341 |
</div> |
| 342 |
<ul class="chosen-results"></ul> |
| 343 |
</div> |
| 344 |
""" |
| 345 |
|
| 346 |
get_multi_html: -> |
| 347 |
""" |
| 348 |
<ul class="chosen-choices"> |
| 349 |
<li class="search-field"> |
| 350 |
<input class="chosen-search-input" type="text" autocomplete="off" value="#{@default_text}" /> |
| 351 |
</li> |
| 352 |
</ul> |
| 353 |
<div class="chosen-drop"> |
| 354 |
<ul class="chosen-results"></ul> |
| 355 |
</div> |
| 356 |
""" |
| 357 |
|
| 358 |
get_no_results_html: (terms) -> |
| 359 |
""" |
| 360 |
<li class="no-results"> |
| 361 |
#{@results_none_found} <span>#{this.escape_html(terms)}</span> |
| 362 |
</li> |
| 363 |
""" |
| 364 |
|
| 365 |
# class methods and variables ============================================================ |
| 366 |
|
| 367 |
@browser_is_supported: -> |
| 368 |
if "Microsoft Internet Explorer" is window.navigator.appName |
| 369 |
return document.documentMode >= 8 |
| 370 |
if /iP(od|hone)/i.test(window.navigator.userAgent) or |
| 371 |
/IEMobile/i.test(window.navigator.userAgent) or |
| 372 |
/Windows Phone/i.test(window.navigator.userAgent) or |
| 373 |
/BlackBerry/i.test(window.navigator.userAgent) or |
| 374 |
/BB10/i.test(window.navigator.userAgent) or |
| 375 |
/Android.*Mobile/i.test(window.navigator.userAgent) |
| 376 |
return false |
| 377 |
return true |
| 378 |
|
| 379 |
@default_multiple_text: "Select Some Options" |
| 380 |
@default_single_text: "Select an Option" |
| 381 |
@default_no_result_text: "No results match" |
| 382 |
|