Skip to content

Commit

Permalink
Feature: Option adding
Browse files Browse the repository at this point in the history
  • Loading branch information
koenpunt committed Oct 8, 2015
1 parent adf64cc commit 848224e
Show file tree
Hide file tree
Showing 5 changed files with 3,052 additions and 7 deletions.
29 changes: 27 additions & 2 deletions coffee/chosen.jquery.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class Chosen extends AbstractChosen
@search_choices.find("li.search-choice").remove()
else if not @is_multiple
this.single_set_selected_text()
if @disable_search or @form_field.options.length <= @disable_search_threshold
if @disable_search or @form_field.options.length <= @disable_search_threshold and not @create_option
@search_field[0].readOnly = true
@container.addClass "chosen-container-single-nosearch"
else
Expand Down Expand Up @@ -334,6 +334,10 @@ class Chosen extends AbstractChosen
if @result_highlight
high = @result_highlight

if high.hasClass "create-option"
this.select_create_option(@search_field.val())
return this.results_hide()

this.result_clear_highlight()

if @is_multiple and @max_selected_options <= this.choices_count()
Expand Down Expand Up @@ -413,17 +417,38 @@ class Chosen extends AbstractChosen
no_results: (terms) ->
no_results_html = $('<li class="no-results">' + @results_none_found + ' "<span></span>"</li>')
no_results_html.find("span").first().html(terms)

@search_results.append no_results_html
@form_field_jq.trigger("chosen:no_results", {chosen:this})

show_create_option: (terms) ->
create_option_html = $('<li class="create-option active-result"><a>' + @create_option_text + '</a>: "' + terms + '"</li>')
@search_results.append create_option_html

create_option_clear: ->
@search_results.find(".create-option").remove()

select_create_option: (terms) ->
if $.isFunction(@create_option)
@create_option.call this, terms
else
this.select_append_option( {value: terms, text: terms} )

select_append_option: ( options ) ->
option = $('<option />', options ).attr('selected', 'selected')
@form_field_jq.append option
@form_field_jq.trigger "chosen:updated"
@form_field_jq.trigger "change"
@search_field.trigger "focus"

no_results_clear: ->
@search_results.find(".no-results").remove()

keydown_arrow: ->
if @results_showing and @result_highlight
next_sib = @result_highlight.nextAll("li.active-result").first()
this.result_do_highlight next_sib if next_sib
else if @results_showing and @create_option
this.result_do_highlight(@search_results.find('.create-option'))
else
this.results_show()

Expand Down
37 changes: 34 additions & 3 deletions coffee/chosen.proto.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class @Chosen extends AbstractChosen
# HTML Templates
@single_temp = new Template('<a class="chosen-single chosen-default"><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>')
@no_results_temp = new Template('<li class="no-results">' + @results_none_found + ' "<span>#{terms}</span>".</li>')
@new_option_temp = new Template('<option value="#{value}">#{text}</option>')
@create_option_temp = new Template('<li class="create-option active-result"><a>#{text}</a>: "#{terms}"</li>')

set_up_html: ->
container_classes = ["chosen-container"]
Expand Down Expand Up @@ -183,7 +185,7 @@ class @Chosen extends AbstractChosen
@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
if @disable_search or @form_field.options.length <= @disable_search_threshold and not @create_option
@search_field.readOnly = true
@container.addClassName "chosen-container-single-nosearch"
else
Expand Down Expand Up @@ -327,6 +329,11 @@ class @Chosen extends AbstractChosen
result_select: (evt) ->
if @result_highlight
high = @result_highlight

if high.hasClassName "create-option"
this.select_create_option(@search_field.value)
return this.results_hide()

this.result_clear_highlight()

if @is_multiple and @max_selected_options <= this.choices_count()
Expand Down Expand Up @@ -409,15 +416,39 @@ class @Chosen extends AbstractChosen
@search_results.insert @no_results_temp.evaluate( terms: terms )
@form_field.fire("chosen:no_results", {chosen: this})

