CRM-15578 - Add crmMailingAB2 module (based on crmMailingAB). Add skeletal list/edit...
[civicrm-core.git] / js / angular-crmMailing2-directives.js
index fe6e4cbac9a54346ce067e5b6e51a856503b9ad0..c2fcd2010cb31f02620c3d664ffd5723e9660287 100644 (file)
@@ -5,9 +5,34 @@
 
   var crmMailing2 = angular.module('crmMailing2');
 
+  crmMailing2.directive('crmMailingReviewBool', function(){
+    return {
+      scope: {
+        crmOn: '@',
+        crmTitle: '@'
+      },
+      template: '<span ng-class="spanClasses"><span class="icon" ng-class="iconClasses"></span>{{crmTitle}} </span>',
+      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 {
+            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);
+      }
+    };
+  });
+
   // 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 () {
+  crmMailing2.directive('crmMailingToken', function (crmUiId) {
     return {
       scope: {
         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)
+          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",
+        $(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();
+          var id = crmUiId(crmForEl);
+          if (CKEDITOR.instances[id]) {
+            CKEDITOR.instances[id].insertText(e.val);
+            $(element).select2('close').select2('val', '');
+            CKEDITOR.instances[id].focus();
+          }
+          else {
+            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).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();
+          scope.$parent.$apply(function(){
+            arrayRemove(scope.mailing[typeKey][option.mode], option.entity_id);
+          });
           e.preventDefault();
         });