CRM-15705 - Add button icons to angular popup
[civicrm-core.git] / js / angular-crmMailing.js
index b383acb5717544fa050b6326133ef90409163a8b..8881051f6d3111cd57f16396bfc35e6d95b97187 100644 (file)
@@ -1,10 +1,7 @@
 (function (angular, $, _) {
-  var partialUrl = function partialUrl(relPath) {
-    return CRM.resourceUrls['civicrm'] + '/partials/crmMailing/' + relPath;
-  };
 
   angular.module('crmMailing', [
-    'crmUtil', 'crmAttachment', 'ngRoute', 'ui.utils', 'crmUi', 'dialogService'
+    'crmUtil', 'crmAttachment', 'crmAutosave', 'ngRoute', 'ui.utils', 'crmUi', 'dialogService'
   ]); // TODO ngSanitize, unsavedChanges
 
   // Time to wait before triggering AJAX update to recipients list
         template: '<div></div>',
         controller: 'ListMailingsCtrl'
       });
-      $routeProvider.when('/mailing/:id', {
-        templateUrl: partialUrl('edit.html'),
-        controller: 'EditMailingCtrl',
-        resolve: {
-          selectedMail: function selectedMail($route, crmMailingMgr) {
-            return crmMailingMgr.getOrCreate($route.current.params.id);
-          }
-        }
-      });
-      $routeProvider.when('/mailing/:id/unified', {
-        templateUrl: partialUrl('edit-unified.html'),
-        controller: 'EditMailingCtrl',
-        resolve: {
-          selectedMail: function selectedMail($route, crmMailingMgr) {
-            return crmMailingMgr.getOrCreate($route.current.params.id);
-          }
-        }
-      });
-      $routeProvider.when('/mailing/:id/unified2', {
-        templateUrl: partialUrl('edit-unified2.html'),
-        controller: 'EditMailingCtrl',
-        resolve: {
-          selectedMail: function selectedMail($route, crmMailingMgr) {
-            return crmMailingMgr.getOrCreate($route.current.params.id);
+
+      var editorPaths = {
+        '': '~/crmMailing/edit.html',
+        '/unified': '~/crmMailing/edit-unified.html',
+        '/unified2': '~/crmMailing/edit-unified2.html',
+        '/wizard': '~/crmMailing/edit-wizard.html'
+      };
+      angular.forEach(editorPaths, function(editTemplate, pathSuffix) {
+        $routeProvider.when('/mailing/new' + pathSuffix, {
+          template: '<p>' + ts('Initializing...') + '</p>',
+          controller: 'CreateMailingCtrl',
+          resolve: {
+            selectedMail: function(crmMailingMgr) {
+              var m = crmMailingMgr.create();
+              return crmMailingMgr.save(m);
+            }
           }
-        }
-      });
-      $routeProvider.when('/mailing/:id/wizard', {
-        templateUrl: partialUrl('edit-wizard.html'),
-        controller: 'EditMailingCtrl',
-        resolve: {
-          selectedMail: function selectedMail($route, crmMailingMgr) {
-            return crmMailingMgr.getOrCreate($route.current.params.id);
+        });
+        $routeProvider.when('/mailing/:id' + pathSuffix, {
+          templateUrl: editTemplate,
+          controller: 'EditMailingCtrl',
+          resolve: {
+            selectedMail: function($route, crmMailingMgr) {
+              return crmMailingMgr.get($route.current.params.id);
+            },
+            attachments: function($route, CrmAttachments) {
+              var attachments = new CrmAttachments(function () {
+                return {entity_table: 'civicrm_mailing', entity_id: $route.current.params.id};
+              });
+              return attachments.load();
+            }
           }
-        }
+        });
       });
     }
   ]);
 
-  angular.module('crmMailing').controller('ListMailingsCtrl', function ListMailingsCtrl() {
+  angular.module('crmMailing').controller('ListMailingsCtrl', ['crmLegacy', 'crmNavigator', function ListMailingsCtrl(crmLegacy, crmNavigator) {
     // We haven't implemented this in Angular, but some users may get clever
     // about typing URLs, so we'll provide a redirect.
-    window.location = CRM.url('civicrm/mailing/browse/unscheduled', {
-      reset: 1,
-      scheduled: 'false'
-    });
+    var new_url = crmLegacy.url('civicrm/mailing/browse/unscheduled', {reset: 1, scheduled: 'false'});
+    crmNavigator.redirect(new_url);
+  }]);
+
+  angular.module('crmMailing').controller('CreateMailingCtrl', function EditMailingCtrl($scope, selectedMail, $location) {
+    // Transition URL "/mailing/new/foo" => "/mailing/123/foo"
+    var parts = $location.path().split('/'); // e.g. "/mailing/new" or "/mailing/123/wizard"
+    parts[2] = selectedMail.id;
+    $location.path(parts.join('/'));
+    $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, attachments, crmMailingPreviewMgr, crmBlocker) {
     $scope.mailing = selectedMail;
-    $scope.attachments = new CrmAttachments(function () {
-      return {entity_table: 'civicrm_mailing', entity_id: $scope.mailing.id};
-    });
-    $scope.attachments.load();
+    $scope.attachments = attachments;
     $scope.crmMailingConst = CRM.crmMailing;
 
-    $scope.partialUrl = partialUrl;
-    var ts = $scope.ts = CRM.ts('CiviMail');
+    var ts = $scope.ts = CRM.ts(null);
+    var block = $scope.block = crmBlocker();
 
     $scope.isSubmitted = function isSubmitted() {
       return _.size($scope.mailing.jobs) > 0;
       var savePromise = crmMailingMgr.save(mailing)
         .then(function () {
           return attachments.save();
-        })
-        .then(updateUrl);
-      return crmStatus({start: ts('Saving...'), success: ''}, savePromise)
+        });
+      return block(crmStatus({start: ts('Saving...'), success: ''}, savePromise)
         .then(function () {
           crmMailingPreviewMgr.sendTest(mailing, recipient);
-        });
+        }));
     };
 
     // @return Promise
     $scope.submit = function submit() {
+      if (block.check() || $scope.crmMailing.$invalid) {
+        return;
+      }
+
       var promise = crmMailingMgr.save($scope.mailing)
           .then(function () {
             // pre-condition: the mailing exists *before* saving attachments to it
             return crmMailingMgr.submit($scope.mailing);
           })
           .then(function () {
-            leave('scheduled');
+            $scope.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();
           })
-          .then(updateUrl)
-      );
+      ));
     };
 
     // @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')
