crmMailingAB - Suspend autosave while submitting. Reload after submission.
authorTim Otten <totten@civicrm.org>
Thu, 19 Feb 2015 02:49:01 +0000 (18:49 -0800)
committerTim Otten <totten@civicrm.org>
Thu, 19 Feb 2015 23:04:06 +0000 (15:04 -0800)
js/angular-crmAutosave.js
js/angular-crmMailingAB.js
js/angular-crmMailingAB/services.js

index f492cb973925dc98d016a4657976506c2e80c00e..a697880b8f115d1c0cc085fd3f37ae6aceeb5b2b 100644 (file)
@@ -18,7 +18,7 @@
   //   autosave.start();
   //   $scope.$on('$destroy', autosave.stop);
   // Note: if the save operation itself
-  angular.module('crmAutosave').service('CrmAutosaveCtrl', function($interval, $timeout) {
+  angular.module('crmAutosave').service('CrmAutosaveCtrl', function($interval, $timeout, $q) {
     function CrmAutosaveCtrl(options) {
       var intervals = angular.extend({poll: 250, save: 5000}, options.interval);
       var jobs = {poll: null, save: null}; // job handles used ot cancel/reschedule timeouts/intervals
@@ -86,6 +86,8 @@
         }
       }
 
+      var self = this;
+
       this.start = function() {
         if (!jobs.poll) {
           lastSeenModel = angular.copy(_.isFunction(options.model) ? options.model() : options.model);
           jobs.save = null;
         }
       };
+
+      this.suspend = function(p) {
+        self.stop();
+        return p.finally(self.start);
+      };
     }
 
     return CrmAutosaveCtrl;
index 73fc5ae904725631365e16578dda300ca4ddd404..91bb7ace4e9743cc30eea58d98aaf7c420661d6f 100644 (file)
     $location.replace();
   });
 
-  angular.module('crmMailingAB').controller('CrmMailingABEditCtrl', function ($scope, abtest, crmMailingABCriteria, crmMailingMgr, crmMailingPreviewMgr, crmStatus, $q, $location, crmBlocker, $interval, $timeout, CrmAutosaveCtrl) {
+  angular.module('crmMailingAB').controller('CrmMailingABEditCtrl', function ($scope, abtest, crmMailingABCriteria, crmMailingMgr, crmMailingPreviewMgr, crmStatus, $q, $location, crmBlocker, $interval, $timeout, CrmAutosaveCtrl, dialogService) {
     $scope.abtest = abtest;
     var ts = $scope.ts = CRM.ts(null);
     var block = $scope.block = crmBlocker();
+    $scope.crmUrl = CRM.url;
+    var myAutosave = null;
     $scope.crmMailingABCriteria = crmMailingABCriteria;
     $scope.crmMailingConst = CRM.crmMailing;
 
       }
       return block(crmStatus({start: ts('Saving...'), success: ''}, abtest.save())
           .then(function() {
-            return crmStatus({start: ts('Submitting...'), success: ts('Submitted')}, abtest.submitTest());
+            return crmStatus({start: ts('Submitting...'), success: ts('Submitted')}, myAutosave.suspend(abtest.submitTest()));
             // Note: We're going to leave, so we don't care that submit() modifies several server-side records.
             // If we stayed on this page, then we'd care about updating and call: abtest.submitTest().then(...abtest.load()...)
           })
       $location.replace();
     };
 
+    $scope.selectWinner = function selectWinner(mailingName) {
+      var model = {
+        abtest: $scope.abtest,
+        mailingName: mailingName
+      };
+      var options = CRM.utils.adjustDialogDefaults({
+        autoOpen: false,
+        title: ts('Select Winner (%1)', {
+          1: mailingName.toUpperCase()
+        })
+      });
+      return myAutosave.suspend(dialogService.open('selectWinnerDialog', '~/crmMailingAB/selectWinner.html', model, options));
+    };
+
     // initialize
     var syncJob = $interval($scope.sync, 333);
     $scope.$on('$destroy', function(){
       $interval.cancel(syncJob);
     });
 
-    var myAutosave = new CrmAutosaveCtrl({
+    myAutosave = new CrmAutosaveCtrl({
       save: $scope.save,
-      saveIf: $scope.sync,
+      saveIf: function(){
+        return abtest.ab.status == 'Draft' && $scope.sync();
+      },
       model: function(){
         return abtest.getAutosaveSignature();
       },
     $scope.$on('$destroy', myAutosave.stop);
   });
 
-  angular.module('crmMailingAB').controller('CrmMailingABReportCtrl', function ($scope, crmApi, crmMailingPreviewMgr, dialogService) {
+  angular.module('crmMailingAB').controller('CrmMailingABReportCtrl', function ($scope, crmApi, crmMailingStats, crmLegacy) {
     var ts = $scope.ts = CRM.ts(null);
 
     $scope.stats = {};
     crmApi('Mailing', 'stats', {mailing_id: $scope.abtest.ab.mailing_id_c}).then(function(data){
       $scope.stats.c = data.values[$scope.abtest.ab.mailing_id_c];
     });
-
-    $scope.previewMailing = function previewMailing(mailingName, mode) {
-      return crmMailingPreviewMgr.preview(abtest.mailings[mailingName], mode);
-    };
-    $scope.selectWinner = function selectWinner(mailingName) {
-      var model = {
-        abtest: $scope.abtest,
-        mailingName: mailingName
-      };
-      var options = CRM.utils.adjustDialogDefaults({
-        autoOpen: false,
-        title: ts('Select Winner (%1)', {
-          1: mailingName.toUpperCase()
-        })
-      });
-      return dialogService.open('selectWinnerDialog', '~/crmMailingAB/selectWinner.html', model, options);
-    };
   });
 
-
   angular.module('crmMailingAB').controller('CrmMailingABWinnerDialogCtrl', function ($scope, $timeout, dialogService, crmMailingMgr, crmStatus) {
     var ts = $scope.ts = CRM.ts(null);
     var abtest = $scope.abtest = $scope.model.abtest;
index 955ba954ce6e58fd7828f1bd602b8a7699dc99b3..3b6123fa8af219777056c2ef02467864321776c2 100644 (file)
         };
         return crmApi('MailingAB', 'submit', params)
           .then(function () {
-            return crmMailingAB;
+            return crmMailingAB.load();
           });
       },
       // Schedule the final mailing