Afform - Refactor icon picker to service
authorColeman Watts <coleman@civicrm.org>
Thu, 7 Jan 2021 22:14:40 +0000 (17:14 -0500)
committerColeman Watts <coleman@civicrm.org>
Mon, 11 Jan 2021 14:49:06 +0000 (09:49 -0500)
ext/afform/admin/ang/afAdmin.aff.html
ext/afform/admin/ang/afGuiEditor.js

index 4bb72f31af4f53f5b838e43cecec8a13d8b0ce2e..ad443fe3e60b73294e36f8e75fad10c9359934d1 100644 (file)
@@ -5,7 +5,3 @@
 <div ng-if="routeParams.name">
   <af-gui-editor name="routeParams.name"></af-gui-editor>
 </div>
-
-<div style="display:none;">
-  <input id="af-gui-icon-picker" title="{{:: ts('Choose Icon') }}" />
-</div>
index 40b148ffab640c5b476692892d7ebf717948e5d9..daed098357b5340c01f7c8146281510b7a0b6c17 100644 (file)
@@ -2,7 +2,7 @@
   "use strict";
   angular.module('afGuiEditor', CRM.angRequires('afGuiEditor'))
 
-    .service('afAdmin', function(crmApi4, $parse) {
+    .service('afAdmin', function(crmApi4, $parse, $q) {
 
       // Parse strings of javascript that php couldn't interpret
       function evaluate(collection) {
         splitClass: splitClass,
         modifyClasses: modifyClasses,
         getStyles: getStyles,
-        setStyle: setStyle
+        setStyle: setStyle,
+
+        pickIcon: function() {
+          var deferred = $q.defer();
+          $('#af-gui-icon-picker').off('change').siblings('.crm-icon-picker-button').click();
+          $('#af-gui-icon-picker').on('change', function() {
+            deferred.resolve($(this).val());
+          });
+          return deferred.promise;
+        }
       };
     });
 
+  // Shoehorn in a non-angular widget for picking icons
+  $(function() {
+    $('#crm-container').append('<div style="display:none"><input id="af-gui-icon-picker"></div>');
+    CRM.loadScript(CRM.config.resourceBase + 'js/jquery/jquery.crmIconPicker.js').done(function() {
+      $('#af-gui-icon-picker').crmIconPicker();
+    });
+  });
+
   angular.module('afGuiEditor').component('afGuiEditor', {
     templateUrl: '~/afGuiEditor/main.html',
     bindings: {
       };
 
       this.$onInit = function() {
-        // Shoehorn in a non-angular widget for picking icons
-        CRM.loadScript(CRM.config.resourceBase + 'js/jquery/jquery.crmIconPicker.js').done(function() {
-          $('#af-gui-icon-picker').crmIconPicker().change(function() {
-            if (editingIcon) {
-              $scope.$apply(function() {
-                editingIcon[editingIconProp] = $('#af-gui-icon-picker').val();
-                editingIcon = null;
-                $('#af-gui-icon-picker').val('').change();
-              });
-            }
-          });
-        });
         // Fetch the current form plus all blocks
         afAdmin.initialize(editor.name)
           .then(initializeForm);
         };
 
         $scope.pickAddIcon = function() {
-          openIconPicker($scope.node, 'add-icon');
+          afAdmin.pickIcon().then(function(val) {
+            $scope.node['add-icon'] = val;
+          });
         };
 
         function getBlockNode() {
         };
 
         $scope.pickIcon = function() {
-          openIconPicker($scope.node, 'crm-icon');
+          afAdmin.pickIcon().then(function(val) {
+            $scope.node['crm-icon'] = val;
+          });
         };
 
       }
     };
   });
 
-  var editingIcon, editingIconProp;
-  function openIconPicker(node, propName) {
-    editingIcon = node;
-    editingIconProp = propName;
-    $('#af-gui-icon-picker ~ .crm-icon-picker-button').click();
-  }
-
 })(angular, CRM.$, CRM._);