Author: mgrigorov
Date: Mon Nov 21 09:53:12 2011
New Revision: 1204424

URL: http://svn.apache.org/viewvc?rev=1204424&view=rev
Log:
WICKET-4236 Use JQuery as a backing library for Wicket's JavaScript code

Add Wicket#inCtx(fn, context) that replaces Function.bind and uses 
jQuery.proxy() as implementation


Modified:
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
    
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java
    
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/progressbar.js
    
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/modal/res/modal.js

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java?rev=1204424&r1=1204423&r2=1204424&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java
 Mon Nov 21 09:53:12 2011
@@ -177,14 +177,14 @@ public abstract class AbstractDefaultAja
                buff.append("var 
").append(IAjaxCallDecorator.WICKET_CALL_RESULT_VAR).append("=");
                buff.append(partialCall);
 
-               buff.append(",function() { 
").append(success).append("}.bind(this)");
-               buff.append(",function() { 
").append(failure).append("}.bind(this)");
+               buff.append(", Wicket.inCtx(function() { 
").append(success).append("}, this)");
+               buff.append(", Wicket.inCtx(function() { 
").append(failure).append("}, this)");
 
                if (precondition != null)
                {
-                       buff.append(", function() {");
+                       buff.append(", Wicket.inCtx(function() {");
                        buff.append(precondition);
-                       buff.append("}.bind(this)");
+                       buff.append("}, this)");
                }
 
                AjaxChannel channel = getChannel();
@@ -209,9 +209,9 @@ public abstract class AbstractDefaultAja
                                "if (");
                        if (precondition != null)
                        {
-                               indicatorWithPrecondition.append("function(){")
+                               
indicatorWithPrecondition.append("Wicket.inCtx(function(){")
                                        .append(precondition)
-                                       .append("}.bind(this)()");
+                                       .append("}, this)()");
                        }
                        else
                        {
@@ -327,8 +327,8 @@ public abstract class AbstractDefaultAja
                return new AppendingStringBuffer("Wicket.throttler.throttle( 
'").append(throttleId)
                        .append("', ")
                        .append(throttleDelay.getMilliseconds())
-                       .append(", function() { ")
+                       .append(", Wicket.inCtx(function() { ")
                        .append(script)
-                       .append("}.bind(this));");
+                       .append("}, this));");
        }
 }

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js?rev=1204424&r1=1204423&r2=1204424&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/wicket-ajax-jquery.js
 Mon Nov 21 09:53:12 2011
@@ -1052,6 +1052,17 @@ jQuery.noConflict();
                $$: function (element) {
                        return Wicket.DOM.inDoc(element);
                },
+               
+               /**
+                * Takes a function and returns a new one that will always have 
a particular context, i.e. 'this' will be the passed context.
+                * 
+                * @param {Function} fn - the function which context will be set
+                * @param {Object} context - the new context for the function
+                * @return {Function} the original function with the changed 
context
+                */
+               inCtx: function(fn, context) {
+                       return jQuery.proxy(fn, context);
+               },
 
                Xml: {
                        parse: function (text) {

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java?rev=1204424&r1=1204423&r2=1204424&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
 Mon Nov 21 09:53:12 2011
@@ -1129,7 +1129,7 @@ public class Form<T> extends WebMarkupCo
                buffer.append(defaultSubmittingComponent.getInputName());
                buffer.append("\" onclick=\" var b=document.getElementById('");
                buffer.append(submittingComponent.getMarkupId());
-               buffer.append("'); if 
(b!=null&amp;&amp;b.onclick!=null&amp;&amp;typeof(b.onclick) != 'undefined') {  
var r = b.onclick.bind(b)(); if (r != false) b.click(); } else { b.click(); };  
return false;\" ");
+               buffer.append("'); if 
(b!=null&amp;&amp;b.onclick!=null&amp;&amp;typeof(b.onclick) != 'undefined') {  
var r = Wicket.inCtx(b.onclick, b)(); if (r != false) b.click(); } else { 
b.click(); };  return false;\" ");
                buffer.append(" />");
 
                // close div

Modified: 
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java?rev=1204424&r1=1204423&r2=1204424&view=diff
==============================================================================
--- 
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java
 (original)
+++ 
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java
 Mon Nov 21 09:53:12 2011
@@ -222,7 +222,8 @@ public class UploadProgressBar extends P
                StringBuilder builder = new StringBuilder(128);
                Formatter formatter = new Formatter(builder);
 
-               formatter.format("new Wicket.WUPB('%s', '%s', '%s', '%s', '%s', 
'%s').bind('%s')",
+               formatter.format(
+                       "Wicket.inCtx(new Wicket.WUPB('%s', '%s', '%s', '%s', 
'%s', '%s'), Wicket.$('%s'))",
                        getMarkupId(), statusDiv.getMarkupId(), 
barDiv.getMarkupId(), url, uploadFieldId,
                        status, getCallbackForm().getMarkupId());
                response.renderOnDomReadyJavaScript(builder.toString());

