Skip to content

Commit

Permalink
update to upstream commit dc024d784a
Browse files Browse the repository at this point in the history
  • Loading branch information
tsechingho committed Jul 4, 2012
1 parent 1802760 commit 365d6b1
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 72 deletions.
103 changes: 59 additions & 44 deletions vendor/assets/javascripts/chosen.jquery.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@ $.fn.extend({
chosen: (options) ->
# Do no harm and return as soon as possible for unsupported browsers, namely IE6 and IE7
return this if $.browser.msie and ($.browser.version is "6.0" or $.browser.version is "7.0")
$(this).each((input_field) ->
new Chosen(this, options) unless ($ this).hasClass "chzn-done"
this.each((input_field) ->
$this = $ this
$this.data('chosen', new Chosen(this, options)) unless $this.hasClass "chzn-done"
)
})

class Chosen extends AbstractChosen

setup: ->
@form_field_jq = $ @form_field
@current_value = @form_field_jq.val()
@is_rtl = @form_field_jq.hasClass "chzn-rtl"

finish_setup: ->
@form_field_jq.addClass "chzn-done"

set_up_html: ->
@container_id = if @form_field.id.length then @form_field.id.replace(/(:|\.)/g, '_') else this.generate_field_id()
@container_id = if @form_field.id.length then @form_field.id.replace(/[^\w]/g, '_') else this.generate_field_id()
@container_id += "_chzn"

@f_width = @form_field_jq.outerWidth()

@default_text = if @form_field_jq.data 'placeholder' then @form_field_jq.data 'placeholder' else @default_text_default


container_div = ($ "<div />", {
id: @container_id
class: "chzn-container#{ if @is_rtl then ' chzn-rtl' else '' }"
style: 'width: ' + (@f_width) + 'px;' #use parens around @f_width so coffeescript doesn't think + ' px' is a function parameter
})

if @is_multiple
container_div.html '<ul class="chzn-choices"><li class="search-field"><input type="text" value="' + @default_text + '" class="default" autocomplete="off" style="width:25px;" /></li></ul><div class="chzn-drop" style="left:-9000px;"><ul class="chzn-results"></ul></div>'
else
Expand All @@ -46,18 +46,18 @@ class Chosen extends AbstractChosen
@container = ($ '#' + @container_id)
@container.addClass( "chzn-container-" + (if @is_multiple then "multi" else "single") )
@dropdown = @container.find('div.chzn-drop').first()

dd_top = @container.height()
dd_width = (@f_width - get_side_border_padding(@dropdown))

@dropdown.css({"width": dd_width + "px", "top": dd_top + "px"})

@search_field = @container.find('input').first()
@search_results = @container.find('ul.chzn-results').first()
this.search_field_scale()

@search_no_results = @container.find('li.no-results').first()

if @is_multiple
@search_choices = @container.find('ul.chzn-choices').first()
@search_container = @container.find('li.search-field').first()
Expand All @@ -66,7 +66,7 @@ class Chosen extends AbstractChosen
@selected_item = @container.find('.chzn-single').first()
sf_width = dd_width - get_side_border_padding(@search_container) - get_side_border_padding(@search_field)
@search_field.css( {"width" : sf_width + "px"} )

this.results_build()
this.set_tab_index()
@form_field_jq.trigger("liszt:ready", {chosen: this})
Expand All @@ -76,7 +76,7 @@ class Chosen extends AbstractChosen
@container.mouseup (evt) => this.container_mouseup(evt)
@container.mouseenter (evt) => this.mouse_enter(evt)
@container.mouseleave (evt) => this.mouse_leave(evt)

@search_results.mouseup (evt) => this.search_results_mouseup(evt)
@search_results.mouseover (evt) => this.search_results_mouseover(evt)
@search_results.mouseout (evt) => this.search_results_mouseout(evt)
Expand Down Expand Up @@ -124,18 +124,18 @@ class Chosen extends AbstractChosen
@pending_destroy_click = false

container_mouseup: (evt) ->
this.results_reset(evt) if evt.target.nodeName is "ABBR"
this.results_reset(evt) if evt.target.nodeName is "ABBR" and not @is_disabled

blur_test: (evt) ->
this.close_field() if not @active_field and @container.hasClass "chzn-container-active"

close_field: ->
$(document).unbind "click", @click_test_action

if not @is_multiple
@selected_item.attr "tabindex", @search_field.attr("tabindex")
@search_field.attr "tabindex", -1

@active_field = false
this.results_hide()

Expand Down Expand Up @@ -163,7 +163,7 @@ class Chosen extends AbstractChosen
@active_field = true
else
this.close_field()

results_build: ->
@parsing = true
@results_data = root.SelectParser.select_to_array @form_field
Expand All @@ -172,7 +172,7 @@ class Chosen extends AbstractChosen
@search_choices.find("li.search-choice").remove()
@choices = 0
else if not @is_multiple
@selected_item.find("span").text @default_text
@selected_item.addClass("chzn-default").find("span").text(@default_text)
if @form_field.options.length <= @disable_search_threshold
@container.addClass "chzn-container-single-nosearch"
else
Expand All @@ -193,7 +193,7 @@ class Chosen extends AbstractChosen
this.search_field_disabled()
this.show_search_field_default()
this.search_field_scale()

@search_results.html content
@parsing = false

Expand All @@ -204,7 +204,7 @@ class Chosen extends AbstractChosen
'<li id="' + group.dom_id + '" class="group-result">' + $("<div />").text(group.label).html() + '</li>'
else
""

result_do_highlight: (el) ->
if el.length
this.result_clear_highlight()
Expand All @@ -215,15 +215,15 @@ class Chosen extends AbstractChosen
maxHeight = parseInt @search_results.css("maxHeight"), 10
visible_top = @search_results.scrollTop()
visible_bottom = maxHeight + visible_top

high_top = @result_highlight.position().top + @search_results.scrollTop()
high_bottom = high_top + @result_highlight.outerHeight()

if high_bottom >= visible_bottom
@search_results.scrollTop if (high_bottom - maxHeight) > 0 then (high_bottom - maxHeight) else 0
else if high_top < visible_top
@search_results.scrollTop high_top

result_clear_highlight: ->
@result_highlight.removeClass "highlighted" if @result_highlight
@result_highlight = null
Expand All @@ -233,8 +233,12 @@ class Chosen extends AbstractChosen
@selected_item.addClass "chzn-single-with-drop"
if @result_single_selected
this.result_do_highlight( @result_single_selected )
else if @max_selected_options <= @choices
@form_field_jq.trigger("liszt:maxselected", {chosen: this})
return false

dd_top = if @is_multiple then @container.height() else (@container.height() - 1)
@form_field_jq.trigger("liszt:showing_dropdown", {chosen: this})
@dropdown.css {"top": dd_top + "px", "left":0}
@results_showing = true

Expand All @@ -246,6 +250,7 @@ class Chosen extends AbstractChosen
results_hide: ->
@selected_item.removeClass "chzn-single-with-drop" unless @is_multiple
this.result_clear_highlight()
@form_field_jq.trigger("liszt:hiding_dropdown", {chosen: this})
@dropdown.css {"left":"-9000px"}
@results_showing = false

Expand Down Expand Up @@ -289,6 +294,9 @@ class Chosen extends AbstractChosen
this.results_show()

choice_build: (item) ->
if @is_multiple and @max_selected_options <= @choices
@form_field_jq.trigger("liszt:maxselected", {chosen: this})
return false # fire event
choice_id = @container_id + "_c_" + item.array_index
@choices += 1
@search_container.before '<li class="search-choice" id="' + choice_id + '"><span>' + item.html + '</span><a href="javascript:void(0)" class="search-choice-close" rel="' + item.array_index + '"></a></li>'
Expand All @@ -312,20 +320,23 @@ class Chosen extends AbstractChosen
this.result_deselect (link.attr "rel")
link.parents('li').first().remove()

results_reset: (evt) ->
results_reset: ->
@form_field.options[0].selected = true
@selected_item.find("span").text @default_text
@selected_item.addClass("chzn-default") if not @is_multiple
this.show_search_field_default()
$(evt.target).remove();
this.results_reset_cleanup()
@form_field_jq.trigger "change"
this.results_hide() if @active_field

results_reset_cleanup: ->
@selected_item.find("abbr").remove()

result_select: (evt) ->
if @result_highlight
high = @result_highlight
high_id = high.attr "id"

this.result_clear_highlight()

if @is_multiple
Expand All @@ -334,9 +345,9 @@ class Chosen extends AbstractChosen
@search_results.find(".result-selected").removeClass "result-selected"
@result_single_selected = high
@selected_item.removeClass("chzn-default")

high.addClass "result-selected"

position = high_id.substr(high_id.lastIndexOf("_") + 1 )
item = @results_data[position]
item.selected = true
Expand All @@ -348,12 +359,13 @@ class Chosen extends AbstractChosen
else
@selected_item.find("span").first().text item.text
this.single_deselect_control_build() if @allow_single_deselect

this.results_hide() unless evt.metaKey and @is_multiple

@search_field.val ""

@form_field_jq.trigger "change"
@form_field_jq.trigger "change", {'selected': @form_field.options[item.options_index].value} if @is_multiple || @form_field_jq.val() != @current_value
@current_value = @form_field_jq.val()
this.search_field_scale()

result_activate: (el) ->
Expand All @@ -373,15 +385,15 @@ class Chosen extends AbstractChosen
this.result_clear_highlight()
this.winnow_results()

@form_field_jq.trigger "change"
@form_field_jq.trigger "change", {deselected: @form_field.options[result_data.options_index].value}
this.search_field_scale()

single_deselect_control_build: ->
@selected_item.find("span").first().after "<abbr class=\"search-choice-close\"></abbr>" if @allow_single_deselect and @selected_item.find("abbr").length < 1

winnow_results: ->
this.no_results_clear()

results = 0

searchText = if @search_field.val() is @default_text then "" else $('<div/>').text($.trim(@search_field.val())).html()
Expand All @@ -397,7 +409,7 @@ class Chosen extends AbstractChosen
found = false
result_id = option.dom_id
result = $("#" + result_id)

if regex.test option.html
found = true
results += 1
Expand All @@ -417,7 +429,7 @@ class Chosen extends AbstractChosen
text = text.substr(0, startpos) + '<em>' + text.substr(startpos)
else
text = option.html

result.html(text)
this.result_activate result

Expand Down Expand Up @@ -449,13 +461,13 @@ class Chosen extends AbstractChosen
do_high = if selected_results.length then selected_results.first() else @search_results.find(".active-result").first()

this.result_do_highlight do_high if do_high?

no_results: (terms) ->
no_results_html = $('<li class="no-results">' + @results_none_found + ' "<span></span>"</li>')
no_results_html.find("span").first().html(terms)

@search_results.append no_results_html

no_results_clear: ->
@search_results.find(".no-results").remove()

Expand All @@ -473,7 +485,7 @@ class Chosen extends AbstractChosen
this.results_show()
else if @result_highlight
prev_sibs = @result_highlight.prevAll("li.active-result")

if prev_sibs.length
this.result_do_highlight prev_sibs.first()
else
Expand All @@ -486,7 +498,10 @@ class Chosen extends AbstractChosen
this.clear_backstroke()
else
@pending_backstroke = @search_container.siblings("li.search-choice").last()
@pending_backstroke.addClass "search-choice-focus"
if @single_backstroke_delete
@keydown_backstroke()
else
@pending_backstroke.addClass "search-choice-focus"

clear_backstroke: ->
@pending_backstroke.removeClass "search-choice-focus" if @pending_backstroke
Expand All @@ -495,9 +510,9 @@ class Chosen extends AbstractChosen
keydown_checker: (evt) ->
stroke = evt.which ? evt.keyCode
this.search_field_scale()

this.clear_backstroke() if stroke != 8 and this.pending_backstroke

switch stroke
when 8
@backstroke_length = this.search_field.val().length
Expand All @@ -516,18 +531,18 @@ class Chosen extends AbstractChosen
when 40
this.keydown_arrow()
break

search_field_scale: ->
if @is_multiple
h = 0
w = 0

style_block = "position:absolute; left: -1000px; top: -1000px; display:none;"
styles = ['font-size','font-style', 'font-weight', 'font-family','line-height', 'text-transform', 'letter-spacing']

for style in styles
style_block += style + ":" + @search_field.css(style) + ";"

div = $('<div />', { 'style' : style_block })
div.text @search_field.val()
$('body').append div
Expand All @@ -542,13 +557,13 @@ class Chosen extends AbstractChosen

dd_top = @container.height()
@dropdown.css({"top": dd_top + "px"})

generate_random_id: ->
string = "sel" + this.generate_random_char() + this.generate_random_char() + this.generate_random_char()
while $("#" + string).length > 0
string += this.generate_random_char()
string

get_side_border_padding = (elmt) ->
side_border_padding = elmt.outerWidth() - elmt.width()

Expand Down
Loading

0 comments on commit 365d6b1

Please sign in to comment.