Merge pull request #2638 from colemanw/CRM-14196
[civicrm-core.git] / js / Common.js
index 8f5294ef60db3c13e12c62db59dc18e291cfe68b..d0108f6ac11359ad5567fbfe589255ed89c045d5 100644 (file)
@@ -1,6 +1,6 @@
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -426,10 +426,6 @@ function popUp(URL) {
   eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=640,height=420,left = 202,top = 184');");
 }
 
-function imagePopUp(path) {
-  window.open(path, 'popupWindow', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,screenX=150,screenY=150,top=150,left=150');
-}
-
 /**
  * Function to show / hide the row in optionFields
  *
@@ -451,58 +447,6 @@ function showHideRow(index) {
   return false;
 }
 
-/**
- * Function to check activity status in relavent to activity date
- *
- * @param element message JSON object.
- */
-function activityStatus(message) {
-  var d = new Date(), time = [], i;
-  var currentDateTime = d.getTime()
-  var activityTime = cj("input#activity_date_time_time").val().replace(":", "");
-
-  //chunk the time in bunch of 2 (hours,minutes,ampm)
-  for (i = 0; i < activityTime.length; i += 2) {
-    time.push(activityTime.slice(i, i + 2));
-  }
-  var activityDate = new Date(cj("input#activity_date_time_hidden").val());
-
-  d.setFullYear(activityDate.getFullYear());
-  d.setMonth(activityDate.getMonth());
-  d.setDate(activityDate.getDate());
-  var hours = time['0'];
-  var ampm = time['2'];
-
-  if (ampm == "PM" && hours != 0 && hours != 12) {
-    // force arithmetic instead of string concatenation
-    hours = hours * 1 + 12;
-  }
-  else {
-    if (ampm == "AM" && hours == 12) {
-      hours = 0;
-    }
-  }
-  d.setHours(hours);
-  d.setMinutes(time['1']);
-
-  var activity_date_time = d.getTime();
-
-  var activityStatusId = cj('#status_id').val();
-
-  if (activityStatusId == 2 && currentDateTime < activity_date_time) {
-    if (!confirm(message.completed)) {
-      return false;
-    }
-  }
-  else {
-    if (activity_date_time && activityStatusId == 1 && currentDateTime >= activity_date_time) {
-      if (!confirm(message.scheduled)) {
-        return false;
-      }
-    }
-  }
-}
-
 CRM.strings = CRM.strings || {};
 CRM.validate = CRM.validate || {
   params: {},
@@ -600,9 +544,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()) {
@@ -615,14 +559,15 @@ 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;
-  CRM.help = function (title, params) {
+  CRM.help = function (title, params, url) {
     h && h.close && h.close();
     var options = {
       expires: 0
@@ -630,7 +575,7 @@ CRM.validate = CRM.validate || {
     h = CRM.alert('...', title, 'crm-help crm-msg-loading', options);
     params.class_name = 'CRM_Core_Page_Inline_Help';
     params.type = 'page';
-    $.ajax(CRM.url('civicrm/ajax/inline'),
+    $.ajax(url || CRM.url('civicrm/ajax/inline'),
       {
         data: params,
         dataType: 'html',
@@ -714,6 +659,7 @@ CRM.validate = CRM.validate || {
       message: ts('Are you sure you want to continue?'),
       resizable: false,
       modal: true,
+      width: 'auto',
       close: function () {
         $(dialog).remove();
       },
@@ -807,7 +753,12 @@ CRM.validate = CRM.validate || {
       CRM.alert(text, title, type, options);
     });
     // Handle qf form errors
-    $('form :input.error', this).one('blur', function () {
+    $('form :input.error', this).one('blur', function() {
+      // ignore autocomplete fields
+      if ($(this).is('.ac_input')) {
+        return;
+      }
+
       $('.ui-notify-message.error a.ui-notify-close').click();
       $(this).removeClass('error');
       $(this).next('span.crm-error').remove();
@@ -824,6 +775,20 @@ CRM.validate = CRM.validate || {
       messagesFromMarkup.call($('#crm-container'));
       $('#crm-container').on('crmFormLoad', '*', messagesFromMarkup);
     }
+
+    // bind the event for image popup
+    $('body').on('click', 'a.crm-image-popup', function() {
+      var o = $('<div class="crm-container crm-custom-image-popup"><img src=' + $(this).attr('href') + '></div>');
+
+      CRM.confirm('',
+        {
+          title: ts('Preview'),
+          message: o
+        },
+        ts('Done')
+      );
+      return false;
+    });
   });
 
   $.fn.crmAccordions = function (speed) {
@@ -863,4 +828,33 @@ CRM.validate = CRM.validate || {
       $(this).toggleClass('collapsed');
     });
   };
+
+  /**
+   * Clientside currency formatting
+   * @param value
+   * @param format - currency representation of the number 1234.56
+   * @return string
+   * @see CRM_Core_Resources::addCoreResources
+   */
+  var currencyTemplate;
+  CRM.formatMoney = function(value, format) {
+    var decimal, separator, sign, i, j, result;
+    if (value === 'init' && format) {
+      currencyTemplate = format;
+      return;
+    }
+    format = format || currencyTemplate;
+    result = /1(.?)234(.?)56/.exec(format);
+    if (result === null) {
+      return 'Invalid format passed to CRM.formatMoney';
+    }
+    separator = result[1];
+    decimal = result[2];
+    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);
+  };
 })(jQuery);