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/79c7cb29
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/79c7cb29
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/79c7cb29

Branch: 
refs/heads/sandbox/preserve-page-parameters-and-execute-listener-interface-after-expiration
Commit: 79c7cb297687a0f214a8531818ce947ccabc0074
Parents: ca4bfba
Author: Martin Tzvetanov Grigorov <mgrigo...@apache.org>
Authored: Wed Jan 15 11:40:42 2014 +0200
Committer: Martin Tzvetanov Grigorov <mgrigo...@apache.org>
Committed: Wed Jan 15 11:40:42 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/79c7cb29/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/79c7cb29/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 e180b4f..9ccba5e 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/79c7cb29/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 7a66c92..159b4b5 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
@@ -732,19 +732,17 @@
                        }, this);
 
                        if (this.settings.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) {
@@ -828,9 +826,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();

Reply via email to