Author: jleroux
Date: Sat Jan 29 16:11:48 2011
New Revision: 1065056

URL: http://svn.apache.org/viewvc?rev=1065056&view=rev
Log:
A patch from Leon "Infinite lookup ajax calls if key enter on the page with 
layer lookup forms" (https://issues.apache.org/jira/browse/OFBIZ-4156) - 
OFBIZ-4156

If the rendered page has one lookup field which is configured as layer mode, at 
first open the lookup dialog and click the empty space of the page to hide it, 
then, key the "enter", then in the console log you will find there's lookup 
request logged. Try to key "enter" serveral times, you will get more lookup 
requests in the backend. In particular case, even if you key one "enter" in, it 
will send hundrends lookup calls. See attached "infinite-lookups.log" for the 
log details.

Cause:
In modifySubmitButton of fieldlookup.js, it bind the keypress event to whole 
"document" to call "lookupFormAjaxRequest" when "enter" keyin. And in 
"lookupFormAjaxRequest" function, it call "modifySubmitButton" again after data 
load.

Resolution:
Move the keypress event bind operation to dialog open process and unbind it 
when dialog is closed.

Besides to fix what have mentioned, also make some trivial improvements to 
modifySubmitButton method:
1. in jquery, "contains" is not a valid method of String prototype, use 
"indexOf(blah) != -1" instead
2. bypass the "javascript:..." link in lookup result table. Although it's not a 
good solution, it makes more sence than "to skip the first Entry of the row".

JLR: I kept Leon's last point solution as anyway it's not a big deal to 
reassign for few rows (ie it's by page). This avoid issue if the 1st cell in 
the row is not the id/link
 I just ketp javascript:set_ in the indexOf( instead of simply javascript:

Modified:
    ofbiz/trunk/framework/images/webapp/images/fieldlookup.js

Modified: ofbiz/trunk/framework/images/webapp/images/fieldlookup.js
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/images/webapp/images/fieldlookup.js?rev=1065056&r1=1065055&r2=1065056&view=diff
==============================================================================
--- ofbiz/trunk/framework/images/webapp/images/fieldlookup.js (original)
+++ ofbiz/trunk/framework/images/webapp/images/fieldlookup.js Sat Jan 29 
16:11:48 2011
@@ -233,6 +233,14 @@ function ConstructLookup(requestUrl, inp
         positioning = ['left', 'top'];
     }
     
+    var lookupFormAction = null;
+    function lookup_onKeyEnter(event) {
+        if (event.which == 13) {
+            lookupFormAjaxRequest(lookupFormAction, "form_" + lookupId);
+            return false;
+        }
+    }
+    
     // Lookup Configuration
     var dialogOpts = {
         modal: (modal == "true") ? true : false,
@@ -251,7 +259,9 @@ function ConstructLookup(requestUrl, inp
                 }
             }
             jQuery("#" + lookupId).load(requestUrlAndArgs, function(data){ 
+                lookupFormAction = jQuery("#" + lookupId + " 
form:first").attr("action");
                 modifySubmitButton(lookupId);
+                jQuery(document).bind("keypress", lookup_onKeyEnter);
                 // set up the window chaining
                 // if the ACTIVATED_LOOKUP var is set there have to be more 
than one lookup,
                 // before registrating the new lookup we store the id of the 
old lookup in the
@@ -269,6 +279,7 @@ function ConstructLookup(requestUrl, inp
             });
         },
         close: function() {
+            jQuery(document).unbind("keypress", lookup_onKeyEnter);
             //when the window is closed the prev Lookup get the focus (if 
exists)
             if (ACTIVATED_LOOKUP) {
                 var prevLookup = 
GLOBAL_LOOKUP_REF.getReference(ACTIVATED_LOOKUP).prevLookup;
@@ -454,14 +465,6 @@ function modifySubmitButton (lookupDiv) 
         }));
 
         input.remove();
-
-        jQuery(document).bind("keypress", function (event) {
-            if (event.which == 13) {
-                lookupFormAjaxRequest(formAction, lookupForm.attr("id"));
-                return false;
-            }
-        });
-
         //modify nav-pager
         var navPagers = jQuery("#" + lookupDiv + " .nav-pager a");
         jQuery.each(navPagers, function(navPager) {
@@ -526,19 +529,17 @@ function modifySubmitButton (lookupDiv) 
                     if (childElements.size() == 1) {
                         continue;
                     }
-                    
                     for (k = 1; k < childElements.size(); k++) {
                         var cell = childElements[k];
                         var cellChild = null;
                         cellChild = cell.childElements();
                         if (cellChild.size() > 0) {
-                            
                             for (l in cellChild) {
                                 var cellElement = cellChild[l];
                                 if (cellElement.tagName == 'A') {
                                     var link = cellElement.href;
                                     var liSub = 
link.substring(link.lastIndexOf('/')+1,(link.length));
-                                    if (liSub.contains("javascript:set_")) {
+                                    if (liSub.indexOf("javascript:set_") != 
-1) {
                                         cellElement.href = liSub;
                                     } else {
                                         cellElement.href = 
"javascript:lookupAjaxRequest('" + liSub + "&presentation=layer')";
@@ -559,16 +560,17 @@ function modifySubmitButton (lookupDiv) 
             var childElements = jQuery(tableChildren[tableChild]);
             var tableRow = childElements.children();
             jQuery.each(tableRow, function(cell){
-                //to skip the first Entry of the row, because it's the normal 
id link
-                if (cell == 0) return true;
-
                 var cellChild = null;
                 cellChild = jQuery(tableRow[cell]).children();
                 jQuery.each(cellChild, function (child) {
                     if (cellChild[child].tagName == "A"){
                         var link = cellChild[child].href;
                         var liSub = 
link.substring(link.lastIndexOf('/')+1,(link.length));
-                        cellChild[child].href = 
"javascript:lookupAjaxRequest('" + liSub + "&presentation=layer')";
+                        if (liSub.indexOf("javascript:set_") != -1) {
+                            cellChild[child].href = liSub;
+                        } else {
+                            cellChild[child].href = 
"javascript:lookupAjaxRequest('" + liSub + "&presentation=layer')";
+                        }
                     }
                 });
 


Reply via email to