/** * Oggetto "Calendario". Può essere utilizzato in un documento HTML con le seguenti caratteristiche: * 1) Un tag "contenitore" con id="calendar" che contenga 42 tag anchor, ovvero * tutte le caselle di una tipica tabellina mensile. Dentro questo container non * devono esserci altri tag anchor * 2) Una dropdown vuota che conterrà i mesi con id="selectmonth" * * 3) Una dropdown vuota (opzionale) che conterrà gli anni con id="selectyear" * * * L'HTML viene "riempito" nel momento in cui viene invocato il metodo pubblico "draw()" da un'istanza dell'oggetto * Es: window.onload = function(){var c=new Calendar();c.draw();} * * I parametri del costruttore sono tutti opzionali e nel caso non siano specificati, il calendario partirà * arbitrariamente dal 1 gennaio 1970 ("The Epoch") e arriverà fino al giorno corrente. Se il mese visualizzato * contiene giorni seguenti a quello corrente o precedenti al primo giorno configurato, questi non saranno * clickabili. Il click su un giorno o il cambio di mese/anno, causeranno la chiamata della stessa pagina con * query string nel formato: ?giorno=[giorno]&mese=[mese]&anno=[anno] - questi parametri sono usati per * la preselezione del calendario * * Se viene passato al costruttore il parametro "paramPrefixIfInPath" o, in alternativa, se lo stesso valore * viene passato tramite il metodo pubblico "setParamPrefixIfInPath()", la gestione dei parametri nei link passa * dalla query string al path della pagina. L'oggetto Calendar si aspetta che l'url della pagina abbia la * seguente forma: [prefix]/yyyy/mm/gg/[suffix] oppure [prefix]/[suffix] (per il giorno corrente) * Tutti i link sono creati dinamicamente in tale formato * * Il significato dei restanti parametri del costruttore (theStartYear, theStartMonth, theStartDay), è intuitivo * * Se viene invocato il metodo pubblico "setFullSingleYear()", il calendario gestirà tutti i mesi e i giorni * dell'anno che riceve in input. Tutti i giorni dell'anno saranno clickabili. Se il secondo parametro è * valorizzato a "true", la tendina relativa all'anno scomparirà. * * L'invocazione del metodo pubblico "hideYearDropDown()" provoca la scomparsa della tendina relativa all'anno. * * Se la tendina per la scelta dell'anno non è visualizzata ed esiste un elemento html con id="calendar_table_submit", * la scelta del mese e dell'anno è confermata dal click su di esso, altrimenti, l'azione è legata all' evento * "onchange" di entramble le tendine * */ function CalendarTable( theStartYear, theStartMonth, theStartDay, paramPrefixIfInPath ){ Date.prototype.getItalianDay = function(){ /* per noi, la domenica è l'ultimo giorno, per gli anglosassoni, il primo*/ var ret = this.getDay()-1; if(ret==-1){ret=6;} return ret; } var startYear = theStartYear ? theStartYear : 1970; var startDay = theStartDay ? theStartDay : 1; var startMonth = theStartMonth ? theStartMonth-1 : 0; var selectmonth = document.getElementById('selectmonth'); var selectyear = document.getElementById('selectyear'); var startDate = new Date(); startDate.setFullYear( startYear, startMonth, startDay ); var currDate = new Date(); // importante istanziare prima di per evitare di doversi preoccupare dell''orario nei confronti var today = new Date(); var thisMonth = today.getMonth(); var thisYear = today.getFullYear(); //var selectedDay = getParameter('giorno') !='' ? parseInt(getParameter('giorno')) : today.getDate(); var selectedDay = getParameter('giorno'); if(selectedDay.charAt(0)=='0') selectedDay=selectedDay.substring(1); if(!selectedDay) selectedDay=today.getDate(); //var selectedMonth = getParameter('mese') !='' ? parseInt(getParameter('mese'))-1 : thisMonth; var selectedMonth = getParameter('mese'); if(selectedMonth.charAt(0)=='0') selectedMonth=selectedMonth.substring(1); if(selectedMonth) selectedMonth=selectedMonth-1; else selectedMonth=thisMonth; var selectedYear = getParameter('anno') !='' ? parseInt(getParameter('anno')) : thisYear; var selectedDate = new Date(); selectedDate.setFullYear( selectedYear, selectedMonth, selectedDay ); var theFirstOfTheMonth = new Date() theFirstOfTheMonth.setFullYear( selectedYear, selectedMonth, 1 ); var everyDayIsLinkedAnyWay = false; var daysInMonth = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]; var months = [ 'gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre' ]; if ( (selectedYear % 4) == 0 ){ daysInMonth[1]=29; } var lastMonthInDropDown = (selectedYear==thisYear) ? thisMonth : 11; var firstMonthInDropDown = (selectedYear==startYear) ? startMonth : 0; function getParameter( theParam ){ var ret = ''; if( !paramPrefixIfInPath ){ var qs = window.location.search.substring(1); var params = qs.split('&'); for ( var i=0; i3 ){ if( theParam=='anno' ){ ret = window.location.pathname.substr( start+1, 4 ); }else if( theParam=='mese' ){ ret = window.location.pathname.substr( start+6, 2 ); }else if( theParam=='giorno' ){ ret = window.location.pathname.substr( start+9, 2 ); } }else{ // siamo nella directory root ==> today! } } return ret; } function composeCaledarUrl( theCurrYear, theCurrMonth, theCurrDay ){ if ( !paramPrefixIfInPath ){ if ( (theCurrDay+'').length == 1 ){ theCurrDay = '0' + theCurrDay; } if ( (theCurrMonth+'').length == 1 ){ theCurrMonth = '0' + theCurrMonth; } return '?giorno=' + theCurrDay + '&mese=' + theCurrMonth + '&anno=' + theCurrYear; }else{ var start = paramPrefixIfInPath.length; var urlSuffix; if ( window.location.pathname.substring(start).split('/').length>3 ){ urlSuffix = window.location.pathname.substring( start+11 ); }else{ // siamo nella directory root ==> today! urlSuffix = window.location.pathname.substring( start ); } if ( (theCurrDay+'').length == 1 ){ theCurrDay = '0' + theCurrDay; } if ( (theCurrMonth+'').length == 1 ){ theCurrMonth = '0' + theCurrMonth; } return paramPrefixIfInPath + '/' + theCurrYear+ '/' + theCurrMonth + '/' + theCurrDay + urlSuffix; } } this.draw = function(){ var anchors = document.getElementById('calendar').getElementsByTagName('a'); for ( var i=0; i<42; i++ ){ var currDay = (i-theFirstOfTheMonth.getItalianDay()+1); if( (i>=theFirstOfTheMonth.getItalianDay() && currDay<=daysInMonth[selectedMonth]) ){ anchors[i].innerHTML = currDay; currDate.setFullYear( selectedYear, selectedMonth, currDay ); if( currDate<=today && currDate>=startDate || everyDayIsLinkedAnyWay ){ anchors[i].href = composeCaledarUrl( selectedYear, selectedMonth+1, currDay ); if( currDay == selectedDay ){ anchors[i].parentNode.className='selected'; } }else{ anchors[i].href = 'javascript:void(0)'; anchors[i].className = "ggSucc"; } }else{ anchors[i].innerHTML = ''; anchors[i].className = "ggSucc"; } } for ( var i=firstMonthInDropDown; i<=lastMonthInDropDown; i++ ){ var opt = document.createElement('option'); selectmonth.appendChild( opt ); var dropDownDate = new Date(); dropDownDate.setFullYear( selectedYear, i, selectedDay ); if(dropDownDate > startDate && dropDownDate < today) opt.value = composeCaledarUrl( selectedYear, i+1, selectedDay ); else if (dropDownDate < startDate) opt.value = composeCaledarUrl( startYear, startMonth+1, startDay ); else if (dropDownDate > today) opt.value = composeCaledarUrl( thisYear, thisMonth+1, today.getDate() ); opt.text = months[i]; if ( i==selectedMonth ){ opt.selected = true; } } if ( selectyear && !isYearDropDownHidden() ){ for ( var i=startYear; i<=thisYear; i++ ){ var opt = document.createElement('option'); selectyear.appendChild( opt ); opt.value = composeCaledarUrl( i, selectedMonth+1, selectedDay ); opt.text = i; if ( i==selectedYear ){ opt.selected = true; } } } if ( isYearDropDownHidden() ){ selectmonth.onchange = function(){ window.location.href = this.value }; if ( document.getElementById('calendar_table_submit') ) { document.getElementById('calendar_table_submit').style.display = 'none'; } }else{ if ( document.getElementById('calendar_table_submit') ) { document.getElementById('calendar_table_submit').style.display = 'block'; document.getElementById('calendar_table_submit').onclick = function(){ var startWith = selectmonth.value.substring( 0, selectmonth.value.indexOf('&anno=') ); var endWith = selectyear.value.substring( selectmonth.value.indexOf('&anno=') ); window.location.href = startWith + endWith; return false; } }else{ /* sempre meglio che niente */ selectmonth.onchange = function(){ window.location.href = this.value }; selectyear.onchange = function(){ window.location.href = this.value }; } } } this.setFullSingleYear = function( theYearToSet, hideYearDropDown ){ lastMonthInDropDown = 11; firstMonthInDropDown = 0; everyDayIsLinkedAnyWay = true; selectedYear = theYearToSet; startYear = theYearToSet; startMonth = 0; thisYear = theYearToSet; theFirstOfTheMonth.setFullYear( selectedYear, selectedMonth, 1 ); selectedDate.setFullYear( selectedYear, selectedMonth, selectedDay ); startDate.setFullYear( startYear, startMonth ); today.setFullYear( thisYear ); if ( hideYearDropDown ){ this.hideYearDropDown(); } } this.hideYearDropDown = function(){ if ( selectyear ){ selectyear.style.display='none'; } } function isYearDropDownHidden(){ return selectyear.style.display=='none'; } this.setParamPrefixIfInPath = function( thePrefix ){ paramPrefixIfInPath = thePrefix; } }