Add forms, add/remove entities
authorColeman Watts <coleman@civicrm.org>
Sun, 3 Nov 2019 00:14:47 +0000 (20:14 -0400)
committerCiviCRM <info@civicrm.org>
Wed, 16 Sep 2020 02:13:20 +0000 (19:13 -0700)
ext/afform/gui/ang/afGuiEditor.js
ext/afform/gui/ang/afGuiEditor/config-form.html
ext/afform/gui/ang/afGuiEditor/palette.html
ext/afform/gui/ang/afGuiList.aff.html

index 95692554266f56f5a2ed9b5e53feea27c78d8625..1e65a946fe4e6589781e1b28651365f98573a490 100644 (file)
         $scope.meta = CRM.afformAdminData;
         $scope.controls = {};
         $scope.fieldList = {};
-        $scope.editor = this;
+        var editor = $scope.editor = this;
         var newForm = {
           title: ts('Untitled Form'),
           layout: [{
             '#tag': 'af-form',
             ctrl: 'modelListCtrl',
-            '#children': [
-              {
-                '#tag': 'af-entity',
-                type: 'Contact',
-                data: {
-                  contact_type: 'Individual'
-                },
-                name: 'Contact1',
-                label: 'Contact 1',
-                'url-autofill': '1',
-                autofill: 'user'
-              }
-            ]
+            '#children': []
           }]
         };
-        if ($scope.afGuiEditor.name) {
+        if ($scope.afGuiEditor.name && $scope.afGuiEditor.name != '0') {
+          // Todo - show error msg if form is not found
           crmApi4('Afform', 'get', {where: [['name', '=', $scope.afGuiEditor.name]], layoutFormat: 'shallow'}, 0)
             .then(initialize);
         }
         else {
-          initialize(newForm);
+          $timeout(function() {
+            initialize(_.cloneDeep(newForm));
+            editor.addEntity('Contact');
+          });
         }
 
         function initialize(afform) {
-          // Todo - show error msg if form is not found
           $scope.afform = afform;
           $scope.layout = getTags($scope.afform.layout, 'af-form')[0];
           evaluate($scope.layout['#children']);
             label: entityType + ' ' + num
           };
           $scope.layout['#children'].unshift($scope.entities[entityType + num]);
-          this.selectEntity(entityType + num);
+          $scope.layout['#children'].push({
+            '#tag': 'fieldset',
+            'af-fieldset': entityType + num,
+            '#children': [
+              {
+                '#tag': 'legend',
+                'class': 'af-text',
+                '#children': [
+                  {
+                    '#text': entityType + ' ' + num
+                  }
+                ]
+              }
+            ]
+          });
+          buildFieldList(entityType + num);
+          return entityType + num;
         };
 
         this.removeEntity = function(entityName) {
           delete $scope.entities[entityName];
           _.remove($scope.layout['#children'], {'#tag': 'af-entity', name: entityName});
+          removeRecursive($scope.layout['#children'], {'af-fieldset': entityName});
           this.selectEntity(null);
         };
 
           return $scope.selectedEntity;
         };
 
+        $scope.addEntity = function(entityType) {
+          var entityName = editor.addEntity(entityType);
+          editor.selectEntity(entityName);
+        };
+
         $scope.rebuildFieldList = function() {
           $timeout(function() {
             $scope.$apply(function() {
           _.each(collection, function (item) {
             if (_.isPlainObject(item)) {
               if (item['af-fieldset']) {
-                expandFields(item['#children'], $scope.editor.getEntity(item['af-fieldset']).type);
+                expandFields(item['#children'], editor.getEntity(item['af-fieldset']).type);
               }
               else if (item['#tag'] === 'af-field') {
                 item.defn = item.defn || {};
-                _.defaults(item.defn, _.cloneDeep(_.pick($scope.editor.getField(entityType, item.name), ['title', 'input_type', 'input_attrs'])));
+                _.defaults(item.defn, _.cloneDeep(_.pick(editor.getField(entityType, item.name), ['title', 'input_type', 'input_attrs'])));
               } else {
                 expandFields(item['#children'], entityType);
               }
     return str ? _.unique(_.trim(str).split(/\s+/g)) : [];
   }
 
+  function removeRecursive(collection, removeParams) {
+    _.remove(collection, removeParams);
+    _.each(collection, function(item) {
+      if (_.isPlainObject(item) && item['#children']) {
+        removeRecursive(item['#children'], removeParams);
+      }
+    });
+  }
+
   angular.module('afGuiEditor').directive('afGuiBlock', function() {
     return {
       restrict: 'A',
         node: '=afGuiText'
       },
       require: '^^afGuiBlock',
-      link: {
-        pre: function($scope, element, attrs, block) {
-          $scope.block = block;
-        },
-        post: function($scope, element, attrs) {
-          if ($scope.block.node && $scope.block.node['#tag'] === 'fieldset') {
-            $scope.tags.legend = ts('Fieldset Legend');
-          }
-        }
+      link: function($scope, element, attrs, block) {
+        $scope.block = block;
       },
       controller: function($scope) {
         $scope.tags = {
           p: ts('Normal Text'),
+          legend: ts('Fieldset Legend'),
           h1: ts('Heading 1'),
           h2: ts('Heading 2'),
           h3: ts('Heading 3'),
index 2af098cdb7e8e81eb6de2e8f3cb6f77a502c0ffb..26794a3cbfc4f236b710d364c0d69ac4ff838d2b 100644 (file)
@@ -2,6 +2,10 @@
   {{ ts('Title:') }}
 </label>
 <input ng-model="afform.title" class="form-control" id="af_config_form_title" />
+<label for="af_config_form_description">
+  {{ ts('Description:') }}
+</label>
+<input ng-model="afform.description" class="form-control" id="af_config_form_description" />
 <label for="af_config_form_server_route">
   {{ ts('URL:') }}
 </label>
index 5278c642fc491434de66495f03def5f04c8c4d31..3e3517aa20d5b1244f42e471fe75c1999e0e25f1 100644 (file)
@@ -16,7 +16,7 @@
         </a>
         <ul class="dropdown-menu">
           <li ng-repeat="entity in meta.entities">
-            <a href ng-click="editor.addEntity(entity.name)">{{ entity.name }}</a>
+            <a href ng-click="addEntity(entity.name)">{{ entity.name }}</a>
           </li>
         </ul>
       </li>
index ed4a6286ee8cf074791a9da502672ec9698ed110..c87d00947aa8bad283379a18a1d45bed070f2fe8 100644 (file)
@@ -1,3 +1,6 @@
+<a href="#/?name=0" class="btn btn-default">
+  <i class="crm-i fa-plus"></i> {{ ts('New Form') }}
+</a>
 <div
   af-api4="['Afform', 'get', {select: ['name','title','is_public','server_route'], orderBy: {name:'ASC'}}]"
   af-api4-ctrl="listCtrl">