PageSourceSearch

https://theartssociety.org/sites/all/modules/civicrm/js/crm.datepicker.js?tlvm4t

js theartssociety.org collected 2026-09-24 20:52:31 UTC 7,441 bytes, 172 lines download raw bytes

1(function($, CRM, _) {
2  "use strict";
3
4  /**
5   * @see https://docs.civicrm.org/dev/en/latest/framework/ui/#date-picker
6   */
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
11        $(this)
12          .off('.crmDatepicker')
13          .css('display', '')
14          .removeClass('crm-hidden-date')
15          .siblings().remove();
16        $(this).unwrap();
17      }
18      if (options === 'destroy') {
19        return;
20      }
21      var
22        $dataField = $(this).wrap('<span class="crm-form-date-wrapper" />'),
23        settings = _.cloneDeep(options || {}),
24        $dateField = $(),
25        $timeField = $(),
26        $clearLink = $(),
27        placeholder,
28        hasDatepicker = settings.date !== false && settings.date !== 'yy',
29        type = hasDatepicker ? 'text' : 'number';
30
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);
34      }
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']);
39        $timeField
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)
45          .timeEntry({
46            spinnerImage: '',
47            useMouseWheel: false,
48            show24Hours: settings.time === true || settings.time === undefined ? CRM.config.timeIs24Hr : settings.time == '24'
49          });
50        if (!placeholder) {
51          $timeField.addClass('crm-placeholder-icon');
52        }
53      }
54      if (settings.date !== false) {
55        // Render "number" field for year-only format, calendar popup for all other formats
56        $dateField = $('<input type="' + type + '">').insertAfter($dataField);
57        CRM.utils.copyAttributes($dataField, $dateField, ['style', 'class', 'disabled', 'aria-label']);
58        placeholder = settings.placeholder || $dataField.attr('placeholder');
59        $dateField.addClass('crm-form-' + type);
60        if (!settings.minDate && isInt(settings.start_date_years)) {
61          settings.minDate = '' + (new Date().getFullYear() - settings.start_date_years) + '-01-01';
62        }
63        if (!settings.maxDate && isInt(settings.end_date_years)) {
64          settings.maxDate = '' + (new Date().getFullYear() + settings.end_date_years) + '-12-31';
65        }
66        if (hasDatepicker) {
67          settings.minDate = settings.minDate ? CRM.utils.makeDate(settings.minDate) : null;
68          settings.maxDate = settings.maxDate ? CRM.utils.makeDate(settings.maxDate) : null;
69          settings.dateFormat = typeof settings.date === 'string' ? settings.date : CRM.config.dateInputFormat;
70          settings.changeMonth = _.includes(settings.dateFormat, 'm');
71          settings.changeYear = _.includes(settings.dateFormat, 'y');
72          if (!settings.yearRange && settings.minDate !== null && settings.maxDate !== null) {
73            settings.yearRange = '' + CRM.utils.formatDate(settings.minDate, 'yy') + ':' + CRM.utils.formatDate(settings.maxDate, 'yy');
74          }
75          $dateField.addClass('crm-form-date').datepicker(settings);
76        } else {
77          $dateField.attr('min', settings.minDate ? CRM.utils.formatDate(settings.minDate, 'yy') : '1000');
78          $dateField.attr('max', settings.maxDate ? CRM.utils.formatDate(settings.maxDate, 'yy') : '4000');
79          placeholder = null;
80        }
81        // Set placeholder as calendar icon (`fa-calendar` is Unicode f073)
82        $dateField.attr({placeholder: placeholder === undefined ? '\uF073' : placeholder}).change(updateDataField);
83        if (!placeholder) {
84          $dateField.addClass('crm-placeholder-icon').attr('aria-label', ts('Select Date'));
85        }
86        else {
87          $dateField.attr('aria-label', placeholder);
88        }
89      }
90      // Rudimentary validation. TODO: Roll into use of jQUery validate and ui.datepicker.validation
91      function isValidDate() {
92        // FIXME: parseDate doesn't work with incomplete date formats; skip validation if no month, day or year in format
93        var lowerFormat = settings.dateFormat.toLowerCase();
94        if (lowerFormat.indexOf('y') < 0 || lowerFormat.indexOf('m') < 0 || !dateHasDay()) {
95          return true;
96        }
97        try {
98          $.datepicker.parseDate(settings.dateFormat, $dateField.val());
99          return true;
100        } catch (e) {
101          return false;
102        }
103      }
104
105      /**
106       * Does the date format contain the day.
107       *
108       * @returns {boolean}
109       */
110      function dateHasDay() {
111        var lowerFormat = settings.dateFormat.toLowerCase();
112        return lowerFormat.indexOf('d') >= 0;
113      }
114      function updateInputFields(e, context) {
115        var val = $dataField.val(),
116          time = null;
117        if (context !== 'userInput' && context !== 'crmClear') {
118          if (hasDatepicker) {
119            $dateField.datepicker('setDate', _.includes(val, '-') ? $.datepicker.parseDate('yy-mm-dd', val) : null);
120          } else if ($dateField.length) {
121            $dateField.val(val.slice(0, 4));
122          }
123          if ($timeField.length) {
124            if (val.length === 8) {
125              time = val;
126            } else if (val.length === 19) {
127              time = val.split(' ')[1];
128            }
129            $timeField.timeEntry('setTime', time);
130          }
131        }
132        $clearLink.css('visibility', val ? 'visible' : 'hidden');
133      }
134      function updateDataField(e, context) {
135        // The crmClear event wipes all the field values anyway, so no need to respond
136        if (context !== 'crmClear') {
137          var val = '';
138          if ($dateField.val()) {
139            if (hasDatepicker && isValidDate() && dateHasDay()) {
140              val = $.datepicker.formatDate('yy-mm-dd', $dateField.datepicker('getDate'));
141              $dateField.removeClass('crm-error');
142            } else if (!hasDatepicker) {
143              val = $dateField.val() + '-01-01';
144            }
145            else if (!dateHasDay()) {
146              // This would be a Year-month date (yyyy-mm)
147              // it could be argued it should not use a datepicker....
148              val = $dateField.val() + '-01';
149            } else {
150              $dateField.addClass('crm-error');
151            }
152          }
153          if ($timeField.val()) {
154            val += (val ? ' ' : '') + $timeField.timeEntry('getTime').toTimeString().substr(0, 8);
155          }
156          $dataField.val(val).trigger('change', ['userInput']);
157        }
158      }
159      $dataField.hide().addClass('crm-hidden-date').on('change.crmDatepicker', updateInputFields);
160      updateInputFields();
161    });
162  };
163
164  function isInt(value) {
165    if (isNaN(value)) {
166      return false;
167    }
168    var x = parseFloat(value);
169    return (x | 0) === x;
170  }
171
172})(jQuery, CRM, CRM._);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.