Chris@0
|
1 /**
|
Chris@0
|
2 * @file
|
Chris@0
|
3 * Polyfill for HTML5 date input.
|
Chris@0
|
4 */
|
Chris@0
|
5
|
Chris@0
|
6 (function ($, Modernizr, Drupal) {
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Attach datepicker fallback on date elements.
|
Chris@0
|
9 *
|
Chris@0
|
10 * @type {Drupal~behavior}
|
Chris@0
|
11 *
|
Chris@0
|
12 * @prop {Drupal~behaviorAttach} attach
|
Chris@0
|
13 * Attaches the behavior. Accepts in `settings.date` an object listing
|
Chris@0
|
14 * elements to process, keyed by the HTML ID of the form element containing
|
Chris@0
|
15 * the human-readable value. Each element is an datepicker settings object.
|
Chris@0
|
16 * @prop {Drupal~behaviorDetach} detach
|
Chris@0
|
17 * Detach the behavior destroying datepickers on effected elements.
|
Chris@0
|
18 */
|
Chris@0
|
19 Drupal.behaviors.date = {
|
Chris@0
|
20 attach(context, settings) {
|
Chris@0
|
21 const $context = $(context);
|
Chris@0
|
22 // Skip if date are supported by the browser.
|
Chris@0
|
23 if (Modernizr.inputtypes.date === true) {
|
Chris@0
|
24 return;
|
Chris@0
|
25 }
|
Chris@0
|
26 $context.find('input[data-drupal-date-format]').once('datePicker').each(function () {
|
Chris@0
|
27 const $input = $(this);
|
Chris@0
|
28 const datepickerSettings = {};
|
Chris@0
|
29 const dateFormat = $input.data('drupalDateFormat');
|
Chris@0
|
30 // The date format is saved in PHP style, we need to convert to jQuery
|
Chris@0
|
31 // datepicker.
|
Chris@0
|
32 datepickerSettings.dateFormat = dateFormat
|
Chris@0
|
33 .replace('Y', 'yy')
|
Chris@0
|
34 .replace('m', 'mm')
|
Chris@0
|
35 .replace('d', 'dd');
|
Chris@0
|
36 // Add min and max date if set on the input.
|
Chris@0
|
37 if ($input.attr('min')) {
|
Chris@0
|
38 datepickerSettings.minDate = $input.attr('min');
|
Chris@0
|
39 }
|
Chris@0
|
40 if ($input.attr('max')) {
|
Chris@0
|
41 datepickerSettings.maxDate = $input.attr('max');
|
Chris@0
|
42 }
|
Chris@0
|
43 $input.datepicker(datepickerSettings);
|
Chris@0
|
44 });
|
Chris@0
|
45 },
|
Chris@0
|
46 detach(context, settings, trigger) {
|
Chris@0
|
47 if (trigger === 'unload') {
|
Chris@0
|
48 $(context).find('input[data-drupal-date-format]').findOnce('datePicker').datepicker('destroy');
|
Chris@0
|
49 }
|
Chris@0
|
50 },
|
Chris@0
|
51 };
|
Chris@0
|
52 }(jQuery, Modernizr, Drupal));
|