CRM-10693 - AJAX - Standardize protocol for page, form, messages
[civicrm-core.git] / js / Common.js
index 437ecffd8bab383f074a3da952cfe98ec5994910..21dca3559eb8042dd686f3a7637a30034d21bd27 100644 (file)
@@ -596,9 +596,9 @@ CRM.validate = CRM.validate || {
   });
 
   $.fn.crmtooltip = function () {
-    $('a.crm-summary-link:not(.crm-processed)')
-      .addClass('crm-processed')
-      .on('mouseover', function (e) {
+    $(document)
+      .on('mouseover', 'a.crm-summary-link:not(.crm-processed)', function (e) {
+        $(this).addClass('crm-processed');
         $(this).addClass('crm-tooltip-active');
         var topDistance = e.pageY - $(window).scrollTop();
         if (topDistance < 300 | topDistance < $(this).children('.crm-tooltip-wrapper').height()) {
@@ -611,10 +611,11 @@ CRM.validate = CRM.validate || {
             .load(this.href);
         }
       })
-      .on('mouseout', function () {
+      .on('mouseout', 'a.crm-summary-link', function () {
+        $(this).removeClass('crm-processed');
         $(this).removeClass('crm-tooltip-active crm-tooltip-down');
       })
-      .on('click', false);
+      .on('click', 'a.crm-summary-link', false);
   };
 
   var h;
@@ -819,6 +820,22 @@ CRM.validate = CRM.validate || {
     });
   }
 
+  // Preprocess all cj ajax calls to display messages
+  $(document).ajaxSuccess(function(event, xhr, settings) {
+    try {
+      if ((!settings.dataType || settings.dataType == 'json') && xhr.responseText) {
+        var response = $.parseJSON(xhr.responseText);
+        if (typeof(response.crmMessages) == 'object') {
+          $.each(response.crmMessages, function(n, msg) {
+            CRM.alert(msg.text, msg.title, msg.type, msg.options);
+          })
+        }
+      }
+    }
+    // Suppress errors
+    catch (e) {}
+  });
+
   $(function () {
     if ($('#crm-notification-container').length) {
       // Initialize notifications
@@ -883,8 +900,9 @@ CRM.validate = CRM.validate || {
   /**
    * Clientside currency formatting
    * @param value
-   * @param format
+   * @param format - currency representation of the number 1234.56
    * @return string
+   * @see CRM_Core_Resources::addCoreResources
    */
   var currencyTemplate;
   CRM.formatMoney = function(value, format) {
@@ -903,7 +921,6 @@ CRM.validate = CRM.validate || {
     sign = (value < 0) ? '-' : '';
     //extracting the absolute value of the integer part of the number and converting to string
     i = parseInt(value = Math.abs(value).toFixed(2)) + '';
-
     j = ((j = i.length) > 3) ? j % 3 : 0;
     result = sign + (j ? i.substr(0, j) + separator : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + separator) + (2 ? decimal + Math.abs(value - i).toFixed(2).slice(2) : '');
     return format.replace(/1.*234.*56/, result);