Afform - Rename blocks and joins for clarity
[civicrm-core.git] / ext / afform / admin / ang / afGuiEditor / elements / afGuiContainer.component.js
index 0953d61916313b0c51bf484545713b145847e511..539d6b96c2e8165a0d0e61af8c2ca69e9472bcb2 100644 (file)
     },
     require: {editor: '^^afGuiEditor'},
     controller: function($scope, crmApi4, dialogService, afGui) {
-      var ts = $scope.ts = CRM.ts(),
+      var ts = $scope.ts = CRM.ts('org.civicrm.afform_admin'),
         ctrl = this;
 
       this.$onInit = function() {
-        if ((ctrl.node['#tag'] in afGui.meta.blocks) || ctrl.join) {
+        if (ctrl.node['#tag'] && ((ctrl.node['#tag'] in afGui.meta.blocks) || ctrl.join)) {
           var blockNode = getBlockNode(),
             blockTag = blockNode ? blockNode['#tag'] : null;
           if (blockTag && (blockTag in afGui.meta.blocks) && !afGui.meta.blocks[blockTag].layout) {
             ctrl.loading = true;
             crmApi4('Afform', 'loadAdminData', {
-              definition: {name: afGui.meta.blocks[blockTag].name}
+              definition: {name: afGui.meta.blocks[blockTag].name},
+              skipEntities: _.transform(afGui.meta.entities, function(result, entity, entityName) {
+                if (entity.fields) {
+                  result.push(entityName);
+                }
+              }, [])
             }, 0).then(function(data) {
               afGui.addMeta(data);
               initializeBlockContainer();
         }
       };
 
+      this.sortableOptions = {
+        handle: '.af-gui-bar',
+        connectWith: '[ui-sortable]',
+        cancel: 'input,textarea,button,select,option,a,.dropdown-menu',
+        placeholder: 'af-gui-dropzone',
+        tolerance: 'pointer',
+        scrollSpeed: 8,
+        containment: '#afGuiEditor-canvas-body'
+      };
+
       $scope.isSelectedFieldset = function(entityName) {
         return entityName === ctrl.editor.getSelectedEntityName();
       };
         };
 
         _.each(afGui.meta.blocks, function(blockInfo, directive) {
-          if (directive === ctrl.node['#tag'] || blockInfo.join === ctrl.getFieldEntityType()) {
+          if (directive === ctrl.node['#tag'] || (blockInfo.join_entity && blockInfo.join_entity === ctrl.getFieldEntityType())) {
             block.options.push({
               id: directive,
               text: blockInfo.title
         var model = {
           title: '',
           name: null,
+          type: 'block',
           layout: ctrl.node['#children']
         };
         if (ctrl.join) {
           model.block = afGui.meta.blocks[$scope.block.original].block;
         }
         else {
-          model.block = ctrl.container.getFieldEntityType() || '*';
+          model.block = ctrl.getFieldEntityType();
         }
         dialogService.open('saveBlockDialog', '~/afGuiEditor/saveBlock.html', model, options)
           .then(function(block) {
       this.node = ctrl.node;
 
       this.getNodeType = function(node) {
-        if (!node) {
+        if (!node || !node['#tag']) {
           return null;
         }
         if (node['#tag'] === 'af-field') {
           return 'field';
         }
-        if (node['af-fieldset']) {
+        if ('af-fieldset' in node) {
           return 'fieldset';
         }
         if (node['af-join']) {
         if (node['#tag'] && node['#tag'] in afGui.meta.blocks) {
           return 'container';
         }
+        if (node['#tag'] && (node['#tag'].slice(0, 19) === 'crm-search-display-')) {
+          return 'searchDisplay';
+        }
         var classes = afGui.splitClass(node['class']),
           types = ['af-container', 'af-text', 'af-button', 'af-markup'],
           type = _.intersection(types, classes);
       };
 
       this.getEntityName = function() {
-        return ctrl.entityName.split('-join-')[0];
+        return ctrl.entityName ? ctrl.entityName.split('-join-')[0] : null;
       };
 
       // Returns the primary entity type for this container e.g. "Contact"
       };
 
       // Returns the entity type for fields within this conainer (join entity type if this is a join, else the primary entity type)
-      this.getFieldEntityType = function() {
-        var joinType = ctrl.entityName.split('-join-');
-        return joinType[1] || (ctrl.editor && ctrl.editor.getEntity(joinType[0]).type);
+      this.getFieldEntityType = function(fieldName) {
+        // If entityName is declared for this fieldset, return entity-type or join-type
+        if (ctrl.entityName) {
+          var joinType = ctrl.entityName.split('-join-');
+          return joinType[1] || (ctrl.editor && ctrl.editor.getEntity(joinType[0]).type);
+        }
+        // If entityName is not declared, this field belongs to a search
+        var entityType,
+          prefix = _.includes(fieldName, '.') ? fieldName.split('.')[0] : null;
+        _.each(afGui.meta.searchDisplays, function(searchDisplay) {
+          if (prefix) {
+            _.each(searchDisplay['saved_search.api_params'].join, function(join) {
+              var joinInfo = join[0].split(' AS ');
+              if (prefix === joinInfo[1]) {
+                entityType = joinInfo[0];
+                return false;
+              }
+            });
+          }
+          if (!entityType && fieldName && afGui.getField(searchDisplay['saved_search.api_entity'], fieldName)) {
+            entityType = searchDisplay['saved_search.api_entity'];
+          }
+          if (entityType) {
+            return false;
+          }
+        });
+        return entityType || _.map(afGui.meta.searchDisplays, 'saved_search.api_entity')[0];
       };
 
     }