From b0461279a682a9cd396734e5f0f7824c2d1799af Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 31 Oct 2014 16:27:07 -0700 Subject: [PATCH] CRM-15578 - crmMailingRecipients - Basic implementation --- js/angular-crmMailing2-directives.js | 118 ++++++++++++++++++ .../crmMailing2/directive/recipients.html | 17 +++ 2 files changed, 135 insertions(+) create mode 100644 partials/crmMailing2/directive/recipients.html diff --git a/js/angular-crmMailing2-directives.js b/js/angular-crmMailing2-directives.js index cdbeb167da..fe6e4cbac9 100644 --- a/js/angular-crmMailing2-directives.js +++ b/js/angular-crmMailing2-directives.js @@ -49,4 +49,122 @@ }; }); + // example: + crmMailing2.directive('crmMailingRecipients', function () { + return { + restrict: 'AE', + scope: { + crmAvailGroups: '@', // available groups + crmAvailMailings: '@', // available mailings + crmMailing: '@' // the mailing for which we are choosing recipients + }, + templateUrl: partialUrl('directive/recipients.html'), + link: function (scope, element, attrs) { + scope.mailing = scope.$parent.$eval(attrs.crmMailing); + scope.groups = scope.$parent.$eval(attrs.crmAvailGroups); + scope.mailings = scope.$parent.$eval(attrs.crmAvailMailings); + + scope.ts = CRM.ts('CiviMail'); + + /// Convert MySQL date ("yyyy-mm-dd hh:mm:ss") to JS date object + scope.parseDate = function (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]); + }; + + /// Remove {value} from {array} + function arrayRemove(array, value) { + var idx = array.indexOf(value); + if (idx >= 0) { + array.splice(idx, 1); + } + } + + // @param string id an encoded string like "4 civicrm_mailing include" + // @return Object keys: entity_id, entity_type, mode + function convertValueToObj(id) { + var a = id.split(" "); + return {entity_id: parseInt(a[0]), entity_type: a[1], mode: a[2]}; + } + + // @param Object mailing + // @return array list of values like "4 civicrm_mailing include" + function convertMailingToValues(mailing) { + var r = []; + angular.forEach(mailing.groups.include, function (v) { + r.push(v + " civicrm_group include"); + }); + angular.forEach(mailing.groups.exclude, function (v) { + r.push(v + " civicrm_group exclude"); + }); + angular.forEach(mailing.mailings.include, function (v) { + r.push(v + " civicrm_mailing include"); + }); + angular.forEach(mailing.mailings.exclude, function (v) { + r.push(v + " civicrm_mailing exclude"); + }); + return r; + } + + // Update $(element) view based on latest data + function refreshUI() { + $(element).select2('val', convertMailingToValues(scope.mailing)); + } + + /// @return string HTML representingn an option + function formatItem(item) { + if (!item.id) { + // return `text` for optgroup + return item.text; + } + var option = convertValueToObj(item.id); + var icon = (option.entity_type === 'civicrm_mailing') ? 'EnvelopeIn.gif' : 'group.png'; + var spanClass = (option.mode == 'exclude') ? 'crmMailing2-exclude' : 'crmMailing2-include'; + return " " + item.text + ""; + } + + $(element).select2({ + dropdownAutoWidth: true, + placeholder: "Groups or Past Recipients", + formatResult: formatItem, + formatSelection: formatItem, + escapeMarkup: function (m) { + return m; + }, + }); + + $(element).on('select2-selecting', function (e) { + var option = convertValueToObj(e.val); + var typeKey = option.entity_type == 'civicrm_mailing' ? 'mailings' : 'groups'; + if (option.mode == 'exclude') { + scope.mailing[typeKey].exclude.push(option.entity_id); + arrayRemove(scope.mailing[typeKey].include, option.entity_id); + } else { + scope.mailing[typeKey].include.push(option.entity_id); + arrayRemove(scope.mailing[typeKey].exclude, option.entity_id); + } + scope.$apply(); + $(element).select2('close'); + e.preventDefault(); + }); + + $(element).on("select2-removing", function (e) { + var option = convertValueToObj(e.val); + var typeKey = option.entity_type == 'civicrm_mailing' ? 'mailings' : 'groups'; + arrayRemove(scope.mailing[typeKey][option.mode], option.entity_id); + scope.$apply(); + e.preventDefault(); + }); + + scope.$watchCollection(attrs.crmMailing + ".groups.include", refreshUI); + scope.$watchCollection(attrs.crmMailing + ".groups.exclude", refreshUI); + scope.$watchCollection(attrs.crmMailing + ".mailings.include", refreshUI); + scope.$watchCollection(attrs.crmMailing + ".mailings.exclude", refreshUI); + setTimeout(refreshUI, 50); + } + }; + }); + })(angular, CRM.$, CRM._); diff --git a/partials/crmMailing2/directive/recipients.html b/partials/crmMailing2/directive/recipients.html new file mode 100644 index 0000000000..bc12d7ae06 --- /dev/null +++ b/partials/crmMailing2/directive/recipients.html @@ -0,0 +1,17 @@ + + -- 2.25.1