Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "destroy" method to allow for easy clean up of initialized popups. #44

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,13 @@ <h3>Methods</h3>
<pre class="prettyprint"><code>$('#my_popup').popup('toggle');</code></pre>
</td>
</tr>
<tr>
<td>.popup('destroy')</td>
<td>
<p>Removes elements and events created during popup initialization.</p>
<pre class="prettyprint"><code>$('#my_popup').popup('destroy');</code></pre>
</td>
</tr>
</tbody>
</table>

Expand Down
41 changes: 39 additions & 2 deletions jquery.popupoverlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@
if (options.transition) {
$background.css('transition', options.transition);
}

$el.data('popupBackground', $background);
}

if (options.type == 'overlay') {
Expand Down Expand Up @@ -221,14 +223,19 @@
} else {

// Handler: Show popup when clicked on `open` element
$(document).on('click', openelement, function (event) {
var popupClickHandler = function (event) {
event.preventDefault();

var ord = $(this).data('popup-ordinal');
setTimeout(function() { // setTimeout is to allow `close` method to finish (for issues with multiple tooltips)
methods.show(el, ord);
}, 0);
});
};

$(document).on('click', openelement, popupClickHandler);

$el.data('popupOpenelement', openelement);
$el.data('popupClickHandler', popupClickHandler);
}

if (options.closebutton) {
Expand All @@ -242,6 +249,36 @@
}
},

/**
* Cleans up elements and event listeners
*
* @param {object} el - popup instance DOM node
*/
destroy: function (el) {
var $el = $(el);
var options = $el.data('popupoptions');
var popupBackground = $el.data('popupBackground');
var openelement = $el.data('popupOpenelement');
var popupClickHandler = $el.data('popupClickHandler');
var $wrapper = $('#' + el.id + '_wrapper');

// remove background
if (popupBackground) {
popupBackground.remove();
}

// remove click listener
if (popupClickHandler) {
$(document).off('click', openelement, popupClickHandler);
}

// remove wrapper
$wrapper.remove();

// remove initialization marker to allow popup to be initialize again
$el.data('popup-initialized', false);
},

/**
* Show method
*
Expand Down