Fixed issue 36838: multiple requests to load product image are performed
authorInigo Sanchez <inigo.sanchez@openbravo.com>
Wed, 18 Oct 2017 15:55:58 +0200
changeset 32870 94f7f5352304
parent 32869 8d578e0e9bbd
child 32871 b2d14bbc5437
Fixed issue 36838: multiple requests to load product image are performed

The problem was that multiple requests were performed when it shouldn't. When a product had an image, multiple
requests were being performed when the product was beeing displayed in form view and a refresh was being done.
Another problem discovered in the research of the issue was that a request was being performed when return from
FormView to the GridView. This request should not be performed because images are not showed in the grid view.

Both problems have been fixed by take into account when a request should be performed to retrieve an image. When
the image is loaded previously, the setValue of the ob-formitem-image avoids to performed an unnecessary request
to the server.
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-image.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-image.js	Wed Oct 18 09:10:51 2017 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/form/formitem/ob-formitem-image.js	Wed Oct 18 15:55:58 2017 +0200
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2011-2014 Openbravo SLU
+ * All portions are Copyright (C) 2011-2017 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -242,32 +242,34 @@
       this.canvas.setImage('');
     } else {
       this.canvas.setImage("../utility/ShowImage?id=" + newValue + '&nocache=' + Math.random());
-      var d = {
-        inpimageId: newValue,
-        command: 'GETSIZE'
-      };
-      var image = this.canvas.image;
-      var imageLayout = this.canvas.imageLayout;
-      OB.RemoteCallManager.call('org.openbravo.client.application.window.ImagesActionHandler', {}, d, function (response, data, request) {
-        var maxHeight = imageLayout.getHeight() - 12;
-        var maxWidth = imageLayout.getWidth() - 12;
-        var maxRatio = maxWidth / maxHeight;
+      if (this.getValue() !== newValue) {
+        var d = {
+          inpimageId: newValue,
+          command: 'GETSIZE'
+        };
+        var image = this.canvas.image;
+        var imageLayout = this.canvas.imageLayout;
+        OB.RemoteCallManager.call('org.openbravo.client.application.window.ImagesActionHandler', {}, d, function (response, data, request) {
+          var maxHeight = imageLayout.getHeight() - 12;
+          var maxWidth = imageLayout.getWidth() - 12;
+          var maxRatio = maxWidth / maxHeight;
 
-        var imgHeight = data.height;
-        var imgWidth = data.width;
-        var imgRatio = imgWidth / imgHeight;
+          var imgHeight = data.height;
+          var imgWidth = data.width;
+          var imgRatio = imgWidth / imgHeight;
 
-        if (imgHeight < maxHeight && imgWidth < maxWidth) {
-          image.setHeight(imgHeight);
-          image.setWidth(imgWidth);
-        } else if (imgRatio < maxRatio) {
-          image.setHeight(maxHeight);
-          image.setWidth(maxHeight * imgRatio);
-        } else {
-          image.setHeight(maxWidth / imgRatio);
-          image.setWidth(maxWidth);
-        }
-      });
+          if (imgHeight < maxHeight && imgWidth < maxWidth) {
+            image.setHeight(imgHeight);
+            image.setWidth(imgWidth);
+          } else if (imgRatio < maxRatio) {
+            image.setHeight(maxHeight);
+            image.setWidth(maxHeight * imgRatio);
+          } else {
+            image.setHeight(maxWidth / imgRatio);
+            image.setWidth(maxWidth);
+          }
+        });
+      }
     }
     //Buttons will not be shown if the form is readonly
     this.canvas.deleteButton.updateState(newValue && (this.form && !this.form.readOnly) && !this.disabled);