Merge pull request #11335 from civicrm/4.7.28-rc
[civicrm-core.git] / js / Common.js
index df3ddb5faecf929792ca069ff395f5b01b619756..15f3528b2d6ebe0a41911306dfe0b52890c946f7 100644 (file)
@@ -243,10 +243,13 @@ if (!CRM.vars) CRM.vars = {};
       var script = document.createElement('script');
       scriptsLoaded[url] = $.Deferred();
       script.onload = function () {
-        if (window.jQuery === CRM.$ && CRM.CMSjQuery) {
-          window.jQuery = CRM.CMSjQuery;
-        }
-        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.$) {
@@ -718,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 {
@@ -728,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;
@@ -753,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');
             }
@@ -1571,6 +1592,10 @@ if (!CRM.vars) CRM.vars = {};
     return format.replace(/1.*234.*56/, result);
   };
 
+  CRM.angRequires = function(name) {
+    return CRM.angular.requires[name] || [];
+  };
+
   CRM.console = function(method, title, msg) {
     if (window.console) {
       method = $.isFunction(console[method]) ? method : 'log';