Skip to content

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.
...
Checking mergeability… Don’t worry, you can still create the pull request.
  • 7 commits
  • 7 files changed
  • 0 commit comments
  • 2 contributors
View
2 README.md
@@ -30,7 +30,7 @@ Add to your `app/assets/stylesheets/application.css`
*= require chosen
-## Gem maintainance
+## Gem maintenance
Maintain `chosen-rails` gem with `Rake` commands.
View
2 lib/chosen-rails/source_file.rb
@@ -6,7 +6,7 @@ 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"
+ remote = "https://github.com/koenpunt/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"
View
2 lib/chosen-rails/version.rb
@@ -1,6 +1,6 @@
module Chosen
module Rails
- VERSION = "0.9.8.1"
+ VERSION = "0.9.8"
EDITOR_VERSION = "0.9.8"
end
end
View
78 vendor/assets/javascripts/chosen.jquery.coffee
@@ -4,11 +4,15 @@ Copyright (c) 2011 by Harvest
###
root = this
$ = jQuery
+isIE6or7 = ->
+ msVersion = navigator.userAgent.match(/MSIE ([0-9]{1,}[\.0-9]{0,})/)
+ msie = !!msVersion
+ msie && parseFloat(msVersion[1]) < 8
$.fn.extend({
chosen: (options) ->
# Do no harm and return as soon as possible for unsupported browsers, namely IE6 and IE7
- return this if $.browser.msie and ($.browser.version is "6.0" or $.browser.version is "7.0")
+ return this if isIE6or7()
this.each((input_field) ->
$this = $ this
$this.data('chosen', new Chosen(this, options)) unless $this.hasClass "chzn-done"
@@ -205,6 +209,23 @@ class Chosen extends AbstractChosen
else
""
+ result_add_option: (option) ->
+ if not option.disabled
+ option.dom_id = @container_id + "_o_" + option.array_index
+
+ classes = if option.selected and @is_multiple then [] else ["active-result"]
+ classes.push "result-selected" if option.selected
+ classes.push "group-option" if option.group_array_index?
+
+ '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + option.html + '</li>'
+ else
+ ""
+
+ results_update_field: ->
+ this.result_clear_highlight()
+ @result_single_selected = null
+ this.results_build()
+
result_do_highlight: (el) ->
if el.length
this.result_clear_highlight()
@@ -287,7 +308,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 )
@@ -308,6 +328,8 @@ class Chosen extends AbstractChosen
if not @is_disabled
@pending_destroy_click = true
this.choice_destroy $(evt.target)
+ this.input_blur(evt)
+ @pending_destroy_click = false
else
evt.stopPropagation
@@ -328,13 +350,18 @@ class Chosen extends AbstractChosen
this.results_reset_cleanup()
@form_field_jq.trigger "change"
this.results_hide() if @active_field
-
+
results_reset_cleanup: ->
@selected_item.find("abbr").remove()
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()
@@ -393,6 +420,7 @@ class Chosen extends AbstractChosen
winnow_results: ->
this.no_results_clear()
+ this.create_option_clear()
results = 0
@@ -400,6 +428,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
@@ -413,6 +444,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(" ")
@@ -440,7 +475,9 @@ class Chosen extends AbstractChosen
if results < 1 and searchText.length
this.no_results searchText
+ @winnow_results_set_highlight()
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: ->
@@ -455,18 +492,43 @@ class Chosen extends AbstractChosen
this.result_activate li
winnow_results_set_highlight: ->
- if not @result_highlight
+ # if not @result_highlight
- selected_results = if not @is_multiple then @search_results.find(".result-selected.active-result") else []
- do_high = if selected_results.length then selected_results.first() else @search_results.find(".active-result").first()
+ selected_results = if not @is_multiple then @search_results.find(".result-selected.active-result") else []
+ do_high = if selected_results.length then selected_results.first() else @search_results.find(".active-result").first()
- this.result_do_highlight do_high if do_high?
+ this.result_do_highlight do_high if do_high?
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
+ # @search_results.append no_results_html
+
+ if @create_option #and not selected
+ @show_create_option( terms )
+ else
+ @search_results.append no_results_html
+
+ show_create_option: (terms) ->
+ create_option_html = $('<li class="create-option active-result"><a href="javascript:void(0);">' + @create_option_text + '</a>: "' + terms + '"</li>').bind "click", (evt) => this.select_create_option(terms)
+ @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
60 vendor/assets/javascripts/chosen.proto.coffee
@@ -20,7 +20,10 @@ class Chosen extends AbstractChosen
@single_temp = new Template('<a href="javascript:void(0)" class="chzn-single chzn-default"><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">' + @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"
@@ -328,6 +331,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
@@ -385,6 +393,7 @@ class Chosen extends AbstractChosen
winnow_results: ->
this.no_results_clear()
+ this.create_option_clear()
results = 0
@@ -392,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
@@ -400,10 +412,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(" ")
@@ -421,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
@@ -431,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: ->
@@ -455,15 +472,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
14 vendor/assets/javascripts/lib/abstract-chosen.coffee
@@ -24,15 +24,19 @@ class AbstractChosen
@activate_action = (evt) => this.activate_field(evt)
@active_field = false
@mouse_on_container = false
+ @pending_destroy_click = false
@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]? and @form_field.options[0].text is "" then @options.allow_single_deselect else false
+ @allow_single_deselect = if @options.allow_single_deselect? then @options.allow_single_deselect else false
@disable_search_threshold = @options.disable_search_threshold || 0
@search_contains = @options.search_contains || false
@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 +47,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
@@ -51,7 +56,7 @@ class AbstractChosen
setTimeout (=> this.container_mousedown()), 50 unless @active_field
input_blur: (evt) ->
- if not @mouse_on_container
+ if not @mouse_on_container or @pending_destroy_click
@active_field = false
setTimeout (=> this.blur_test()), 100
@@ -69,7 +74,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
14 vendor/assets/stylesheets/chosen.css.sass
@@ -74,7 +74,7 @@
background: image-url('chosen-sprite.png') right top no-repeat
&:hover
background-position: right -11px
- &.chzn-disabled abbr:hover
+ &.chzn-disabled .chzn-single abbr:hover
background-position: right top
.chzn-single div
position: absolute
@@ -118,7 +118,7 @@
/* @end
-.chzn-container-single-nosearch input
+.chzn-container-single-nosearch .chzn-search input
position: absolute
left: -9000px
@@ -241,6 +241,8 @@
background-image: -ms-linear-gradient(top, #3875d7 20%, #2a62bc 90%)
background-image: linear-gradient(top, #3875d7 20%, #2a62bc 90%)
color: #fff
+ a
+ color: #fff
li em
background: #feffde
font-style: normal
@@ -249,6 +251,8 @@
.no-results
background: #f4f4f4
display: list-item
+ .create-option
+ display: list-item
.group-result
cursor: default
color: #999
@@ -256,7 +260,7 @@
.group-option
padding-left: 15px
-.chzn-container-multi .result-selected
+.chzn-container-multi .chzn-drop .result-selected
display: none
.chzn-container
@@ -332,7 +336,7 @@
.chzn-disabled
cursor: default
opacity: 0.5 !important
- .chzn-single, .chzn-choices .search-choice-close
+ .chzn-single, .chzn-choices .search-choice .search-choice-close
cursor: default
/* @group Right to Left
@@ -370,7 +374,7 @@
.chzn-results .group-option
padding-left: 0
padding-right: 15px
- &.chzn-container-active div
+ &.chzn-container-active .chzn-single-with-drop div
border-right: none
.chzn-search input
background: white image-url('chosen-sprite.png') no-repeat -38px -22px

No commit comments for this range

Something went wrong with that request. Please try again.