calendar.js
上传用户:lishan0805
上传日期:2019-12-08
资源大小:12048k
文件大小:15k
源码类别:

OA系统

开发平台:

C#

  1. <!--
  2. /**
  3.  * Calendar
  4.  * @param   beginYear           1990
  5.  * @param   endYear             2010
  6.  * @param   language            0(zh_cn)|1(en_us)|2(en_en)|3(zh_tw)
  7.  * @param   patternDelimiter    "-"
  8.  * @param   date2StringPattern  "yyyy-MM-dd"
  9.  * @param   string2DatePattern  "ymd"
  10.  * @version 1.0 build 2006-04-01
  11.  * @version 1.1 build 2006-12-17
  12.  * @author  KimSoft (jinqinghua [at] gmail.com)
  13.  * NOTE!    you can use it free, but keep the copyright please
  14.  * IMPORTANT:you must include this script file inner html body elment 
  15.  * @see http://code.google.com/p/kimsoft-jscalendar/
  16.  */
  17. function Calendar(beginYear, endYear, language, patternDelimiter, date2StringPattern, string2DatePattern) {
  18. this.beginYear = beginYear || 1990;
  19. this.endYear   = endYear   || 2020;
  20. this.language  = language  || 0;
  21. this.patternDelimiter = patternDelimiter     || "-";
  22. this.date2StringPattern = date2StringPattern || Calendar.language["date2StringPattern"][this.language].replace(/-/g, this.patternDelimiter);
  23. this.string2DatePattern = string2DatePattern || Calendar.language["string2DatePattern"][this.language];
  24. this.dateControl = null;
  25. this.panel  = this.getElementById("__calendarPanel");
  26. this.iframe = window.frames["__calendarIframe"];
  27. this.form   = null;
  28. this.date = new Date();
  29. this.year = this.date.getFullYear();
  30. this.month = this.date.getMonth();
  31. this.colors = {"bg_cur_day":"#00CC33","bg_over":"#EFEFEF","bg_out":"#FFCC00"}
  32. };
  33. Calendar.language = {
  34. "year"   : ["u5e74", "", "", "u5e74"],
  35. "months" : [
  36. ["u4e00u6708","u4e8cu6708","u4e09u6708","u56dbu6708","u4e94u6708","u516du6708","u4e03u6708","u516bu6708","u4e5du6708","u5341u6708","u5341u4e00u6708","u5341u4e8cu6708"],
  37. ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"],
  38. ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"],
  39. ["u4e00u6708","u4e8cu6708","u4e09u6708","u56dbu6708","u4e94u6708","u516du6708","u4e03u6708","u516bu6708","u4e5du6708","u5341u6708","u5341u4e00u6708","u5341u4e8cu6708"]
  40. ],
  41. "weeks"  : [["u65e5","u4e00","u4e8c","u4e09","u56db","u4e94","u516d"],
  42. ["Sun","Mon","Tur","Wed","Thu","Fri","Sat"],
  43. ["Sun","Mon","Tur","Wed","Thu","Fri","Sat"],
  44. ["u65e5","u4e00","u4e8c","u4e09","u56db","u4e94","u516d"]
  45. ],
  46. "clear"  : ["u6e05u7a7a", "Clear", "Clear", "u6e05u7a7a"],
  47. "today"  : ["u4ecau5929", "Today", "Today", "u4ecau5929"],
  48. "close"  : ["u5173u95ed", "Close", "Close", "u95dcu9589"],
  49. "date2StringPattern" : ["yyyy-MM-dd", "yyyy-MM-dd", "yyyy-MM-dd", "yyyy-MM-dd"],
  50. "string2DatePattern" : ["ymd","ymd", "ymd", "ymd"]
  51. };
  52. Calendar.prototype.draw = function() {
  53. calendar = this;
  54. var _cs = [];
  55. _cs[_cs.length] = '<form id="__calendarForm" name="__calendarForm" method="post">';
  56. _cs[_cs.length] = '<table id="__calendarTable" width="100%" border="0" cellpadding="3" cellspacing="1" align="center">';
  57. _cs[_cs.length] = ' <tr>';
  58. _cs[_cs.length] = '  <th><input class="l" name="goPrevMonthButton" type="button" id="goPrevMonthButton" value="&lt;" /></th>';
  59. _cs[_cs.length] = '  <th colspan="5"><select class="year" name="yearSelect" id="yearSelect"></select><select class="month" name="monthSelect" id="monthSelect"></select></th>';
  60. _cs[_cs.length] = '  <th><input class="r" name="goNextMonthButton" type="button" id="goNextMonthButton" value="&gt;" /></th>';
  61. _cs[_cs.length] = ' </tr>';
  62. _cs[_cs.length] = ' <tr>';
  63. for(var i = 0; i < 7; i++) {
  64. _cs[_cs.length] = '<th class="theader">';
  65. _cs[_cs.length] = Calendar.language["weeks"][this.language][i];
  66. _cs[_cs.length] = '</th>';
  67. }
  68. _cs[_cs.length] = '</tr>';
  69. for(var i = 0; i < 6; i++){
  70. _cs[_cs.length] = '<tr align="center">';
  71. for(var j = 0; j < 7; j++) {
  72. switch (j) {
  73. case 0: _cs[_cs.length] = '<td class="sun">&nbsp;</td>'; break;
  74. case 6: _cs[_cs.length] = '<td class="sat">&nbsp;</td>'; break;
  75. default:_cs[_cs.length] = '<td class="normal">&nbsp;</td>'; break;
  76. }
  77. }
  78. _cs[_cs.length] = '</tr>';
  79. }
  80. _cs[_cs.length] = ' <tr>';
  81. _cs[_cs.length] = '  <th colspan="2"><input type="button" class="b" name="clearButton" id="clearButton" /></th>';
  82. _cs[_cs.length] = '  <th colspan="3"><input type="button" class="b" name="selectTodayButton" id="selectTodayButton" /></th>';
  83. _cs[_cs.length] = '  <th colspan="2"><input type="button" class="b" name="closeButton" id="closeButton" /></th>';
  84. _cs[_cs.length] = ' </tr>';
  85. _cs[_cs.length] = '</table>';
  86. _cs[_cs.length] = '</form>';
  87. this.iframe.document.body.innerHTML = _cs.join("");
  88. this.form = this.iframe.document.forms["__calendarForm"];
  89. this.form.clearButton.value = Calendar.language["clear"][this.language];
  90. this.form.selectTodayButton.value = Calendar.language["today"][this.language];
  91. this.form.closeButton.value = Calendar.language["close"][this.language];
  92. this.form.goPrevMonthButton.onclick = function () {calendar.goPrevMonth(this);}
  93. this.form.goNextMonthButton.onclick = function () {calendar.goNextMonth(this);}
  94. this.form.yearSelect.onchange = function () {calendar.update(this);}
  95. this.form.monthSelect.onchange = function () {calendar.update(this);}
  96. this.form.clearButton.onclick = function () {calendar.dateControl.value = "";calendar.hide();}
  97. this.form.closeButton.onclick = function () {calendar.hide();}
  98. this.form.selectTodayButton.onclick = function () {
  99. var today = new Date();
  100. calendar.date = today;
  101. calendar.year = today.getFullYear();
  102. calendar.month = today.getMonth();
  103. calendar.dateControl.value = today.format(calendar.date2StringPattern);
  104. calendar.hide();
  105. }
  106. };
  107. Calendar.prototype.bindYear = function() {
  108. var ys = this.form.yearSelect;
  109. ys.length = 0;
  110. for (var i = this.beginYear; i <= this.endYear; i++){
  111. ys.options[ys.length] = new Option(i + Calendar.language["year"][this.language], i);
  112. }
  113. };
  114. Calendar.prototype.bindMonth = function() {
  115. var ms = this.form.monthSelect;
  116. ms.length = 0;
  117. for (var i = 0; i < 12; i++){
  118. ms.options[ms.length] = new Option(Calendar.language["months"][this.language][i], i);
  119. }
  120. };
  121. Calendar.prototype.goPrevMonth = function(e){
  122. if (this.year == this.beginYear && this.month == 0){return;}
  123. this.month--;
  124. if (this.month == -1) {
  125. this.year--;
  126. this.month = 11;
  127. }
  128. this.date = new Date(this.year, this.month, 1);
  129. this.changeSelect();
  130. this.bindData();
  131. };
  132. Calendar.prototype.goNextMonth = function(e){
  133. if (this.year == this.endYear && this.month == 11){return;}
  134. this.month++;
  135. if (this.month == 12) {
  136. this.year++;
  137. this.month = 0;
  138. }
  139. this.date = new Date(this.year, this.month, 1);
  140. this.changeSelect();
  141. this.bindData();
  142. };
  143. Calendar.prototype.changeSelect = function() {
  144. var ys = this.form.yearSelect;
  145. var ms = this.form.monthSelect;
  146. for (var i= 0; i < ys.length; i++){
  147. if (ys.options[i].value == this.date.getFullYear()){
  148. ys[i].selected = true;
  149. break;
  150. }
  151. }
  152. for (var i= 0; i < ms.length; i++){
  153. if (ms.options[i].value == this.date.getMonth()){
  154. ms[i].selected = true;
  155. break;
  156. }
  157. }
  158. };
  159. Calendar.prototype.update = function (e){
  160. this.year  = e.form.yearSelect.options[e.form.yearSelect.selectedIndex].value;
  161. this.month = e.form.monthSelect.options[e.form.monthSelect.selectedIndex].value;
  162. this.date = new Date(this.year, this.month, 1);
  163. this.changeSelect();
  164. this.bindData();
  165. };
  166. Calendar.prototype.bindData = function () {
  167. var calendar = this;
  168. var dateArray = this.getMonthViewDateArray(this.date.getFullYear(), this.date.getMonth());
  169. var tds = this.getElementsByTagName("td", this.getElementById("__calendarTable", this.iframe.document));
  170. for(var i = 0; i < tds.length; i++) {
  171.    tds[i].style.backgroundColor = calendar.colors["bg_over"];
  172. tds[i].onclick = null;
  173. tds[i].onmouseover = null;
  174. tds[i].onmouseout = null;
  175. tds[i].innerHTML = dateArray[i] || "&nbsp;";
  176. if (i > dateArray.length - 1) continue;
  177. if (dateArray[i]){
  178. tds[i].onclick = function () {
  179. if (calendar.dateControl){
  180. calendar.dateControl.value = new Date(calendar.date.getFullYear(),
  181. calendar.date.getMonth(),
  182. this.innerHTML).format(calendar.date2StringPattern);
  183. }
  184. calendar.hide();
  185. }
  186. tds[i].onmouseover = function () {this.style.backgroundColor = calendar.colors["bg_out"];}
  187. tds[i].onmouseout  = function () {this.style.backgroundColor = calendar.colors["bg_over"];}
  188. var today = new Date();
  189. if (today.getFullYear() == calendar.date.getFullYear()) {
  190. if (today.getMonth() == calendar.date.getMonth()) {
  191. if (today.getDate() == dateArray[i]) {
  192. tds[i].style.backgroundColor = calendar.colors["bg_cur_day"];
  193. tds[i].onmouseover = function () {this.style.backgroundColor = calendar.colors["bg_out"];}
  194. tds[i].onmouseout  = function () {this.style.backgroundColor = calendar.colors["bg_cur_day"];}
  195. }
  196. }
  197. }
  198. }//end if
  199. }//end for
  200. };
  201. Calendar.prototype.getMonthViewDateArray = function (y, m) {
  202. var dateArray = new Array(42);
  203. var dayOfFirstDate = new Date(y, m, 1).getDay();
  204. var dateCountOfMonth = new Date(y, m + 1, 0).getDate();
  205. for (var i = 0; i < dateCountOfMonth; i++) {
  206. dateArray[i + dayOfFirstDate] = i + 1;
  207. }
  208. return dateArray;
  209. };
  210. Calendar.prototype.show = function (dateControl, popuControl) {
  211. if (this.panel.style.visibility == "visible") {
  212. this.panel.style.visibility = "hidden";
  213. }
  214. if (!dateControl){
  215. throw new Error("arguments[0] is necessary!")
  216. }
  217. this.dateControl = dateControl;
  218. popuControl = popuControl || dateControl;
  219. this.draw();
  220. this.bindYear();
  221. this.bindMonth();
  222. if (dateControl.value.length > 0){
  223. this.date  = new Date(dateControl.value.toDate(this.patternDelimiter, this.string2DatePattern));
  224. this.year  = this.date.getFullYear();
  225. this.month = this.date.getMonth();
  226. }
  227. this.changeSelect();
  228. this.bindData();
  229. var xy = this.getAbsPoint(popuControl);
  230. this.panel.style.left = xy.x + "px";
  231. this.panel.style.top = (xy.y + dateControl.offsetHeight) + "px";
  232. this.panel.style.visibility = "visible";
  233. };
  234. Calendar.prototype.hide = function() {
  235. this.panel.style.visibility = "hidden";
  236. };
  237. Calendar.prototype.getElementById = function(id, object){
  238. object = object || document;
  239. return document.getElementById ? object.getElementById(id) : document.all(id);
  240. };
  241. Calendar.prototype.getElementsByTagName = function(tagName, object){
  242. object = object || document;
  243. return document.getElementsByTagName ? object.getElementsByTagName(tagName) : document.all.tags(tagName);
  244. };
  245. Calendar.prototype.getAbsPoint = function (e){
  246. var x = e.offsetLeft;
  247. var y = e.offsetTop;
  248. while(e = e.offsetParent){
  249. x += e.offsetLeft;
  250. y += e.offsetTop;
  251. }
  252. return {"x": x, "y": y};
  253. };
  254. /**
  255.  * @param   d the delimiter
  256.  * @param   p the pattern of your date
  257.  * @author  meizz
  258.  * @author  kimsoft add w+ pattern
  259.  */
  260. Date.prototype.format = function(style) {
  261. var o = {
  262. "M+" : this.getMonth() + 1, //month
  263. "d+" : this.getDate(),      //day
  264. "h+" : this.getHours(),     //hour
  265. "m+" : this.getMinutes(),   //minute
  266. "s+" : this.getSeconds(),   //second
  267. "w+" : "u65e5u4e00u4e8cu4e09u56dbu4e94u516d".charAt(this.getDay()),   //week
  268. "q+" : Math.floor((this.getMonth() + 3) / 3),  //quarter
  269. "S"  : this.getMilliseconds() //millisecond
  270. }
  271. if (/(y+)/.test(style)) {
  272. style = style.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  273. }
  274. for(var k in o){
  275. if (new RegExp("("+ k +")").test(style)){
  276. style = style.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  277. }
  278. }
  279. return style;
  280. };
  281. /**
  282.  * @param d the delimiter
  283.  * @param p the pattern of your date
  284.  * @rebuilder kimsoft
  285.  * @version build 2006.12.15
  286.  */
  287. String.prototype.toDate = function(delimiter, pattern) {
  288. delimiter = delimiter || "-";
  289. pattern = pattern || "ymd";
  290. var a = this.split(delimiter);
  291. var y = parseInt(a[pattern.indexOf("y")], 10);
  292. //remember to change this next century ;)
  293. if(y.toString().length <= 2) y += 2000;
  294. if(isNaN(y)) y = new Date().getFullYear();
  295. var m = parseInt(a[pattern.indexOf("m")], 10) - 1;
  296. var d = parseInt(a[pattern.indexOf("d")], 10);
  297. if(isNaN(d)) d = 1;
  298. return new Date(y, m, d);
  299. };
  300. document.writeln('<div id="__calendarPanel" style="position:absolute;visibility:hidden;z-index:9999;background-color:#FFFFFF;border:1px solid #666666;width:200px;height:216px;">');
  301. document.writeln('<iframe name="__calendarIframe" id="__calendarIframe" width="100%" height="100%" scrolling="no" frameborder="0" style="margin:0px;"></iframe>');
  302. var __ci = window.frames['__calendarIframe'];
  303. __ci.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
  304. __ci.document.writeln('<html xmlns="http://www.w3.org/1999/xhtml">');
  305. __ci.document.writeln('<head>');
  306. __ci.document.writeln('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
  307. __ci.document.writeln('<title>Web Calendar(UTF-8) Written By KimSoft</title>');
  308. __ci.document.writeln('<style type="text/css">');
  309. __ci.document.writeln('<!--');
  310. __ci.document.writeln('body {font-size:12px;margin:0px;text-align:center;}');
  311. __ci.document.writeln('form {margin:0px;}');
  312. __ci.document.writeln('select {font-size:12px;background-color:#EFEFEF;}');
  313. __ci.document.writeln('table {border:0px solid #CCCCCC;background-color:#FFFFFF}');
  314. __ci.document.writeln('th {font-size:12px;font-weight:normal;background-color:#FFFFFF;}');
  315. __ci.document.writeln('th.theader {font-weight:normal;background-color:#666666;color:#FFFFFF;width:24px;}');
  316. __ci.document.writeln('select.year {width:64px;}');
  317. __ci.document.writeln('select.month {width:60px;}');
  318. __ci.document.writeln('td {font-size:12px;text-align:center;}');
  319. __ci.document.writeln('td.sat {color:#0000FF;background-color:#EFEFEF;}');
  320. __ci.document.writeln('td.sun {color:#FF0000;background-color:#EFEFEF;}');
  321. __ci.document.writeln('td.normal {background-color:#EFEFEF;}');
  322. __ci.document.writeln('input.l {border: 1px solid #CCCCCC;background-color:#EFEFEF;width:20px;height:20px;}');
  323. __ci.document.writeln('input.r {border: 1px solid #CCCCCC;background-color:#EFEFEF;width:20px;height:20px;}');
  324. __ci.document.writeln('input.b {border: 1px solid #CCCCCC;background-color:#EFEFEF;width:100%;height:20px;}');
  325. __ci.document.writeln('-->');
  326. __ci.document.writeln('</style>');
  327. __ci.document.writeln('</head>');
  328. __ci.document.writeln('<body>');
  329. __ci.document.writeln('</body>');
  330. __ci.document.writeln('</html>');
  331. __ci.document.close();
  332. document.writeln('</div>');
  333. var calendar = new Calendar();
  334. //-->