Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: tsechingho/chosen-rails
base: master
...
head fork: irotem/chosen-rails
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 1 commit
  • 6 files changed
  • 0 commit comments
  • 1 contributor
Commits on Dec 05, 2012
@irotem irotem change chosen repository 095892e
View
18 lib/chosen-rails/source_file.rb
@@ -6,14 +6,16 @@ class SourceFile < Thor
desc "fetch source files", "fetch source files from GitHub"
def fetch
self.destination_root = "vendor/assets"
- remote = "https://github.com/harvesthq/chosen"
- get "#{remote}/raw/master/chosen/chosen-sprite.png", "images/chosen-sprite.png"
- get "#{remote}/raw/master/chosen/chosen.css", "stylesheets/chosen.css"
- get "#{remote}/raw/master/coffee/lib/abstract-chosen.coffee", "javascripts/lib/abstract-chosen.coffee"
- get "#{remote}/raw/master/coffee/lib/select-parser.coffee", "javascripts/lib/select-parser.coffee"
- get "#{remote}/raw/master/coffee/chosen.jquery.coffee", "javascripts/chosen.jquery.coffee"
- get "#{remote}/raw/master/coffee/chosen.proto.coffee", "javascripts/chosen.proto.coffee"
- get "#{remote}/raw/master/VERSION", "VERSION"
+ #tree = 'master'
+ tree = 'option_adding'
+ remote = "https://github.com/koenpunt/chosen"
+ get "#{remote}/raw/#{tree}/chosen/chosen-sprite.png", "images/chosen-sprite.png"
+ get "#{remote}/raw/#{tree}/chosen/chosen.css", "stylesheets/chosen.css"
+ get "#{remote}/raw/#{tree}/coffee/lib/abstract-chosen.coffee", "javascripts/lib/abstract-chosen.coffee"
+ get "#{remote}/raw/#{tree}/coffee/lib/select-parser.coffee", "javascripts/lib/select-parser.coffee"
+ get "#{remote}/raw/#{tree}/coffee/chosen.jquery.coffee", "javascripts/chosen.jquery.coffee"
+ get "#{remote}/raw/#{tree}/coffee/chosen.proto.coffee", "javascripts/chosen.proto.coffee"
+ get "#{remote}/raw/#{tree}/VERSION", "VERSION"
inside destination_root do
version = File.read("VERSION").sub("\n", "")
gsub_file "../../lib/chosen-rails/version.rb", /VERSION\s=\s"(\d|\.)+"$/ do |match|
View
4 lib/chosen-rails/version.rb
@@ -1,6 +1,6 @@
module Chosen
module Rails
- VERSION = "0.9.9"
- CHOSEN_VERSION = "0.9.9"
+ VERSION = "0.9.8"
+ CHOSEN_VERSION = "0.9.8"
end
end
View
48 vendor/assets/javascripts/chosen.jquery.coffee
@@ -169,10 +169,10 @@ class Chosen extends AbstractChosen
@choices = 0
else if not @is_multiple
@selected_item.addClass("chzn-default").find("span").text(@default_text)
- if @disable_search or @form_field.options.length <= @disable_search_threshold
- @container.addClass "chzn-container-single-nosearch"
- else
+ if @create_option and not @disable_search
@container.removeClass "chzn-container-single-nosearch"
+ else if @disable_search or @form_field.options.length <= @disable_search_threshold
+ @container.addClass "chzn-container-single-nosearch"
content = ''
for data in @results_data
@@ -279,7 +279,6 @@ class Chosen extends AbstractChosen
search_results_mouseout: (evt) ->
this.result_clear_highlight() if $(evt.target).hasClass "active-result" or $(evt.target).parents('.active-result').first()
-
choices_click: (evt) ->
evt.preventDefault()
if( @active_field and not($(evt.target).hasClass "search-choice" or $(evt.target).parents('.search-choice').first) and not @results_showing )
@@ -332,6 +331,11 @@ class Chosen extends AbstractChosen
result_select: (evt) ->
if @result_highlight
high = @result_highlight
+
+ if high.hasClass 'create-option'
+ this.select_create_option(@search_field.val())
+ return this.results_hide()
+
high_id = high.attr "id"
this.result_clear_highlight()
@@ -396,6 +400,7 @@ class Chosen extends AbstractChosen
winnow_results: ->
this.no_results_clear()
+ this.create_option_clear()
results = 0
@@ -403,7 +408,10 @@ 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
if option.group
@@ -416,6 +424,10 @@ class Chosen extends AbstractChosen
if regex.test option.html
found = true
results += 1
+ if eregex.test option.html
+ exact_result = true
+
+
else if 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(" ")
@@ -444,6 +456,7 @@ class Chosen extends AbstractChosen
if results < 1 and searchText.length
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: ->
@@ -468,9 +481,32 @@ 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
+
+ if @create_option
+ this.show_create_option( terms )
+ show_create_option: (terms) ->
+ create_option_html = $('<li class="create-option active-result"><a href="javascript:void(0);">' + @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 "liszt:updated"
+ #@active_field = false
+ @search_field.trigger('focus')
+
no_results_clear: ->
@search_results.find(".no-results").remove()
View
65 vendor/assets/javascripts/chosen.proto.coffee
@@ -21,7 +21,9 @@ class Chosen extends AbstractChosen
@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>')
+ @no_results_temp = new Template('<li class="no-results">#{text} "<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 href="javascript:void(0);">#{text}</a>: "#{terms}"</li>')
set_up_html: ->
@container_id = @form_field.identify().replace(/[^\w]/g, '_') + "_chzn"
@@ -158,10 +160,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
@@ -320,6 +322,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
@@ -382,6 +389,7 @@ class Chosen extends AbstractChosen
winnow_results: ->
this.no_results_clear()
+ this.create_option_clear()
results = 0
@@ -389,6 +397,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
@@ -397,10 +408,15 @@ class Chosen extends AbstractChosen
else if not (@is_multiple and option.selected)
found = false
result_id = option.dom_id
+ result = $(result_id)
if regex.test option.html
found = true
results += 1
+ if eregex.test option.html
+ exact_result = true
+
+
else if 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(" ")
@@ -418,9 +434,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
@@ -428,8 +443,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: ->
@@ -452,15 +468,40 @@ class Chosen extends AbstractChosen
do_high = @search_results.down(".active-result")
this.result_do_highlight do_high if do_high?
-
+
no_results: (terms) ->
- @search_results.insert @no_results_temp.evaluate( terms: terms )
-
+ no_results_html = @no_results_temp.evaluate( terms: terms, text: @results_none_found )
+
+ @search_results.insert no_results_html
+
+ if @create_option #and not selected
+ this.show_create_option( terms )
+
+ 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, "liszt:updated"
+ this.result_select()
+
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
View
11 vendor/assets/javascripts/lib/abstract-chosen.coffee
@@ -7,9 +7,10 @@ root = this
class AbstractChosen
constructor: (@form_field, @options={}) ->
+ this.set_default_values()
+
@is_multiple = @form_field.multiple
this.set_default_text()
- this.set_default_values()
this.setup()
@@ -33,6 +34,8 @@ class AbstractChosen
@choices = 0
@single_backstroke_delete = @options.single_backstroke_delete || false
@max_selected_options = @options.max_selected_options || Infinity
+ @create_option = @options.create_option || false
+ @persistent_create_option = @options.persistent_create_option || false
set_default_text: ->
if @form_field.getAttribute("data-placeholder")
@@ -43,6 +46,7 @@ class AbstractChosen
@default_text = @options.placeholder_text_single || @options.placeholder_text || "Select an Option"
@results_none_found = @form_field.getAttribute("data-no_results_text") || @options.no_results_text || "No results match"
+ @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
@@ -72,7 +76,10 @@ class AbstractChosen
'<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"'+style+'>' + option.html + '</li>'
else
""
-
+
+ append_option: (option) ->
+ this.select_append_option(option)
+
results_update_field: ->
this.results_reset_cleanup() if not @is_multiple
this.result_clear_highlight()
View
2  vendor/assets/stylesheets/chosen.css.sass
@@ -254,6 +254,8 @@
.no-results
background: #f4f4f4
display: list-item
+ .create-option
+ display: list-item
.group-result
cursor: default
color: #999

No commit comments for this range

Something went wrong with that request. Please try again.