Related to issue 19327: code improvement
authorDavid Baz Fayos <david.baz@openbravo.com>
Mon, 19 Dec 2011 13:56:18 +0100
changeset 15072 dc800102fd82
parent 15071 e3c9c9b1424f
child 15073 a986de790b08
Related to issue 19327: code improvement
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/toolbar/ob-toolbar.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/toolbar/ob-toolbar.js	Sun Dec 18 20:58:28 2011 +0100
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/toolbar/ob-toolbar.js	Mon Dec 19 13:56:18 2011 +0100
@@ -231,29 +231,62 @@
   ATTACHMENTS_BUTTON_PROPERTIES: {
     action: function(){
       var selectedRows = this.view.viewGrid.getSelectedRecords(),
-          attachmentExists = this.view.attachmentExists, i;
+          attachmentExists = this.view.attachmentExists,
+          attachmentSection = this.view.viewForm.getItem('_attachments_'),
+          me = this, i;
       if(this.view.isShowingForm){
-        if (!this.view.viewForm.getItem('_attachments_').isExpanded()) {
-          this.view.viewForm.getItem('_attachments_').expandSection();
+        if (!attachmentSection.isExpanded()) {
+          attachmentSection.expandSection();
         }
-        this.view.viewForm.getItem('_attachments_').focusInItem();
+        attachmentSection.focusInItem();
         if (this.view.viewForm.parentElement) {
           // scroll after things have been expanded
-          this.view.viewForm.parentElement.delayCall('scrollTo', [null, this.view.viewForm.getItem('_attachments_').getTop()], 100);
+          this.view.viewForm.parentElement.delayCall('scrollTo', [null, attachmentSection.getTop()], 100);
         }
 
         if(!attachmentExists){
-          this.view.viewForm.getItem('_attachments_').attachmentCanvasItem.canvas.getMember(0).getMember(0).click();
+          attachmentSection.attachmentCanvasItem.canvas.getMember(0).getMember(0).click();
         }
         return;
       }
       if(selectedRows.size() === 1){
-        this.view.viewForm.setFocusItem(this.view.viewForm.getItem('_attachments_'));
+        this.view.viewForm.setFocusItem(attachmentSection);
         this.view.viewForm.forceFocusedField = '_attachments_';
         this.view.viewForm.expandAttachments = true;
         this.view.editRecord(selectedRows[0]);
+
+        // Move from grid view to form view could take a while.
+        // Section needs to be expanded before the viewport adjustment.
+        var expandedCount = 0, expandedInterval;
+        expandedInterval = setInterval(function() {
+          expandedCount += 1;
+          if(attachmentSection.isExpanded()){
+            me.view.viewForm.parentElement.scrollTo(null, attachmentSection.getTop());
+            clearInterval(expandedInterval);
+          }
+          if (expandedCount === 50) {
+            clearInterval(expandedInterval);
+          }
+        }, 100);
+
         if(!attachmentExists){
-          this.view.viewForm.getItem('_attachments_').attachmentCanvasItem.canvas.getMember(0).getMember(0).click();
+          if (attachmentSection.attachmentCanvasItem.canvas.getMember(0)) {
+            attachmentSection.attachmentCanvasItem.canvas.getMember(0).getMember(0).click();
+          } else {
+            // The first time the form view is loaded, the section is not already built and it could take a while to be.
+            // Section needs to be built before the click event.
+            var clickCount = 0, clickInterval;
+            clickInterval = setInterval(function() {
+              clickCount += 1;
+              if(attachmentSection.attachmentCanvasItem.canvas.getMember(0)){
+                attachmentSection.attachmentCanvasItem.canvas.getMember(0).getMember(0).click();
+                clearInterval(clickInterval);
+              }
+              if (clickCount === 50) {
+                clearInterval(clickInterval);
+              }
+            }, 100);
+          }
         }
       } else {
         var recordIds = "";