CRM-15855 - crmMailingAB - Use auto-save and blocked-button behaviors
authorTim Otten <totten@civicrm.org>
Fri, 6 Feb 2015 20:03:42 +0000 (12:03 -0800)
committerTim Otten <totten@civicrm.org>
Mon, 9 Feb 2015 11:26:49 +0000 (03:26 -0800)
js/angular-crmMailingAB.js
js/angular-crmMailingAB/services.js
partials/crmMailingAB/edit.html

index 77262db65f560f90f802640a0abe451b5ef74447..da502bd094ca8e239d0ac369380ec0192978b005 100644 (file)
           }
         }
       });
+      $routeProvider.when('/abtest/new', {
+        template: '<p>' + ts('Initializing...') + '</p>',
+        controller: 'CrmMailingABNewCtrl',
+        resolve: {
+          abtest: function ($route, CrmMailingAB) {
+            var abtest = new CrmMailingAB(null);
+            return abtest.load().then(function(){
+              return abtest.save();
+            });
+          }
+        }
+      });
       $routeProvider.when('/abtest/:id', {
         templateUrl: '~/crmMailingAB/edit.html',
         controller: 'CrmMailingABEditCtrl',
     $scope.crmMailingABStatus = crmMailingABStatus;
   });
 
-  angular.module('crmMailingAB').controller('CrmMailingABEditCtrl', function ($scope, abtest, crmMailingABCriteria, crmMailingMgr, crmMailingPreviewMgr, crmStatus, $q, $location) {
+  angular.module('crmMailingAB').controller('CrmMailingABNewCtrl', function ($scope, abtest, $location) {
+    // Transition URL "/abtest/new/foo" => "/abtest/123/foo"
+    var parts = $location.path().split('/'); // e.g. "/mailing/new" or "/mailing/123/wizard"
+    parts[2] = abtest.id;
+    $location.path(parts.join('/'));
+    $location.replace();
+  });
+
+  angular.module('crmMailingAB').controller('CrmMailingABEditCtrl', function ($scope, abtest, crmMailingABCriteria, crmMailingMgr, crmMailingPreviewMgr, crmStatus, $q, $location, crmBlocker) {
     $scope.abtest = abtest;
     var ts = $scope.ts = CRM.ts(null);
+    var block = $scope.block = crmBlocker();
     $scope.crmMailingABCriteria = crmMailingABCriteria;
     $scope.crmMailingConst = CRM.crmMailing;
 
     // @return Promise
     $scope.save = function save() {
       $scope.sync();
-      return crmStatus({start: ts('Saving...'), success: ts('Saved')}, abtest.save().then(updateUrl));
+      return block(crmStatus({start: ts('Saving...'), success: ts('Saved')}, abtest.save().then(updateUrl)));
     };
 
     // @return Promise
     // @return Promise
     $scope.sendTest = function sendTest(mailingName, recipient) {
       $scope.sync();
-      return crmStatus({start: ts('Saving...'), success: ''}, abtest.save().then(updateUrl))
+      return block(crmStatus({start: ts('Saving...'), success: ''}, abtest.save().then(updateUrl))
         .then(function () {
           crmMailingPreviewMgr.sendTest(abtest.mailings[mailingName], recipient);
-        });
+        }));
     };
 
     // @return Promise
     $scope.delete = function () {
-      return crmStatus({start: ts('Deleting...'), success: ts('Deleted')}, abtest.delete().then(leave));
+      return block(crmStatus({start: ts('Deleting...'), success: ts('Deleted')}, abtest.delete().then($scope.leave)));
     };
 
     // @return Promise
     $scope.submit = function submit() {
       $scope.sync();
-      return crmStatus({start: ts('Saving...'), success: ''}, abtest.save())
-        .then(function () {
-          return crmStatus({start: ts('Submitting...'), success: ts('Submitted')}, 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()...)
-        })
-        .then(leave);
+      if (block.check() || $scope.crmMailing.$invalid) {
+        return;
+      }
+      return block(crmStatus({start: ts('Saving...'), success: ''}, abtest.save())
+          .then(function() {
+            return crmStatus({start: ts('Submitting...'), success: ts('Submitted')}, 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()...)
+          })
+      .then($scope.leave));
     };
 
-    function leave() {
+    $scope.leave = function leave() {
       $location.path('abtest');
       $location.replace();
-    }
+    };
 
     function updateCriteriaName() {
       var criteria = crmMailingABCriteria.get($scope.abtest.ab.testing_criteria_id);
index e23c905b1feab15f1aedd091497d84679e1bdc1e..61e44b468503172a8f76b90d90871245799be3a1 100644 (file)
     }
 
     angular.extend(CrmMailingAB.prototype, {
+      getAutosaveSignature: function() {
+        return [
+          this.ab,
+          this.mailings,
+          this.attachments.a.getAutosaveSignature(),
+          this.attachments.b.getAutosaveSignature(),
+          this.attachments.c.getAutosaveSignature()
+        ];
+      },
       // @return Promise CrmMailingAB
       load: function load() {
         var crmMailingAB = this;
index 7776dea48fa01cca429b905e5b2fb77a5db450e2..ecc405bac2f546ed4a0ad43ed7b03c349e7eee14 100644 (file)
@@ -13,7 +13,7 @@
   {{ts('This mailing has been submitted.')}}
 </div>
 
-<form name="crmMailingAB" novalidate ng-hide="isSubmitted()">
+<form name="crmMailingAB" novalidate ng-hide="isSubmitted()" crm-autosave="save()" crm-autosave-model="abtest.getAutosaveSignature()" crm-autosave-if="true">
   <div class="crm-block crm-form-block crmMailing">
     <div crm-ui-wizard>
       <div crm-ui-wizard-step="10" crm-title="ts('Setup')" ng-form="setupForm">
           }"
           crm-abtest="abtest"></div>
         <center>
-          <a class="crmMailing-submit-button" ng-click="submit()" ng-class="{disabled: crmMailing.$invalid}">
+          <a class="crmMailing-submit-button" ng-click="submit()" ng-class="{blocking: block.check(), disabled: crmMailing.$invalid}">
             <div>{{ts('Submit Mailing')}}</div>
           </a>
         </center>
       </div>
       <span crm-ui-wizard-buttons style="float:right;">
         <button
+          ng-disabled="block.check()"
           crm-confirm="{title:ts('Delete Draft?'), message:ts('Are you sure you want to delete the draft mailing?')}"
           on-yes="delete()">{{ts('Delete Draft')}}
         </button>
-        <button ng-click="save()">{{ts('Save Draft')}}</button>
+        <button ng-disabled="block.check()" ng-click="save().then(leave) ">{{ts('Save Draft')}}</button>
       </span>
     </div>
 </form>