[client-event-handlers] Implement pre-save action calling in standard windows
authorCarlos Aristu <carlos.aristu@openbravo.com>
Wed, 15 Jun 2016 19:50:13 +0200
changeset 29791 82937b40e3da
parent 29790 c918bb5c287b
child 29792 9cfd2f44ff7a
[client-event-handlers] Implement pre-save action calling in standard windows
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/main/ob-standard-view.js
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/main/ob-standard-window.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js	Wed Jun 15 18:27:45 2016 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js	Wed Jun 15 19:50:13 2016 +0200
@@ -4254,6 +4254,12 @@
       buttonType: 'save',
       prompt: OB.I18N.getLabel('OBUIAPP_GridSaveButtonPrompt'),
       action: function () {
+        if (me.grid.view && me.grid.view.existsAction && me.grid.view.existsAction('PRESAVE')) {
+          me.grid.view.executePreSaveActions(function () {
+            me.doSave();
+          });
+          return;
+        }
         me.doSave();
       }
     });
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/main/ob-standard-view.js	Wed Jun 15 18:27:45 2016 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/main/ob-standard-view.js	Wed Jun 15 19:50:13 2016 +0200
@@ -2119,6 +2119,17 @@
   },
 
   saveRow: function () {
+    var me = this;
+    if (this.existsAction('PRESAVE')) {
+      me.executePreSaveActions(function () {
+        me.doSaveRow();
+      });
+      return;
+    }
+    me.doSaveRow();
+  },
+
+  doSaveRow: function () {
     if (this.isEditingGrid) {
       this.viewGrid.endEditing();
     } else {
@@ -2126,6 +2137,22 @@
     }
   },
 
+  executePreSaveActions: function (saveRowCallback) {
+    var editForm, eventHandlerParams = {};
+
+    if (this.isEditingGrid) {
+      editForm = this.viewGrid.getEditForm();
+      if (editForm) {
+        eventHandlerParams.data = isc.clone(editForm.getValues());
+        eventHandlerParams.isNewRecord = editForm.isNew;
+      }
+    } else {
+      eventHandlerParams.data = isc.clone(this.viewForm.getValues());
+      eventHandlerParams.isNewRecord = this.viewForm.isNew;
+    }
+    this.callSaveActions('PRESAVE', eventHandlerParams, saveRowCallback);
+  },
+
   existsAction: function (actionType) {
     return this.tabId && OB.EventHandlerRegistry.hasAction(this.tabId, actionType);
   },
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/main/ob-standard-window.js	Wed Jun 15 18:27:45 2016 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/main/ob-standard-window.js	Wed Jun 15 19:50:13 2016 +0200
@@ -848,10 +848,31 @@
       isc.ask(OB.I18N.getLabel('OBUIAPP_AutosaveConfirm'), saveCallback);
     } else {
       // Auto save confirmation not required: continue as confirmation was accepted
+      if (this.hasChanges() && this.activeView.existsAction && this.activeView.existsAction('PRESAVE')) {
+        this.activeView.executePreSaveActions(function () {
+          saveCallback(true);
+        });
+        return;
+      }
       saveCallback(true);
     }
   },
 
+  hasChanges: function () {
+    var editForm;
+    if (!this.activeView) {
+      return false;
+    }
+    if (this.activeView.isShowingForm) {
+      return this.activeView.viewForm.hasChanged;
+    }
+    editForm = this.activeView.viewGrid.getEditForm();
+    if (editForm) {
+      return editForm.hasChanged;
+    }
+    return false;
+  },
+
   callAutoSaveAction: function () {
     var action = this.autoSaveAction;
     this.cleanUpAutoSaveProperties();