Advertisement
mike2545

calendar.js

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