5 * @see https://docs.civicrm.org/dev/en/latest/framework/ui/#date-picker
7 $.fn
.crmDatepicker = function(options
) {
8 return $(this).each(function() {
9 if ($(this).is('.crm-form-date-wrapper .crm-hidden-date')) {
10 // Already initialized - destroy
12 .off('.crmDatepicker')
14 .removeClass('crm-hidden-date')
18 if (options
=== 'destroy') {
22 $dataField
= $(this).wrap('<span class="crm-form-date-wrapper" />'),
23 settings
= _
.cloneDeep(options
|| {}),
28 hasDatepicker
= settings
.date
!== false && settings
.date
!== 'yy',
29 type
= hasDatepicker
? 'text' : 'number';
31 if (settings
.allowClear
!== undefined ? settings
.allowClear
: !$dataField
.is('.required, [required]')) {
32 $clearLink
= $('<a class="crm-hover-button crm-clear-link" title="'+ _
.escape(ts('Clear')) +'"><i class="crm-i fa-times" aria-hidden="true"></i></a>')
33 .insertAfter($dataField
);
35 if (settings
.time
!== false) {
36 $timeField
= $('<input>').insertAfter($dataField
);
37 placeholder
= settings
.timePlaceholder
|| $dataField
.attr('time-placeholder');
38 CRM
.utils
.copyAttributes($dataField
, $timeField
, ['class', 'disabled']);
40 .addClass('crm-form-text crm-form-time')
41 // Set default placeholder as clock icon (`fa-clock` is Unicode f017)
42 .attr('placeholder', placeholder
=== undefined ? '\uf017' : placeholder
)
43 .attr('aria-label', placeholder
=== undefined ? ts('Time') : placeholder
)
44 .change(updateDataField
)
47 show24Hours
: settings
.time
=== true || settings
.time
=== undefined ? CRM
.config
.timeIs24Hr
: settings
.time
== '24'
50 $timeField
.addClass('crm-placeholder-icon');
53 if (settings
.date
!== false) {
54 // Render "number" field for year-only format, calendar popup for all other formats
55 $dateField
= $('<input type="' + type
+ '">').insertAfter($dataField
);
56 CRM
.utils
.copyAttributes($dataField
, $dateField
, ['style', 'class', 'disabled', 'aria-label']);
57 placeholder
= settings
.placeholder
|| $dataField
.attr('placeholder');
58 $dateField
.addClass('crm-form-' + type
);
59 if (!settings
.minDate
&& !_
.isUndefined(settings
.start_date_years
)) {
60 settings
.minDate
= '' + (new Date().getFullYear() - settings
.start_date_years
) + '-01-01';
62 if (!settings
.maxDate
&& !_
.isUndefined(settings
.end_date_years
)) {
63 settings
.maxDate
= '' + (new Date().getFullYear() + settings
.end_date_years
) + '-12-31';
66 settings
.minDate
= settings
.minDate
? CRM
.utils
.makeDate(settings
.minDate
) : null;
67 settings
.maxDate
= settings
.maxDate
? CRM
.utils
.makeDate(settings
.maxDate
) : null;
68 settings
.dateFormat
= typeof settings
.date
=== 'string' ? settings
.date
: CRM
.config
.dateInputFormat
;
69 settings
.changeMonth
= _
.includes(settings
.dateFormat
, 'm');
70 settings
.changeYear
= _
.includes(settings
.dateFormat
, 'y');
71 if (!settings
.yearRange
&& settings
.minDate
!== null && settings
.maxDate
!== null) {
72 settings
.yearRange
= '' + CRM
.utils
.formatDate(settings
.minDate
, 'yy') + ':' + CRM
.utils
.formatDate(settings
.maxDate
, 'yy');
74 $dateField
.addClass('crm-form-date').datepicker(settings
);
76 $dateField
.attr('min', settings
.minDate
? CRM
.utils
.formatDate(settings
.minDate
, 'yy') : '1000');
77 $dateField
.attr('max', settings
.maxDate
? CRM
.utils
.formatDate(settings
.maxDate
, 'yy') : '4000');
80 // Set placeholder as calendar icon (`fa-calendar` is Unicode f073)
81 $dateField
.attr({placeholder
: placeholder
=== undefined ? '\uF073' : placeholder
}).change(updateDataField
);
83 $dateField
.addClass('crm-placeholder-icon');
86 // Rudimentary validation. TODO: Roll into use of jQUery validate and ui.datepicker.validation
87 function isValidDate() {
88 // FIXME: parseDate doesn't work with incomplete date formats; skip validation if no month, day or year in format
89 var lowerFormat
= settings
.dateFormat
.toLowerCase();
90 if (lowerFormat
.indexOf('y') < 0 || lowerFormat
.indexOf('m') < 0 || !dateHasDay()) {
94 $.datepicker
.parseDate(settings
.dateFormat
, $dateField
.val());
102 * Does the date format contain the day.
106 function dateHasDay() {
107 var lowerFormat
= settings
.dateFormat
.toLowerCase();
108 if (lowerFormat
.indexOf('d') < 0) {
113 function updateInputFields(e
, context
) {
114 var val
= $dataField
.val(),
116 if (context
!== 'userInput' && context
!== 'crmClear') {
118 $dateField
.datepicker('setDate', _
.includes(val
, '-') ? $.datepicker
.parseDate('yy-mm-dd', val
) : null);
119 } else if ($dateField
.length
) {
120 $dateField
.val(val
.slice(0, 4));
122 if ($timeField
.length
) {
123 if (val
.length
=== 8) {
125 } else if (val
.length
=== 19) {
126 time
= val
.split(' ')[1];
128 $timeField
.timeEntry('setTime', time
);
131 $clearLink
.css('visibility', val
? 'visible' : 'hidden');
133 function updateDataField(e
, context
) {
134 // The crmClear event wipes all the field values anyway, so no need to respond
135 if (context
!== 'crmClear') {
137 if ($dateField
.val()) {
138 if (hasDatepicker
&& isValidDate() && dateHasDay()) {
139 val
= $.datepicker
.formatDate('yy-mm-dd', $dateField
.datepicker('getDate'));
140 $dateField
.removeClass('crm-error');
141 } else if (!hasDatepicker
) {
142 val
= $dateField
.val() + '-01-01';
144 else if (!dateHasDay()) {
145 // This would be a Year-month date (yyyy-mm)
146 // it could be argued it should not use a datepicker....
147 val
= $dateField
.val() + '-01';
149 $dateField
.addClass('crm-error');
152 if ($timeField
.val()) {
153 val
+= (val
? ' ' : '') + $timeField
.timeEntry('getTime').toTimeString().substr(0, 8);
155 $dataField
.val(val
).trigger('change', ['userInput']);
158 $dataField
.hide().addClass('crm-hidden-date').on('change.crmDatepicker', updateInputFields
);
162 })(jQuery
, CRM
, CRM
._
);