vendor/assets/javascripts/chosen.proto.coffee in chosen-rails-0.9.8.2 vs vendor/assets/javascripts/chosen.proto.coffee in chosen-rails-0.9.8.3

- old
+ new

@@ -18,10 +18,11 @@ # HTML Templates @single_temp = new Template('<a href="javascript:void(0)" class="chzn-single chzn-default"><span>#{default}</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><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" 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>') + @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: -> @container_id = @form_field.identify().replace(/[^\w]/g, '_') + "_chzn" @@ -72,10 +73,12 @@ @search_results.observe "mouseup", (evt) => this.search_results_mouseup(evt) @search_results.observe "mouseover", (evt) => this.search_results_mouseover(evt) @search_results.observe "mouseout", (evt) => this.search_results_mouseout(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_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) @@ -162,11 +165,11 @@ if @is_multiple and @choices > 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 @form_field.options.length <= @disable_search_threshold + if @disable_search or @form_field.options.length <= @disable_search_threshold @container.addClassName "chzn-container-single-nosearch" else @container.removeClassName "chzn-container-single-nosearch" content = '' @@ -288,31 +291,32 @@ @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 + before: (if item.disabled then @choice_noclose_temp else @choice_temp).evaluate id: choice_id choice: item.html position: item.array_index - link = $(choice_id).down('a') - link.observe "click", (evt) => this.choice_destroy_link_click(evt) + if not item.disabled + link = $(choice_id).down('a') + link.observe "click", (evt) => this.choice_destroy_link_click(evt) choice_destroy_link_click: (evt) -> evt.preventDefault() if not @is_disabled @pending_destroy_click = true this.choice_destroy evt.target choice_destroy: (link) -> - @choices -= 1 - this.show_search_field_default() + 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 @choices > 0 and @search_field.value.length < 1 - this.result_deselect link.readAttribute("rel") - link.up('li').remove() + link.up('li').remove() results_reset: -> @form_field.options[0].selected = true @selected_item.down("span").update(@default_text) @selected_item.addClassName("chzn-default") if not @is_multiple @@ -320,10 +324,11 @@ this.results_reset_cleanup() @form_field.simulate("change") if typeof Event.simulate is 'function' this.results_hide() if @active_field results_reset_cleanup: -> + @current_value = @form_field.value deselect_trigger = @selected_item.down("abbr") deselect_trigger.remove() if(deselect_trigger) result_select: (evt) -> if @result_highlight @@ -366,21 +371,26 @@ result_deactivate: (el) -> el.removeClassName("active-result") result_deselect: (pos) -> result_data = @results_data[pos] - result_data.selected = false + + if not @form_field.options[result_data.options_index].disabled + result_data.selected = false - @form_field.options[result_data.options_index].selected = false - result = $(@container_id + "_o_" + pos) - result.removeClassName("result-selected").addClassName("active-result").show() + @form_field.options[result_data.options_index].selected = false + result = $(@container_id + "_o_" + pos) + result.removeClassName("result-selected").addClassName("active-result").show() - this.result_clear_highlight() - this.winnow_results() + this.result_clear_highlight() + this.winnow_results() - @form_field.simulate("change") if typeof Event.simulate is 'function' - this.search_field_scale() + @form_field.simulate("change") if typeof Event.simulate is 'function' + this.search_field_scale() + return true + else + return false single_deselect_control_build: -> @selected_item.down("span").insert { after: "<abbr class=\"search-choice-close\"></abbr>" } if @allow_single_deselect and not @selected_item.down("abbr") winnow_results: -> @@ -492,14 +502,17 @@ keydown_backstroke: -> if @pending_backstroke this.choice_destroy @pending_backstroke.down("a") this.clear_backstroke() else - @pending_backstroke = @search_container.siblings("li.search-choice").last() - if @single_backstroke_delete - @keydown_backstroke() - else - @pending_backstroke.addClassName("search-choice-focus") + next_available_destroy = @search_container.siblings().last() + if next_available_destroy and next_available_destroy.hasClassName("search-choice") and not next_available_destroy.hasClassName("search-choice-disabled") + @pending_backstroke = next_available_destroy + @pending_backstroke.addClassName("search-choice-focus") if @pending_backstroke + 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