From b180855502e415e9e0b2b88b78a502eae0e8d65b Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 22 Dec 2015 15:57:11 -0500 Subject: [PATCH] CRM-16019 - crmDatepicker improvements --- js/Common.js | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/js/Common.js b/js/Common.js index 27a8a74030..6f4b189fdf 100644 --- a/js/Common.js +++ b/js/Common.js @@ -612,15 +612,25 @@ if (!CRM.vars) CRM.vars = {}; $.fn.crmDatepicker = function(options) { return $(this).each(function() { if ($(this).is('.crm-form-date-wrapper .crm-hidden-date')) { - // Already initialized + // Already initialized - destroy + $(this) + .off('.crmDatepicker') + .css('display', '') + .removeClass('crm-hidden-date') + .siblings().remove(); + $(this).unwrap(); + } + if (options === 'destroy') { return; } var $dataField = $(this).wrap(''), - settings = $.extend({}, $dataField.data('datepicker') || {}, options || {}), + settings = _.cloneDeep(options || {}), $dateField = $(), $timeField = $(), - $clearLink = $(); + $clearLink = $(), + hasDatepicker = settings.date !== false && settings.date !== 'yy', + type = hasDatepicker ? 'text' : 'number'; if (settings.allowClear !== undefined ? settings.allowClear : !$dataField.is('.required, [required]')) { $clearLink = $('') @@ -639,15 +649,22 @@ if (!CRM.vars) CRM.vars = {}; }); } if (settings.date !== false) { - $dateField = $('').insertAfter($dataField); + // Render "number" field for year-only format, calendar popup for all other formats + $dateField = $('').insertAfter($dataField); copyAttributes($dataField, $dateField, ['placeholder', 'style', 'class', 'disabled']); - $dateField.addClass('crm-form-text crm-form-date'); - settings.dateFormat = typeof settings.date === 'string' ? settings.date : CRM.config.dateInputFormat; - settings.changeMonth = _.includes(settings.dateFormat, 'm'); - settings.changeYear = _.includes(settings.dateFormat, 'y'); + $dateField.addClass('crm-form-' + type); settings.minDate = settings.minDate ? CRM.utils.makeDate(settings.minDate) : null; settings.maxDate = settings.maxDate ? CRM.utils.makeDate(settings.maxDate) : null; - $dateField.datepicker(settings).change(updateDataField); + if (hasDatepicker) { + settings.dateFormat = typeof settings.date === 'string' ? settings.date : CRM.config.dateInputFormat; + settings.changeMonth = _.includes(settings.dateFormat, 'm'); + settings.changeYear = _.includes(settings.dateFormat, 'y'); + $dateField.addClass('crm-form-date').datepicker(settings); + } else { + $dateField.attr('min', settings.minDate ? CRM.utils.formatDate(settings.minDate, 'yy') : '1000'); + $dateField.attr('max', settings.maxDate ? CRM.utils.formatDate(settings.maxDate, 'yy') : '4000'); + } + $dateField.change(updateDataField); } // Rudimentary validation. TODO: Roll into use of jQUery validate and ui.datepicker.validation function isValidDate() { @@ -667,8 +684,10 @@ if (!CRM.vars) CRM.vars = {}; var val = $dataField.val(), time = null; if (context !== 'userInput' && context !== 'crmClear') { - if ($dateField.length) { + if (hasDatepicker) { $dateField.datepicker('setDate', _.includes(val, '-') ? $.datepicker.parseDate('yy-mm-dd', val) : null); + } else if ($dateField.length) { + $dateField.val(val.slice(0, 4)); } if ($timeField.length) { if (val.length === 8) { @@ -686,9 +705,11 @@ if (!CRM.vars) CRM.vars = {}; if (context !== 'crmClear') { var val = ''; if ($dateField.val()) { - if (isValidDate()) { + if (hasDatepicker && isValidDate()) { val = $.datepicker.formatDate('yy-mm-dd', $dateField.datepicker('getDate')); $dateField.removeClass('crm-error'); + } else if (!hasDatepicker) { + val = $dateField.val() + '-01-01'; } else { $dateField.addClass('crm-error'); } @@ -699,7 +720,7 @@ if (!CRM.vars) CRM.vars = {}; $dataField.val(val).trigger('change', ['userInput']); } } - $dataField.hide().addClass('crm-hidden-date').on('change', updateInputFields); + $dataField.hide().addClass('crm-hidden-date').on('change.crmDatepicker', updateInputFields); updateInputFields(); }); }; -- 2.25.1