crmMailing - Block Save/Delete/Submit while saving/deleting/submitting.
authorTim Otten <totten@civicrm.org>
Sat, 24 Jan 2015 03:17:43 +0000 (19:17 -0800)
committerTim Otten <totten@civicrm.org>
Sat, 24 Jan 2015 08:02:05 +0000 (00:02 -0800)
js/angular-crm-util.js
js/angular-crmMailing.js
partials/crmMailing/edit-unified.html
partials/crmMailing/edit-unified2.html
partials/crmMailing/edit-wizard.html
partials/crmMailing/edit.html

index 62437f558f1714290f48969c18a6252066ba32bc..f122c9c8e48735d8d09756e0d1302fb2e031a473 100644 (file)
     };
   });
 
+  // usage:
+  // var block = $scope.block = crmBlocker();
+  // $scope.save = function() { return block(crmApi('MyEntity','create',...)); };
+  // <button ng-click="save()" ng-disabled="block.check()">Do something</button>
+  angular.module('crmUtil').factory('crmBlocker', function() {
+    return function() {
+      var blocks = 0;
+      var result = function(promise) {
+        blocks++;
+        promise.finally(function() {
+          blocks--;
+        });
+      };
+      result.check = function() {
+        return blocks > 0;
+      };
+      return result;
+    };
+  });
+
   angular.module('crmUtil').factory('crmLegacy', function() {
     return CRM;
   });
index bb58cf4d443e5bf2140de38659e7c234f9cec30b..d62443a12d2efb74a2bca7c6394534f0585377b3 100644 (file)
@@ -61,7 +61,7 @@
     $location.replace();
   });
 
-  angular.module('crmMailing').controller('EditMailingCtrl', function EditMailingCtrl($scope, selectedMail, $location, crmMailingMgr, crmStatus, CrmAttachments, crmMailingPreviewMgr) {
+  angular.module('crmMailing').controller('EditMailingCtrl', function EditMailingCtrl($scope, selectedMail, $location, crmMailingMgr, crmStatus, CrmAttachments, crmMailingPreviewMgr, crmBlocker) {
     $scope.mailing = selectedMail;
     $scope.attachments = new CrmAttachments(function () {
       return {entity_table: 'civicrm_mailing', entity_id: $scope.mailing.id};
@@ -70,6 +70,7 @@
     $scope.crmMailingConst = CRM.crmMailing;
 
     var ts = $scope.ts = CRM.ts(null);
+    var block = $scope.block = crmBlocker();
 
     $scope.isSubmitted = function isSubmitted() {
       return _.size($scope.mailing.jobs) > 0;
         .then(function () {
           return attachments.save();
         });
-      return crmStatus({start: ts('Saving...'), success: ''}, savePromise)
+      return block(crmStatus({start: ts('Saving...'), success: ''}, savePromise)
         .then(function () {
           crmMailingPreviewMgr.sendTest(mailing, recipient);
-        });
+        }));
     };
 
     // @return Promise
             leave('scheduled');
           })
         ;
-      return crmStatus({start: ts('Submitting...'), success: ts('Submitted')}, promise);
+      return block(crmStatus({start: ts('Submitting...'), success: ts('Submitted')}, promise));
     };
 
     // @return Promise
     $scope.save = function save() {
-      return crmStatus(null,
+      return block(crmStatus(null,
         crmMailingMgr
           .save($scope.mailing)
           .then(function () {
             // pre-condition: the mailing exists *before* saving attachments to it
             return $scope.attachments.save();
           })
-      );
+      ));
     };
 
     // @return Promise
     $scope.delete = function cancel() {
-      return crmStatus({start: ts('Deleting...'), success: ts('Deleted')},
+      return block(crmStatus({start: ts('Deleting...'), success: ts('Deleted')},
         crmMailingMgr.delete($scope.mailing)
           .then(function () {
             leave('unscheduled');
           })
-      );
+      ));
     };
 
     // @param string listingScreen 'archive', 'scheduled', 'unscheduled'
index 20611907d6b101d559ad2123f4d112edbefaff8d..55e80aaf8fcf2e4e6ea9865e8040812966eb3299 100644 (file)
@@ -44,8 +44,8 @@
       <div crm-mailing-block-schedule crm-mailing="mailing"/>
     </div>
 
-    <button ng-click="submit()">{{ts('Submit Mailing')}}</button>
-    <button ng-click="save()">{{ts('Save Draft')}}</button>
-    <button 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-disabled="block.check()" ng-click="submit()">{{ts('Submit Mailing')}}</button>
+    <button ng-disabled="block.check()" ng-click="save()">{{ts('Save Draft')}}</button>
+    <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>
   </div>
 </form>
index 4e06cc7df225f568f87c27d723f4b6d1d5db5603..e5ea4994458e27d31f71f28b00fc6751005133c6 100644 (file)
@@ -40,8 +40,8 @@
       <div crm-mailing-block-schedule crm-mailing="mailing"/>
     </div>
 
-    <button ng-click="submit()">{{ts('Submit Mailing')}}</button>
-    <button ng-click="save()">{{ts('Save Draft')}}</button>
-    <button 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-disabled="block.check()" ng-click="submit()">{{ts('Submit Mailing')}}</button>
+    <button ng-disabled="block.check()" ng-click="save()">{{ts('Save Draft')}}</button>
+    <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>
   </div>
 </form>
index 124d432dd5fa05bb1f7c426e030c28758341d200..4a8141db09529904997cc435bb0433956f90cdbc 100644 (file)
           <div crm-mailing-block-review crm-mailing="mailing"/>
         </div>
         <center>
-          <a class="crmMailing-submit-button" ng-click="submit()">
+          <a class="crmMailing-submit-button" ng-click="submit()" ng-disabled="block.check()">
             <div>{{ts('Submit Mailing')}}</div>
           </a>
         </center>
       </div>
 
       <span crm-ui-wizard-buttons style="float:right;">
-        <button 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()" 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-disabled="block.check()" ng-click="save()">{{ts('Save Draft')}}</button>
       </span>
     </div>
   </div>
index feb6969f71038ffe91cc10af02f9e995d36fe620..9340e37a9fcf158ab45d190ac36e98e75a9d2e26 100644 (file)
           <div crm-mailing-block-schedule crm-mailing="mailing"/>
         </div>
         <center>
-          <a class="crmMailing-submit-button" ng-click="submit()">
+          <a class="crmMailing-submit-button" ng-click="submit()" ng-disabled="block.check()">
             <div>{{ts('Submit Mailing')}}</div>
           </a>
         </center>
       </div>
 
       <span crm-ui-wizard-buttons style="float:right;">
-        <button 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()" 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-disabled="block.check()" ng-click="save()">{{ts('Save Draft')}}</button>
       </span>
     </div>
   </div>