Chris@0
|
1 /**
|
Chris@0
|
2 * @file
|
Chris@0
|
3 * Polyfill for HTML5 date input.
|
Chris@0
|
4 */
|
Chris@0
|
5
|
Chris@17
|
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@17
|
26 $context
|
Chris@17
|
27 .find('input[data-drupal-date-format]')
|
Chris@17
|
28 .once('datePicker')
|
Chris@17
|
29 .each(function() {
|
Chris@17
|
30 const $input = $(this);
|
Chris@17
|
31 const datepickerSettings = {};
|
Chris@17
|
32 const dateFormat = $input.data('drupalDateFormat');
|
Chris@17
|
33 // The date format is saved in PHP style, we need to convert to jQuery
|
Chris@17
|
34 // datepicker.
|
Chris@17
|
35 datepickerSettings.dateFormat = dateFormat
|
Chris@17
|
36 .replace('Y', 'yy')
|
Chris@17
|
37 .replace('m', 'mm')
|
Chris@17
|
38 .replace('d', 'dd');
|
Chris@17
|
39 // Add min and max date if set on the input.
|
Chris@17
|
40 if ($input.attr('min')) {
|
Chris@17
|
41 datepickerSettings.minDate = $input.attr('min');
|
Chris@17
|
42 }
|
Chris@17
|
43 if ($input.attr('max')) {
|
Chris@17
|
44 datepickerSettings.maxDate = $input.attr('max');
|
Chris@17
|
45 }
|
Chris@17
|
46 $input.datepicker(datepickerSettings);
|
Chris@17
|
47 });
|
Chris@0
|
48 },
|
Chris@0
|
49 detach(context, settings, trigger) {
|
Chris@0
|
50 if (trigger === 'unload') {
|
Chris@17
|
51 $(context)
|
Chris@17
|
52 .find('input[data-drupal-date-format]')
|
Chris@17
|
53 .findOnce('datePicker')
|
Chris@17
|
54 .datepicker('destroy');
|
Chris@0
|
55 }
|
Chris@0
|
56 },
|
Chris@0
|
57 };
|
Chris@17
|
58 })(jQuery, Modernizr, Drupal);
|