[1] | 1 | // JavaScript Document |
---|
| 2 | // <!-- <![CDATA[ |
---|
| 3 | |
---|
| 4 | // Project: Dynamic Date Selector (DtTvB) - 2006-03-16 |
---|
| 5 | // Script featured on JavaScript Kit- http://www.javascriptkit.com |
---|
| 6 | // Code begin... |
---|
| 7 | // Set the initial date. |
---|
| 8 | var ds_i_date = new Date(); |
---|
| 9 | ds_c_month = ds_i_date.getMonth() + 1; |
---|
| 10 | ds_c_year = ds_i_date.getFullYear(); |
---|
| 11 | |
---|
| 12 | var sufixe = ""; |
---|
| 13 | |
---|
| 14 | // Get Element By Id |
---|
| 15 | function ds_getel(id) { |
---|
| 16 | return document.getElementById(id); |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | // Get the left and the top of the element. |
---|
| 20 | function ds_getleft(el) { |
---|
| 21 | var tmp = el.offsetLeft; |
---|
| 22 | el = el.offsetParent |
---|
| 23 | while(el) { |
---|
| 24 | tmp += el.offsetLeft; |
---|
| 25 | el = el.offsetParent; |
---|
| 26 | } |
---|
| 27 | return tmp; |
---|
| 28 | } |
---|
| 29 | function ds_gettop(el) { |
---|
| 30 | var tmp = el.offsetTop; |
---|
| 31 | el = el.offsetParent |
---|
| 32 | while(el) { |
---|
| 33 | tmp += el.offsetTop; |
---|
| 34 | el = el.offsetParent; |
---|
| 35 | } |
---|
| 36 | return tmp; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | // Output Element |
---|
| 40 | var ds_oe = ds_getel('ds_calclass'); |
---|
| 41 | // Container |
---|
| 42 | var ds_ce = ds_getel('ds_conclass'); |
---|
| 43 | |
---|
| 44 | // Output Buffering |
---|
| 45 | var ds_ob = ''; |
---|
| 46 | function ds_ob_clean() { |
---|
| 47 | ds_ob = ''; |
---|
| 48 | } |
---|
| 49 | function ds_ob_flush() { |
---|
| 50 | ds_oe.innerHTML = ds_ob; |
---|
| 51 | ds_ob_clean(); |
---|
| 52 | } |
---|
| 53 | function ds_echo(t) { |
---|
| 54 | ds_ob += t; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | var ds_element; // Text Element... |
---|
| 58 | |
---|
| 59 | var ds_monthnames = [ |
---|
| 60 | 'Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', |
---|
| 61 | 'Juillet', 'Aout', 'Septembre', 'Octobre', 'Novembre', 'Décembre' |
---|
| 62 | ]; // You can translate it for your language. |
---|
| 63 | |
---|
| 64 | var ds_daynames = [ |
---|
| 65 | 'Dim', 'Lun', 'Mar', 'Me', 'Jeu', 'Ven', 'Sam' |
---|
| 66 | ]; // You can translate it for your language. |
---|
| 67 | |
---|
| 68 | // Calendar template |
---|
| 69 | function ds_template_main_above(t) { |
---|
| 70 | return '<table cellpadding="3" cellspacing="1" class="ds_tbl">' |
---|
| 71 | + '<tr>' |
---|
| 72 | + '<td class="ds_head" style="cursor: pointer" onclick="ds_py();"><<</td>' |
---|
| 73 | + '<td class="ds_head" style="cursor: pointer" onclick="ds_pm();"><</td>' |
---|
| 74 | + '<td class="ds_head" style="cursor: pointer" onclick="ds_hi();" colspan="3">[Fermer]</td>' |
---|
| 75 | + '<td class="ds_head" style="cursor: pointer" onclick="ds_nm();">></td>' |
---|
| 76 | + '<td class="ds_head" style="cursor: pointer" onclick="ds_ny();">>></td>' |
---|
| 77 | + '</tr>' |
---|
| 78 | + '<tr>' |
---|
| 79 | + '<td colspan="7" class="ds_head">' + t + '</td>' |
---|
| 80 | + '</tr>' |
---|
| 81 | + '<tr>'; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | function ds_template_day_row(t) { |
---|
| 85 | return '<td class="ds_subhead">' + t + '</td>'; |
---|
| 86 | // Define width in CSS, XHTML 1.0 Strict doesn't have width property for it. |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | function ds_template_new_week() { |
---|
| 90 | return '</tr><tr>'; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | function ds_template_blank_cell(colspan) { |
---|
| 94 | return '<td colspan="' + colspan + '"></td>' |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | function ds_template_day(d, m, y) { |
---|
| 98 | return '<td class="ds_cell" onclick="ds_onclick(' + d + ',' + m + ',' + y + ')">' + d + '</td>'; |
---|
| 99 | // Define width the day row. |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | function ds_template_main_below() { |
---|
| 103 | return '</tr>' |
---|
| 104 | + '</table>'; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // This one draws calendar... |
---|
| 108 | function ds_draw_calendar(m, y) { |
---|
| 109 | // First clean the output buffer. |
---|
| 110 | ds_ob_clean(); |
---|
| 111 | // Here we go, do the header |
---|
| 112 | ds_echo (ds_template_main_above(ds_monthnames[m - 1] + ' ' + y)); |
---|
| 113 | for (i = 0; i < 7; i ++) { |
---|
| 114 | ds_echo (ds_template_day_row(ds_daynames[i])); |
---|
| 115 | } |
---|
| 116 | // Make a date object. |
---|
| 117 | var ds_dc_date = new Date(); |
---|
| 118 | ds_dc_date.setMonth(m - 1); |
---|
| 119 | ds_dc_date.setFullYear(y); |
---|
| 120 | ds_dc_date.setDate(1); |
---|
| 121 | if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) { |
---|
| 122 | days = 31; |
---|
| 123 | } else if (m == 4 || m == 6 || m == 9 || m == 11) { |
---|
| 124 | days = 30; |
---|
| 125 | } else { |
---|
| 126 | days = (y % 4 == 0) ? 29 : 28; |
---|
| 127 | } |
---|
| 128 | var first_day = ds_dc_date.getDay(); |
---|
| 129 | var first_loop = 1; |
---|
| 130 | // Start the first week |
---|
| 131 | ds_echo (ds_template_new_week()); |
---|
| 132 | // If sunday is not the first day of the month, make a blank cell... |
---|
| 133 | if (first_day != 0) { |
---|
| 134 | ds_echo (ds_template_blank_cell(first_day)); |
---|
| 135 | } |
---|
| 136 | var j = first_day; |
---|
| 137 | for (i = 0; i < days; i ++) { |
---|
| 138 | // Today is sunday, make a new week. |
---|
| 139 | // If this sunday is the first day of the month, |
---|
| 140 | // we've made a new row for you already. |
---|
| 141 | if (j == 0 && !first_loop) { |
---|
| 142 | // New week!! |
---|
| 143 | ds_echo (ds_template_new_week()); |
---|
| 144 | } |
---|
| 145 | // Make a row of that day! |
---|
| 146 | ds_echo (ds_template_day(i + 1, m, y)); |
---|
| 147 | // This is not first loop anymore... |
---|
| 148 | first_loop = 0; |
---|
| 149 | // What is the next day? |
---|
| 150 | j ++; |
---|
| 151 | j %= 7; |
---|
| 152 | } |
---|
| 153 | // Do the footer |
---|
| 154 | ds_echo (ds_template_main_below()); |
---|
| 155 | // And let's display.. |
---|
| 156 | ds_ob_flush(); |
---|
| 157 | // Scroll it into view. |
---|
| 158 | // ds_ce.scrollIntoView(); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | // A function to show the calendar. |
---|
| 162 | // When user click on the date, it will set the content of t. |
---|
| 163 | function ds_sh(t, _sufixe) { |
---|
| 164 | sufixe = _sufixe; |
---|
| 165 | // Set the element to set... |
---|
| 166 | ds_element = t; |
---|
| 167 | // Make a new date, and set the current month and year. |
---|
| 168 | var ds_sh_date = new Date(); |
---|
| 169 | ds_c_month = ds_sh_date.getMonth() + 1; |
---|
| 170 | ds_c_year = ds_sh_date.getFullYear(); |
---|
| 171 | // Draw the calendar |
---|
| 172 | ds_draw_calendar(ds_c_month, ds_c_year); |
---|
| 173 | // To change the position properly, we must show it first. |
---|
| 174 | ds_ce.style.display = ''; |
---|
| 175 | // Move the calendar container! |
---|
| 176 | the_left = ds_getleft(t); |
---|
| 177 | the_top = ds_gettop(t) + t.offsetHeight; |
---|
| 178 | ds_ce.style.left = the_left + 'px'; |
---|
| 179 | ds_ce.style.top = the_top + 'px'; |
---|
| 180 | // Scroll it into view. |
---|
| 181 | // ds_ce.scrollIntoView(); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | // Hide the calendar. |
---|
| 185 | function ds_hi() { |
---|
| 186 | ds_ce.style.display = 'none'; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | // Moves to the next month... |
---|
| 190 | function ds_nm() { |
---|
| 191 | // Increase the current month. |
---|
| 192 | ds_c_month ++; |
---|
| 193 | // We have passed December, let's go to the next year. |
---|
| 194 | // Increase the current year, and set the current month to January. |
---|
| 195 | if (ds_c_month > 12) { |
---|
| 196 | ds_c_month = 1; |
---|
| 197 | ds_c_year++; |
---|
| 198 | } |
---|
| 199 | // Redraw the calendar. |
---|
| 200 | ds_draw_calendar(ds_c_month, ds_c_year); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | // Moves to the previous month... |
---|
| 204 | function ds_pm() { |
---|
| 205 | ds_c_month = ds_c_month - 1; // Can't use dash-dash here, it will make the page invalid. |
---|
| 206 | // We have passed January, let's go back to the previous year. |
---|
| 207 | // Decrease the current year, and set the current month to December. |
---|
| 208 | if (ds_c_month < 1) { |
---|
| 209 | ds_c_month = 12; |
---|
| 210 | ds_c_year = ds_c_year - 1; // Can't use dash-dash here, it will make the page invalid. |
---|
| 211 | } |
---|
| 212 | // Redraw the calendar. |
---|
| 213 | ds_draw_calendar(ds_c_month, ds_c_year); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | // Moves to the next year... |
---|
| 217 | function ds_ny() { |
---|
| 218 | // Increase the current year. |
---|
| 219 | ds_c_year++; |
---|
| 220 | // Redraw the calendar. |
---|
| 221 | ds_draw_calendar(ds_c_month, ds_c_year); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | // Moves to the previous year... |
---|
| 225 | function ds_py() { |
---|
| 226 | // Decrease the current year. |
---|
| 227 | ds_c_year = ds_c_year - 1; // Can't use dash-dash here, it will make the page invalid. |
---|
| 228 | // Redraw the calendar. |
---|
| 229 | ds_draw_calendar(ds_c_month, ds_c_year); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | // Format the date to output. |
---|
| 233 | function __ds_format_date(d, m, y) { |
---|
| 234 | // 2 digits month. |
---|
| 235 | m2 = '00' + m; |
---|
| 236 | m2 = m2.substr(m2.length - 2); |
---|
| 237 | // 2 digits day. |
---|
| 238 | d2 = '00' + d; |
---|
| 239 | d2 = d2.substr(d2.length - 2); |
---|
| 240 | // YYYY-MM-DD |
---|
| 241 | return y + '-' + m2 + '-' + d2 + sufixe; |
---|
| 242 | // return d2 + '-' + m2 + '-' + y; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | function ds_format_date(d, m, y) { |
---|
| 247 | // 2 digits month. |
---|
| 248 | m2 = '00' + m; |
---|
| 249 | m2 = m2.substr(m2.length - 2); |
---|
| 250 | // 2 digits day. |
---|
| 251 | d2 = '00' + d; |
---|
| 252 | d2 = d2.substr(d2.length - 2); |
---|
| 253 | // YYYY-MM-DD |
---|
| 254 | // return y + '-' + m2 + '-' + d2 + sufixe; |
---|
| 255 | return y + '-' + m2 + '-' + d2 + sufixe; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | // When the user clicks the day. |
---|
| 259 | function ds_onclick(d, m, y) { |
---|
| 260 | // Hide the calendar. |
---|
| 261 | ds_hi(); |
---|
| 262 | // Set the value of it, if we can. |
---|
| 263 | |
---|
| 264 | if (typeof(ds_element.value) != 'undefined') { |
---|
| 265 | ds_element.value = ds_format_date(d, m, y); |
---|
| 266 | // Maybe we want to set the HTML in it. |
---|
| 267 | } else if (typeof(ds_element.innerHTML) != 'undefined') { |
---|
| 268 | ds_element.innerHTML = ds_format_date(d, m, y); |
---|
| 269 | // I don't know how should we display it, just alert it to user. |
---|
| 270 | } else { |
---|
| 271 | alert (ds_format_date(d, m, y)); |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | // And here is the end. |
---|
| 275 | |
---|
| 276 | // ]]> --> |
---|