Skip to content

Commit

Permalink
rake update-chosen
Browse files Browse the repository at this point in the history
  • Loading branch information
tsechingho committed Jun 4, 2013
1 parent 779495e commit 26e12ed
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 55 deletions.
2 changes: 1 addition & 1 deletion lib/chosen-rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Chosen
module Rails
VERSION = '0.9.14'
CHOSEN_VERSION = '0.9.14'
CHOSEN_VERSION = '0.9.15'
end
end
42 changes: 24 additions & 18 deletions vendor/assets/javascripts/chosen.jquery.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,12 @@ class Chosen extends AbstractChosen

results_build: ->
@parsing = true
@selected_option_count = null

@results_data = root.SelectParser.select_to_array @form_field

if @is_multiple and @choices > 0
if @is_multiple and this.choices_count() > 0
@search_choices.find("li.search-choice").remove()
@choices = 0
else if not @is_multiple
@selected_item.addClass("chzn-default").find("span").text(@default_text)
if @disable_search or @form_field.options.length <= @disable_search_threshold
Expand Down Expand Up @@ -229,7 +230,7 @@ class Chosen extends AbstractChosen
results_show: ->
if @result_single_selected?
this.result_do_highlight @result_single_selected
else if @is_multiple and @max_selected_options <= @choices
else if @is_multiple and @max_selected_options <= this.choices_count()
@form_field_jq.trigger("liszt:maxselected", {chosen: this})
return false

Expand Down Expand Up @@ -261,13 +262,13 @@ class Chosen extends AbstractChosen
set_label_behavior: ->
@form_field_label = @form_field_jq.parents("label") # first check for a parent label
if not @form_field_label.length and @form_field.id.length
@form_field_label = $("label[for=#{@form_field.id}]") #next check for a for=#{id}
@form_field_label = $("label[for='#{@form_field.id}']") #next check for a for=#{id}

if @form_field_label.length > 0
@form_field_label.click (evt) => if @is_multiple then this.container_mousedown(evt) else this.activate_field()

show_search_field_default: ->
if @is_multiple and @choices < 1 and not @active_field
if @is_multiple and this.choices_count() < 1 and not @active_field
@search_field.val(@default_text)
@search_field.addClass "default"
else
Expand All @@ -289,18 +290,16 @@ class Chosen extends AbstractChosen
this.result_clear_highlight() if $(evt.target).hasClass "active-result" or $(evt.target).parents('.active-result').first()

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
choice = $('<li />', { class: "search-choice" }).html("<span>#{item.html}</span>")

if item.disabled
html = '<li class="search-choice search-choice-disabled" id="' + choice_id + '"><span>' + item.html + '</span></li>'
choice.addClass 'search-choice-disabled'
else
html = '<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>'
@search_container.before html
link = $('#' + choice_id).find("a").first()
link.click (evt) => this.choice_destroy_link_click(evt)
close_link = $('<a />', { href: '#', class: 'search-choice-close', rel: item.array_index })
close_link.click (evt) => this.choice_destroy_link_click(evt)
choice.append close_link

@search_container.before choice

choice_destroy_link_click: (evt) ->
evt.preventDefault()
Expand All @@ -309,17 +308,17 @@ class Chosen extends AbstractChosen

choice_destroy: (link) ->
if this.result_deselect (link.attr "rel")
@choices -= 1
this.show_search_field_default()

this.results_hide() if @is_multiple and @choices > 0 and @search_field.val().length < 1
this.results_hide() if @is_multiple and this.choices_count() > 0 and @search_field.val().length < 1

link.parents('li').first().remove()

this.search_field_scale()

results_reset: ->
@form_field.options[0].selected = true
@selected_option_count = null
@selected_item.find("span").text @default_text
@selected_item.addClass("chzn-default") if not @is_multiple
this.show_search_field_default()
Expand All @@ -338,6 +337,10 @@ class Chosen extends AbstractChosen

this.result_clear_highlight()

if @is_multiple and @max_selected_options <= this.choices_count()
@form_field_jq.trigger("liszt:maxselected", {chosen: this})
return false

if @is_multiple
this.result_deactivate high
else
Expand All @@ -352,6 +355,7 @@ class Chosen extends AbstractChosen
item.selected = true

@form_field.options[item.options_index].selected = true
@selected_option_count = null