+            $scope.leave('unscheduled');
           })
-      );
+      ));
     };
 
     // @param string listingScreen 'archive', 'scheduled', 'unscheduled'
-    function leave(listingScreen) {
+    $scope.leave = function leave(listingScreen) {
       switch (listingScreen) {
         case 'archive':
           window.location = CRM.url('civicrm/mailing/browse/archived', {
           });
           break;
         case 'unscheduled':
+          /* falls through */
         default:
           window.location = CRM.url('civicrm/mailing/browse/unscheduled', {
             reset: 1,
           });
       }
     };
-
-    // Transition URL "/mailing/new" => "/mailing/123"
-    function updateUrl() {
-      var parts = $location.path().split('/'); // e.g. "/mailing/new" or "/mailing/123/wizard"
-      if (parts[2] != $scope.mailing.id) {
-        parts[2] = $scope.mailing.id;
-        $location.path(parts.join('/'));
-        $location.replace();
-        // FIXME: Angular unnecessarily refreshes UI
-        // WARNING: Changing the URL triggers a full reload. Any pending AJAX operations
-        // could be inconsistently applied. Run updateUrl() after other changes complete.
-      }
-    }
   });
 
   // Controller for the edit-recipients fields (
   //  - [input] mailing: object
   //  - [output] recipients: array of recipient records
   angular.module('crmMailing').controller('EditRecipCtrl', function EditRecipCtrl($scope, dialogService, crmApi, crmMailingMgr) {
-    var ts = $scope.ts = CRM.ts('CiviMail');
+    var ts = $scope.ts = CRM.ts(null);
     $scope.recipients = null;
     $scope.getRecipientsEstimate = function () {
       var ts = $scope.ts;
-      if ($scope.recipients == null) {
+      if ($scope.recipients === null) {
         return ts('(Estimating)');
       }
-      if ($scope.recipients.length == 0) {
+      if ($scope.recipients.length === 0) {
         return ts('No recipients');
       }
-      if ($scope.recipients.length == 1) {
+      if ($scope.recipients.length === 1) {
         return ts('~1 recipient');
       }
       if (RECIPIENTS_PREVIEW_LIMIT > 0 && $scope.recipients.length >= RECIPIENTS_PREVIEW_LIMIT) {
           1: $scope.getRecipientsEstimate()
         })
       };
-      dialogService.open('recipDialog', partialUrl('dialog/recipients.html'), model, options);
+      dialogService.open('recipDialog', '~/crmMailing/dialog/recipients.html', model, options);
     };
   });
 
   // Note: Expects $scope.model to be an object with properties:
   //   - recipients: array of contacts
   angular.module('crmMailing').controller('PreviewRecipCtrl', function ($scope) {
-    $scope.ts = CRM.ts('CiviMail');
+    $scope.ts = CRM.ts(null);
   });
 
   // Controller for the "Preview Mailing" dialog
   //   - "body_html"
   //   - "body_text"
   angular.module('crmMailing').controller('PreviewMailingDialogCtrl', function PreviewMailingDialogCtrl($scope) {
-    $scope.ts = CRM.ts('CiviMail');
+    $scope.ts = CRM.ts(null);
   });
 
   // Controller for the "Preview Mailing Component" segment
   // which displays header/footer/auto-responder
   angular.module('crmMailing').controller('PreviewComponentCtrl', function PreviewMailingDialogCtrl($scope, dialogService) {
-    var ts = $scope.ts = CRM.ts('CiviMail');
+    var ts = $scope.ts = CRM.ts(null);
 
     $scope.previewComponent = function previewComponent(title, componentId) {
       var component = _.where(CRM.crmMailing.headerfooterList, {id: "" + componentId});
         modal: true,
         title: title // component[0].name
       };
-      dialogService.open('previewComponentDialog', partialUrl('dialog/previewComponent.html'), component[0], options);
+      dialogService.open('previewComponentDialog', '~/crmMailing/dialog/previewComponent.html', component[0], options);
     };
   });
 
   //   - "body_html"
   //   - "body_text"
   angular.module('crmMailing').controller('PreviewComponentDialogCtrl', function PreviewMailingDialogCtrl($scope) {
-    $scope.ts = CRM.ts('CiviMail');
+    $scope.ts = CRM.ts(null);
   });
 
   // Controller for the in-place msg-template management
   angular.module('crmMailing').controller('MsgTemplateCtrl', function MsgTemplateCtrl($scope, crmMsgTemplates, dialogService) {
-    var ts = $scope.ts = CRM.ts('CiviMail');
+    var ts = $scope.ts = CRM.ts(null);
     $scope.crmMsgTemplates = crmMsgTemplates;
 
     // @return Promise MessageTemplate (per APIv3)
         modal: true,
         title: ts('Save Template')
       };
-      return dialogService.open('saveTemplateDialog', partialUrl('dialog/saveTemplate.html'), model, options)
+      return dialogService.open('saveTemplateDialog', '~/crmMailing/dialog/saveTemplate.html', model, options)
         .then(function (item) {
           mailing.msg_template_id = item.id;
           return item;
   //       - "msg_text": string
   //       - "msg_html": string
   angular.module('crmMailing').controller('SaveMsgTemplateDialogCtrl', function SaveMsgTemplateDialogCtrl($scope, crmMsgTemplates, dialogService) {
-    var ts = $scope.ts = CRM.ts('CiviMail');
+    var ts = $scope.ts = CRM.ts(null);
     $scope.saveOpt = {mode: '', newTitle: ''};
     $scope.selected = null;
 
       // When using dialogService with a button bar, the major button actions
       // need to be registered with the dialog widget (and not embedded in
       // the body of the dialog).
-      var buttons = {};
-      buttons[ts('Save')] = function () {
-        $scope.save().then(function (item) {
-          dialogService.close('saveTemplateDialog', item);
-        });
-      };
-      buttons[ts('Cancel')] = function () {
-        dialogService.cancel('saveTemplateDialog');
-      };
+      var buttons = [
+        {
+          text: ts('Save'),
+          icons: {primary: 'ui-icon-check'},
+          click: function () {
+            $scope.save().then(function (item) {
+              dialogService.close('saveTemplateDialog', item);
+            });
+          }
+        },
+        {
+          text: ts('Cancel'),
+          icons: {primary: 'ui-icon-close'},
+          click: function () {
+            dialogService.cancel('saveTemplateDialog');
+          }
+        }
+      ];
       dialogService.setButtons('saveTemplateDialog', buttons);
     }