WorkCalendar.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. var WorkCalendar = function(year) {
  2. this.now = new Date();
  3. if (!year) {
  4. this.year = this.now.getFullYear();
  5. } else {
  6. this.year = year;
  7. }
  8. }
  9. WorkCalendar.prototype = {
  10. render: function(id) {
  11. var html = '<div class="container" style="width:960px;"><div class="row" style="padding-bottom:10px;">';
  12. for (var i = 0; i < 12; i++) {
  13. if (i == 4 || i == 8) {
  14. html += '</div><div class="row" style="padding-bottom:10px;">';
  15. }
  16. html += this.generateMonth(i);
  17. }
  18. html += '</div></div>';
  19. $(id).append(html);
  20. },
  21. generateMonth: function(month) {
  22. var html = '<div class="datepicker dropdown-menu col-md-3" style="display: block; position: relative; z-index: 500;">'
  23. +' <div class="datepicker-days" style="display: block;">'
  24. +' <table class="table-condensed" style="width: 100%">'
  25. +' <thead>'
  26. +' <tr>'
  27. +' <th class="prev"></th>'
  28. +' <th class="switch" colspan="5">' + this.year + '年' + (month + 1) + '月</th>'
  29. +' <th class="next"></th>'
  30. +' </tr>'
  31. +' <tr>'
  32. +' <th class="dow">日</th>'
  33. +' <th class="dow">一</th>'
  34. +' <th class="dow">二</th>'
  35. +' <th class="dow">三</th>'
  36. +' <th class="dow">四</th>'
  37. +' <th class="dow">五</th>'
  38. +' <th class="dow">六</th>'
  39. +' </tr>'
  40. +' </thead>'
  41. +' <tbody>'
  42. +' <tr>';
  43. var date = new Date(this.year, month, 1);
  44. var day = date.getDay();
  45. var lastDayOfMonth = this.getLastDayOfMonth(month);
  46. for (var j = 0; j < day; j++) {
  47. html += '<td>&nbsp;</td>';
  48. }
  49. for (var j = 0; j < lastDayOfMonth; j++) {
  50. var week = (j + day) % 7;
  51. if (week == 0) {
  52. html += '</tr><tr>';
  53. }
  54. var date = this.year + '' + this.completion(month + 1) + '' + this.completion(j + 1);
  55. html += '<td class="week' + week + ' date' + date + '">' + (j + 1) + '</td>';
  56. }
  57. if ((lastDayOfMonth + day) % 7 != 0) {
  58. for (var j = (lastDayOfMonth + day) % 7; j < 7; j++) {
  59. html += '<td>&nbsp;</td>';
  60. }
  61. }
  62. html += '</tr>';
  63. if (lastDayOfMonth + day < 36) {
  64. html += "<tr><td>&nbsp;</td></tr>"
  65. }
  66. html += '</tbody>'
  67. +' </table>'
  68. +' </div>'
  69. +'</div>';
  70. return html;
  71. },
  72. completion: function(value) {
  73. if (value >= 10) {
  74. return '' + value;
  75. } else {
  76. return '0' + value;
  77. }
  78. },
  79. getLastDayOfMonth: function(month) {
  80. switch (month) {
  81. case 0:
  82. case 2:
  83. case 4:
  84. case 6:
  85. case 7:
  86. case 9:
  87. case 11:
  88. return 31;
  89. case 3:
  90. case 5:
  91. case 8:
  92. case 10:
  93. return 30;
  94. case 1:
  95. if (((this.year % 100 != 0) && (this.year % 4 == 0)) || (this.year % 400 == 0)) {
  96. return 29;
  97. } else {
  98. return 28;
  99. }
  100. default:
  101. throw new Error('invalid month : ' + month);
  102. }
  103. },
  104. activeByWeek: function(weeks) {
  105. for (var i = 0; i < weeks.length; i++) {
  106. $('.week' + weeks[i]).css({
  107. backgroundColor: 'lightgray'
  108. });
  109. }
  110. },
  111. markHolidays: function(holidays) {
  112. for (var i = 0; i < holidays.length; i++) {
  113. var item = holidays[i];
  114. $('.date' + item.date).css({
  115. backgroundColor: '#DFF0D8'
  116. });
  117. $('.date' + item.date).attr('title', item.name);
  118. }
  119. },
  120. markWorkdays: function(workdays) {
  121. for (var i = 0; i < workdays.length; i++) {
  122. var item = workdays[i];
  123. $('.date' + item.date).css({
  124. backgroundColor: '#F2DEDE'
  125. });
  126. $('.date' + item.date).attr('title', item.name);
  127. }
  128. },
  129. markExtrdays: function(extrdays) {
  130. for (var i = 0; i < extrdays.length; i++) {
  131. var item = extrdays[i];
  132. $('.date' + item.date).css({
  133. backgroundColor: '#ADD8E6'
  134. });
  135. $('.date' + item.date).attr('title', item.name);
  136. }
  137. },
  138. markNow: function() {
  139. var now = new Date();
  140. var dateText = now.getFullYear() + "" + this.completion(now.getMonth() + 1) + "" + now.getDate();
  141. $('.date' + dateText).css({
  142. backgroundColor: '#000000',
  143. color: '#FFFFFF'
  144. });
  145. $('.date' + dateText).attr('title', '今天');
  146. }
  147. };