From f4f103faaf1e9b10c1a7132f8b047bbdae053f17 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 11 Dec 2014 22:07:08 -0800 Subject: [PATCH] crmMailing2 - Autoformat --- js/angular-crmMailing2-directives.js | 16 ++++-- js/angular-crmMailing2-services.js | 69 ++++++++++++---------- js/angular-crmMailing2.js | 86 ++++++++++++++++++---------- 3 files changed, 104 insertions(+), 67 deletions(-) diff --git a/js/angular-crmMailing2-directives.js b/js/angular-crmMailing2-directives.js index c2fcd2010c..1de1e9eec1 100644 --- a/js/angular-crmMailing2-directives.js +++ b/js/angular-crmMailing2-directives.js @@ -5,24 +5,26 @@ var crmMailing2 = angular.module('crmMailing2'); - crmMailing2.directive('crmMailingReviewBool', function(){ + crmMailing2.directive('crmMailingReviewBool', function () { return { scope: { crmOn: '@', crmTitle: '@' }, template: '{{crmTitle}} ', - link: function(scope, element, attrs){ + link: function (scope, element, attrs) { function refresh() { if (scope.$parent.$eval(attrs.crmOn)) { scope.spanClasses = {'crmMailing2-active': true}; scope.iconClasses = {'ui-icon-check': true}; - } else { + } + else { scope.spanClasses = {'crmMailing2-inactive': true}; scope.iconClasses = {'ui-icon-close': true}; } scope.crmTitle = scope.$parent.$eval(attrs.crmTitle); } + refresh(); scope.$parent.$watch(attrs.crmOn, refresh); scope.$parent.$watch(attrs.crmTitle, refresh); @@ -103,8 +105,9 @@ /// Convert MySQL date ("yyyy-mm-dd hh:mm:ss") to JS date object scope.parseDate = function (date) { - if (!angular.isString(date)) + if (!angular.isString(date)) { return date; + } var p = date.split(/[\- :]/); return new Date(p[0], p[1], p[2], p[3], p[4], p[5]); }; @@ -176,7 +179,8 @@ if (option.mode == 'exclude') { scope.mailing[typeKey].exclude.push(option.entity_id); arrayRemove(scope.mailing[typeKey].include, option.entity_id); - } else { + } + else { scope.mailing[typeKey].include.push(option.entity_id); arrayRemove(scope.mailing[typeKey].exclude, option.entity_id); } @@ -188,7 +192,7 @@ $(element).on("select2-removing", function (e) { var option = convertValueToObj(e.val); var typeKey = option.entity_type == 'civicrm_mailing' ? 'mailings' : 'groups'; - scope.$parent.$apply(function(){ + scope.$parent.$apply(function () { arrayRemove(scope.mailing[typeKey][option.mode], option.entity_id); }); e.preventDefault(); diff --git a/js/angular-crmMailing2-services.js b/js/angular-crmMailing2-services.js index 340608a38a..f85798d89d 100644 --- a/js/angular-crmMailing2-services.js +++ b/js/angular-crmMailing2-services.js @@ -30,15 +30,16 @@ // so the UI must do some adaptation. The crmFromAddresses provides a richer way to slice/dice // the available "From:" addrs. Records are like the underlying OptionValues -- but add "email" // and "author". - crmMailing2.factory('crmFromAddresses', function($q, crmApi) { + crmMailing2.factory('crmFromAddresses', function ($q, crmApi) { var emailRegex = /^"(.*)" \<([^@\>]*@[^@\>]*)\>$/; - var addrs = _.map(CRM.crmMailing.fromAddress, function(addr){ + var addrs = _.map(CRM.crmMailing.fromAddress, function (addr) { var match = emailRegex.exec(addr.label); return _.extend({}, addr, { email: match ? match[2] : '(INVALID)', author: match ? match[1] : '(INVALID)' }); }); + function first(array) { return (array.length == 0) ? null : array[0]; } @@ -49,7 +50,7 @@ }, getByAuthorEmail: function getByAuthorEmail(author, email, autocreate) { var result = null; - _.each(addrs, function(addr){ + _.each(addrs, function (addr) { if (addr.author == author && addr.email == email) { result = addr; } @@ -67,7 +68,7 @@ getByEmail: function getByEmail(email) { return first(_.where(addrs, {email: email})); }, - getByLabel: function(label) { + getByLabel: function (label) { return first(_.where(addrs, {label: label})); }, getDefault: function getDefault() { @@ -76,8 +77,8 @@ }; }); - crmMailing2.factory('crmMsgTemplates', function($q, crmApi) { - var tpls = _.map(CRM.crmMailing.mesTemplate, function(tpl){ + crmMailing2.factory('crmMsgTemplates', function ($q, crmApi) { + var tpls = _.map(CRM.crmMailing.mesTemplate, function (tpl) { return _.extend({}, tpl, { //id: tpl parseInt(tpl.id) }); @@ -87,12 +88,13 @@ return { // @return Promise MessageTemplate (per APIv3) get: function get(id) { - id = ''+id; // parseInt(id); + id = '' + id; // parseInt(id); var dfr = $q.defer(); var tpl = _.where(tpls, {id: id}); if (id && tpl && tpl[0]) { dfr.resolve(tpl[0]); - } else { + } + else { dfr.reject(id); } return dfr.promise; @@ -100,10 +102,10 @@ // Save a template // @param tpl MessageTemplate (per APIv3) For new templates, omit "id" // @return Promise MessageTemplate (per APIv3) - save: function(tpl) { - return crmApi('MessageTemplate', 'create', tpl).then(function(response){ + save: function (tpl) { + return crmApi('MessageTemplate', 'create', tpl).then(function (response) { if (!tpl.id) { - tpl.id = ''+response.id; //parseInt(response.id); + tpl.id = '' + response.id; //parseInt(response.id); tpls.push(tpl); } lastModifiedTpl = tpl @@ -111,7 +113,7 @@ }); }, // @return Object MessageTemplate (per APIv3) - getLastModifiedTpl: function() { + getLastModifiedTpl: function () { return lastModifiedTpl; }, getAll: function getAll() { @@ -121,10 +123,10 @@ }); // The crmMailingMgr service provides business logic for loading, saving, previewing, etc - crmMailing2.factory('crmMailingMgr', function($q, crmApi, crmFromAddresses) { + crmMailing2.factory('crmMailingMgr', function ($q, crmApi, crmFromAddresses) { var pickDefaultMailComponent = function pickDefaultMailComponent(type) { var mcs = _.where(CRM.crmMailing.headerfooterList, { - component_type:type, + component_type: type, is_default: "1" }); return (mcs.length >= 1) ? mcs[0].id : null; @@ -138,11 +140,11 @@ }, // @return Promise Mailing (per APIv3) get: function get(id) { - return crmApi('Mailing', 'getsingle', {id: id}).then(function(mailing){ - return crmApi('MailingGroup', 'get', {mailing_id: id}).then(function(groupResult){ + return crmApi('Mailing', 'getsingle', {id: id}).then(function (mailing) { + return crmApi('MailingGroup', 'get', {mailing_id: id}).then(function (groupResult) { mailing.groups = {include: [], exclude: []}; mailing.mailings = {include: [], exclude: []}; - _.each(groupResult.values, function(mailingGroup) { + _.each(groupResult.values, function (mailingGroup) { var bucket = (mailingGroup.entity_table == 'civicrm_group') ? 'groups' : 'mailings'; var entityId = parseInt(mailingGroup.entity_id); mailing[bucket][mailingGroup.group_type].push(entityId); @@ -183,10 +185,11 @@ // @param mailing Object (per APIv3) // @return Promise - 'delete': function(mailing) { + 'delete': function (mailing) { if (mailing.id) { return crmApi('Mailing', 'delete', {id: mailing.id}); - } else { + } + else { var d = $q.defer(); d.resolve(); return d.promise; @@ -237,12 +240,12 @@ // @return Promise an object with "subject", "body_text", "body_html" preview: function preview(mailing) { var params = _.extend({}, mailing, { - options: {force_rollback: 1}, + options: {force_rollback: 1}, 'api.Mailing.preview': { id: '$value.id' } }); - return crmApi('Mailing', 'create', params).then(function(result){ + return crmApi('Mailing', 'create', params).then(function (result) { // changes rolled back, so we don't care about updating mailing return result.values[result.id]['api.Mailing.preview'].values; }); @@ -255,7 +258,7 @@ // To get list of recipients, we tentatively save the mailing and // get the resulting recipients -- then rollback any changes. var params = _.extend({}, mailing, { - options: {force_rollback: 1}, + options: {force_rollback: 1}, 'api.mailing_job.create': 1, // note: exact match to API default 'api.MailingRecipients.get': { mailing_id: '$value.id', @@ -264,7 +267,7 @@ 'api.email.getvalue': {'return': 'email'} } }); - return crmApi('Mailing', 'create', params).then(function(recipResult){ + return crmApi('Mailing', 'create', params).then(function (recipResult) { // changes rolled back, so we don't care about updating mailing return recipResult.values[recipResult.id]['api.MailingRecipients.get'].values; }); @@ -273,7 +276,7 @@ // Save a (draft) mailing // @param mailing Object (per APIv3) // @return Promise - save: function(mailing) { + save: function (mailing) { var params = _.extend({}, mailing, { 'api.mailing_job.create': 0 // note: exact match to API default }); @@ -283,8 +286,10 @@ // is therefore not allowed. Remove this after fixing Mailing.create's contract. delete params.scheduled_date; - return crmApi('Mailing', 'create', params).then(function(result){ - if (result.id && !mailing.id) mailing.id = result.id; // no rollback, so update mailing.id + return crmApi('Mailing', 'create', params).then(function (result) { + if (result.id && !mailing.id) { + mailing.id = result.id; + } // no rollback, so update mailing.id // Perhaps we should reload mailing based on result? return result.values[result.id]; }); @@ -305,7 +310,9 @@ 'api.mailing_job.create': 0 // note: exact match to API default }); return crmApi('Mailing', 'create', params).then(function (result) { - if (result.id && !mailing.id) mailing.id = result.id; // no rollback, so update mailing.id + if (result.id && !mailing.id) { + mailing.id = result.id; + } // no rollback, so update mailing.id _.extend(mailing, changes); // Perhaps we should reload mailing based on result? return result.values[result.id]; }); @@ -316,7 +323,7 @@ // @param testEmail string // @param testGroup int (id#) // @return Promise for a list of delivery reports - sendTest: function(mailing, testEmail, testGroup) { + sendTest: function (mailing, testEmail, testGroup) { var params = _.extend({}, mailing, { // options: {force_rollback: 1}, // Test mailings include tracking features, so the mailing must be persistent 'api.Mailing.send_test': { @@ -331,8 +338,10 @@ // is therefore not allowed. Remove this after fixing Mailing.create's contract. delete params.scheduled_date; - return crmApi('Mailing', 'create', params).then(function(result){ - if (result.id && !mailing.id) mailing.id = result.id; // no rollback, so update mailing.id + return crmApi('Mailing', 'create', params).then(function (result) { + if (result.id && !mailing.id) { + mailing.id = result.id; + } // no rollback, so update mailing.id return result.values[result.id]['api.Mailing.send_test'].values; }); } diff --git a/js/angular-crmMailing2.js b/js/angular-crmMailing2.js index e4992b168a..2c83572c71 100644 --- a/js/angular-crmMailing2.js +++ b/js/angular-crmMailing2.js @@ -3,13 +3,16 @@ return CRM.resourceUrls['civicrm'] + '/partials/crmMailing2/' + relPath; }; - var crmMailing2 = angular.module('crmMailing2', ['crmUtil', 'crmAttachment', 'ngRoute', 'ui.utils', 'crmUi', 'dialogService']); // TODO ngSanitize, unsavedChanges + var crmMailing2 = angular.module('crmMailing2', [ + 'crmUtil', 'crmAttachment', 'ngRoute', 'ui.utils', 'crmUi', 'dialogService' + ]); // TODO ngSanitize, unsavedChanges // Time to wait before triggering AJAX update to recipients list var RECIPIENTS_DEBOUNCE_MS = 100; var RECIPIENTS_PREVIEW_LIMIT = 10000; - crmMailing2.config(['$routeProvider', + crmMailing2.config([ + '$routeProvider', function ($routeProvider) { $routeProvider.when('/mailing2', { template: '
', @@ -19,28 +22,36 @@ templateUrl: partialUrl('edit.html'), controller: 'EditMailingCtrl', resolve: { - selectedMail: function selectedMail($route, crmMailingMgr) { return crmMailingMgr.getOrCreate($route.current.params.id); } + selectedMail: function selectedMail($route, crmMailingMgr) { + return crmMailingMgr.getOrCreate($route.current.params.id); + } } }); $routeProvider.when('/mailing2/:id/unified', { templateUrl: partialUrl('edit-unified.html'), controller: 'EditMailingCtrl', resolve: { - selectedMail: function selectedMail($route, crmMailingMgr) { return crmMailingMgr.getOrCreate($route.current.params.id); } + selectedMail: function selectedMail($route, crmMailingMgr) { + return crmMailingMgr.getOrCreate($route.current.params.id); + } } }); $routeProvider.when('/mailing2/:id/unified2', { templateUrl: partialUrl('edit-unified2.html'), controller: 'EditMailingCtrl', resolve: { - selectedMail: function selectedMail($route, crmMailingMgr) { return crmMailingMgr.getOrCreate($route.current.params.id); } + selectedMail: function selectedMail($route, crmMailingMgr) { + return crmMailingMgr.getOrCreate($route.current.params.id); + } } }); $routeProvider.when('/mailing2/:id/wizard', { templateUrl: partialUrl('edit-wizard.html'), controller: 'EditMailingCtrl', resolve: { - selectedMail: function selectedMail($route, crmMailingMgr) { return crmMailingMgr.getOrCreate($route.current.params.id); } + selectedMail: function selectedMail($route, crmMailingMgr) { + return crmMailingMgr.getOrCreate($route.current.params.id); + } } }); } @@ -57,7 +68,7 @@ crmMailing2.controller('EditMailingCtrl', function EditMailingCtrl($scope, selectedMail, $location, crmMailingMgr, crmFromAddresses, crmStatus, CrmAttachments) { $scope.mailing = selectedMail; - $scope.attachments = new CrmAttachments(function() { + $scope.attachments = new CrmAttachments(function () { return {entity_table: 'civicrm_mailing', entity_id: $scope.mailing.id}; }); $scope.crmMailingConst = CRM.crmMailing; @@ -103,7 +114,7 @@ }; // Transition URL "/mailing2/new" => "/mailing2/123" as soon as ID is known - $scope.$watch('mailing.id', function(newValue, oldValue) { + $scope.$watch('mailing.id', function (newValue, oldValue) { if (newValue && newValue != oldValue) { var parts = $location.path().split('/'); // e.g. "/mailing2/new" or "/mailing2/123/wizard" parts[2] = newValue; @@ -116,7 +127,7 @@ $scope.fromPlaceholder = { label: crmFromAddresses.getByAuthorEmail($scope.mailing.from_name, $scope.mailing.from_email, true).label }; - $scope.$watch('fromPlaceholder.label', function(newValue){ + $scope.$watch('fromPlaceholder.label', function (newValue) { var addr = crmFromAddresses.getByLabel(newValue); $scope.mailing.from_name = addr.author; $scope.mailing.from_email = addr.email; @@ -133,45 +144,57 @@ $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) + } + if (RECIPIENTS_PREVIEW_LIMIT > 0 && $scope.recipients.length >= RECIPIENTS_PREVIEW_LIMIT) { return ts('>%1 recipients', {1: RECIPIENTS_PREVIEW_LIMIT}); + } return ts('~%1 recipients', {1: $scope.recipients.length}); }; - $scope.getIncludesAsString = function() { + $scope.getIncludesAsString = function () { var first = true; var names = ''; - _.each($scope.mailing.groups.include, function(id){ - if (!first) names = names + ', '; - var group = _.where(CRM.crmMailing.groupNames, {id: ''+id}); + _.each($scope.mailing.groups.include, function (id) { + if (!first) { + names = names + ', '; + } + var group = _.where(CRM.crmMailing.groupNames, {id: '' + id}); names = names + group[0].title; first = false; }); - _.each($scope.mailing.mailings.include, function(id){ - if (!first) names = names + ', '; - var oldMailing = _.where(CRM.crmMailing.civiMails, {id: ''+id}); + _.each($scope.mailing.mailings.include, function (id) { + if (!first) { + names = names + ', '; + } + var oldMailing = _.where(CRM.crmMailing.civiMails, {id: '' + id}); names = names + oldMailing[0].name; first = false; }); return names; }; - $scope.getExcludesAsString = function() { + $scope.getExcludesAsString = function () { var first = true; var names = ''; - _.each($scope.mailing.groups.exclude, function(id){ - if (!first) names = names + ', '; - var group = _.where(CRM.crmMailing.groupNames, {id: ''+id}); + _.each($scope.mailing.groups.exclude, function (id) { + if (!first) { + names = names + ', '; + } + var group = _.where(CRM.crmMailing.groupNames, {id: '' + id}); names = names + group[0].title; first = false; }); - _.each($scope.mailing.mailings.exclude, function(id){ - if (!first) names = names + ', '; - var oldMailing = _.where(CRM.crmMailing.civiMails, {id: ''+id}); + _.each($scope.mailing.mailings.exclude, function (id) { + if (!first) { + names = names + ', '; + } + var oldMailing = _.where(CRM.crmMailing.civiMails, {id: '' + id}); names = names + oldMailing[0].name; first = false; }); @@ -239,7 +262,7 @@ $scope.previewDialog = function previewDialog(template) { var p = crmMailingMgr .preview($scope.mailing) - .then(function(content){ + .then(function (content) { var options = { autoOpen: false, modal: true, @@ -249,7 +272,7 @@ }; dialogService.open('previewDialog', template, content, options); }); - CRM.status({start: ts('Previewing'), success: ''}, CRM.toJqPromise(p)); + CRM.status({start: ts('Previewing'), success: ''}, CRM.toJqPromise(p)); }; $scope.sendTestToContact = function sendTestToContact() { $scope.sendTest($scope.mailing, $scope.attachments, $scope.testContact.email, null); @@ -291,7 +314,7 @@ var ts = $scope.ts = CRM.ts('CiviMail'); $scope.previewComponent = function previewComponent(title, componentId) { - var component = _.where(CRM.crmMailing.headerfooterList, {id: ""+componentId}); + var component = _.where(CRM.crmMailing.headerfooterList, {id: "" + componentId}); if (!component || !component[0]) { CRM.alert(ts('Invalid component ID (%1)', { 1: componentId @@ -341,7 +364,7 @@ title: ts('Save Template') }; return dialogService.open('saveTemplateDialog', partialUrl('dialog/saveTemplate.html'), model, options) - .then(function(item){ + .then(function (item) { $parse('mailing.msg_template_id').assign($scope, item.id); return item; }); @@ -425,6 +448,7 @@ }; dialogService.setButtons('saveTemplateDialog', buttons); } + setTimeout(scopeApply(init), 0); }); -- 2.25.1