CRM-13783 - ProfileBuilder - Auto-add new custom field to canvas
[civicrm-core.git] / js / view / crm.designer.js
index 8d1509c3f15222a2bcf19c441e1fccccbf2eea20..6c33665f9749600469852d683b30be086e8b648e 100644 (file)
@@ -20,7 +20,7 @@
     result._collection = this.collection;
     result._options = this.options;
     return result;
-  }
+  };
 
   /**
    * Display a dialog window with an editable form for a UFGroupModel
@@ -99,6 +99,8 @@
             });
           }
           designerDialog.undoState = false;
+          // CRM-12188
+          CRM.designerApp.DetachedProfiles = [];
         },
         close: function() {
           window.onbeforeunload = designerDialog.oldOnBeforeUnload;
               return false;
             });
           }
+          // CRM-12188
+          CRM.designerApp.restorePreviewArea();
         },
         resize: function() {
           CRM.designerApp.vent.trigger('resize');
       'click .crm-designer-preview': 'doPreview'
     },
     onRender: function() {
-      this.$('.crm-designer-save').button();
+      this.$('.crm-designer-save').button().attr({
+        disabled: 'disabled',
+        style: 'opacity:.5; box-shadow:none; cursor:default;'
+      });
       this.$('.crm-designer-preview').button();
     },
+    initialize: function(options) {
+      CRM.designerApp.vent.on('ufUnsaved', this.onUfChanged, this);
+    },
+    onUfChanged: function(isUfUnsaved) {
+      if (isUfUnsaved) {
+        this.$('.crm-designer-save').removeAttr('style').removeAttr('disabled');
+      }
+    },
     doSave: function(event) {
       var ufGroupModel = this.model;
       if (ufGroupModel.getRel('ufFieldCollection').hasDuplicates()) {
       }
       var $dialog = this.$el.closest('.crm-designer-dialog'); // FIXME use events
       $dialog.block({message: 'Loading...', theme: true});
+      // CRM-12188
+      CRM.designerApp.clearPreviewArea();
       $.ajax({
         url: CRM.url("civicrm/ajax/inline"),
         type: 'POST',
     events: {
       'keyup .crm-designer-palette-search input': 'doSearch',
       'click .crm-designer-palette-clear-search': 'clearSearch',
-      'click .crm-designer-palette-refresh': 'doRefresh',
       'click .crm-designer-palette-toggle': 'toggleAll'
     },
     initialize: function() {
     },
     doAddField: function(section) {
       var paletteView = this;
-      var openAddNewWindow = function() {
-        var url = CRM.url('civicrm/admin/custom/group/field/add', {
-          reset: 1,
-          action: 'add',
-          gid: section.custom_group_id
-        });
-        window.open(url, '_blank');
-      };
-
-      if (paletteView.hideAddFieldAlert) {
-        openAddNewWindow();
-      } else {
-        CRM.confirm({
-          title: ts('Add Field'),
-          message: ts('A new window or tab will open. Use the new window to add your field, and then return to this window and click "Refresh."'),
-          onContinue: function() {
-            paletteView.hideAddFieldAlert = true;
-            openAddNewWindow();
+      var url = CRM.url('civicrm/admin/custom/group/field/add', {
+        reset: 1,
+        action: 'add',
+        gid: section.custom_group_id
+      });
+      CRM.loadForm(url, {
+        resetButton: 'next_new',
+        onSuccess: function(data, settings) {
+          paletteView.doRefresh('custom_' + data.customField.id);
+          if (data.buttonName != 'next_new') {
+            $(settings.target).dialog('close');
           }
-        });
-      }
+        }
+      });
       return false;
     },
-    doRefresh: function(event) {
+    doRefresh: function(fieldToAdd) {
       var ufGroupModel = this.model;
       CRM.Schema.reloadModels()
         .done(function(data){
           ufGroupModel.resetEntities();
+          if (fieldToAdd) {
+            var field = ufGroupModel.getRel('paletteFieldCollection').getFieldByName(null, fieldToAdd);
+            field.addToUFCollection(ufGroupModel.getRel('ufFieldCollection'));
+          }
         })
         .fail(function() {
           CRM.alert(ts('Failed to retrieve schema'), ts('Error'), 'error');
         fields: fields
       });
       this.form.on('change', this.onFormChange, this);
+      this.model.on('change', this.onModelChange, this);
     },
     render: function() {
       this.$el.html(this.form.render().el);
       this.onFormChange();
     },
+    onModelChange: function() {
+      $.each(this.form.fields, function(i, field) {
+        this.form.setValue(field.key, this.model.get(field.key));
+      });
+    },
     onFormChange: function() {
       this.form.commit();
       this.$('.field-is_multi_summary').toggle(this.options.fieldSchema.civiIsMultiple ? true : false);