Permalink
Browse files

Ported 'Add Item' link to chosen.proto

  • Loading branch information...
1 parent c24f7e1 commit e4974f618fbd18237ed2fc57cfed9cb093c852f2 @kamal kamal committed Aug 5, 2011
Showing with 14 additions and 3 deletions.
  1. +14 −3 coffee/chosen.proto.coffee
View
@@ -34,7 +34,8 @@ class Chosen
@single_temp = new Template('<a href="javascript:void(0)" class="chzn-single"><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>')
- @no_results_temp = new Template('<li class="no-results">No results match "<span>#{terms}</span>"</li>')
+ @no_results_temp = new Template('<li class="no-results">No results match "<span>#{terms}</span>".#{add_item_link}</li>')
+ @new_option_html = new Template('<option value="#{terms}">#{terms}</option>')
set_up_html: ->
@@ -461,8 +462,18 @@ class Chosen
this.result_do_highlight do_high if do_high?
no_results: (terms) ->
- @search_results.insert @no_results_temp.evaluate( terms: terms )
-
+ regex = new RegExp('^' + terms + '$', 'i')
+ selected = (option for option in @results_data when regex.test(option.value) and option.selected)
+ add_item_link = if selected.length == 0 then ' <a href="javascript:void(0);" class="option-add">Add this item</a>' else ''
+ @search_results.insert @no_results_temp.evaluate( terms: terms, add_item_link: add_item_link )
+ if selected.length == 0
+ @search_results.down("a.option-add").observe "click", (evt) => this.select_add_option(terms)
+
+ select_add_option: (terms) ->
+ @form_field.insert @new_option_html.evaluate( terms: terms )
+ Event.fire @form_field, "liszt:updated"
+ this.result_select()
+
no_results_clear: ->
nr = null
nr.remove() while nr = @search_results.down(".no-results")

0 comments on commit e4974f6

Please sign in to comment.