From 0e1f919619f317e0ab433c568f7f82711dc614d7 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Sat, 20 Jul 2013 23:09:44 +0200 Subject: [PATCH 1/6] bind all events with namespace --- coffee/chosen.jquery.coffee | 46 ++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index 8b8f843dbe0..58492534230 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -63,41 +63,41 @@ class Chosen extends AbstractChosen @form_field_jq.trigger("liszt:ready", {chosen: this}) register_observers: -> - @container.mousedown (evt) => this.container_mousedown(evt); return - @container.mouseup (evt) => this.container_mouseup(evt); return - @container.mouseenter (evt) => this.mouse_enter(evt); return - @container.mouseleave (evt) => this.mouse_leave(evt); return + @container.bind 'mousedown.chosen', (evt) => this.container_mousedown(evt); return + @container.bind 'mouseup.chosen', (evt) => this.container_mouseup(evt); return + @container.bind 'mouseenter.chosen', (evt) => this.mouse_enter(evt); return + @container.bind 'mouseleave.chosen', (evt) => this.mouse_leave(evt); return - @search_results.mouseup (evt) => this.search_results_mouseup(evt); return - @search_results.mouseover (evt) => this.search_results_mouseover(evt); return - @search_results.mouseout (evt) => this.search_results_mouseout(evt); return - @search_results.bind 'mousewheel DOMMouseScroll', (evt) => this.search_results_mousewheel(evt); return + @search_results.bind 'mouseup.chosen', (evt) => this.search_results_mouseup(evt); return + @search_results.bind 'mouseover.chosen', (evt) => this.search_results_mouseover(evt); return + @search_results.bind 'mouseout.chosen', (evt) => this.search_results_mouseout(evt); return + @search_results.bind 'mousewheel.chosen DOMMouseScroll.chosen', (evt) => this.search_results_mousewheel(evt); return - @form_field_jq.bind "liszt:updated", (evt) => this.results_update_field(evt); return - @form_field_jq.bind "liszt:activate", (evt) => this.activate_field(evt); return - @form_field_jq.bind "liszt:open", (evt) => this.container_mousedown(evt); return + @form_field_jq.bind "liszt:updated.chosen", (evt) => this.results_update_field(evt); return + @form_field_jq.bind "liszt:activate.chosen", (evt) => this.activate_field(evt); return + @form_field_jq.bind "liszt:open.chosen", (evt) => this.container_mousedown(evt); return - @search_field.blur (evt) => this.input_blur(evt); return - @search_field.keyup (evt) => this.keyup_checker(evt); return - @search_field.keydown (evt) => this.keydown_checker(evt); return - @search_field.focus (evt) => this.input_focus(evt); return + @search_field.bind 'blur.chosen', (evt) => this.input_blur(evt); return + @search_field.bind 'keyup.chosen', (evt) => this.keyup_checker(evt); return + @search_field.bind 'keydown.chosen', (evt) => this.keydown_checker(evt); return + @search_field.bind 'focus.chosen', (evt) => this.input_focus(evt); return if @is_multiple - @search_choices.click (evt) => this.choices_click(evt); return + @search_choices.bind 'click.chosen', (evt) => this.choices_click(evt); return else - @container.click (evt) => evt.preventDefault(); return # gobble click of anchor + @container.bind 'click.chosen', (evt) => evt.preventDefault(); return # gobble click of anchor search_field_disabled: -> @is_disabled = @form_field_jq[0].disabled if(@is_disabled) @container.addClass 'chzn-disabled' @search_field[0].disabled = true - @selected_item.unbind "focus", @activate_action if !@is_multiple + @selected_item.unbind "focus.chosen", @activate_action if !@is_multiple this.close_field() else @container.removeClass 'chzn-disabled' @search_field[0].disabled = false - @selected_item.bind "focus", @activate_action if !@is_multiple + @selected_item.bind "focus.chosen", @activate_action if !@is_multiple container_mousedown: (evt) -> if !@is_disabled @@ -107,7 +107,7 @@ class Chosen extends AbstractChosen if not (evt? and ($ evt.target).hasClass "search-choice-close") if not @active_field @search_field.val "" if @is_multiple - $(document).click @click_test_action + $(document).bind 'click.chosen', @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) evt.preventDefault() @@ -129,7 +129,7 @@ class Chosen extends AbstractChosen this.close_field() if not @active_field and @container.hasClass "chzn-container-active" close_field: -> - $(document).unbind "click", @click_test_action + $(document).unbind "click.chosen", @click_test_action @active_field = false this.results_hide() @@ -242,7 +242,7 @@ class Chosen extends AbstractChosen @form_field_label = $("label[for='#{@form_field.id}']") #next check for a for=#{id} if @form_field_label.length > 0 - @form_field_label.click (evt) => if @is_multiple then this.container_mousedown(evt) else this.activate_field() + @form_field_label.bind 'click.chosen', (evt) => if @is_multiple then this.container_mousedown(evt) else this.activate_field() show_search_field_default: -> if @is_multiple and this.choices_count() < 1 and not @active_field @@ -273,7 +273,7 @@ class Chosen extends AbstractChosen choice.addClass 'search-choice-disabled' else close_link = $('', { href: '#', class: 'search-choice-close', rel: item.array_index }) - close_link.click (evt) => this.choice_destroy_link_click(evt) + close_link.bind 'click.chosen', (evt) => this.choice_destroy_link_click(evt) choice.append close_link @search_container.before choice From 32a824e4e386c0f4ad54338b2317cf1d9023aa98 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Sat, 20 Jul 2013 22:54:55 +0200 Subject: [PATCH 2/6] Added destroy method for jquery version --- coffee/chosen.jquery.coffee | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index 58492534230..3273247d0ac 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -5,10 +5,14 @@ $.fn.extend({ # Do no harm and return as soon as possible for unsupported browsers, namely IE6 and IE7 # Continue on if running IE document type but in compatibility mode return this unless AbstractChosen.browser_is_supported() - this.each((input_field) -> + this.each (input_field) -> $this = $ this - $this.data('chosen', new Chosen(this, options)) unless $this.hasClass "chzn-done" - ) + chosen = $this.data('chosen') + if options == 'destroy' + return chosen.destroy() if chosen + else + return $this.data('chosen', new Chosen(this, options)) unless chosen + }) class Chosen extends AbstractChosen @@ -87,6 +91,22 @@ class Chosen extends AbstractChosen else @container.bind 'click.chosen', (evt) => evt.preventDefault(); return # gobble click of anchor + destroy: -> + $(document).unbind '.chosen' + @container.unbind '.chosen' + @search_results.unbind '.chosen' + @form_field_jq.unbind '.chosen' + @search_field.unbind '.chosen' + if @is_multiple + @search_choices.unbind '.chosen' + + if @search_field.attr "tabindex" + @form_field_jq.attr "tabindex", @search_field.attr("tabindex") + + @container.remove() + @form_field_jq.data('chosen', null) + @form_field_jq.show() + search_field_disabled: -> @is_disabled = @form_field_jq[0].disabled if(@is_disabled) From f35e213e684e43778f12c858c830c872669cfeed Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Sat, 20 Jul 2013 23:53:50 +0200 Subject: [PATCH 3/6] destroy method for proto version --- coffee/chosen.proto.coffee | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/coffee/chosen.proto.coffee b/coffee/chosen.proto.coffee index 5c86705bdbc..72547f5edb1 100644 --- a/coffee/chosen.proto.coffee +++ b/coffee/chosen.proto.coffee @@ -77,6 +77,19 @@ class @Chosen extends AbstractChosen else @container.observe "click", (evt) => evt.preventDefault() # gobble click of anchor + destroy: -> + @container.stopObserving() + @search_results.stopObserving() + @search_field.stopObserving() + if @is_multiple + @search_choices.stopObserving() + + if @search_field.tabIndex + @form_field.tabIndex = @search_field.tabIndex + + @container.remove() + @form_field.show() + search_field_disabled: -> @is_disabled = @form_field.disabled if(@is_disabled) From b9a0b997b7ccd8415db836765dda621605e95834 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Mon, 22 Jul 2013 21:55:01 +0200 Subject: [PATCH 4/6] use remove data --- 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 3273247d0ac..c61d34cad7f 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -104,7 +104,7 @@ class Chosen extends AbstractChosen @form_field_jq.attr "tabindex", @search_field.attr("tabindex") @container.remove() - @form_field_jq.data('chosen', null) + @form_field_jq.removeData('chosen') @form_field_jq.show() search_field_disabled: -> From 0e7f8a5a68c66c188c5503d2052e9947bd31d379 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Mon, 22 Jul 2013 22:18:50 +0200 Subject: [PATCH 5/6] always return $this no need for separate unbinding unbind event from document in proto version --- coffee/chosen.jquery.coffee | 22 ++++++++-------------- coffee/chosen.proto.coffee | 1 + 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index c61d34cad7f..f0f8b02f121 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -8,10 +8,12 @@ $.fn.extend({ this.each (input_field) -> $this = $ this chosen = $this.data('chosen') - if options == 'destroy' - return chosen.destroy() if chosen - else - return $this.data('chosen', new Chosen(this, options)) unless chosen + if options is 'destroy' && chosen + chosen.destroy() + else unless chosen + $this.data('chosen', new Chosen(this, options)) + + $this }) @@ -92,16 +94,8 @@ class Chosen extends AbstractChosen @container.bind 'click.chosen', (evt) => evt.preventDefault(); return # gobble click of anchor destroy: -> - $(document).unbind '.chosen' - @container.unbind '.chosen' - @search_results.unbind '.chosen' - @form_field_jq.unbind '.chosen' - @search_field.unbind '.chosen' - if @is_multiple - @search_choices.unbind '.chosen' - - if @search_field.attr "tabindex" - @form_field_jq.attr "tabindex", @search_field.attr("tabindex") + if @search_field[0].tabIndex + @form_field_jq[0].tabIndex = @search_field[0].tabIndex @container.remove() @form_field_jq.removeData('chosen') diff --git a/coffee/chosen.proto.coffee b/coffee/chosen.proto.coffee index 72547f5edb1..be80d7bfb1f 100644 --- a/coffee/chosen.proto.coffee +++ b/coffee/chosen.proto.coffee @@ -78,6 +78,7 @@ class @Chosen extends AbstractChosen @container.observe "click", (evt) => evt.preventDefault() # gobble click of anchor destroy: -> + document.stopObserving "click", @click_test_action @container.stopObserving() @search_results.stopObserving() @search_field.stopObserving() From b8d3b7196ae8312eda0a33355aa887536bed610f Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Wed, 24 Jul 2013 09:23:47 +0200 Subject: [PATCH 6/6] unbind document event plain return --- coffee/chosen.jquery.coffee | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index f0f8b02f121..8dc0005ff81 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -13,7 +13,7 @@ $.fn.extend({ else unless chosen $this.data('chosen', new Chosen(this, options)) - $this + return }) @@ -91,9 +91,10 @@ class Chosen extends AbstractChosen if @is_multiple @search_choices.bind 'click.chosen', (evt) => this.choices_click(evt); return else - @container.bind 'click.chosen', (evt) => evt.preventDefault(); return # gobble click of anchor + @container.bind 'click.chosen', (evt) -> evt.preventDefault(); return # gobble click of anchor destroy: -> + $(document).unbind "click.chosen", @click_test_action if @search_field[0].tabIndex @form_field_jq[0].tabIndex = @search_field[0].tabIndex