vendor/assets/javascripts/chosen.proto.coffee in chosen-rails-0.13.2 vs vendor/assets/javascripts/chosen.proto.coffee in chosen-rails-1.0.0
- old
+ new
@@ -1,59 +1,54 @@
-root = this
+class @Chosen extends AbstractChosen
-class Chosen extends AbstractChosen
-
setup: ->
@current_selectedIndex = @form_field.selectedIndex
- @is_rtl = @form_field.hasClassName "chzn-rtl"
+ @is_rtl = @form_field.hasClassName "chosen-rtl"
- finish_setup: ->
- @form_field.addClassName "chzn-done"
-
set_default_values: ->
super()
# 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>')
+ @single_temp = new Template('<a class="chosen-single chosen-default" tabindex="-1"><span>#{default}</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>')
+ @multi_temp = new Template('<ul class="chosen-choices"><li class="search-field"><input type="text" value="#{default}" class="default" autocomplete="off" style="width:25px;" /></li></ul><div class="chosen-drop"><ul class="chosen-results"></ul></div>')
@no_results_temp = new Template('<li class="no-results">' + @results_none_found + ' "<span>#{terms}</span>"</li>')
set_up_html: ->
- container_classes = ["chzn-container"]
- container_classes.push "chzn-container-" + (if @is_multiple then "multi" else "single")
+ container_classes = ["chosen-container"]
+ container_classes.push "chosen-container-" + (if @is_multiple then "multi" else "single")
container_classes.push @form_field.className if @inherit_select_classes && @form_field.className
- container_classes.push "chzn-rtl" if @is_rtl
+ container_classes.push "chosen-rtl" if @is_rtl
container_props =
'class': container_classes.join ' '
'style': "width: #{this.container_width()};"
'title': @form_field.title
- container_props.id = @form_field.id.replace(/[^\w]/g, '_') + "_chzn" if @form_field.id.length
+ container_props.id = @form_field.id.replace(/[^\w]/g, '_') + "_chosen" if @form_field.id.length
@container = 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: @container })
- @dropdown = @container.down('div.chzn-drop')
+ @dropdown = @container.down('div.chosen-drop')
@search_field = @container.down('input')
- @search_results = @container.down('ul.chzn-results')
+ @search_results = @container.down('ul.chosen-results')
this.search_field_scale()
@search_no_results = @container.down('li.no-results')
if @is_multiple
- @search_choices = @container.down('ul.chzn-choices')
+ @search_choices = @container.down('ul.chosen-choices')
@search_container = @container.down('li.search-field')
else
- @search_container = @container.down('div.chzn-search')
- @selected_item = @container.down('.chzn-single')
+ @search_container = @container.down('div.chosen-search')
+ @selected_item = @container.down('.chosen-single')
this.results_build()
this.set_tab_index()
this.set_label_behavior()
- @form_field.fire("liszt:ready", {chosen: this})
+ @form_field.fire("chosen:ready", {chosen: this})
register_observers: ->
@container.observe "mousedown", (evt) => this.container_mousedown(evt)
@container.observe "mouseup", (evt) => this.container_mouseup(evt)
@container.observe "mouseenter", (evt) => this.mouse_enter(evt)
@@ -63,33 +58,59 @@
@search_results.observe "mouseover", (evt) => this.search_results_mouseover(evt)
@search_results.observe "mouseout", (evt) => this.search_results_mouseout(evt)
@search_results.observe "mousewheel", (evt) => this.search_results_mousewheel(evt)
@search_results.observe "DOMMouseScroll", (evt) => this.search_results_mousewheel(evt)
- @form_field.observe "liszt:updated", (evt) => this.results_update_field(evt)
- @form_field.observe "liszt:activate", (evt) => this.activate_field(evt)
- @form_field.observe "liszt:open", (evt) => this.container_mousedown(evt)
+ @search_results.observe "touchstart", (evt) => this.search_results_touchstart(evt)
+ @search_results.observe "touchmove", (evt) => this.search_results_touchmove(evt)
+ @search_results.observe "touchend", (evt) => this.search_results_touchend(evt)
+ @form_field.observe "chosen:updated", (evt) => this.results_update_field(evt)
+ @form_field.observe "chosen:activate", (evt) => this.activate_field(evt)
+ @form_field.observe "chosen:open", (evt) => this.container_mousedown(evt)
+
@search_field.observe "blur", (evt) => this.input_blur(evt)
@search_field.observe "keyup", (evt) => this.keyup_checker(evt)
@search_field.observe "keydown", (evt) => this.keydown_checker(evt)
@search_field.observe "focus", (evt) => this.input_focus(evt)
if @is_multiple
@search_choices.observe "click", (evt) => this.choices_click(evt)
else
@container.observe "click", (evt) => evt.preventDefault() # gobble click of anchor
+ destroy: ->
+ document.stopObserving "click", @click_test_action
+
+ @form_field.stopObserving()
+ @container.stopObserving()
+ @search_results.stopObserving()
+ @search_field.stopObserving()
+ @form_field_label.stopObserving() if @form_field_label?
+
+ if @is_multiple
+ @search_choices.stopObserving()
+ @container.select(".search-choice-close").each (choice) ->
+ choice.stopObserving()
+ else
+ @selected_item.stopObserving()
+
+ if @search_field.tabIndex
+ @form_field.tabIndex = @search_field.tabIndex
+
+ @container.remove()
+ @form_field.show()
+
search_field_disabled: ->
@is_disabled = @form_field.disabled
if(@is_disabled)
- @container.addClassName 'chzn-disabled'
+ @container.addClassName 'chosen-disabled'
@search_field.disabled = true
@selected_item.stopObserving "focus", @activate_action if !@is_multiple
this.close_field()
else
- @container.removeClassName 'chzn-disabled'
+ @container.removeClassName 'chosen-disabled'
@search_field.disabled = false
@selected_item.observe "focus", @activate_action if !@is_multiple
container_mousedown: (evt) ->
if !@is_disabled
@@ -99,11 +120,11 @@
if not (evt? and evt.target.hasClassName "search-choice-close")
if not @active_field
@search_field.clear() if @is_multiple
document.observe "click", @click_test_action
this.results_show()
- else if not @is_multiple and evt and (evt.target is @selected_item || evt.target.up("a.chzn-single"))
+ else if not @is_multiple and evt and (evt.target is @selected_item || evt.target.up("a.chosen-single"))
this.results_toggle()
this.activate_field()
container_mouseup: (evt) ->
@@ -115,53 +136,53 @@
evt.preventDefault()
delta = delta * 40 if evt.type is 'DOMMouseScroll'
@search_results.scrollTop = delta + @search_results.scrollTop
blur_test: (evt) ->
- this.close_field() if not @active_field and @container.hasClassName("chzn-container-active")
+ this.close_field() if not @active_field and @container.hasClassName("chosen-container-active")
close_field: ->
document.stopObserving "click", @click_test_action
@active_field = false
this.results_hide()
- @container.removeClassName "chzn-container-active"
+ @container.removeClassName "chosen-container-active"
this.clear_backstroke()
this.show_search_field_default()
this.search_field_scale()
activate_field: ->
- @container.addClassName "chzn-container-active"
+ @container.addClassName "chosen-container-active"
@active_field = true
@search_field.value = @search_field.value
@search_field.focus()
test_active_click: (evt) ->
- if evt.target.up('.chzn-container') is @container
+ if evt.target.up('.chosen-container') is @container
@active_field = true
else
this.close_field()
results_build: ->
@parsing = true
@selected_option_count = null
- @results_data = root.SelectParser.select_to_array @form_field
+ @results_data = SelectParser.select_to_array @form_field
if @is_multiple
@search_choices.select("li.search-choice").invoke("remove")
else if not @is_multiple
this.single_set_selected_text()
if @disable_search or @form_field.options.length <= @disable_search_threshold
@search_field.readOnly = true
- @container.addClassName "chzn-container-single-nosearch"
+ @container.addClassName "chosen-container-single-nosearch"
else
@search_field.readOnly = false
- @container.removeClassName "chzn-container-single-nosearch"
+ @container.removeClassName "chosen-container-single-nosearch"
this.update_results_content this.results_option_build({first:true})
this.search_field_disabled()
this.show_search_field_default()
@@ -191,15 +212,15 @@
@result_highlight.removeClassName('highlighted') if @result_highlight
@result_highlight = null
results_show: ->
if @is_multiple and @max_selected_options <= this.choices_count()
- @form_field.fire("liszt:maxselected", {chosen: this})
+ @form_field.fire("chosen:maxselected", {chosen: this})
return false
- @container.addClassName "chzn-with-drop"
- @form_field.fire("liszt:showing_dropdown", {chosen: this})
+ @container.addClassName "chosen-with-drop"
+ @form_field.fire("chosen:showing_dropdown", {chosen: this})
@results_showing = true
@search_field.focus()
@search_field.value = @search_field.value
@@ -211,12 +232,12 @@
results_hide: ->
if @results_showing
this.result_clear_highlight()
- @container.removeClassName "chzn-with-drop"
- @form_field.fire("liszt:hiding_dropdown", {chosen: this})
+ @container.removeClassName "chosen-with-drop"
+ @form_field.fire("chosen:hiding_dropdown", {chosen: this})
@results_showing = false
set_tab_index: (el) ->
@@ -300,17 +321,21 @@
if @result_highlight
high = @result_highlight
this.result_clear_highlight()
if @is_multiple and @max_selected_options <= this.choices_count()
- @form_field.fire("liszt:maxselected", {chosen: this})
+ @form_field.fire("chosen:maxselected", {chosen: this})
return false
if @is_multiple
high.removeClassName("active-result")
else
- @search_results.descendants(".result-selected").invoke "removeClassName", "result-selected"
+ if @result_single_selected
+ @result_single_selected.removeClassName("result-selected")
+ selected_index = @result_single_selected.getAttribute('data-option-array-index')
+ @results_data[selected_index].selected = false
+
@result_single_selected = high
high.addClassName("result-selected")
item = @results_data[ high.getAttribute("data-option-array-index") ]
@@ -333,14 +358,14 @@
this.search_field_scale()
single_set_selected_text: (text=@default_text) ->
if text is @default_text
- @selected_item.addClassName("chzn-default")
+ @selected_item.addClassName("chosen-default")
else
this.single_deselect_control_build()
- @selected_item.removeClassName("chzn-default")
+ @selected_item.removeClassName("chosen-default")
@selected_item.down("span").update(text)
result_deselect: (pos) ->
result_data = @results_data[pos]
@@ -361,11 +386,11 @@
return false
single_deselect_control_build: ->
return unless @allow_single_deselect
@selected_item.down("span").insert { after: "<abbr class=\"search-choice-close\"></abbr>" } unless @selected_item.down("abbr")
- @selected_item.addClassName("chzn-single-with-deselect")
+ @selected_item.addClassName("chosen-single-with-deselect")
get_search_text: ->
if @search_field.value is @default_text then "" else @search_field.value.strip().escapeHTML()
winnow_results_set_highlight: ->
@@ -471,7 +496,5 @@
if( w > f_width-10 )
w = f_width - 10
@search_field.setStyle({'width': w + 'px'})
-
-root.Chosen = Chosen