CRM-14215 - Fix group tpl markup
[civicrm-core.git] / js / view / crm.designer.js
index 43abe18831e7c8ef87287b94beac0d3a7b6f70d8..de2e88ab04ec10c95d8b2f9fa7ab3f8a8b574a75 100644 (file)
     openTreeNodes: [],
     events: {
       'keyup .crm-designer-palette-search input': 'doSearch',
+      'change .crm-contact-types': 'doSetPaletteEntity',
       'click .crm-designer-palette-clear-search': 'clearSearch',
       'click .crm-designer-palette-toggle': 'toggleAll',
       'click .crm-designer-palette-add button': 'doNewCustomFieldDialog',
       var paletteFieldsByEntitySection = this.model.getRel('paletteFieldCollection').getFieldsByEntitySection();
 
       paletteView.model.getRel('ufEntityCollection').each(function(ufEntityModel){
-         var ufGroupTypes = [];
+        _.each(ufEntityModel.getSections(), function(section, sectionKey){
+          var defaultValue = paletteView.selectedContactType;
+          if (!defaultValue) {
+            defaultValue = paletteView.model.calculateContactEntityType();
+          }
 
-         if (typeof paletteView.model.attributes.group_type == 'undefined'){
-             //Case 'Copy' where we don't get the group_type directly
-             //TODO: extract the group_type from it and split to carry forward the normal functionality
-             //as in case of Edit
-             CRM.api('UFGroup', 'getsingle' , {'name': paletteView.model.previousAttributes().name}, {
-                 success: function(data) {
-                     ufGroupTypes.push(data.group_type.split(','));
-                 }
-             });
-             //console.log(ufGroupTypes['']);
-         }
-         else {
-             ufGroupTypes = paletteView.model.attributes.group_type.split(',');
-         }
+          // set selected option as default, since we are rebuilding palette
+          paletteView.$('.crm-contact-types').val(defaultValue).prop('selected','selected');
 
-         var allowedEntityNames = [];
-         for(key in ufGroupTypes) {
-             allowedEntityNames[key] = CRM.UF.guessEntityName(ufGroupTypes[key]);
-         }
-        _.each(ufEntityModel.getSections(), function(section, sectionKey){
-           var entitySection = null;
-           if ($.inArray(ufEntityModel.get('entity_name'), allowedEntityNames) != -1 || !ufGroupTypes) {
-               entitySection = ufEntityModel.get('entity_name') + '-' + sectionKey;
-           }
+          var entitySection = ufEntityModel.get('entity_name') + '-' + sectionKey;
           var items = [];
-          if (entitySection) {
-            if (paletteFieldsByEntitySection[entitySection]) {
-              _.each(paletteFieldsByEntitySection[entitySection], function(paletteFieldModel, k) {
-                items.push({data: paletteFieldModel.getLabel(), attr: {'class': 'crm-designer-palette-field', 'data-plm-cid': paletteFieldModel.cid}});
-              });
-            }
-            if (section.is_addable) {
-              items.push({data: ts('+ Add New Field'), attr: {'class': 'crm-designer-palette-add'}});
-            }
-            if (items.length > 0) {
-             treeData.push({
-                data: section.title,
-                children: items,
-                state: _.contains(paletteView.openTreeNodes, sectionKey) ? 'open' : 'closed',
-                attr: {
-                  'class': 'crm-designer-palette-section',
-                  'data-section': sectionKey,
-                  'data-entity': ufEntityModel.get('entity_name')
-               }
-              });
-            }
-         }
+          if (paletteFieldsByEntitySection[entitySection]) {
+            _.each(paletteFieldsByEntitySection[entitySection], function(paletteFieldModel, k) {
+              items.push({data: paletteFieldModel.getLabel(), attr: {'class': 'crm-designer-palette-field', 'data-plm-cid': paletteFieldModel.cid}});
+            });
+          }
+          if (section.is_addable) {
+            items.push({data: ts('+ Add New Field'), attr: {'class': 'crm-designer-palette-add'}});
+          }
+          if (items.length > 0) {
+            treeData.push({
+              data: section.title,
+              children: items,
+              state: _.contains(paletteView.openTreeNodes, sectionKey) ? 'open' : 'closed',
+              attr: {
+                'class': 'crm-designer-palette-section',
+                'data-section': sectionKey,
+                'data-entity': ufEntityModel.get('entity_name')
+              }
+            });
+          }
         })
       });
 
     doSearch: function(event) {
       $('.crm-designer-palette-tree').jstree("search", $(event.target).val());
     },
+    doSetPaletteEntity: function(event) {
+      this.selectedContactType = $('.crm-contact-types :selected').val();
+      // loop through entity collection and remove non-valid entity section's
+      var newUfEntityModels = [];
+      this.model.getRel('ufEntityCollection').each(function(oldUfEntityModel){
+        var values = oldUfEntityModel.toJSON();
+        if (values.entity_name == 'contact_1') {
+          values.entity_type = $('.crm-contact-types :selected').val();
+        }
+        newUfEntityModels.push(new CRM.UF.UFEntityModel(values));
+      });
+      this.model.getRel('ufEntityCollection').reset(newUfEntityModels);
+    },
     doAddToCanvas: function(event) {
       var paletteFieldModel = this.model.getRel('paletteFieldCollection').get($(event.currentTarget).attr('data-plm-cid'));
       paletteFieldModel.addToUFCollection(this.model.getRel('ufFieldCollection'));
       var paletteFieldCollection = this.model.getRel('paletteFieldCollection');
       var paletteFieldModel = paletteFieldCollection.getFieldByName(ufFieldModel.get('entity_name'), ufFieldModel.get('field_name'));
       var isAddable = ufFieldCollection.isAddable(ufFieldModel);
-      this.$('[data-plm-cid='+paletteFieldModel.cid+']').toggleClass('disabled', !isAddable);
+      if (paletteFieldModel) {
+        this.$('[data-plm-cid='+paletteFieldModel.cid+']').toggleClass('disabled', !isAddable);
+      }
     },
     toggleAll: function(event) {
       if ($('.crm-designer-palette-search input').val() == '') {
       this.model.getRel('ufFieldCollection')
         .on('add', this.updatePlaceholder, this)
         .on('remove', this.updatePlaceholder, this)
-        .on('add', this.addUFFieldView, this);
+        .on('add', this.addUFFieldView, this)
+        .on('reset', this.render, this);
     },
     onClose: function() {
       this.model.getRel('ufFieldCollection')
         .off('add', this.updatePlaceholder, this)
         .off('remove', this.updatePlaceholder, this)
-        .off('add', this.addUFFieldView, this);
+        .off('add', this.addUFFieldView, this)
+        .off('reset', this.render, this);
     },
     render: function() {
       var ufFieldCanvasView = this;