diff --git a/core/misc/date.js b/core/misc/date.js index 3f401d5..fe2a239 100644 --- a/core/misc/date.js +++ b/core/misc/date.js @@ -1,37 +1,41 @@ +/** + * @file + * Polyfill for HTML5 date input. + */ + (function ($, Modernizr, Drupal) { "use strict"; /** * Attach datepicker fallback on date elements. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches the behavior. Accepts in `settings.date` an object listing + * elements to process, keyed by the HTML ID of the form element containing + * the human-readable value. Each element is an datepicker settings object. + * @prop {Drupal~behaviorDetach} detach + * Detach the behavior destroying datepickers on effected elements. */ Drupal.behaviors.date = { - /** - * Attaches the behavior. - * - * @param settings.date - * A list of elements to process, keyed by the HTML ID of the form element - * containing the human-readable value. Each element is an datepicker - * settings object. - */ attach: function (context, settings) { var $context = $(context); // Skip if date are supported by the browser. if (Modernizr.inputtypes.date === true) { return; } - $context.find('input[data-drupal-date-format]').not('.hasDatepicker').each(function () { + $context.find('input[data-drupal-date-format]').once('datePicker').each(function () { var $input = $(this); var datepickerSettings = {}; - var dateFormat; - dateFormat = $input.data('drupalDateFormat'); + var dateFormat = $input.data('drupalDateFormat'); // The date format is saved in PHP style, we need to convert to jQuery // datepicker. - dateFormat = dateFormat + datepickerSettings.dateFormat = dateFormat .replace('Y', 'yy') .replace('m', 'mm') .replace('d', 'dd'); - datepickerSettings.dateFormat = dateFormat; // Add min and max date if set on the input. if ($input.attr('min')) { datepickerSettings.minDate = $input.attr('min'); @@ -42,12 +46,9 @@ $input.datepicker(datepickerSettings); }); }, - /** - * Detach the behavior destroying datepickers on effected elements. - */ detach: function (context, settings, trigger) { if (trigger === 'unload') { - $(context).find('input[data-drupal-date-format].hasDatepicker').datepicker("destroy"); + $(context).find('input[data-drupal-date-format]').findOnce('datePicker').datepicker('destroy'); } } };