crmMailingToken - Use $broadcast to link insertions and their targets
authorTim Otten <totten@civicrm.org>
Thu, 12 Feb 2015 16:53:38 +0000 (08:53 -0800)
committerTim Otten <totten@civicrm.org>
Thu, 12 Feb 2015 19:59:18 +0000 (11:59 -0800)
js/angular-crm-ui.js
js/angular-crmMailing/directives.js
partials/crmMailing/body_html.html
partials/crmMailing/body_text.html
partials/crmMailing/mailing.html
partials/crmMailingAB/joint-mailing.html

index cfc451866a339a6d5114e1c8e7d8e4510ff8dcb8..0527396f20eea3caec488994ec66c7a4a2c6e0a5 100644 (file)
       };
     })
 
+    // Example:
+    //   <a ng-click="$broadcast('my-insert-target', 'some new text')>Insert</a>
+    //   <textarea crm-ui-insert-rx='my-insert-target'></textarea>
+    // TODO Consider ways to separate the plain-text/rich-text implementations
+    .directive('crmUiInsertRx', function() {
+      return {
+        link: function(scope, element, attrs) {
+          scope.$on(attrs.crmUiInsertRx, function(e, tokenName) {
+            var id = element.attr('id');
+            if (CKEDITOR.instances[id]) {
+              CKEDITOR.instances[id].insertText(tokenName);
+              $(element).select2('close').select2('val', '');
+              CKEDITOR.instances[id].focus();
+            }
+            else {
+              var crmForEl = $('#' + id);
+              var origVal = crmForEl.val();
+              var origPos = crmForEl[0].selectionStart;
+              var newVal = origVal.substring(0, origPos) + tokenName + origVal.substring(origPos, origVal.length);
+              crmForEl.val(newVal);
+              var newPos = (origPos + tokenName.length);
+              crmForEl[0].selectionStart = newPos;
+              crmForEl[0].selectionEnd = newPos;
+
+              $(element).select2('close').select2('val', '');
+              crmForEl.triggerHandler('change');
+              crmForEl.focus();
+            }
+          });
+        }
+      };
+    })
+
     // Define a rich text editor.
     // example: <textarea crm-ui-id="myForm.body_html" crm-ui-richtext name="body_html" ng-model="mailing.body_html"></textarea>
     .directive('crmUiRichtext', function ($timeout) {
index 1ba5449fbe065c6698fb3fea3704e78f0a663779..214ed8e5e319d89e78a9d8020b57d2832f807686 100644 (file)
     };
   });
 