if @is_multiple
this.choice_build item
Expand Down Expand Up @@ -380,6 +384,8 @@ class Chosen extends AbstractChosen
result_data.selected = false

@form_field.options[result_data.options_index].selected = false
@selected_option_count = null

result = $("#" + @container_id + "_o_" + pos)
result.removeClass("result-selected").addClass("active-result").show()

Expand Down Expand Up @@ -494,7 +500,7 @@ class Chosen extends AbstractChosen
if prev_sibs.length
this.result_do_highlight prev_sibs.first()
else
this.results_hide() if @choices > 0
this.results_hide() if this.choices_count() > 0
this.result_clear_highlight()

keydown_backstroke: ->
Expand Down
51 changes: 27 additions & 24 deletions vendor/assets/javascripts/chosen.proto.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class Chosen extends AbstractChosen
# HTML Templates
@single_temp = new Template('<a href="javascript:void(0)" class="chzn-single chzn-default" tabindex="-1"><span>#{default}</span><div><b></b></div></a><div class="chzn-drop"><div class="chzn-search"><input type="text" autocomplete="off" /></div><ul class="chzn-results"></ul></div>')
@multi_temp = new Template('<ul class="chzn-choices"><li class="search-field"><input type="text" value="#{default}" class="default" autocomplete="off" style="width:25px;" /></li></ul><div class="chzn-drop"><ul class="chzn-results"></ul></div>')
@choice_temp = new Template('<li class="search-choice" id="#{id}"><span>#{choice}</span><a href="javascript:void(0)" class="search-choice-close" rel="#{position}"></a></li>')
@choice_noclose_temp = new Template('<li class="search-choice search-choice-disabled" id="#{id}"><span>#{choice}</span></li>')
@no_results_temp = new Template('<li class="no-results">' + @results_none_found + ' "<span>#{terms}</span>"</li>')

set_up_html: ->
Expand Down Expand Up @@ -154,11 +152,12 @@ class Chosen extends AbstractChosen

results_build: ->
@parsing = true
@selected_option_count = null

@results_data = root.SelectParser.select_to_array @form_field

if @is_multiple and @choices > 0
if @is_multiple and this.choices_count() > 0
@search_choices.select("li.search-choice").invoke("remove")
@choices = 0
else if not @is_multiple
@selected_item.addClassName("chzn-default").down("span").update(@default_text)
if @disable_search or @form_field.options.length <= @disable_search_threshold
Expand Down Expand Up @@ -203,7 +202,7 @@ class Chosen extends AbstractChosen
visible_top = @search_results.scrollTop
visible_bottom = maxHeight + visible_top

high_top = @result_highlight.positionedOffset().top
high_top = @result_highlight.positionedOffset().top + @search_results.scrollTop
high_bottom = high_top + @result_highlight.getHeight()

if high_bottom >= visible_bottom
Expand All @@ -218,7 +217,7 @@ class Chosen extends AbstractChosen
results_show: ->
if @result_single_selected?
this.result_do_highlight @result_single_selected
else if @is_multiple and @max_selected_options <= @choices
else if @is_multiple and @max_selected_options <= this.choices_count()
@form_field.fire("liszt:maxselected", {chosen: this})
return false

Expand Down Expand Up @@ -250,13 +249,13 @@ class Chosen extends AbstractChosen
set_label_behavior: ->
@form_field_label = @form_field.up("label") # first check for a parent label
if not @form_field_label?
@form_field_label = $$("label[for=#{@form_field.id}]").first() #next check for a for=#{id}
@form_field_label = $$("label[for='#{@form_field.id}']").first() #next check for a for=#{id}

if @form_field_label?
@form_field_label.observe "click", (evt) => if @is_multiple then this.container_mousedown(evt) else this.activate_field()

show_search_field_default: ->
if @is_multiple and @choices < 1 and not @active_field
if @is_multiple and this.choices_count() < 1 and not @active_field
@search_field.value = @default_text
@search_field.addClassName "default"
else
Expand All @@ -278,19 +277,16 @@ class Chosen extends AbstractChosen
this.result_clear_highlight() if evt.target.hasClassName('active-result') or evt.target.up('.active-result')

