# Copyright (c) 2001-2010 bivio Software, Inc. All rights reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: DateYearHandler.pm,v 2.4 2010/02/20 21:02:47 nagler Exp $ package Bivio::UI::HTML::Widget::DateYearHandler; use strict; use Bivio::Base 'UI.Widget'; # form_name : string (inherited) # # Used to access the form within JavaScript. # # target_field : string # # The date field to receive the new value. This value only get assigned # if the source date is less than the current value. our($VERSION) = sprintf('%d.%02d', q$Revision: 2.4 $ =~ /\d+/g); my($_JS) = b_use('HTMLWidget.JavaScript'); my($_FUNCS) = $_JS->strip(<<'EOF'); function dy_complete_date(s) { if (s.value.length == 0) return; var c_year = new Date().getFullYear() + ''; var century = c_year.substring(0, 2); var century var pattern = new RegExp('^[0-9]?[0-9][/\.][0-9]?[0-9][/\.]?[0-9]?[0-9]?$'); if (!pattern.test(s.value)) { pattern = new RegExp('^[0-9][0-9][0-9][0-9]$'); if (!pattern.test(s.value)) { pattern = new RegExp('^[0-9][0-9][0-9][0-9][0-9][0-9]$'); if (!pattern.test(s.value)) return; c_year = century + s.value.substring(4, 6); } s.value = s.value.substring(0, 2) + '/' + s.value.substring(2, 4) + '/' + c_year; return; } var sep = '/'; if ((sep_1 = s.value.indexOf(sep)) < 0) sep_1 = s.value.indexOf(sep = '.'); var sep_2 = s.value.indexOf(sep, sep_1 + 1); var s_month = s.value.substring(0, sep_1); var s_day, s_year; if (sep_2 == -1) { s_day = s.value.substring(sep_1 + 1, s.value.length); s_year = ''; } else { s_day = s.value.substring(sep_1 + 1, sep_2); s_year = s.value.substring(sep_2 + 1, s.value.length); } if (s_month.length > 2 || s_day.length > 2) return; if (sep_2 == -1) s.value = s.value + sep + c_year; else if (s_year.length == 0) s.value = s.value + c_year; else if (s_year.length <= 2) s.value = s_month + sep + s_day + sep + (s_year.length == 1 ? century + '0' : c_year - (century + '00') >= s_year - 20 ? century : century - 1) + s_year; } EOF sub JAVASCRIPT_FUNCTION_NAME { return 'dy'; } sub get_html_field_attributes { my($self, $field_name, $source) = @_; return ' onblur="dy_complete_date(this)"'; } sub render { my($self, $source, $buffer) = @_; $_JS->render($source, $buffer, shift->JAVASCRIPT_FUNCTION_NAME, $_FUNCS); return; } 1;