Modified: 
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/progressbar.js
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/progressbar.js?rev=1204424&r1=1204423&r2=1204424&view=diff
==============================================================================
--- 
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/progressbar.js
 (original)
+++ 
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/progressbar.js
 Mon Nov 21 09:53:12 2011
@@ -31,7 +31,7 @@ Wicket.WUPB.prototype = {
        bind : function(formid) {
                formElement = Wicket.$(formid);
                this.originalCallback = formElement.onsubmit;
-               formElement.onsubmit = this.submitCallback.bind(this);
+               formElement.onsubmit = Wicket.inCtx(this.submitCallback, this);
        },
 
        submitCallback : function() {
@@ -73,7 +73,7 @@ Wicket.WUPB.prototype = {
        },
        
        scheduleUpdate : function(){
-               window.setTimeout(this.load.bind(this), 1000);
+               window.setTimeout(Wicket.inCtx(this.load, this), 1000);
        },
 
        load : function() {
@@ -83,7 +83,7 @@ Wicket.WUPB.prototype = {
                
        document.body.appendChild(this.iframe);
                
-               Wicket.Event.add(this.iframe, "load", this.update.bind(this));
+               Wicket.Event.add(this.iframe, "load", Wicket.inCtx(this.update, 
this));
                this.iframe.src = URL; 
        },
 

Modified: 
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/modal/res/modal.js
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/modal/res/modal.js?rev=1204424&r1=1204423&r2=1204424&view=diff
==============================================================================
--- 
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/modal/res/modal.js
 (original)
+++ 
wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/modal/res/modal.js
 Mon Nov 21 09:53:12 2011
@@ -302,7 +302,7 @@ Wicket.Window.prototype = { 
                
                        title: null, /* window title. if null and window 
content is iframe, title of iframe document will be used. */
                
-                       onCloseButton: function() {                             
+                       onCloseButton: Wicket.inCtx(function() {                
                
                                /* On firefox on Linux, at least, we need to 
blur() textfields, etc.
                                 * to get it to update its DOM model. Otherwise 
you'll lose any changes
                                 * made to the current form component you're 
editing.
@@ -311,7 +311,7 @@ Wicket.Window.prototype = { 
                                
this.caption.getElementsByTagName("a")[0].blur();
                                this.close();
                                return false;
-                       }.bind(this), /* called when close button is clicked */
+                       }, this), /* called when close button is clicked */
                        
                        onClose: function() { }, /* called when window is 
closed */
                
@@ -409,7 +409,7 @@ Wicket.Window.prototype = { 
         * Binds the handler to the drag event on given element.
         */
        bind: function(element, handler) {
-               Wicket.Drag.init(element, this.onBegin.bind(this), 
this.onEnd.bind(this), handler.bind(this)); 
+               Wicket.Drag.init(element, Wicket.inCtx(this.onBegin, this), 
Wicket.inCtx(this.onEnd, this), Wicket.inCtx(handler, this)); 
        },
 
        /**
@@ -445,7 +445,7 @@ Wicket.Window.prototype = { 
                        this.bind(this.top, this.onMove);
                }       
                                
-               this.caption.getElementsByTagName("a")[0].onclick = 
this.settings.onCloseButton.bind(this);
+               this.caption.getElementsByTagName("a")[0].onclick = 
Wicket.inCtx(this.settings.onCloseButton, this);
        },
 
        /**
@@ -617,13 +617,13 @@ Wicket.Window.prototype = { 
         */
        load: function() {
                if (this.settings.title == null)
-                       this.update = 
window.setInterval(this.updateTitle.bind(this), 100);
+                       this.update = 
window.setInterval(Wicket.inCtx(this.updateTitle, this), 100);
                
                // opera seems to have problem accessing contentWindow here
                if (Wicket.Browser.isOpera()) {
-                       this.content.onload = function() {
+                       this.content.onload = Wicket.inCtx(function() {
                                this.content.contentWindow.name = 
this.settings.iframeName;
-                       }.bind(this);
+                       }, this);
                } else {
                        this.content.contentWindow.name = 
this.settings.iframeName;
                }
@@ -698,11 +698,11 @@ Wicket.Window.prototype = { 
                // load position from cookie
                this.loadPosition();
 
-               var doShow = function() {
+               var doShow = Wicket.inCtx(function() {
                        this.adjustOpenWindowZIndexesOnShow();
                        this.window.style.visibility="visible";
                        
-               }.bind(this);
+               }, this);
                                
                this.adjustOpenWindowsStatusOnShow();
 
@@ -726,11 +726,11 @@ Wicket.Window.prototype = { 
                this.old_onunload = window.onunload;
                
                // new unload handler - close the window to prevent memory 
leaks in ie
-               window.onunload = function() {
+               window.onunload = Wicket.inCtx(function() {
                        this.close(true);
                        if (this.old_onunload != null)
                                return this.old_onunload();
-               }.bind(this);
+               }, this);
                
                // preserve old beforeunload handler
                this.old_onbeforeunload = window.onbeforeunload;
@@ -892,7 +892,7 @@ Wicket.Window.prototype = { 
                                        // konqueror doesn't refresh caption 
text properly
                                        if (Wicket.Browser.isKHTML()) {
                                                this.captionText.style.display 
= 'none';
-                                               window.setTimeout(function() { 
this.captionText.style.display="block";}.bind(this), 0);
+                                               
window.setTimeout(Wicket.inCtx(function() { 
this.captionText.style.display="block";}, this), 0);
                                        }
 
                                }
@@ -922,7 +922,7 @@ Wicket.Window.prototype = { 
                        this.revertList = null;
                        if (Wicket.Browser.isKHTML() || 
this.content.style.visibility=='hidden') {                      
                                this.content.style.visibility='hidden';
-                               window.setTimeout(function() { 
this.content.style.visibility='visible'; }.bind(this),  0 );
+                               window.setTimeout(Wicket.inCtx(function() { 
this.content.style.visibility='visible'; }, this),  0 );
                        }
                        this.revertList = null;
                }
@@ -1307,8 +1307,8 @@ Wicket.Window.Mask.prototype = {
                        this.old_onresize = window.onresize;
                        
                        // set new handlers
-                       window.onscroll = this.onScrollResize.bind(this);
-                       window.onresize = this.onScrollResize.bind(this);
+                       window.onscroll = Wicket.inCtx(this.onScrollResize, 
this);
+                       window.onresize = Wicket.inCtx(this.onScrollResize, 
this);
                        
                        // fix the mask position
                        this.onScrollResize(true);
@@ -1367,7 +1367,7 @@ Wicket.Window.Mask.prototype = {
        
        tasks: [],
        startTask: function (fn, delay) {
-               var taskId=setTimeout(function() { fn(); 
this.clearTask(taskId); }.bind(this), delay);
+               var taskId=setTimeout(Wicket.inCtx(function() { fn(); 
this.clearTask(taskId); }, this), delay);
                this.tasks.push(taskId);
        },
        clearTask: function (taskId) {
@@ -1391,9 +1391,9 @@ Wicket.Window.Mask.prototype = {
        // disable user interaction for content that is covered by the mask 
inside the given document, taking into consideration that this modal window is 
or not in an iframe
        // and has the given content
        doDisable: function(doc, win) {
-               this.startTask(function() {this.hideSelectBoxes(doc, 
win)}.bind(this), 300);
-               this.startTask(function() {this.disableTabs(doc, 
win)}.bind(this), 400);
-               this.startTask(function() {this.disableFocus(doc, 
win)}.bind(this), 1000);
+               this.startTask(Wicket.inCtx(function() 
{this.hideSelectBoxes(doc, win)}, this), 300);
+               this.startTask(Wicket.inCtx(function() {this.disableTabs(doc, 
win)}, this), 400);
+               this.startTask(Wicket.inCtx(function() {this.disableFocus(doc, 
win)}, this), 1000);
        },
        
        // reenable user interaction for content that was covered by the mask


Reply via email to