')
set_up_html: ->
@container_id = @form_field.identify().replace(/[^\w]/g, '_') + "_chzn"
@@ -128,7 +130,6 @@ class Chosen extends AbstractChosen
close_field: ->
document.stopObserving "click", @click_test_action
-
@active_field = false
this.results_hide()
@@ -161,10 +162,10 @@ class Chosen extends AbstractChosen
@choices = 0
else if not @is_multiple
@selected_item.addClassName("chzn-default").down("span").update(@default_text)
- if @disable_search or @form_field.options.length <= @disable_search_threshold
- @container.addClassName "chzn-container-single-nosearch"
- else
+ if @create_option and not @disable_search
@container.removeClassName "chzn-container-single-nosearch"
+ else if @disable_search or @form_field.options.length <= @disable_search_threshold
+ @container.addClassName "chzn-container-single-nosearch"
content = ''
for data in @results_data
@@ -325,6 +326,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
@@ -387,6 +393,7 @@ class Chosen extends AbstractChosen
winnow_results: ->
this.no_results_clear()
+ this.create_option_clear()
results = 0
@@ -394,6 +401,9 @@ class Chosen extends AbstractChosen
regexAnchor = if @search_contains then "" else "^"
regex = new RegExp(regexAnchor + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i')
zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i')
+ eregex = new RegExp('^' + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") + '$', 'i')
+
+ exact_result = false
for option in @results_data
if not option.disabled and not option.empty
@@ -402,10 +412,15 @@ class Chosen extends AbstractChosen
else if not (@is_multiple and option.selected)
found = false
result_id = option.dom_id
+ result = $(option.dom_id)
if regex.test option.html
found = true
results += 1
+
+ if eregex.test option.html
+ exact_result = true
+
else if @enable_split_word_search and (option.html.indexOf(" ") >= 0 or option.html.indexOf("[") == 0)
#TODO: replace this substitution of /\[\]/ with a list of characters to skip.
parts = option.html.replace(/\[|\]/g, "").split(" ")
@@ -423,9 +438,8 @@ class Chosen extends AbstractChosen
else
text = option.html
- $(result_id).update text if $(result_id).innerHTML != text
-
- this.result_activate $(result_id)
+ result.update text if result.innerHTML != text
+ this.result_activate result
$(@results_data[option.group_array_index].dom_id).setStyle({display: 'list-item'}) if option.group_array_index?
else
@@ -433,8 +447,9 @@ class Chosen extends AbstractChosen
this.result_deactivate $(result_id)
if results < 1 and searchText.length
- this.no_results(searchText)
+ this.no_results searchText
else
+ this.show_create_option( searchText ) if @create_option and not exact_result and @persistent_create_option and searchText.length
this.winnow_results_set_highlight()
winnow_results_clear: ->
@@ -460,12 +475,32 @@ class Chosen extends AbstractChosen
no_results: (terms) ->
@search_results.insert @no_results_temp.evaluate( terms: terms )
+ if @create_option
+ this.show_create_option( terms )
+
+ show_create_option: (terms) ->
+ @search_results.insert @create_option_temp.evaluate( text: @create_option_text, terms: terms )
+
+ create_option_clear: ->
+ create_option = @search_results.select(".create-option")
+ create_option.each (el) ->
+ el.remove()
+
+ 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, "liszt:updated"
no_results_clear: ->
nr = null
nr.remove() while nr = @search_results.down(".no-results")
-
keydown_arrow: ->
actives = @search_results.select("li.active-result")
if actives.length
diff --git a/coffee/lib/abstract-chosen.coffee b/coffee/lib/abstract-chosen.coffee
index d83d2c84603..50807d84724 100644
--- a/coffee/lib/abstract-chosen.coffee
+++ b/coffee/lib/abstract-chosen.coffee
@@ -36,6 +36,8 @@ class AbstractChosen
@single_backstroke_delete = @options.single_backstroke_delete || false
@max_selected_options = @options.max_selected_options || Infinity
@inherit_select_classes = @options.inherit_select_classes || false
+ @create_option = @options.create_option || false
+ @persistent_create_option = @options.persistent_create_option || false
set_default_text: ->
if @form_field.getAttribute("data-placeholder")
@@ -46,6 +48,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 || "Add option"
mouse_enter: -> @mouse_on_container = true
mouse_leave: -> @mouse_on_container = false
@@ -75,7 +78,10 @@ class AbstractChosen
'
' + option.html + '
'
else
""
-
+
+ append_option: (option) ->
+ this.select_append_option(option)
+
results_update_field: ->
this.set_default_text()
this.results_reset_cleanup() if not @is_multiple
diff --git a/create-example.jquery.html b/create-example.jquery.html
new file mode 100644
index 00000000000..e18904f757d
--- /dev/null
+++ b/create-example.jquery.html
@@ -0,0 +1,1369 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/create-example.proto.html b/create-example.proto.html
new file mode 100644
index 00000000000..9243872ec2c
--- /dev/null
+++ b/create-example.proto.html
@@ -0,0 +1,1383 @@
+
+
+
+
+
+
+
+
+
Chosen
+
Chosen is a JavaScript plugin for jQuery and Prototype that makes long, unwieldy select boxes much more user-friendly. For more information (including usage, explanation and faqs), check out the online documentation.
+
+
Standard Select
+
+
+ Turns This
+
+
+
+ Into This
+
+
+
+
+
Multiple Select
+
+
+ Turns This
+
+
+
+ Into This
+
+
+
+
+
<optgroup> Support
+
+
+ Single Select with Groups
+
+
+
+ Multiple Select with Groups
+
+
+
+
+
Selected and Disabled Support
+
+
Chosen automatically highlights selected options and removes disabled options.
+
+ Single Select
+
+
+
+ Multiple Select
+
+
+
+
+
+
It is also possible to prevent selected options being deselected by also making them disabled.
+
+
+
+
+
+
Default Text Support
+
+
Chosen automatically sets the default field text ("Choose a country...") by reading the select element's data-placeholder value. If no data-placeholder value is present, it will default to "Select Some Option" or "Select Some Options" depending on whether the select is single or multiple. You can change these elements in the plugin js file as you see fit.
+ <select data-placeholder="Choose a country..." style="width:350px;" multiple class="chzn-select">
+
Note: on single selects, the first element is assumed to be selected by the browser. To take advantage of the default text support, you will need to include a blank option as the first element of your select list.
+
+
+
No Results Text Support
+
+
Setting the "No results" search text is as easy as passing an option when you create Chosen:
+
+ new Chosen($("chzn_select_field"),{no_results_text: "No results matched"});
+
+
+
+
Limit Selected Options in Multiselect
+
+
You can easily limit how many options can user select:
+
+ new Chosen($("chzn_select_field"),{max_selected_options: 5});
+
+
If you try to select another option with limit reached liszt:maxselected event is triggered:
When a single select box isn't a required field, you can set allow_single_deselect: true and Chosen will add a UI element for option deselection. This will only work if the first option has blank text.
+
+
+
+
+
+
Right to Left Support
+
+
Chosen supports right to left select boxes too. just add "chzn-rtl" in addition to "chzn-select" to your select tags and you are good to go.
+
<select class="chzn-select chzn-rtl">
+
+ Single right to left select
+
+
+
+ Multiple right to left select
+
+
+
+
+
Change / Update Events
+
+
+
+
Form Field Change
+
When working with form fields, you often want to perform some behavior after a value has been selected or deselected. Whenever a user selects a field in Chosen, it triggers a "change" event* on the original form field. That let's you do something like this:
+
$("#form_field").chosen().change( … );
+
Note: Prototype doesn't offer support for triggering standard browser events. Event.simulate is required to trigger the change event when using the Prototype version.
+
+
+
Updating Chosen Dynamically
+
If you need to update the options in your select field and want Chosen to pick up the changes, you'll need to trigger the "liszt:updated" event on the field. Chosen will re-build itself based on the updated content.