Skip to content

Commit

Permalink
add destroy method for DOM cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aehlke committed Oct 12, 2013
1 parent 41ad210 commit bd5bb36
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
4 changes: 3 additions & 1 deletion css/jquery.tagit.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ ul.tagit li.tagit-choice {
position: relative;
line-height: inherit;
}

input.tagit-hidden-field {
display: none;
}
ul.tagit li.tagit-choice-read-only {
padding: .2em .5em .2em .5em;
}
Expand Down
46 changes: 44 additions & 2 deletions js/tag-it.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
this.tagList = $('<ul></ul>').insertAfter(this.element);
this.options.singleField = true;
this.options.singleFieldNode = this.element;
this.element.css('display', 'none');
this.element.addClass('tagit-hidden-field');
} else {
this.tagList = this.element.find('ul, ol').andSelf().last();
}
Expand Down Expand Up @@ -302,6 +302,48 @@
}
},

destroy: function() {
$.Widget.prototype.destroy.call(this);

this.tagList.removeClass([
'tagit',
'ui-widget',
'ui-widget-content',
'ui-corner-all',
'tagit-hidden-field'
].join(' '));

if (this.element.is('input')) {
this.element.removeClass('tagit-hidden-field');
this.tagList.remove();
} else {
this.element.children('li').each(function() {
if ($(this).hasClass('tagit-new')) {
$(this).remove();
} else {
$(this).removeClass([
'tagit-choice',
'ui-widget-content',
'ui-state-default',
'ui-state-highlight',
'ui-corner-all',
'remove',
'tagit-choice-editable',
'tagit-choice-read-only'
].join(' '));

$(this).text($(this).children('.tagit-label').text());
}
});

if (this.singleFieldNode) {
this.singleFieldNode.remove();
}
}

return this;
},

_cleanedInput: function() {
// Returns the contents of the tag input, cleaned and ready to be passed to createTag
return $.trim(this.tagInput.val().replace(/^"(.*)"$/, '$1'));
Expand Down Expand Up @@ -446,7 +488,7 @@
// Unless options.singleField is set, each tag has a hidden input field inline.
if (!this.options.singleField) {
var escapedValue = label.html();
tag.append('<input type="hidden" style="display:none;" value="' + escapedValue + '" name="' + this.options.fieldName + '" />');
tag.append('<input type="hidden" value="' + escapedValue + '" name="' + this.options.fieldName + '" class="tagit-hidden-field" />');
}

if (this._trigger('beforeTagAdded', null, {
Expand Down
21 changes: 11 additions & 10 deletions js/tag-it.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bd5bb36

Please sign in to comment.