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