Skip to content

Commit

Permalink
Merge pull request #1396 from harvesthq/koenpunt-chosen-destroy
Browse files Browse the repository at this point in the history
Add Destroy Method
  • Loading branch information
pfiller committed Jul 26, 2013
2 parents 77efc4f + 165de1b commit 5d22658
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 26 deletions.
67 changes: 41 additions & 26 deletions coffee/chosen.jquery.coffee
Expand Up @@ -5,10 +5,16 @@ $.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 is 'destroy' && chosen
chosen.destroy()
else unless chosen
$this.data('chosen', new Chosen(this, options))

return

})

class Chosen extends AbstractChosen
Expand Down Expand Up @@ -63,41 +69,50 @@ 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

destroy: ->
$(document).unbind "click.chosen", @click_test_action
if @search_field[0].tabIndex
@form_field_jq[0].tabIndex = @search_field[0].tabIndex

@container.remove()
@form_field_jq.removeData('chosen')
@form_field_jq.show()

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
Expand All @@ -107,7 +122,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()
Expand All @@ -129,7 +144,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()
Expand Down Expand Up @@ -242,7 +257,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
Expand Down Expand Up @@ -273,7 +288,7 @@ class Chosen extends AbstractChosen
choice.addClass 'search-choice-disabled'
else
close_link = $('<a />', { class: 'search-choice-close', 'data-option-array-index': 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
Expand Down
22 changes: 22 additions & 0 deletions coffee/chosen.proto.coffee
Expand Up @@ -77,6 +77,28 @@ class @Chosen extends AbstractChosen
else
@container.observe "click", (evt) => evt.preventDefault() # gobble click of anchor

destroy: ->
document.stopObserving "click", @click_test_action

@form_field.stopObserving()
@container.stopObserving()
@search_results.stopObserving()
@search_field.stopObserving()
@form_field_label.stopObserving() if @form_field_label?

if @is_multiple
@search_choices.stopObserving()
@container.select(".search-choice-close").each (choice) ->
choice.stopObserving()
else
@selected_item.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)
Expand Down

0 comments on commit 5d22658

Please sign in to comment.