Merge pull request #10136 from eileenmcnaughton/iida
[civicrm-core.git] / js / Common.js
index 86f21fb46e73ee6b77ea9e10c84b0f7a965ae16c..691df6c15e222b651ce25c73fe6e1aa073c3e41e 100644 (file)
@@ -243,8 +243,19 @@ if (!CRM.vars) CRM.vars = {};
       var script = document.createElement('script');
       scriptsLoaded[url] = $.Deferred();
       script.onload = function () {
-        scriptsLoaded[url].resolve();
+        // Give the script time to execute
+        window.setTimeout(function () {
+          if (window.jQuery === CRM.$ && CRM.CMSjQuery) {
+            window.jQuery = CRM.CMSjQuery;
+          }
+          scriptsLoaded[url].resolve();
+        }, 100);
       };
+      // Make jQuery global available while script is loading
+      if (window.jQuery !== CRM.$) {
+        CRM.CMSjQuery = window.jQuery;
+        window.jQuery = CRM.$;
+      }
       script.src = url;
       document.getElementsByTagName("head")[0].appendChild(script);
     }
@@ -710,7 +721,7 @@ if (!CRM.vars) CRM.vars = {};
       function isValidDate() {
         // FIXME: parseDate doesn't work with incomplete date formats; skip validation if no month, day or year in format
         var lowerFormat = settings.dateFormat.toLowerCase();
-        if (lowerFormat.indexOf('y') < 0 || lowerFormat.indexOf('m') < 0 || lowerFormat.indexOf('d') < 0) {
+        if (lowerFormat.indexOf('y') < 0 || lowerFormat.indexOf('m') < 0 || !dateHasDay()) {
           return true;
         }
         try {
@@ -720,6 +731,19 @@ if (!CRM.vars) CRM.vars = {};
           return false;
         }
       }
+
+      /**
+       * Does the date format contain the day.
+       *
+       * @returns {boolean}
+       */
+      function dateHasDay() {
+        var lowerFormat = settings.dateFormat.toLowerCase();
+        if (lowerFormat.indexOf('d') < 0) {
+          return false;
+        }
+        return true;
+      }
       function updateInputFields(e, context) {
         var val = $dataField.val(),
           time = null;
@@ -745,11 +769,16 @@ if (!CRM.vars) CRM.vars = {};
         if (context !== 'crmClear') {
           var val = '';
           if ($dateField.val()) {
-            if (hasDatepicker && isValidDate()) {
+            if (hasDatepicker && isValidDate() && dateHasDay()) {
               val = $.datepicker.formatDate('yy-mm-dd', $dateField.datepicker('getDate'));
               $dateField.removeClass('crm-error');
             } else if (!hasDatepicker) {
               val = $dateField.val() + '-01-01';
+            }
+            else if (!dateHasDay()) {
+              // This would be a Year-month date (yyyy-mm)
+              // it could be argued it should not use a datepicker....
+              val = $dateField.val() + '-01';
             } else {
               $dateField.addClass('crm-error');
             }
@@ -765,58 +794,6 @@ if (!CRM.vars) CRM.vars = {};
     });
   };
 
-  $.fn.crmAjaxTable = function() {
-    // Strip the ids from ajax urls to make pageLength storage more generic
-    function simplifyUrl(ajax) {
-      // Datatables ajax prop could be a url string or an object containing the url
-      var url = typeof ajax === 'object' ? ajax.url : ajax;
-      return typeof url === 'string' ? url.replace(/[&?]\w*id=\d+/g, '') : null;
-    }
-
-    return $(this).each(function() {
-      // Recall pageLength for this table
-      var url = simplifyUrl($(this).data('ajax'));
-      if (url && window.localStorage && localStorage['dataTablePageLength:' + url]) {
-        $(this).data('pageLength', localStorage['dataTablePageLength:' + url]);
-      }
-      // Declare the defaults for DataTables
-      var defaults = {
-        "processing": true,
-        "serverSide": true,
-        "order": [],
-        "dom": '<"crm-datatable-pager-top"lfp>rt<"crm-datatable-pager-bottom"ip>',
-        "pageLength": 25,
-        "pagingType": "full_numbers",
-        "drawCallback": function(settings) {
-          //Add data attributes to cells
-          $('thead th', settings.nTable).each( function( index ) {
-            $.each(this.attributes, function() {
-              if(this.name.match("^cell-")) {
-                var cellAttr = this.name.substring(5);
-                var cellValue = this.value;
-                $('tbody tr', settings.nTable).each( function() {
-                  $('td:eq('+ index +')', this).attr( cellAttr, cellValue );
-                });
-              }
-            });
-          });
-          //Reload table after draw
-          $(settings.nTable).trigger('crmLoad');
-        }
-      };
-      //Include any table specific data
-      var settings = $.extend(true, defaults, $(this).data('table'));
-      // Remember pageLength
-      $(this).on('length.dt', function(e, settings, len) {
-        if (settings.ajax && window.localStorage) {
-          localStorage['dataTablePageLength:' + simplifyUrl(settings.ajax)] = len;
-        }
-      });
-      //Make the DataTables call
-      $(this).DataTable(settings);
-    });
-  };
-
   CRM.utils.formatSelect2Result = function (row) {
     var markup = '<div class="crm-select2-row">';
     if (row.image !== undefined) {
@@ -1037,14 +1014,19 @@ if (!CRM.vars) CRM.vars = {};
       $('table.crm-ajax-table', e.target).each(function() {
         var
           $table = $(this),
+          script = CRM.config.resourceBase + 'js/jquery/jquery.crmAjaxTable.js',
           $accordion = $table.closest('.crm-accordion-wrapper.collapsed, .crm-collapsible.collapsed');
         // For tables hidden by collapsed accordions, wait.
         if ($accordion.length) {
           $accordion.one('crmAccordion:open', function() {
-            $table.crmAjaxTable();
+            CRM.loadScript(script).done(function() {
+              $table.crmAjaxTable();
+            });
           });
         } else {
-          $table.crmAjaxTable();
+          CRM.loadScript(script).done(function() {
+            $table.crmAjaxTable();
+          });
         }
       });
       if ($("input:radio[name=radio_ts]").size() == 1) {
@@ -1056,6 +1038,12 @@ if (!CRM.vars) CRM.vars = {};
       $('.crm-form-text[data-crm-datepicker]', e.target).each(function() {
         $(this).crmDatepicker($(this).data('crmDatepicker'));
       });
+      $('.crm-editable', e.target).not('thead *').each(function() {
+        var $el = $(this);
+        CRM.loadScript(CRM.config.resourceBase + 'js/jquery/jquery.crmEditable.js').done(function() {
+          $el.crmEditable();
+        });
+      });
       // Cache Form Input initial values
       $('form[data-warn-changes] :input', e.target).each(function() {
         $(this).data('crm-initial-value', $(this).is(':checkbox, :radio') ? $(this).prop('checked') : $(this).val());
@@ -1083,6 +1071,7 @@ if (!CRM.vars) CRM.vars = {};
           if ($el.data('origSize')) {
             $el.dialog('option', $el.data('origSize'));
             $el.data('origSize', null);
+            $(this).button('option', 'icons', {primary: 'fa-expand'});
           } else {
             var menuHeight = $('#civicrm-menu').outerHeight();
             $el.data('origSize', {
@@ -1091,6 +1080,7 @@ if (!CRM.vars) CRM.vars = {};
               height: $el.dialog('option', 'height')
             });
             $el.dialog('option', {width: '100%', height: ($(window).height() - menuHeight), position: {my: "top", at: "top+"+menuHeight, of: window}});
+            $(this).button('option', 'icons', {primary: 'fa-compress'});
           }
           $el.trigger('dialogresize');
           e.preventDefault();
@@ -1522,10 +1512,13 @@ if (!CRM.vars) CRM.vars = {};
         $(this).siblings('input:text').val('').trigger('change', ['crmClear']);
         return false;
       })
-      .on('change', 'input.crm-form-radio:checked, input[allowclear=1]', function(e, context) {
+      .on('change keyup', 'input.crm-form-radio:checked, input[allowclear=1]', function(e, context) {
         if (context !== 'crmClear' && ($(this).is(':checked') || ($(this).is('[allowclear=1]') && $(this).val()))) {
           $(this).siblings('.crm-clear-link').css({visibility: ''});
         }
+        if (context !== 'crmClear' && $(this).is('[allowclear=1]') && $(this).val() === '') {
+          $(this).siblings('.crm-clear-link').css({visibility: 'hidden'});
+        }
       })
 
       // Allow normal clicking of links within accordions
@@ -1658,8 +1651,11 @@ if (!CRM.vars) CRM.vars = {};
         return input;
 
       case 'string':
-        // convert iso format
-        return $.datepicker.parseDate('yy-mm-dd', input.substr(0, 10));
+        // convert iso format with or without dashes
+        if (input.indexOf('-') > 0) {
+          return $.datepicker.parseDate('yy-mm-dd', input.substr(0, 10));
+        }
+        return $.datepicker.parseDate('yymmdd', input.substr(0, 8));
 
       case 'number':
         // convert unix timestamp