/* * #%L * synat-portal-webapp Maven Webapp * %% * Copyright (C) 2010 - 2013 ICM, Warsaw University * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * #L% */ function initializeDateInput(dateInput, buttonImage, parentInputRangeClass) { dateInput.each(function () { if ($(this).hasClass('with-datepicker')) { if ($(this).hasClass('from')) { $(this).datepicker({ showOn: "button", buttonImage: buttonImage, buttonImageOnly: true, dateFormat: "yy-mm-dd", onClose: function(selectedDate) { $(this).closest('.' + parentInputRangeClass).find('.with-datepicker.to').datepicker('option', 'minDate', selectedDate ); } }); $(this).inputmask("yyyy-mm-dd", { "showMaskOnHover": false, "onincomplete": function(){ function getLastDayOfMonth(year, month) { if (month == 2) { return ((year%4 == 0 && year%100 != 0) || year%400 == 0) ? '29' : '28'; } else if (month <= 7) { return (month%2 == 0) ? '30' : '31'; } else { return (month%2 == 0) ? '31' : '30'; } } incompleteVal = $(this).val(); inputYear = incompleteVal.substr(0,4); if (inputYear.indexOf('_') == -1) { // complete year inputMonth = incompleteVal.substr(5,2); inputDay = incompleteVal.substr(8.2); isMonthComplete = inputMonth.indexOf('_') == -1; isDayComplete = inputDay.indexOf('_') == -1; if (!isMonthComplete && !isDayComplete) { $(this).val(inputYear + '-01-01'); } else if (isMonthComplete && !isDayComplete) { $(this).val(inputYear + '-' + inputMonth + '-01'); } else { $(this).val(''); } } else { $(this).val(''); } } }); } else if ($(this).hasClass('to')) { $(this).datepicker({ showOn: "button", buttonImage: buttonImage, buttonImageOnly: true, dateFormat: "yy-mm-dd", onClose: function(selectedDate) { $(this).closest('.' + parentInputRangeClass).find('.with-datepicker.from').datepicker('option', 'maxDate', selectedDate ); } }); $(this).inputmask("yyyy-mm-dd", { "showMaskOnHover": false, "onincomplete": function(){ function getLastDayOfMonth(year, month) { if (month == 2) { return ((year%4 == 0 && year%100 != 0) || year%400 == 0) ? '29' : '28'; } else if (month <= 7) { return (month%2 == 0) ? '30' : '31'; } else { return (month%2 == 0) ? '31' : '30'; } } incompleteVal = $(this).val(); inputYear = incompleteVal.substr(0,4); if (inputYear.indexOf('_') == -1) { // complete year inputMonth = incompleteVal.substr(5,2); inputDay = incompleteVal.substr(8.2); isMonthComplete = inputMonth.indexOf('_') == -1; isDayComplete = inputDay.indexOf('_') == -1; if (!isMonthComplete && !isDayComplete) { $(this).val(inputYear + '-12-31'); } else if (isMonthComplete && !isDayComplete) { $(this).val(inputYear + '-' + inputMonth + '-' + getLastDayOfMonth(parseInt(inputYear), parseInt(inputMonth))); } else { $(this).val(''); } } else { $(this).val(''); } } }); } } }); }