From b7c3beb26de8525870275d2bae4c5653e1634e27 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 22 Jan 2015 23:53:10 -0800 Subject: [PATCH] CRM-15855 - crmMailing - For new mailings, automatically save and redirect. --- js/angular-crmMailing.js | 83 ++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 49 deletions(-) diff --git a/js/angular-crmMailing.js b/js/angular-crmMailing.js index ff417eb1ed..317a6f899e 100644 --- a/js/angular-crmMailing.js +++ b/js/angular-crmMailing.js @@ -18,41 +18,33 @@ template: '
', 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 = { + '': partialUrl('edit.html'), + '/unified': partialUrl('edit-unified.html'), + '/unified2': partialUrl('edit-unified2.html'), + '/wizard': partialUrl('edit-wizard.html') + }; + angular.forEach(editorPaths, function(editTemplate, pathSuffix) { + $routeProvider.when('/mailing/new' + pathSuffix, { + template: '

' + ts('Initializing...') + '

', + 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); + } } - } + }); }); } ]); @@ -64,6 +56,14 @@ 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) { $scope.mailing = selectedMail; $scope.attachments = new CrmAttachments(function () { @@ -89,8 +89,7 @@ var savePromise = crmMailingMgr.save(mailing) .then(function () { return attachments.save(); - }) - .then(updateUrl); + }); return crmStatus({start: ts('Saving...'), success: ''}, savePromise) .then(function () { crmMailingPreviewMgr.sendTest(mailing, recipient); @@ -123,7 +122,6 @@ // pre-condition: the mailing exists *before* saving attachments to it return $scope.attachments.save(); }) - .then(updateUrl) ); }; @@ -160,19 +158,6 @@ }); } } - - // 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 ( -- 2.25.1