vendor/assets/javascripts/chosen.proto.coffee in chosen-rails-0.9.8 vs vendor/assets/javascripts/chosen.proto.coffee in chosen-rails-0.9.8.1
- old
+ new
@@ -5,10 +5,11 @@
root = this
class Chosen extends AbstractChosen
setup: ->
+ @current_value = @form_field.value
@is_rtl = @form_field.hasClassName "chzn-rtl"
finish_setup: ->
@form_field.addClassName "chzn-done"
@@ -20,21 +21,19 @@
@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" style="left:-9000px;"><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>')
@no_results_temp = new Template('<li class="no-results">' + @results_none_found + ' "<span>#{terms}</span>"</li>')
set_up_html: ->
- @container_id = @form_field.identify().replace(/(:|\.)/g, '_') + "_chzn"
+ @container_id = @form_field.identify().replace(/[^\w]/g, '_') + "_chzn"
@f_width = if @form_field.getStyle("width") then parseInt @form_field.getStyle("width"), 10 else @form_field.getWidth()
container_props =
'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
- @default_text = if @form_field.readAttribute 'data-placeholder' then @form_field.readAttribute 'data-placeholder' else @default_text_default
-
base_template = if @is_multiple then new Element('div', container_props).update( @multi_temp.evaluate({ "default": @default_text}) ) else new Element('div', container_props).update( @single_temp.evaluate({ "default":@default_text }) )
@form_field.hide().insert({ after: base_template })
@container = $(@container_id)
@container.addClassName( "chzn-container-" + (if @is_multiple then "multi" else "single") )
@@ -114,11 +113,11 @@
this.activate_field()
else
@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.hasClassName("chzn-container-active")
close_field: ->
@@ -162,11 +161,11 @@
if @is_multiple and @choices > 0
@search_choices.select("li.search-choice").invoke("remove")
@choices = 0
else if not @is_multiple
- @selected_item.down("span").update(@default_text)
+ @selected_item.addClassName("chzn-default").down("span").update(@default_text)
if @form_field.options.length <= @disable_search_threshold
@container.addClassName "chzn-container-single-nosearch"
else
@container.removeClassName "chzn-container-single-nosearch"
@@ -222,12 +221,16 @@
results_show: ->
if not @is_multiple
@selected_item.addClassName('chzn-single-with-drop')
if @result_single_selected
this.result_do_highlight( @result_single_selected )
+ else if @max_selected_options <= @choices
+ @form_field.fire("liszt:maxselected", {chosen: this})
+ return false
dd_top = if @is_multiple then @container.getHeight() else (@container.getHeight() - 1)
+ @form_field.fire("liszt:showing_dropdown", {chosen: this})
@dropdown.setStyle {"top": dd_top + "px", "left":0}
@results_showing = true
@search_field.focus()
@search_field.value = @search_field.value
@@ -235,10 +238,11 @@
this.winnow_results()
results_hide: ->
@selected_item.removeClassName('chzn-single-with-drop') unless @is_multiple
this.result_clear_highlight()
+ @form_field.fire("liszt:hiding_dropdown", {chosen: this})
@dropdown.setStyle({"left":"-9000px"})
@results_showing = false
set_tab_index: (el) ->
@@ -278,10 +282,13 @@
evt.preventDefault()
if( @active_field and not(evt.target.hasClassName('search-choice') or evt.target.up('.search-choice')) and not @results_showing )
this.results_show()
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: @choice_temp.evaluate
id: choice_id
@@ -303,18 +310,22 @@
this.results_hide() if @is_multiple and @choices > 0 and @search_field.value.length < 1
this.result_deselect link.readAttribute("rel")
link.up('li').remove()
- results_reset: (evt) ->
+ results_reset: ->
@form_field.options[0].selected = true
@selected_item.down("span").update(@default_text)
@selected_item.addClassName("chzn-default") if not @is_multiple
this.show_search_field_default()
- evt.target.remove()
+ this.results_reset_cleanup()
@form_field.simulate("change") if typeof Event.simulate is 'function'
this.results_hide() if @active_field
+
+ results_reset_cleanup: ->
+ deselect_trigger = @selected_item.down("abbr")
+ deselect_trigger.remove() if(deselect_trigger)
result_select: (evt) ->
if @result_highlight
high = @result_highlight
this.result_clear_highlight()
@@ -341,12 +352,14 @@
this.single_deselect_control_build() if @allow_single_deselect
this.results_hide() unless evt.metaKey and @is_multiple
@search_field.value = ""
-
- @form_field.simulate("change") if typeof Event.simulate is 'function'
+
+ @form_field.simulate("change") if typeof Event.simulate is 'function' && (@is_multiple || @form_field.value != @current_value)
+ @current_value = @form_field.value
+
this.search_field_scale()
result_activate: (el) ->
el.addClassName("active-result")
@@ -480,10 +493,13 @@
if @pending_backstroke
this.choice_destroy @pending_backstroke.down("a")
this.clear_backstroke()
else
@pending_backstroke = @search_container.siblings("li.search-choice").last()
- @pending_backstroke.addClassName("search-choice-focus")
+ if @single_backstroke_delete
+ @keydown_backstroke()
+ else
+ @pending_backstroke.addClassName("search-choice-focus")
clear_backstroke: ->
@pending_backstroke.removeClassName("search-choice-focus") if @pending_backstroke
@pending_backstroke = null