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: linc01n/chosen-rails
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 6 commits
  • 8 files changed
  • 0 commit comments
  • 3 contributors
View
4 chosen-rails.gemspec
@@ -15,8 +15,8 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]
gem.version = Chosen::Rails::VERSION
- gem.add_dependency "railties", "~> 3.0"
- gem.add_dependency "thor", "~> 0.14"
+ gem.add_dependency "railties"
+ gem.add_dependency "thor"
gem.add_development_dependency "bundler", "~> 1.0"
gem.add_development_dependency "rails", "~> 3.0"
gem.add_development_dependency "sass", "~> 3.1"
View
17 lib/chosen-rails/source_file.rb
@@ -6,14 +6,15 @@ 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"
+ remote = "https://github.com/fatfreecrm/chosen"
+ branch = "master"
+ get "#{remote}/raw/#{branch}/chosen/chosen-sprite.png", "images/chosen-sprite.png"
+ get "#{remote}/raw/#{branch}/chosen/chosen.css", "stylesheets/chosen.css"
+ get "#{remote}/raw/#{branch}/coffee/lib/abstract-chosen.coffee", "javascripts/lib/abstract-chosen.coffee"
+ get "#{remote}/raw/#{branch}/coffee/lib/select-parser.coffee", "javascripts/lib/select-parser.coffee"
+ get "#{remote}/raw/#{branch}/coffee/chosen.jquery.coffee", "javascripts/chosen.jquery.coffee"
+ get "#{remote}/raw/#{branch}/coffee/chosen.proto.coffee", "javascripts/chosen.proto.coffee"
+ get "#{remote}/raw/#{branch}/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
2  lib/chosen-rails/version.rb
@@ -1,6 +1,6 @@
module Chosen
module Rails
- VERSION = "0.9.5.1"
+ VERSION = "0.9.6"
EDITOR_VERSION = "0.9.5"
end
end
View
BIN  vendor/assets/images/chosen-sprite.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
68 vendor/assets/javascripts/chosen.jquery.coffee
@@ -45,6 +45,7 @@ class Chosen extends AbstractChosen
@form_field_jq.hide().after container_div
@container = ($ '#' + @container_id)
@container.addClass( "chzn-container-" + (if @is_multiple then "multi" else "single") )
+ @container.addClass "chzn-container-single-nosearch" if not @is_multiple and @form_field.options.length <= @disable_search_threshold
@dropdown = @container.find('div.chzn-drop').first()
dd_top = @container.height()
@@ -92,28 +93,28 @@ class Chosen extends AbstractChosen
@search_field.focus (evt) => this.input_focus(evt)
search_field_disabled: ->
- @is_disabled = @form_field_jq[0].disabled
+ @is_disabled = @form_field_jq.attr 'disabled'
if(@is_disabled)
@container.addClass 'chzn-disabled'
- @search_field[0].disabled = true
+ @search_field.attr 'disabled', true
@selected_item.unbind "focus", @activate_action if !@is_multiple
this.close_field()
else
@container.removeClass 'chzn-disabled'
- @search_field[0].disabled = false
+ @search_field.attr 'disabled', false
@selected_item.bind "focus", @activate_action if !@is_multiple
container_mousedown: (evt) ->
if !@is_disabled
target_closelink = if evt? then ($ evt.target).hasClass "search-choice-close" else false
- if evt and evt.type is "mousedown"
+ if evt and evt.type is "mousedown" and not @results_showing
evt.stopPropagation()
if not @pending_destroy_click and not target_closelink
if not @active_field
@search_field.val "" if @is_multiple
$(document).click @click_test_action
this.results_show()
- else if not @is_multiple and evt and (($(evt.target)[0] == @selected_item[0]) || $(evt.target).parents("a.chzn-single").length)
+ else if not @is_multiple and evt and ($(evt.target) is @selected_item || $(evt.target).parents("a.chzn-single").length)
evt.preventDefault()
this.results_toggle()
@@ -163,6 +164,7 @@ class Chosen extends AbstractChosen
this.close_field()
results_build: ->
+ startTime = new Date()
@parsing = true
@results_data = root.SelectParser.select_to_array @form_field
@@ -171,10 +173,6 @@ class Chosen extends AbstractChosen
@choices = 0
else if not @is_multiple
@selected_item.find("span").text @default_text
- if @form_field.options.length <= @disable_search_threshold
- @container.addClass "chzn-container-single-nosearch"
- else
- @container.removeClass "chzn-container-single-nosearch"
content = ''
for data in @results_data
@@ -351,6 +349,23 @@ class Chosen extends AbstractChosen
@form_field_jq.trigger "change"
this.search_field_scale()
+ else if @options.allow_option_creation
+ new_option = @search_field.val()
+ return unless new_option
+ if @allow_creation(new_option)
+ $('<option>', {selected: true, value: new_option}).text(new_option).appendTo(@form_field_jq)
+ @results_update_field(evt)
+ @form_field_jq.trigger "change"
+ @search_field.val("")
+ @results_hide()
+
+ allow_creation: (new_option) ->
+ if @is_multiple
+ matches = @search_choices.find("li.search-choice span").filter ->
+ $(this).text().toLowerCase() == new_option.toLowerCase()
+ !matches.length
+ else
+ @selected_item.find('span').text().toLowerCase() != new_option.toLowerCase()
result_activate: (el) ->
el.addClass("active-result")
@@ -376,24 +391,30 @@ class Chosen extends AbstractChosen
@selected_item.find("span").first().after "<abbr class=\"search-choice-close\"></abbr>" if @allow_single_deselect and @selected_item.find("abbr").length < 1
winnow_results: ->
+ startTime = new Date()
this.no_results_clear()
results = 0
searchText = if @search_field.val() is @default_text then "" else $('<div/>').text($.trim(@search_field.val())).html()
- regex = new RegExp('^' + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i')
- zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i')
+ textToSearch = searchText.replace(/[\-\[\]\{\}\(\)\*\+\?\.,\\\^\$\|\#\s]/g, "\\$&")
+ regex = new RegExp('^' + textToSearch, 'i')
+ zregex = new RegExp(textToSearch, 'i')
+ fregex = new RegExp("^" + textToSearch + "$", 'i')
for option in @results_data
if not option.disabled and not option.empty
if option.group
- $('#' + option.dom_id).css('display', 'none')
+ $('#' + option.dom_id).hide()
else if not (@is_multiple and option.selected)
found = false
result_id = option.dom_id
- result = $("#" + result_id)
- if regex.test option.html
+ if @options.allow_option_creation && searchText && fregex.test(option.html)
+ found = true
+ results += 1
+ @result_do_highlight($('#' + option.dom_id))
+ else if regex.test option.html
found = true
results += 1
else if option.html.indexOf(" ") >= 0 or option.html.indexOf("[") == 0
@@ -412,18 +433,20 @@ class Chosen extends AbstractChosen
text = text.substr(0, startpos) + '<em>' + text.substr(startpos)
else
text = option.html
-
- result.html(text)
- this.result_activate result
- $("#" + @results_data[option.group_array_index].dom_id).css('display', 'list-item') if option.group_array_index?
+ $("#" + result_id).html text if $("#" + result_id).html != text
+
+ this.result_activate $("#" + result_id)
+
+ $("#" + @results_data[option.group_array_index].dom_id).show() if option.group_array_index?
else
this.result_clear_highlight() if @result_highlight and result_id is @result_highlight.attr 'id'
- this.result_deactivate result
-
+ this.result_deactivate $("#" + result_id)
+
if results < 1 and searchText.length
this.no_results searchText
- else
+ @results_hide() if @options.allow_option_creation && @is_multiple
+ else if not @options.allow_option_creation
this.winnow_results_set_highlight()
winnow_results_clear: ->
@@ -433,7 +456,7 @@ class Chosen extends AbstractChosen
for li in lis
li = $(li)
if li.hasClass "group-result"
- li.css('display', 'auto')
+ li.show()
else if not @is_multiple or not li.hasClass "result-selected"
this.result_activate li
@@ -446,6 +469,7 @@ class Chosen extends AbstractChosen
this.result_do_highlight do_high if do_high?
no_results: (terms) ->
+ return if !@is_multiple && @options.allow_option_creation
no_results_html = $('<li class="no-results">' + @results_none_found + ' "<span></span>"</li>')
no_results_html.find("span").first().html(terms)
View
54 vendor/assets/javascripts/chosen.proto.coffee
@@ -38,6 +38,7 @@ class Chosen extends AbstractChosen
@form_field.hide().insert({ after: base_template })
@container = $(@container_id)
@container.addClassName( "chzn-container-" + (if @is_multiple then "multi" else "single") )
+ @container.addClassName "chzn-container-single-nosearch" if not @is_multiple and @form_field.options.length <= @disable_search_threshold
@dropdown = @container.down('div.chzn-drop')
dd_top = @container.getHeight()
@@ -99,7 +100,7 @@ class Chosen extends AbstractChosen
container_mousedown: (evt) ->
if !@is_disabled
target_closelink = if evt? then evt.target.hasClassName "search-choice-close" else false
- if evt and evt.type is "mousedown"
+ if evt and evt.type is "mousedown" and not @results_showing
evt.stop()
if not @pending_destroy_click and not target_closelink
if not @active_field
@@ -155,6 +156,7 @@ class Chosen extends AbstractChosen
this.close_field()
results_build: ->
+ startTime = new Date()
@parsing = true
@results_data = root.SelectParser.select_to_array @form_field
@@ -163,10 +165,6 @@ class Chosen extends AbstractChosen
@choices = 0
else if not @is_multiple
@selected_item.down("span").update(@default_text)
- if @form_field.options.length <= @disable_search_threshold
- @container.addClassName "chzn-container-single-nosearch"
- else
- @container.removeClassName "chzn-container-single-nosearch"
content = ''
for data in @results_data
@@ -336,12 +334,34 @@ class Chosen extends AbstractChosen
@selected_item.down("span").update(item.html)
this.single_deselect_control_build() if @allow_single_deselect
+ @on_option_add(item.value) if @on_option_add
+
this.results_hide() unless evt.metaKey and @is_multiple
@search_field.value = ""
@form_field.simulate("change") if typeof Event.simulate is 'function'
this.search_field_scale()
+ else if @options.allow_option_creation
+ new_option = @search_field.value
+ return unless new_option
+ if @allow_creation(new_option)
+ @form_field.insert(Element('option', {selected: true, value: new_option}).update(new_option))
+ @results_update_field(evt)
+ @on_option_add(new_option) if @on_option_add
+ @form_field.simulate("change") if typeof Event.simulate is 'function'
+ @search_field.value = ""
+ @results_hide()
+
+ allow_creation: (new_option) ->
+ if @is_multiple
+ matches = @search_choices.getElementsBySelector("li.search-choice span").select (el) ->
+ console.debug(el)
+ el.innerHTML.toLowerCase() == new_option.toLowerCase()
+ console.debug(matches)
+ !matches.length
+ else
+ @selected_item.getElementsBySelector('span').innerHTML.toLowerCase() != new_option.toLowerCase()
result_activate: (el) ->
el.addClassName("active-result")
@@ -353,7 +373,10 @@ class Chosen extends AbstractChosen
result_data = @results_data[pos]
result_data.selected = false
- @form_field.options[result_data.options_index].selected = false
+ option = @form_field.options[result_data.options_index]
+ option.selected = false
+ @on_option_remove(option.value) if @on_option_remove
+
result = $(@container_id + "_o_" + pos)
result.removeClassName("result-selected").addClassName("active-result").show()
@@ -367,13 +390,16 @@ class Chosen extends AbstractChosen
@selected_item.down("span").insert { after: "<abbr class=\"search-choice-close\"></abbr>" } if @allow_single_deselect and not @selected_item.down("abbr")
winnow_results: ->
+ startTime = new Date()
this.no_results_clear()
results = 0
searchText = if @search_field.value is @default_text then "" else @search_field.value.strip().escapeHTML()
- regex = new RegExp('^' + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i')
- zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i')
+ textToSearch = searchText.replace(/[\-\[\]\{\}\(\)\*\+\?\.,\\\^\$\|\#\s]/g, "\\$&")
+ regex = new RegExp('^' + textToSearch, 'i')
+ zregex = new RegExp(textToSearch, 'i')
+ fregex = new RegExp("^" + textToSearch + "$", 'i')
for option in @results_data
if not option.disabled and not option.empty
@@ -383,7 +409,11 @@ class Chosen extends AbstractChosen
found = false
result_id = option.dom_id
- if regex.test option.html
+ if @options.allow_option_creation && searchText && fregex.test(option.html)
+ found = true
+ results += 1
+ @result_do_highlight($(option.dom_id))
+ else if regex.test option.html
found = true
results += 1
else if option.html.indexOf(" ") >= 0 or option.html.indexOf("[") == 0
@@ -407,14 +437,15 @@ class Chosen extends AbstractChosen
this.result_activate $(result_id)
- $(@results_data[option.group_array_index].dom_id).setStyle({display: 'list-item'}) if option.group_array_index?
+ $(@results_data[option.group_array_index].dom_id).show() if option.group_array_index?
else
this.result_clear_highlight() if $(result_id) is @result_highlight
this.result_deactivate $(result_id)
if results < 1 and searchText.length
this.no_results(searchText)
- else
+ @results_hide() if @options.allow_option_creation && @is_multiple
+ else if not @options.allow_option_creation
this.winnow_results_set_highlight()
winnow_results_clear: ->
@@ -439,6 +470,7 @@ class Chosen extends AbstractChosen
this.result_do_highlight do_high if do_high?
no_results: (terms) ->
+ return if !@is_multiple && @options.allow_option_creation
@search_results.insert @no_results_temp.evaluate( terms: terms )
no_results_clear: ->
View
6 vendor/assets/javascripts/lib/abstract-chosen.coffee
@@ -27,7 +27,9 @@ class AbstractChosen
@results_showing = false
@result_highlighted = null
@result_single_selected = null
- @allow_single_deselect = if @options.allow_single_deselect? and @form_field.options[0]? and @form_field.options[0].text is "" then @options.allow_single_deselect else false
+ @allow_single_deselect = if @options.allow_single_deselect? and @form_field.options[0].text == "" then @options.allow_single_deselect else false
+ @on_option_add = @options.on_option_add
+ @on_option_remove = @options.on_option_remove
@disable_search_threshold = @options.disable_search_threshold || 0
@choices = 0
@results_none_found = @options.no_results_text or "No results match"
@@ -88,7 +90,7 @@ class AbstractChosen
this.results_search()
when 13
evt.preventDefault()
- this.result_select(evt) if this.results_showing
+ this.result_select(evt) if this.results_showing || this.options.allow_option_creation
when 27
this.results_hide() if @results_showing
when 9, 38, 40, 16, 91, 17
View
3  vendor/assets/stylesheets/chosen.css.sass
@@ -328,9 +328,6 @@
-webkit-border-radius: 4px 0 0 4px
-moz-border-radius: 4px 0 0 4px
border-radius: 4px 0 0 4px
- abbr
- left: 26px
- right: auto
.chzn-choices
li
float: right

No commit comments for this range

Something went wrong with that request. Please try again.