show_create_option: (terms) ->
create_option_html = @create_option_temp.evaluate( terms: terms, text: @create_option_text )
@search_results.insert create_option_html
@search_results.down(".create-option").observe "click", (evt) => this.select_create_option(terms)

create_option_clear: ->
co = null
co.remove() while co = @search_results.down(".create-option")

select_create_option: ( terms ) ->
if Object.isFunction( @create_option )
@create_option.call this, terms
else
this.select_append_option( value: terms, text: terms )

select_append_option: ( options ) ->
option = @new_option_temp.evaluate( options )
@form_field.insert option
Event.fire @form_field, "chosen:updated"
if typeof Event.simulate is 'function'
@form_field.simulate("change")
@search_field.simulate("focus")

no_results_clear: ->
nr = null
nr.remove() while nr = @search_results.down(".no-results")


keydown_arrow: ->
if @results_showing and @result_highlight
next_sib = @result_highlight.next('.active-result')
this.result_do_highlight next_sib if next_sib
else if @results_showing and @create_option
this.result_do_highlight(@search_results.select('.create-option').first())
else
this.results_show()

Expand Down
18 changes: 16 additions & 2 deletions coffee/lib/abstract-chosen.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class AbstractChosen
@display_disabled_options = if @options.display_disabled_options? then @options.display_disabled_options else true
@include_group_label_in_selected = @options.include_group_label_in_selected || false
@max_shown_results = @options.max_shown_results || Number.POSITIVE_INFINITY
@create_option = @options.create_option || false
@persistent_create_option = @options.persistent_create_option || false
@skip_no_results = @options.skip_no_results || false

set_default_text: ->
if @form_field.getAttribute("data-placeholder")
Expand All @@ -43,6 +46,7 @@ class AbstractChosen
@default_text = @options.placeholder_text_single || @options.placeholder_text || AbstractChosen.default_single_text

@results_none_found = @form_field.getAttribute("data-no_results_text") || @options.no_results_text || AbstractChosen.default_no_result_text
@create_option_text = @form_field.getAttribute("data-create_option_text") || @options.create_option_text || AbstractChosen.default_create_option_text

choice_label: (item) ->
if @include_group_label_in_selected and item.group_label?
Expand Down Expand Up @@ -125,6 +129,9 @@ class AbstractChosen

this.outerHTML(group_el)

append_option: (option) ->
this.select_append_option(option)

results_update_field: ->
this.set_default_text()
this.results_reset_cleanup() if not @is_multiple
Expand Down Expand Up @@ -152,11 +159,13 @@ class AbstractChosen
this.no_results_clear()

results = 0
exact_result = false

searchText = this.get_search_text()
escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
zregex = new RegExp(escapedSearchText, 'i')
regex = this.get_search_regex(escapedSearchText)
eregex = new RegExp('^' + escapedSearchText + '$', 'i')

for option in @results_data

Expand All @@ -180,6 +189,8 @@ class AbstractChosen
option.search_match = this.search_string_match(option.search_text, regex)
results += 1 if option.search_match and not option.group

exact_result = eregex.test option.html

if option.search_match
if searchText.length
startpos = option.search_text.search zregex
Expand All @@ -195,11 +206,14 @@ class AbstractChosen

if results < 1 and searchText.length
this.update_results_content ""
this.no_results searchText
this.no_results searchText unless @create_option and @skip_no_results
else
this.update_results_content this.results_option_build()
this.winnow_results_set_highlight()

if @create_option and (results < 1 or (!exact_result and @persistent_create_option)) and searchText.length
this.show_create_option( searchText )

get_search_regex: (escaped_search_string) ->
regex_anchor = if @search_contains then "" else "^"
new RegExp(regex_anchor + escaped_search_string, 'i')
Expand Down Expand Up @@ -301,4 +315,4 @@ class AbstractChosen
@default_multiple_text: "Select Some Options"
@default_single_text: "Select an Option"
@default_no_result_text: "No results match"

@default_create_option_text: "Add Option"
Loading

0 comments on commit 848224e

Please sign in to comment.