1/** 2 * @file 3 * JavaScript behaviors for the front-end display of webforms. 4 */ 5 6(function ($) { 7 8Drupal.behaviors.webform = Drupal.behaviors.webform || {}; 9 10Drupal.behaviors.webform.attach = function (context) { 11 // Calendar datepicker behavior. 12 Drupal.webform.datepicker(context); 13}; 14 15Drupal.webform = Drupal.webform || {}; 16 17Drupal.webform.datepicker = function (context) { 18 $('div.webform-datepicker').each(function () { 19 var $webformDatepicker = $(this); 20 var $calendar = $webformDatepicker.find('input.webform-calendar'); 21 22 // Ensure the page we're on actually contains a datepicker. 23 if ($calendar.length == 0) { 24 return; 25 } 26 27 var startDate = $calendar[0].className.replace(/.*webform-calendar-start-(\d{4}-\d{2}-\d{2}).*/, '$1').split('-'); 28 var endDate = $calendar[0].className.replace(/.*webform-calendar-end-(\d{4}-\d{2}-\d{2}).*/, '$1').split('-'); 29 var firstDay = $calendar[0].className.replace(/.*webform-calendar-day-(\d).*/, '$1'); 30 // Convert date strings into actual Date objects. 31 startDate = new Date(startDate[0], startDate[1] - 1, startDate[2]); 32 endDate = new Date(endDate[0], endDate[1] - 1, endDate[2]); 33 34 // Ensure that start comes before end for datepicker. 35 if (startDate > endDate) { 36 var laterDate = startDate; 37 startDate = endDate; 38 endDate = laterDate; 39 } 40 41 var startYear = startDate.getFullYear(); 42 var endYear = endDate.getFullYear(); 43 44 // Set up the jQuery datepicker element. 45 $calendar.datepicker({ 46 dateFormat: 'yy-mm-dd', 47 yearRange: startYear + ':' + endYear, 48 firstDay: parseInt(firstDay), 49 minDate: startDate, 50 maxDate: endDate, 51 onSelect: function (dateText, inst) { 52 var date = dateText.split('-'); 53 $webformDatepicker.find('select.year, input.year').val(+date[0]).trigger('change'); 54 $webformDatepicker.find('select.month').val(+date[1]).trigger('change'); 55 $webformDatepicker.find('select.day').val(+date[2]).trigger('change'); 56 }, 57 beforeShow: function (input, inst) { 58 // Get the select list values. 59 var year = $webformDatepicker.find('select.year, input.year').val(); 60 var month = $webformDatepicker.find('select.month').val(); 61 var day = $webformDatepicker.find('select.day').val(); 62 63 // If empty, default to the current year/month/day in the popup. 64 var today = new Date(); 65 year = year ? year : today.getFullYear(); 66 month = month ? month : today.getMonth() + 1; 67 day = day ? day : today.getDate(); 68 69 // Make sure that the default year fits in the available options. 70 year = (year < startYear || year > endYear) ? startYear : year; 71 72 // jQuery UI Datepicker will read the input field and base its date off 73 // of that, even though in our case the input field is a button. 74 $(input).val(year + '-' + month + '-' + day); 75 } 76 }); 77 78 // Prevent the calendar button from submitting the form. 79 $calendar.click(function (event) { 80 $(this).focus(); 81 event.preventDefault(); 82 }); 83 }); 84} 85 86})(jQuery);
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.