Updated Branches: refs/heads/wicket-6.x 0f0be95c8 -> e7dd6ea38
WICKET-5469 ModalWindow unload warning always displayed (even if window is closed) Use event listeners instead of property based event handler for onbeforeunload. We don't do the same for 'onunload' because event listeners can be skipped with event.stopImmediatePropagation() and this may lead to memory leaks in IE. Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/e7dd6ea3 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/e7dd6ea3 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/e7dd6ea3 Branch: refs/heads/wicket-6.x Commit: e7dd6ea3895ed6e19d7850a25d846cebb5551d10 Parents: 0f0be95 Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Wed Jan 15 11:40:42 2014 +0200 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Wed Jan 15 11:46:26 2014 +0200 ---------------------------------------------------------------------- .../wicket/ajax/res/js/wicket-event-jquery.js | 26 +++++++++++++++++--- wicket-core/src/test/js/event.js | 20 +++++++++++++++ .../ajax/markup/html/modal/res/modal.js | 16 +++++------- 3 files changed, 49 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/e7dd6ea3/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-event-jquery.js ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-event-jquery.js b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-event-jquery.js index 9529761..8827cde 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-event-jquery.js +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-event-jquery.js @@ -137,9 +137,18 @@ jQuery(element).trigger(event); }, - // adds an event of specified type to the element - // also supports the domready event on window - // domready is event fired when the DOM is complete, but before loading external resources (images, ...) + /** + * Binds an event listener for an element + * + * Also supports the special 'domready' event on window. + * 'domready' is event fired when the DOM is complete, but + * before loading external resources (images, scripts, ...) + * + * @param element {HTMLElement} The host HTML element + * @param type {String} The type of the DOM event + * @param fn {Function} The event handler to unbind + * @param data {Object} Extra data for the event + */ add: function (element, type, fn, data) { if (type === 'domready') { jQuery(fn); @@ -163,6 +172,17 @@ }, /** + * Unbinds an event listener for an element + * + * @param element {HTMLElement} The host HTML element + * @param type {String} The type of the DOM event + * @param fn {Function} The event handler to unbind + */ + remove: function (element, type, fn) { + jQuery(element).off(type, fn); + }, + + /** * Adds a subscriber for the passed topic. * * @param topic {String} - the channel name for which this subscriber will be notified http://git-wip-us.apache.org/repos/asf/wicket/blob/e7dd6ea3/wicket-core/src/test/js/event.js ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/js/event.js b/wicket-core/src/test/js/event.js index bb3cde5..b2e908c 100644 --- a/wicket-core/src/test/js/event.js +++ b/wicket-core/src/test/js/event.js @@ -133,6 +133,26 @@ jQuery(document).ready(function() { $el.remove(); }); + test('remove - any event', function () { + + expect(1); + + var $el = jQuery('<div id="addTestId">element body</div>'); + $el.appendTo(jQuery('#qunit-fixture')); + + var handler = function() { + ok(true, 'This event must be fired!'); + }; + + var el = $el[0]; + Wicket.Event.add(el, 'click', handler); + + Wicket.Event.fire(el, 'click'); + + Wicket.Event.remove(el, 'click', handler); + + Wicket.Event.fire(el, 'click'); + }); test('add - mousewheel', function () { http://git-wip-us.apache.org/repos/asf/wicket/blob/e7dd6ea3/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/modal/res/modal.js ---------------------------------------------------------------------- diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/modal/res/modal.js b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/modal/res/modal.js index f3e51c8..65eebf8 100644 --- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/modal/res/modal.js +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/modal/res/modal.js @@ -738,19 +738,17 @@ // Wicket.Window.unloadConfirmation is deprecated but we need to check it // for backward compatibility. Remove it after Wicket 7.0 if (this.settings.unloadConfirmation && Wicket.Window.unloadConfirmation) { - // preserve old beforeunload handler - this.old_onbeforeunload = window.onbeforeunload; - - // new beforeunload handler - ask user before reloading window - window.onbeforeunload = function() { - return "Reloading this page will cause the modal window to disappear."; - }; + Wicket.Event.add(window, 'beforeunload',this.onbeforeunload); } // create the mask that covers the background this.createMask(); }, + onbeforeunload: function() { + return "Reloading this page will cause the modal window to disappear."; + }, + adjustOpenWindowZIndexesOnShow: function() { // if there is a previous window if (this.oldWindow) { @@ -834,9 +832,7 @@ window.onunload = this.old_onunload; this.old_onunload = null; - // restore old beforeunload handler - window.onbeforeunload = this.old_onbeforeunload; - this.old_onbeforeunload = null; + Wicket.Event.remove(window, 'beforeunload',this.onbeforeunload); // hids and cleanup the mask this.destroyMask();
