CRM-15578 - crmMailingToken - Basic implementation
authorTim Otten <totten@civicrm.org>
Fri, 31 Oct 2014 21:47:25 +0000 (14:47 -0700)
committerTim Otten <totten@civicrm.org>
Tue, 11 Nov 2014 00:20:42 +0000 (16:20 -0800)
CRM/Mailing/Info.php
js/angular-crmMailing2-directives.js [new file with mode: 0644]

index d8d9a2ac0ddf793093b86af6bb2cb65ea5b4987b..c220bd977ad4f4a3ca35ceceee5e73461118c520 100644 (file)
@@ -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 (file)
index 0000000..cdbeb16
--- /dev/null
@@ -0,0 +1,52 @@
+(function (angular, $, _) {
+  var partialUrl = function (relPath) {
+    return CRM.resourceUrls['civicrm'] + '/partials/crmMailing2/' + relPath;
+  };
+
+  var crmMailing2 = angular.module('crmMailing2');
+
+  // example: <input name="subject" /> <input crm-mailing-token crm-for="subject"/>
+  // WISHLIST: Instead of global CRM.crmMailing.mailTokens, accept token list as an input
+  crmMailing2.directive('crmMailingToken', function () {
+    return {
+      scope: {
+        crmFor: '@'
+      },
+      template: '<input type="text" class="crmMailingToken" />',
+      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._);