2005-07-16 12:27:41 +08:00
// Inserts shortcut buttons after all of the following:
// <input type="text" class="vDateField">
// <input type="text" class="vTimeField">
var DateTimeShortcuts = {
calendars : [ ] ,
calendarInputs : [ ] ,
clockInputs : [ ] ,
calendarDivName1 : 'calendarbox' , // name of calendar <div> that gets toggled
calendarDivName2 : 'calendarin' , // name of <div> that contains calendar
clockDivName : 'clockbox' , // name of clock <div> that gets toggled
init : function ( ) {
var inputs = document . getElementsByTagName ( 'input' ) ;
for ( var i = 0 ; i < inputs . length ; i ++ ) {
var inp = inputs [ i ] ;
if ( inp . getAttribute ( 'type' ) == 'text' && inp . className . match ( /vTimeField/ ) ) {
DateTimeShortcuts . addClock ( inp ) ;
}
else if ( inp . getAttribute ( 'type' ) == 'text' && inp . className . match ( /vDateField/ ) ) {
DateTimeShortcuts . addCalendar ( inp ) ;
}
}
} ,
// Add clock widget to a given field
addClock : function ( inp ) {
var num = DateTimeShortcuts . clockInputs . length ;
DateTimeShortcuts . clockInputs [ num ] = inp ;
// Shortcut links (clock icon and "Now" link)
var shortcuts _span = document . createElement ( 'span' ) ;
inp . parentNode . insertBefore ( shortcuts _span , inp . nextSibling ) ;
var now _link = document . createElement ( 'a' ) ;
now _link . setAttribute ( 'href' , "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().getHourMinute());" ) ;
now _link . appendChild ( document . createTextNode ( 'Now' ) ) ;
var clock _link = document . createElement ( 'a' ) ;
clock _link . setAttribute ( 'href' , 'javascript:DateTimeShortcuts.openClock(' + num + ');' ) ;
quickElement ( 'img' , clock _link , '' , 'src' , 'http://media.ljworld.com/img/admin/icon_clock.gif' , 'alt' , 'Clock' ) ;
shortcuts _span . appendChild ( document . createTextNode ( '\240' ) ) ;
shortcuts _span . appendChild ( now _link ) ;
shortcuts _span . appendChild ( document . createTextNode ( '\240|\240' ) ) ;
shortcuts _span . appendChild ( clock _link ) ;
// Create clock link div
//
// Markup looks like:
// <div id="clockbox1" class="clockbox module">
// <h2>Choose a time</h2>
// <ul class="timelist">
// <li><a href="#">Now</a></li>
// <li><a href="#">Midnight</a></li>
// <li><a href="#">6 a.m.</a></li>
// <li><a href="#">Noon</a></li>
// </ul>
// <p class="calendar-cancel"><a href="#">Cancel</a></p>
// </div>
var clock _box = document . createElement ( 'div' ) ;
clock _box . style . display = 'none' ;
clock _box . style . position = 'absolute' ;
clock _box . style . left = findPosX ( clock _link ) + 17 + 'px' ;
clock _box . style . top = findPosY ( clock _link ) - 30 + 'px' ;
clock _box . className = 'clockbox module' ;
clock _box . setAttribute ( 'id' , DateTimeShortcuts . clockDivName + num ) ;
document . body . appendChild ( clock _box ) ;
addEvent ( clock _box , 'click' , DateTimeShortcuts . cancelEventPropagation ) ;
quickElement ( 'h2' , clock _box , 'Choose a time' ) ;
time _list = quickElement ( 'ul' , clock _box , '' , 'class' , 'timelist' ) ;
quickElement ( "a" , quickElement ( "li" , time _list , "" ) , "Now" , "href" , "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().getHourMinute());" )
quickElement ( "a" , quickElement ( "li" , time _list , "" ) , "Midnight" , "href" , "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", '00:00');" )
quickElement ( "a" , quickElement ( "li" , time _list , "" ) , "6 a.m." , "href" , "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", '06:00');" )
quickElement ( "a" , quickElement ( "li" , time _list , "" ) , "Noon" , "href" , "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", '12:00');" )
cancel _p = quickElement ( 'p' , clock _box , '' , 'class' , 'calendar-cancel' ) ;
2005-07-23 05:56:50 +08:00
quickElement ( 'a' , cancel _p , 'Cancel' , 'href' , 'javascript:DateTimeShortcuts.dismissClock(' + num + ');' ) ;
2005-07-16 12:27:41 +08:00
} ,
openClock : function ( num ) {
document . getElementById ( DateTimeShortcuts . clockDivName + num ) . style . display = 'block' ;
addEvent ( window , 'click' , function ( ) { DateTimeShortcuts . dismissClock ( num ) ; return true ; } ) ;
} ,
dismissClock : function ( num ) {
document . getElementById ( DateTimeShortcuts . clockDivName + num ) . style . display = 'none' ;
window . onclick = null ;
} ,
handleClockQuicklink : function ( num , val ) {
DateTimeShortcuts . clockInputs [ num ] . value = val ;
DateTimeShortcuts . dismissClock ( num ) ;
} ,
// Add calendar widget to a given field.
addCalendar : function ( inp ) {
var num = DateTimeShortcuts . calendars . length ;
DateTimeShortcuts . calendarInputs [ num ] = inp ;
// Shortcut links (calendar icon and "Today" link)
var shortcuts _span = document . createElement ( 'span' ) ;
inp . parentNode . insertBefore ( shortcuts _span , inp . nextSibling ) ;
var today _link = document . createElement ( 'a' ) ;
today _link . setAttribute ( 'href' , 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', 0);' ) ;
today _link . appendChild ( document . createTextNode ( 'Today' ) ) ;
var cal _link = document . createElement ( 'a' ) ;
cal _link . setAttribute ( 'href' , 'javascript:DateTimeShortcuts.openCalendar(' + num + ');' ) ;
quickElement ( 'img' , cal _link , '' , 'src' , 'http://media.ljworld.com/img/admin/icon_calendar.gif' , 'alt' , 'Calendar' ) ;
shortcuts _span . appendChild ( document . createTextNode ( '\240' ) ) ;
shortcuts _span . appendChild ( today _link ) ;
shortcuts _span . appendChild ( document . createTextNode ( '\240|\240' ) ) ;
shortcuts _span . appendChild ( cal _link ) ;
// Create calendarbox div.
//
// Markup looks like:
//
// <div id="calendarbox3" class="calendarbox module">
// <h2>
// <a href="#" class="link-previous">‹</a>
// <a href="#" class="link-next">›</a> February 2003
// </h2>
// <div class="calendar" id="calendarin3">
// <!-- (cal) -->
// </div>
// <div class="calendar-shortcuts">
// <a href="#">Yesterday</a> | <a href="#">Today</a> | <a href="#">Tomorrow</a>
// </div>
// <p class="calendar-cancel"><a href="#">Cancel</a></p>
// </div>
var cal _box = document . createElement ( 'div' ) ;
cal _box . style . display = 'none' ;
cal _box . style . position = 'absolute' ;
cal _box . style . left = findPosX ( cal _link ) + 17 + 'px' ;
cal _box . style . top = findPosY ( cal _link ) - 75 + 'px' ;
cal _box . className = 'calendarbox module' ;
cal _box . setAttribute ( 'id' , DateTimeShortcuts . calendarDivName1 + num ) ;
document . body . appendChild ( cal _box ) ;
addEvent ( cal _box , 'click' , DateTimeShortcuts . cancelEventPropagation ) ;
// next-prev links
var cal _nav = quickElement ( 'div' , cal _box , '' ) ;
quickElement ( 'a' , cal _nav , '<' , 'class' , 'calendarnav-previous' , 'href' , 'javascript:DateTimeShortcuts.drawPrev(' + num + ');' ) ;
quickElement ( 'a' , cal _nav , '>' , 'class' , 'calendarnav-next' , 'href' , 'javascript:DateTimeShortcuts.drawNext(' + num + ');' ) ;
cal _box . appendChild ( cal _nav ) ;
// main box
var cal _main = quickElement ( 'div' , cal _box , '' , 'id' , DateTimeShortcuts . calendarDivName2 + num ) ;
cal _main . className = 'calendar' ;
DateTimeShortcuts . calendars [ num ] = new Calendar ( DateTimeShortcuts . calendarDivName2 + num , DateTimeShortcuts . handleCalendarCallback ( num ) ) ;
DateTimeShortcuts . calendars [ num ] . drawCurrent ( ) ;
// calendar shortcuts
var shortcuts = quickElement ( 'div' , cal _box , '' , 'class' , 'calendar-shortcuts' ) ;
quickElement ( 'a' , shortcuts , 'Yesterday' , 'href' , 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', -1);' ) ;
shortcuts . appendChild ( document . createTextNode ( '\240|\240' ) ) ;
quickElement ( 'a' , shortcuts , 'Today' , 'href' , 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', 0);' ) ;
shortcuts . appendChild ( document . createTextNode ( '\240|\240' ) ) ;
quickElement ( 'a' , shortcuts , 'Tomorrow' , 'href' , 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', +1);' ) ;
// cancel bar
var cancel _p = quickElement ( 'p' , cal _box , '' , 'class' , 'calendar-cancel' ) ;
quickElement ( 'a' , cancel _p , 'Cancel' , 'href' , 'javascript:DateTimeShortcuts.dismissCalendar(' + num + ');' ) ;
} ,
openCalendar : function ( num ) {
document . getElementById ( DateTimeShortcuts . calendarDivName1 + num ) . style . display = 'block' ;
addEvent ( window , 'click' , function ( ) { DateTimeShortcuts . dismissCalendar ( num ) ; return true ; } ) ;
} ,
dismissCalendar : function ( num ) {
document . getElementById ( DateTimeShortcuts . calendarDivName1 + num ) . style . display = 'none' ;
window . onclick = null ;
} ,
drawPrev : function ( num ) {
DateTimeShortcuts . calendars [ num ] . drawPreviousMonth ( ) ;
} ,
drawNext : function ( num ) {
DateTimeShortcuts . calendars [ num ] . drawNextMonth ( ) ;
} ,
handleCalendarCallback : function ( num ) {
return "function(y, m, d) { DateTimeShortcuts.calendarInputs[" + num + "].value = y+'-'+m+'-'+d; document.getElementById(DateTimeShortcuts.calendarDivName1+" + num + ").style.display='none';}" ;
} ,
handleCalendarQuickLink : function ( num , offset ) {
var d = new Date ( ) ;
d . setDate ( d . getDate ( ) + offset )
DateTimeShortcuts . calendarInputs [ num ] . value = d . getISODate ( ) ;
DateTimeShortcuts . dismissCalendar ( num ) ;
} ,
cancelEventPropagation : function ( e ) {
if ( ! e ) var e = window . event ;
e . cancelBubble = true ;
if ( e . stripPropagation ) e . stopPropagation ( ) ;
}
}
addEvent ( window , 'load' , DateTimeShortcuts . init ) ;