Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-12-01-14-40-22
[civicrm-core.git] / js / crm.ajax.js
index 31eed91b2d92414a3b3f2f5d96a391ccaa85f8d8..276d02ddfd729d9ebaf9574c37d2baeba7fdd29c 100644 (file)
@@ -15,7 +15,7 @@
       return tplURL = path;
     }
     if (!tplURL) {
-      console && console.log && console.log('Warning: CRM.url called before initialization');
+      CRM.console('error', 'Error: CRM.url called before initialization');
     }
     if (!mode) {
       mode = CRM.config && CRM.config.isFrontend ? 'front' : 'back';
     return url;
   };
 
-  // Backwards compatible with jQuery fn
+  // @deprecated
   $.extend ({'crmURL':
     function (p, params) {
-      console && console.log && console.log('Calling crmURL from jQuery is deprecated. Please use CRM.url() instead.');
+      CRM.console('warn', 'Calling crmURL from jQuery is deprecated. Please use CRM.url() instead.');
       return CRM.url(p, params);
     }
   });
    * @deprecated
    */
   $.fn.crmAPI = function(entity, action, params, options) {
-    console && console.log && console.log('Calling crmAPI from jQuery is deprecated. Please use CRM.api() instead.');
+    CRM.console('warn', 'Calling crmAPI from jQuery is deprecated. Please use CRM.api3() instead.');
     return CRM.api.call(this, entity, action, params, options);
   };
 
           return;
         }
         data.url = url;
-        that.element.trigger('crmBeforeLoad', data).html(data.content);
+        that.element.trigger('crmUnload').trigger('crmBeforeLoad', data);
+        that._beforeRemovingContent();
+        that.element.html(data.content);
         that._handleOrderLinks();
         that.element.trigger('crmLoad', data);
         that.options.crmForm && that.element.trigger('crmFormLoad', data);
         that._onFailure();
       });
     },
-    _destroy: function() {
-      this.element.removeClass('crm-ajax-container');
+    // Perform any cleanup needed before removing/replacing content
+    _beforeRemovingContent: function() {
+      var that = this;
+      if (window.tinyMCE && tinyMCE.editors) {
+        $.each(tinyMCE.editors, function(k) {
+          if ($.contains(that.element[0], this.getElement())) {
+            this.remove();
+          }
+        });
+      }
       this.options.crmForm && $('form', this.element).ajaxFormUnbind();
+    },
+    _destroy: function() {
+      this.element.removeClass('crm-ajax-container').trigger('crmUnload');
+      this._beforeRemovingContent();
       if (this._originalContent !== null) {
         this.element.empty().append(this._originalContent);
       }
       if (typeof settings.dialog.height === 'string' && settings.dialog.height.indexOf('%') > 0) {
         settings.dialog.height = parseInt($(window).height() * (parseFloat(settings.dialog.height)/100), 10);
       }
+      // Increase percent width on small screens
+      if (typeof settings.dialog.width === 'string' && settings.dialog.width.indexOf('%') > 0) {
+        var screenWidth = $(window).width(),
+          percentage = parseInt(settings.dialog.width.replace('%', ''), 10),
+          gap = 100-percentage;
+        if (screenWidth < 701) {
+          settings.dialog.width = '100%';
+        }
+        else if (screenWidth < 1400) {
+          settings.dialog.width = '' + parseInt(percentage+gap-((screenWidth - 700)/7*(gap)/100), 10) + '%';
+        }
+      }
       $('<div id="'+ settings.target.substring(1) +'"><div class="crm-loading-element">' + ts('Loading') + '...</div></div>').dialog(settings.dialog);
       $(settings.target)
         .on('dialogclose', function() {
               this.updateElement && this.updateElement();
             });
           }
+          if (window.tinyMCE && tinyMCE.editors) {
+            $.each(tinyMCE.editors, function() {
+              this.save();
+            });
+          }
         },
         beforeSubmit: function(submission) {
           $.each(formErrors, function() {
       }
       // Show form buttons as part of the dialog
       if ($el.data('uiDialog')) {
-        var buttons = [], added = [];
-        $('input.crm-form-submit, a.button', $el).each(function() {
+        var buttonContainers = '.crm-submit-buttons, .action-link',
+          buttons = [],
+          added = [];
+        $(buttonContainers, $el).find('input.crm-form-submit, a.button').each(function() {
           var $el = $(this),
             label = $el.is('input') ? $el.attr('value') : $el.text(),
             identifier = $el.attr('name') || $el.attr('href');
           if (!identifier || identifier === '#' || $.inArray(identifier, added) < 0) {
-            var button = {text: label, click: function () {
-              $el.click();
-            }};
-            if ($el.find('.icon').length) {
-              button.icons = {primary: $el.find('.icon').attr('class')};
+            var $icon = $el.find('.icon'),
+              button = {'data-identifier': identifier, text: label, click: function() {
+                $el.click();
+              }};
+            if ($icon.length) {
+              button.icons = {primary: $icon.attr('class')};
             } else {
-              button.icons = {primary: $el.hasClass('cancel') ? 'ui-icon-close' : 'ui-icon-check'};
-              if (identifier.substr(identifier.length-4) === '_new') {
-                button.icons.secondary = 'ui-icon-plus';
-              }
+              var action = $el.hasClass('cancel') ? 'close' : (identifier.substr(identifier.length-4) === '_new' ? 'plus' : 'check');
+              button.icons = {primary: 'ui-icon-' + action};
             }
             buttons.push(button);
             added.push(identifier);
           }
-          $el.hide().parents('.crm-button, .crm-submit-buttons, .action-link').hide();
+          // display:none causes the form to not submit when pressing "enter"
+          $el.parents(buttonContainers).css({height: 0, padding: 0, margin: 0, overflow: 'hidden'});
         });
         $el.dialog('option', 'buttons', buttons);
       }
     else if ($el.hasClass('medium-popup')) {
       settings.dialog.width = settings.dialog.height = '50%';
     }
-    else if ($el.hasClass('huge-popup')) {
-      settings.dialog.height = '90%';
-    }
     var dialog = popup(url, settings);
     // Trigger events from the dialog on the original link element
     $el.trigger('crmPopupOpen', [dialog]);