From 5d72b4e237744347b436c059d9040beab2826d6c Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 31 Oct 2014 14:47:25 -0700 Subject: [PATCH] CRM-15578 - crmMailingToken - Basic implementation --- CRM/Mailing/Info.php | 2 +- js/angular-crmMailing2-directives.js | 52 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 js/angular-crmMailing2-directives.js diff --git a/CRM/Mailing/Info.php b/CRM/Mailing/Info.php index d8d9a2ac0d..c220bd977a 100644 --- a/CRM/Mailing/Info.php +++ b/CRM/Mailing/Info.php @@ -67,7 +67,7 @@ class CRM_Mailing_Info extends CRM_Core_Component_Info { ); $result['crmMailing2'] = array( 'ext' => 'civicrm', - 'js' => array('js/angular-crmMailing2.js'), + 'js' => array('js/angular-crmMailing2.js', 'js/angular-crmMailing2-directives.js'), ); $result['crmMailingAB'] = array( 'ext' => 'civicrm', diff --git a/js/angular-crmMailing2-directives.js b/js/angular-crmMailing2-directives.js new file mode 100644 index 0000000000..cdbeb167da --- /dev/null +++ b/js/angular-crmMailing2-directives.js @@ -0,0 +1,52 @@ +(function (angular, $, _) { + var partialUrl = function (relPath) { + return CRM.resourceUrls['civicrm'] + '/partials/crmMailing2/' + relPath; + }; + + var crmMailing2 = angular.module('crmMailing2'); + + // example: + // WISHLIST: Instead of global CRM.crmMailing.mailTokens, accept token list as an input + crmMailing2.directive('crmMailingToken', function () { + return { + scope: { + crmFor: '@' + }, + template: '', + link: function (scope, element, attrs) { + // 1. Find the corresponding input element (crmFor) + + var form = $(element).closest('form'); + var crmForEl = $('input[name="' + attrs.crmFor + '"],textarea[name="' + attrs.crmFor + '"]', form); + if (form.length != 1 || crmForEl.length != 1) { + if (console.log) + console.log('crmMailingToken cannot be matched to input element. Expected to find one form and one input.', form.length, crmForEl.length); + return; + } + + // 2. Setup the token selector + $(element).select2({width: "10em", + dropdownAutoWidth: true, + data: CRM.crmMailing.mailTokens, + placeholder: ts('Insert') + }); + $(element).on('select2-selecting', function (e) { + var origVal = crmForEl.val(); + var origPos = crmForEl[0].selectionStart; + var newVal = origVal.substring(0, origPos) + e.val + origVal.substring(origPos, origVal.length); + crmForEl.val(newVal); + var newPos = (origPos + e.val.length); + crmForEl[0].selectionStart = newPos; + crmForEl[0].selectionEnd = newPos; + + $(element).select2('close').select2('val', ''); + crmForEl.triggerHandler('change'); + crmForEl.focus(); + + e.preventDefault(); + }); + } + }; + }); + +})(angular, CRM.$, CRM._); -- 2.25.1