Merge pull request #3691 from eileenmcnaughton/master
[civicrm-core.git] / js / view / crm.designer.js
index dba7d32b2980e236fbe55e924404ec65504eaa83..07501e6b8ff340ef318e35909b5534cf46a76f12 100644 (file)
@@ -49,6 +49,7 @@
 
     initialize: function(options) {
       CRM.designerApp.vent.on('ufUnsaved', this.onUfChanged, this);
+      CRM.designerApp.vent.on('ufSaved', this.onUfSaved, this);
     },
     onClose: function() {
       this.undoAlert && this.undoAlert.close && this.undoAlert.close();
     onUfChanged: function(isUfUnsaved) {
       this.isUfUnsaved = isUfUnsaved;
     },
+    onUfSaved: function() {
+      CRM.designerApp.vent.off('ufUnsaved', this.onUfChanged, this);
+      this.isUfUnsaved = false;
+    },
     onRender: function() {
       var designerDialog = this;
       designerDialog.$el.dialog({
         autoOpen: true, // note: affects accordion height
-        title: 'Edit Profile',
+        title: ts('Edit Profile'),
+        modal: true,
         width: '75%',
-        height: 600,
+        height: parseInt($(window).height() *.8, 10),
         minWidth: 500,
         minHeight: 600, // to allow dropping in big whitespace, coordinate with min-height of .crm-designer-fields
         open: function() {
@@ -83,7 +89,7 @@
           // Initialize new dialog if we are not re-opening unsaved changes
           if (designerDialog.undoState === false) {
             designerDialog.designerRegion && designerDialog.designerRegion.close && designerDialog.designerRegion.close();
-            designerDialog.$el.block({message: 'Loading...', theme: true});
+            designerDialog.$el.block();
             designerDialog.options.findCreateUfGroupModel({
               onLoad: function(ufGroupModel) {
                 designerDialog.model = ufGroupModel;
 
           designerDialog.undoAlert && designerDialog.undoAlert.close && designerDialog.undoAlert.close();
           if (designerDialog.isUfUnsaved) {
-            designerDialog.undoAlert = CRM.alert('<p>' + ts('Your changes to "%1" have not been saved.', {1: designerDialog.model.get('title')}) + '</p><a href="#" class="crm-undo">' + ts('Restore unsaved changes') + '</a>', ts('Unsaved Changes'), 'alert', {expires: 60000});
-            $('.ui-notify-message a.crm-undo').click(function() {
+            designerDialog.undoAlert = CRM.alert('<p>' + ts('%1 has not been saved.', {1: designerDialog.model.get('title')}) + '</p><a href="#" class="crm-undo">' + ts('Restore') + '</a>', ts('Unsaved Changes'), 'alert', {expires: 60000});
+            $('.ui-notify-message a.crm-undo').button({icons: {primary: 'ui-icon-arrowreturnthick-1-w'}}).click(function(e) {
+              e.preventDefault();
               designerDialog.undoState = true;
               designerDialog.$el.dialog('open');
-              return false;
             });
           }
           // CRM-12188
         this.$('.crm-designer-save').removeAttr('style').prop('disabled', false);
       }
     },
-    doSave: function(event) {
+    doSave: function(e) {
+      e.preventDefault();
       var ufGroupModel = this.model;
       if (ufGroupModel.getRel('ufFieldCollection').hasDuplicates()) {
         CRM.alert(ts('Please correct errors before saving.'), '', 'alert');
         return;
       }
       var $dialog = this.$el.closest('.crm-designer-dialog'); // FIXME use events
-      $dialog.block({message: 'Saving...', theme: true});
+      $dialog.block();
       var profile = ufGroupModel.toStrictJSON();
       profile["api.UFField.replace"] = {values: ufGroupModel.getRel('ufFieldCollection').toSortedJSON(), 'option.autoweight': 0};
       CRM.api('UFGroup', 'create', profile, {
           }
         }
       });
-      return false;
     },
-    doPreview: function(event) {
+    doPreview: function(e) {
+      e.preventDefault();
       this.previewMode = !this.previewMode;
       if (!this.previewMode) {
         $('.crm-designer-preview-canvas').html('');
         return;
       }
       var $dialog = this.$el.closest('.crm-designer-dialog'); // FIXME use events
-      $dialog.block({message: 'Loading...', theme: true});
+      $dialog.block();
       // CRM-12188
       CRM.designerApp.clearPreviewArea();
-      $.ajax({
-        url: CRM.url("civicrm/ajax/inline"),
-        type: 'POST',
-        data: {
-          'qfKey': CRM.profilePreviewKey,
-          'class_name': 'CRM_UF_Form_Inline_Preview',
-          'snippet': 1,
-          'ufData': JSON.stringify({
-            ufGroup: this.model.toStrictJSON(),
-            ufFieldCollection: this.model.getRel('ufFieldCollection').toSortedJSON()
-          })
-        }
+      $.post(CRM.url("civicrm/ajax/inline"), {
+        'qfKey': CRM.profilePreviewKey,
+        'class_name': 'CRM_UF_Form_Inline_Preview',
+        'snippet': 1,
+        'ufData': JSON.stringify({
+          ufGroup: this.model.toStrictJSON(),
+          ufFieldCollection: this.model.getRel('ufFieldCollection').toSortedJSON()
+        })
       }).done(function(data) {
         $dialog.unblock();
         $('.crm-designer-canvas > *, .crm-designer-palette-region').hide();
-        $('.crm-designer-preview-canvas').html(data).show();
+        $('.crm-designer-preview-canvas').html(data).show().trigger('crmLoad').find(':input').prop('readOnly', true);
         $('.crm-designer-preview span').html(ts('Edit'));
       });
-      return false;
     }
   });
 
       var div = this.$('.crm-designer-palette-tree').closest('.crm-container').height();
       this.$('.crm-designer-palette-tree').css({height: div - pos.top});
     },
-    doSearch: function(event) {
-      $('.crm-designer-palette-tree').jstree("search", $(event.target).val());
+    doSearch: function(e) {
+      var str = $(e.target).val();
+      this.$('.crm-designer-palette-clear-search').css('visibility', str ? 'visible' : 'hidden');
+      this.$('.crm-designer-palette-tree').jstree("search", str);
     },
     doSetPaletteEntity: function(event) {
       this.selectedContactType = $('.crm-contact-types :selected').val();
       paletteFieldModel.addToUFCollection(this.model.getRel('ufFieldCollection'));
       event.stopPropagation();
     },
-    doNewCustomFieldDialog: function(event) {
+    doNewCustomFieldDialog: function(e) {
+      e.preventDefault();
       var paletteView = this;
-      var entityKey = $(event.currentTarget).closest('.crm-designer-palette-section').attr('data-entity');
-      var sectionKey = $(event.currentTarget).closest('.crm-designer-palette-section').attr('data-section');
+      var entityKey = $(e.currentTarget).closest('.crm-designer-palette-section').attr('data-entity');
+      var sectionKey = $(e.currentTarget).closest('.crm-designer-palette-section').attr('data-section');
       var ufEntityModel = paletteView.model.getRel('ufEntityCollection').getByName(entityKey);
       var sections = ufEntityModel.getSections();
       var url = CRM.url('civicrm/admin/custom/group/field/add', {
       CRM.loadForm(url).on('crmFormSuccess', function(e, data) {
         paletteView.doRefresh('custom_' + data.id);
       });
-      return false;
     },
-    doNewCustomSetDialog: function(event) {
+    doNewCustomSetDialog: function(e) {
+      e.preventDefault();
       var paletteView = this;
       var url = CRM.url('civicrm/admin/custom/group', 'action=add&reset=1');
       // Create custom field set and automatically go to next step (create fields) after save button is clicked.
           data.customField && ($(this).data('civiCrmSnippet').options.crmForm.refreshAction = ['next_new']);
           paletteView.doRefresh(data.customField ? 'custom_' + data.id : null);
         });
-      return false;
     },
     doRefresh: function(fieldToAdd) {
       var ufGroupModel = this.model;
           CRM.alert(ts('Failed to retrieve schema'), ts('Error'), 'error');
         });
     },
-    clearSearch: function(event) {
+    clearSearch: function(e) {
+      e.preventDefault();
       $('.crm-designer-palette-search input').val('').keyup();
-      return false;
     },
     toggleActive: function(ufFieldModel, ufFieldCollection, options) {
       var paletteFieldCollection = this.model.getRel('paletteFieldCollection');
         this.$('[data-plm-cid='+paletteFieldModel.cid+']').toggleClass('disabled', !isAddable);
       }
     },
-    toggleAll: function(event) {
+    toggleAll: function(e) {
       if ($('.crm-designer-palette-search input').val() == '') {
-        $('.crm-designer-palette-tree').jstree($(event.target).attr('rel'));
+        $('.crm-designer-palette-tree').jstree($(e.target).attr('rel'));
       }
-      return false;
+      e.preventDefault();
     },
     getOpenTreeNodes: function() {
       var paletteView = this;
         }
       }
     },
-    doEditCustomField: function() {
+    doEditCustomField: function(e) {
+      e.preventDefault();
       var url = CRM.url('civicrm/admin/custom/group/field/update', {
         action: 'update',
         reset: 1,
             var buttons = {};
             buttons[$link.text()] = function() {
               var form2 = CRM.loadForm($link.attr('href'), {
-                openInline: 'a.action-item:not([href="#"])',
                 dialog: {
                   width: '60%',
                   height: '70%'
                 }
               });
-            }
+            };
             $(this).dialog('option', 'buttons', buttons);
           }
-        })
-      return false;
+        });
     },
     onChangeIsDuplicate: function(model, value, options) {
       this.$el.toggleClass('crm-designer-duplicate', value);