From 251310e2860536843185ac84d97f5b555c1b75bb Mon Sep 17 00:00:00 2001 From: Patrick Filler Date: Tue, 16 Jul 2013 14:52:48 -0400 Subject: [PATCH 01/10] Group ID's are no longer used. Stop adding them to the HTML --- coffee/lib/abstract-chosen.coffee | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/coffee/lib/abstract-chosen.coffee b/coffee/lib/abstract-chosen.coffee index e3ad75fbfcf..a008dabd60d 100644 --- a/coffee/lib/abstract-chosen.coffee +++ b/coffee/lib/abstract-chosen.coffee @@ -90,8 +90,7 @@ class AbstractChosen """
  • #{option.search_text}
  • """ result_add_group: (group) -> - group.dom_id = @container_id + "_g_" + group.array_index - """
  • #{group.search_text}
  • """ + """
  • #{group.search_text}
  • """ results_update_field: -> this.set_default_text() From 23cc35c6dbc9175628842e8515b054e8acb35c00 Mon Sep 17 00:00:00 2001 From: Patrick Filler Date: Tue, 16 Jul 2013 15:03:00 -0400 Subject: [PATCH 02/10] results_option_build is calle after a result is deselected so there's no need to manually update the visibility of an option. --- coffee/chosen.jquery.coffee | 3 --- coffee/chosen.proto.coffee | 3 --- 2 files changed, 6 deletions(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index e5fb086f7f6..4bcb93defe9 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -366,9 +366,6 @@ class Chosen extends AbstractChosen @form_field.options[result_data.options_index].selected = false @selected_option_count = null - result = $("#" + @container_id + "_o_" + pos) - result.removeClass("result-selected").addClass("active-result").show() - this.result_clear_highlight() this.winnow_results() if @results_showing diff --git a/coffee/chosen.proto.coffee b/coffee/chosen.proto.coffee index 3a1009024a0..5fbbe291ce4 100644 --- a/coffee/chosen.proto.coffee +++ b/coffee/chosen.proto.coffee @@ -353,9 +353,6 @@ class Chosen extends AbstractChosen @form_field.options[result_data.options_index].selected = false @selected_option_count = null - result = $(@container_id + "_o_" + pos) - result.removeClassName("result-selected").addClassName("active-result").show() - this.result_clear_highlight() this.winnow_results() if @results_showing From baba4622166310c9543a089ce2dcff89773be89a Mon Sep 17 00:00:00 2001 From: Patrick Filler Date: Tue, 16 Jul 2013 15:40:35 -0400 Subject: [PATCH 03/10] Remove id dependency from test_active_click --- coffee/chosen.jquery.coffee | 2 +- coffee/chosen.proto.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index 4bcb93defe9..20048c39802 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -152,7 +152,7 @@ class Chosen extends AbstractChosen test_active_click: (evt) -> - if $(evt.target).parents('#' + @container_id).length + if @container[0] is $(evt.target).parents('.chzn-container')[0] @active_field = true else this.close_field() diff --git a/coffee/chosen.proto.coffee b/coffee/chosen.proto.coffee index 5fbbe291ce4..81365b94459 100644 --- a/coffee/chosen.proto.coffee +++ b/coffee/chosen.proto.coffee @@ -140,7 +140,7 @@ class Chosen extends AbstractChosen @search_field.focus() test_active_click: (evt) -> - if evt.target.up('#' + @container_id) + if evt.target.up('.chzn-container') is @container @active_field = true else this.close_field() From 0d61a5a6999f86344449dc0fcbdd6f9c3f3a3831 Mon Sep 17 00:00:00 2001 From: Patrick Filler Date: Tue, 16 Jul 2013 15:59:51 -0400 Subject: [PATCH 04/10] Use a data- attribute to link an option with its chosen HTML element. Removing ID dependencies will allow us to remove some ugly code down the road. I like that. --- coffee/chosen.jquery.coffee | 4 +--- coffee/chosen.proto.coffee | 3 +-- coffee/lib/abstract-chosen.coffee | 2 +- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index 20048c39802..062f6a7c839 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -312,7 +312,6 @@ class Chosen extends AbstractChosen result_select: (evt) -> if @result_highlight high = @result_highlight - high_id = high.attr "id" this.result_clear_highlight() @@ -328,8 +327,7 @@ class Chosen extends AbstractChosen high.addClass "result-selected" - position = high_id.substr(high_id.lastIndexOf("_") + 1 ) - item = @results_data[position] + item = @results_data[ high[0].getAttribute("data-array_index") ] item.selected = true @form_field.options[item.options_index].selected = true diff --git a/coffee/chosen.proto.coffee b/coffee/chosen.proto.coffee index 81365b94459..84cef6dbb43 100644 --- a/coffee/chosen.proto.coffee +++ b/coffee/chosen.proto.coffee @@ -314,8 +314,7 @@ class Chosen extends AbstractChosen high.addClassName("result-selected") - position = high.id.substr(high.id.lastIndexOf("_") + 1 ) - item = @results_data[position] + item = @results_data[ high.getAttribute("data-array_index") ] item.selected = true @form_field.options[item.options_index].selected = true diff --git a/coffee/lib/abstract-chosen.coffee b/coffee/lib/abstract-chosen.coffee index a008dabd60d..bd8ae832e9a 100644 --- a/coffee/lib/abstract-chosen.coffee +++ b/coffee/lib/abstract-chosen.coffee @@ -87,7 +87,7 @@ class AbstractChosen style = if option.style.cssText != "" then " style=\"#{option.style}\"" else "" - """
  • #{option.search_text}
  • """ + """
  • #{option.search_text}
  • """ result_add_group: (group) -> """
  • #{group.search_text}
  • """ From 5eb560cc6ae00859c0dd2fa0e59e21b4b7f43830 Mon Sep 17 00:00:00 2001 From: Patrick Filler Date: Tue, 16 Jul 2013 16:06:08 -0400 Subject: [PATCH 05/10] No longer add an ID for each option. --- coffee/lib/abstract-chosen.coffee | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/coffee/lib/abstract-chosen.coffee b/coffee/lib/abstract-chosen.coffee index bd8ae832e9a..0863ce4e7b5 100644 --- a/coffee/lib/abstract-chosen.coffee +++ b/coffee/lib/abstract-chosen.coffee @@ -76,8 +76,6 @@ class AbstractChosen content result_add_option: (option) -> - option.dom_id = @container_id + "_o_" + option.array_index - classes = [] classes.push "active-result" if !option.disabled and !(option.selected and @is_multiple) classes.push "disabled-result" if option.disabled and !(option.selected and @is_multiple) @@ -87,7 +85,7 @@ class AbstractChosen style = if option.style.cssText != "" then " style=\"#{option.style}\"" else "" - """
  • #{option.search_text}
  • """ + """
  • #{option.search_text}
  • """ result_add_group: (group) -> """
  • #{group.search_text}
  • """ From 3bae381188b728bd675ad032f42438b17874fbb1 Mon Sep 17 00:00:00 2001 From: Patrick Filler Date: Tue, 16 Jul 2013 16:14:54 -0400 Subject: [PATCH 06/10] Stop generating random IDs for ID-less selects as ID is no longer a requirment for Chosen to function. If a select has an ID, give Chosen an appropriately named ID. If it doesn't have an ID, keep calm and carry on. --- coffee/chosen.jquery.coffee | 6 ++---- coffee/chosen.proto.coffee | 5 ++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index 062f6a7c839..982daab7947 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -23,20 +23,18 @@ class Chosen extends AbstractChosen @form_field_jq.addClass "chzn-done" set_up_html: -> - @container_id = if @form_field.id.length then @form_field.id.replace(/[^\w]/g, '_') else this.generate_field_id() - @container_id += "_chzn" - container_classes = ["chzn-container"] container_classes.push "chzn-container-" + (if @is_multiple then "multi" else "single") container_classes.push @form_field.className if @inherit_select_classes && @form_field.className container_classes.push "chzn-rtl" if @is_rtl container_props = - 'id': @container_id 'class': container_classes.join ' ' 'style': "width: #{this.container_width()};" 'title': @form_field.title + container_props.id = @form_field.id.replace(/[^\w]/g, '_') + "_chzn" if @form_field.id.length + @container = ($ "
    ", container_props) if @is_multiple diff --git a/coffee/chosen.proto.coffee b/coffee/chosen.proto.coffee index 84cef6dbb43..2415782619c 100644 --- a/coffee/chosen.proto.coffee +++ b/coffee/chosen.proto.coffee @@ -18,19 +18,18 @@ class Chosen extends AbstractChosen @no_results_temp = new Template('
  • ' + @results_none_found + ' "#{terms}"
  • ') set_up_html: -> - @container_id = @form_field.identify().replace(/[^\w]/g, '_') + "_chzn" - container_classes = ["chzn-container"] container_classes.push "chzn-container-" + (if @is_multiple then "multi" else "single") container_classes.push @form_field.className if @inherit_select_classes && @form_field.className container_classes.push "chzn-rtl" if @is_rtl container_props = - 'id': @container_id 'class': container_classes.join ' ' 'style': "width: #{this.container_width()};" 'title': @form_field.title + container_props.id = @form_field.id.replace(/[^\w]/g, '_') + "_chzn" if @form_field.id.length + @container = if @is_multiple then new Element('div', container_props).update( @multi_temp.evaluate({ "default": @default_text}) ) else new Element('div', container_props).update( @single_temp.evaluate({ "default":@default_text }) ) @form_field.hide().insert({ after: @container }) From 64ae33199101b4192183e8fe33494b729c8d057c Mon Sep 17 00:00:00 2001 From: Patrick Filler Date: Tue, 16 Jul 2013 17:47:21 -0400 Subject: [PATCH 07/10] Remove the (now unused) ID generation functionality. --- coffee/chosen.jquery.coffee | 6 ------ coffee/lib/abstract-chosen.coffee | 10 ---------- 2 files changed, 16 deletions(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index 982daab7947..e2dd0b6fd8d 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -482,11 +482,5 @@ class Chosen extends AbstractChosen w = @f_width - 10 @search_field.css({'width': w + 'px'}) - - generate_random_id: -> - string = "sel" + this.generate_random_char() + this.generate_random_char() + this.generate_random_char() - while $("#" + string).length > 0 - string += this.generate_random_char() - string root.Chosen = Chosen diff --git a/coffee/lib/abstract-chosen.coffee b/coffee/lib/abstract-chosen.coffee index 0863ce4e7b5..40a886e780f 100644 --- a/coffee/lib/abstract-chosen.coffee +++ b/coffee/lib/abstract-chosen.coffee @@ -194,16 +194,6 @@ class AbstractChosen # don't do anything on these keys else this.results_search() - generate_field_id: -> - new_id = this.generate_random_id() - @form_field.id = new_id - new_id - - generate_random_char: -> - chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" - rand = Math.floor(Math.random() * chars.length) - newchar = chars.substring rand, rand+1 - container_width: -> return if @options.width? then @options.width else "#{@form_field.offsetWidth}px" From c335d5fabb9a01dbd78618e1c492adfc0d1ae514 Mon Sep 17 00:00:00 2001 From: Patrick Filler Date: Tue, 16 Jul 2013 18:26:01 -0400 Subject: [PATCH 08/10] Use closest instead of parents because closest short circuits once it finds a result. --- coffee/chosen.jquery.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index e2dd0b6fd8d..004ab65846d 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -150,7 +150,7 @@ class Chosen extends AbstractChosen test_active_click: (evt) -> - if @container[0] is $(evt.target).parents('.chzn-container')[0] + if @container[0] is $(evt.target).closest('.chzn-container')[0] @active_field = true else this.close_field() From 37214687677e078cd2a9466ebef8fe2c0b231eb7 Mon Sep 17 00:00:00 2001 From: Patrick Filler Date: Tue, 16 Jul 2013 18:29:42 -0400 Subject: [PATCH 09/10] Rename array_index data attribute for clairty and conformity. --- coffee/chosen.jquery.coffee | 2 +- coffee/chosen.proto.coffee | 2 +- coffee/lib/abstract-chosen.coffee | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index 004ab65846d..733db183293 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -325,7 +325,7 @@ class Chosen extends AbstractChosen high.addClass "result-selected" - item = @results_data[ high[0].getAttribute("data-array_index") ] + item = @results_data[ high[0].getAttribute("data-option-array-index") ] item.selected = true @form_field.options[item.options_index].selected = true diff --git a/coffee/chosen.proto.coffee b/coffee/chosen.proto.coffee index 2415782619c..45e0176cb48 100644 --- a/coffee/chosen.proto.coffee +++ b/coffee/chosen.proto.coffee @@ -313,7 +313,7 @@ class Chosen extends AbstractChosen high.addClassName("result-selected") - item = @results_data[ high.getAttribute("data-array_index") ] + item = @results_data[ high.getAttribute("data-option-array-index") ] item.selected = true @form_field.options[item.options_index].selected = true diff --git a/coffee/lib/abstract-chosen.coffee b/coffee/lib/abstract-chosen.coffee index 40a886e780f..7426e099926 100644 --- a/coffee/lib/abstract-chosen.coffee +++ b/coffee/lib/abstract-chosen.coffee @@ -85,7 +85,7 @@ class AbstractChosen style = if option.style.cssText != "" then " style=\"#{option.style}\"" else "" - """
  • #{option.search_text}
  • """ + """
  • #{option.search_text}
  • """ result_add_group: (group) -> """
  • #{group.search_text}
  • """ From ad2de99dfcb7ffc55dc57451cf1cd60dfa68dbce Mon Sep 17 00:00:00 2001 From: Patrick Filler Date: Tue, 16 Jul 2013 21:51:45 -0400 Subject: [PATCH 10/10] .closest finds 0 or 1 closest matching parent - update syntax --- coffee/chosen.jquery.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index 733db183293..7b24e81346c 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -150,7 +150,7 @@ class Chosen extends AbstractChosen test_active_click: (evt) -> - if @container[0] is $(evt.target).closest('.chzn-container')[0] + if @container.is($(evt.target).closest('.chzn-container')) @active_field = true else this.close_field()