choice_build: (item) ->
if @is_multiple and @max_selected_options <= @choices
@form_field.fire("liszt:maxselected", {chosen: this})
return false
choice_id = @container_id + "_c_" + item.array_index
@choices += 1
@search_container.insert
before: (if item.disabled then @choice_noclose_temp else @choice_temp).evaluate
id: choice_id
choice: item.html
position: item.array_index
if not item.disabled
link = $(choice_id).down('a')
link.observe "click", (evt) => this.choice_destroy_link_click(evt)
choice = new Element('li', { class: "search-choice" }).update("<span>#{item.html}</span>")

if item.disabled
choice.addClassName 'search-choice-disabled'
else
close_link = new Element('a', { href: '#', class: 'search-choice-close', rel: item.array_index })
close_link.observe "click", (evt) => this.choice_destroy_link_click(evt)
choice.insert close_link

@search_container.insert { before: choice }

choice_destroy_link_click: (evt) ->
evt.preventDefault()
Expand All @@ -299,17 +295,17 @@ class Chosen extends AbstractChosen

choice_destroy: (link) ->
if this.result_deselect link.readAttribute("rel")
@choices -= 1
this.show_search_field_default()

this.results_hide() if @is_multiple and @choices > 0 and @search_field.value.length < 1
this.results_hide() if @is_multiple and this.choices_count() > 0 and @search_field.value.length < 1

link.up('li').remove()

this.search_field_scale()

results_reset: ->
@form_field.options[0].selected = true
@selected_option_count = null
@selected_item.down("span").update(@default_text)
@selected_item.addClassName("chzn-default") if not @is_multiple
this.show_search_field_default()
Expand All @@ -327,6 +323,10 @@ class Chosen extends AbstractChosen
high = @result_highlight
this.result_clear_highlight()

if @is_multiple and @max_selected_options <= this.choices_count()
@form_field.fire("liszt:maxselected", {chosen: this})
return false

if @is_multiple
this.result_deactivate high
else
Expand All @@ -341,6 +341,7 @@ class Chosen extends AbstractChosen
item.selected = true

@form_field.options[item.options_index].selected = true
@selected_option_count = null

if @is_multiple
this.choice_build item
Expand Down Expand Up @@ -370,6 +371,8 @@ class Chosen extends AbstractChosen
result_data.selected = false

@form_field.options[result_data.options_index].selected = false
@selected_option_count = null

result = $(@container_id + "_o_" + pos)
result.removeClassName("result-selected").addClassName("active-result").show()

Expand Down Expand Up @@ -488,7 +491,7 @@ class Chosen extends AbstractChosen
if prevs.length
this.result_do_highlight prevs.first()
else
this.results_hide() if @choices > 0
this.results_hide() if this.choices_count() > 0
this.result_clear_highlight()

keydown_backstroke: ->
Expand Down
23 changes: 11 additions & 12 deletions vendor/assets/javascripts/lib/abstract-chosen.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class AbstractChosen
@disable_search = @options.disable_search || false
@enable_split_word_search = if @options.enable_split_word_search? then @options.enable_split_word_search else true
@search_contains = @options.search_contains || false
@choices = 0
@single_backstroke_delete = @options.single_backstroke_delete || false
@max_selected_options = @options.max_selected_options || Infinity
@inherit_select_classes = @options.inherit_select_classes || false
Expand Down Expand Up @@ -95,6 +94,15 @@ class AbstractChosen
else
this.results_show()

choices_count: ->
return @selected_option_count if @selected_option_count?

@selected_option_count = 0
for option in @form_field.options
@selected_option_count += 1 if option.selected

return @selected_option_count

choices_click: (evt) ->
evt.preventDefault()
this.results_show() unless @results_showing
Expand All @@ -105,7 +113,7 @@ class AbstractChosen

switch stroke
when 8
if @is_multiple and @backstroke_length < 1 and @choices > 0
if @is_multiple and @backstroke_length < 1 and this.choices_count() > 0
this.keydown_backstroke()
else if not @pending_backstroke
this.result_clear_highlight()
Expand All @@ -131,16 +139,7 @@ class AbstractChosen
newchar = chars.substring rand, rand+1

container_width: ->
return @options.width if @options.width?

width = if window.getComputedStyle?
parseFloat window.getComputedStyle(@form_field).getPropertyValue('width')
else if jQuery? and @form_field_jq?
@form_field_jq.outerWidth()
else
@form_field.getWidth()

width + "px"
return if @options.width? then @options.width else "#{@form_field.offsetWidth}px"

# class methods and variables ============================================================

Expand Down

0 comments on commit 26e12ed

Please sign in to comment.