-  // example: <input name="subject" /> <input crm-mailing-token crm-for="subject"/>
+  // example: <input name="subject" /> <input crm-mailing-token on-select="doSomething(token.name)" />
   // WISHLIST: Instead of global CRM.crmMailing.mailTokens, accept token list as an input
   angular.module('crmMailing').directive('crmMailingToken', function () {
     return {
       require: '^crmUiIdScope',
       scope: {
-        crmFor: '@'
+        onSelect: '@'
       },
       template: '<input type="text" class="crmMailingToken" />',
       link: function (scope, element, attrs, crmUiIdCtrl) {
           placeholder: ts('Tokens')
         });
         $(element).on('select2-selecting', function (e) {
-          var id = crmUiIdCtrl.get(attrs.crmFor);
-          if (CKEDITOR.instances[id]) {
-            CKEDITOR.instances[id].insertText(e.val);
-            $(element).select2('close').select2('val', '');
-            CKEDITOR.instances[id].focus();
-          }
-          else {
-            var crmForEl = $('#' + id);
-            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();
+          $(element).select2('close').select2('val', '');
+          scope.$parent.$eval(attrs.onSelect, {
+            token: {name: e.val}
+          });
         });
       }
     };
index 1953d51291113ac43d349e83debaf0c9ea9c80b8..9fd7469b77ad8b65cdafe7e962d60f9a1073e926 100644 (file)
@@ -2,12 +2,19 @@
 Required vars: mailing
 -->
 <div ng-form="htmlForm" crm-ui-id-scope>
-  <div style="float: right;">
-    <input crm-mailing-token crm-for="htmlForm.body_html"/>
-  </div>
-
   <div ng-controller="EmailBodyCtrl">
-    <textarea crm-ui-id="htmlForm.body_html" crm-ui-richtext name="body_html" ng-model="mailing.body_html" ng-blur="checkTokens(mailing)"></textarea>
-    <span ng-model="body_html_tokens" crm-ui-validate="hasAllTokens(mailing, 'body_html')"></span>
+    <div style="float: right;">
+      <input crm-mailing-token on-select="$broadcast('insert:body_html', token.name)"/>
+    </div>
+
+    <div>
+      <textarea
+        crm-ui-id="htmlForm.body_html"
+        crm-ui-richtext name="body_html"
+        crm-ui-insert-rx="insert:body_html"
+        ng-model="mailing.body_html"
+        ng-blur="checkTokens(mailing)"></textarea>
+      <span ng-model="body_html_tokens" crm-ui-validate="hasAllTokens(mailing, 'body_html')"></span>
+    </div>
   </div>
 </div>
index 2d4665c8dd97d312ea3dea159fcb3f11a60b8341..1e2ad5cda38fc90e6e7b7f08e3fa723fb9329a31 100644 (file)
@@ -2,11 +2,19 @@
 Required vars: mailing, crmMailingConst
 -->
 <div ng-form="textForm" crm-ui-id-scope>
-  <div style="float: right;">
-    <input crm-mailing-token crm-for="textForm.body_text"/>
-  </div>
   <div ng-controller="EmailBodyCtrl">
-    <textarea crm-ui-id="textForm.body_text" name="body_text" ng-model="mailing.body_text" ng-blur="checkTokens(mailing)"></textarea>
-    <span ng-model="body_text_tokens" crm-ui-validate="hasAllTokens(mailing, 'body_text')"></span>
+    <div style="float: right;">
+      <input crm-mailing-token on-select="$broadcast('insert:body_text', token.name)" />
+    </div>
+
+    <div>
+      <textarea
+        crm-ui-id="textForm.body_text"
+        crm-ui-insert-rx="insert:body_text"
+        name="body_text"
+        ng-model="mailing.body_text"
+        ng-blur="checkTokens(mailing)"></textarea>
+      <span ng-model="body_text_tokens" crm-ui-validate="hasAllTokens(mailing, 'body_text')"></span>
+    </div>
   </div>
 </div>
index 9bbfd282959ac1eb544671d561500e646a36167a..de2068fdda0dcb74463dafa58f959f8acd62e6c6 100644 (file)
@@ -54,10 +54,11 @@ It could perhaps be thinned by 30-60% by making more directives.
     </div>
     <div crm-ui-field="subform.subject" crm-title="ts('Subject')">
       <div style="float: right;">
-        <input crm-mailing-token crm-for="subform.subject" />
+        <input crm-mailing-token on-select="$broadcast('insert:subject', token.name)" />
       </div>
       <input
         crm-ui-id="subform.subject"
+        crm-ui-insert-rx="insert:subject"
         type="text"
         class="crm-form-text"
         ng-model="mailing.subject"
index 1059048bd04314f8e1b652fb2d30b38560d875eb..e0585bea62069f77e94874932f8b981c493425b4 100644 (file)
@@ -152,10 +152,11 @@ processed by Angular; if false, the field will be hidden and completely ignored
 
     <div crm-ui-field="subform.subject" crm-title="ts('Subject')" ng-if="fields.subject">
       <div style="float: right;">
-        <input crm-mailing-token crm-for="subform.subject" />
+        <input crm-mailing-token on-select="$broadcast('insert:subject', token.name)" />
       </div>
       <input
         crm-ui-id="subform.subject"
+        crm-ui-insert-rx="insert:subject"
         type="text"
         class="crm-form-text"
         ng-model="abtest.mailings.a.subject"
@@ -165,10 +166,11 @@ processed by Angular; if false, the field will be hidden and completely ignored
     </div>
     <div crm-ui-field="subform.subjectA" crm-title="ts('Subject (A)')" ng-if="fields.subjectA">
       <div style="float: right;">
-        <input crm-mailing-token crm-for="subform.subjectA" />
+        <input crm-mailing-token on-select="$broadcast('insert:subjectA', token.name)" />
       </div>
       <input
         crm-ui-id="subform.subjectA"
+        crm-ui-insert-rx="insert:subjectA"
         type="text"
         class="crm-form-text"
         ng-model="abtest.mailings.a.subject"
@@ -178,10 +180,11 @@ processed by Angular; if false, the field will be hidden and completely ignored
     </div>
     <div crm-ui-field="subform.subjectB" crm-title="ts('Subject (B)')" ng-if="fields.subjectB">
       <div style="float: right;">
-        <input crm-mailing-token crm-for="subform.subjectB" />
+        <input crm-mailing-token on-select="$broadcast('insert:subjectB', token.name)" />
       </div>
       <input
         crm-ui-id="subform.subjectB"
+        crm-ui-insert-rx="insert:subjectB"
         type="text"
         class="crm-form-text"
         ng-model="abtest.mailings.b.subject"