WebCalendar

  1. // JScript 文件
  2. <!--
  3. /**//**
  4. * 返回日期
  5. * @param d the delimiter
  6. * @param p the pattern of your date
  7. */
  8. String.prototype.toDate = function(x, p) {
  9. if(x == null) x = "-";
  10. if(p == null) p = "ymd";
  11. var a = this.split(x);
  12. var y = parseInt(a[p.indexOf("y")]);
  13. //remember to change this next century ;)
  14. if(y.toString().length <= 2) y += 2000;
  15. if(isNaN(y)) y = new Date().getFullYear();
  16. var m = parseInt(a[p.indexOf("m")]) - 1;
  17. var d = parseInt(a[p.indexOf("d")]);
  18. if(isNaN(d)) d = 1;
  19. return new Date(y, m, d);
  20. }
  21. /**//**
  22. * 格式化日期
  23. * @param   d the delimiter
  24. * @param   p the pattern of your date
  25. * @author meizz
  26. */
  27. Date.prototype.format = function(style) {
  28. var o = {
  29.     "M+" : this.getMonth() + 1, //month
  30.     "d+" : this.getDate(),      //day
  31.     "h+" : this.getHours(),     //hour
  32.     "m+" : this.getMinutes(),   //minute
  33.     "s+" : this.getSeconds(),   //second
  34.     "w+" : "天一二三四五六".charAt(this.getDay()),   //week
  35.     "q+" : Math.floor((this.getMonth() + 3) / 3), //quarter
  36.     "S" : this.getMilliseconds() //millisecond
  37. }
  38. if(/(y+)/.test(style)) {
  39.     style = style.replace(RegExp.$1,
  40.     (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  41. }
  42. for(var k in o){
  43.     if(new RegExp("("+ k +")").test(style)){
  44.       style = style.replace(RegExp.$1,
  45.         RegExp.$1.length == 1 ? o[k] :
  46.         ("00" + o[k]).substr(("" + o[k]).length));
  47.     }
  48. }
  49. return style;
  50. };
  51. /**//**
  52. * 日历类
  53. * @param   beginYear 1970
  54. * @param   endYear   2030
  55. * @param   lang      0(中文)|1(英语) 可自由扩充
  56. * @param   dateFormatStyle "yyyy-MM-dd";
  57. * @version 2006-04-01
  58. * @author KimSoft (jinqinghua [at] gmail.com)
  59. * @update
  60. */
  61. function Calendar(beginYear, endYear, lang, dateFormatStyle) {
  62. this.beginYear = 1970;
  63. this.endYear = 2030;
  64. this.lang = 0;            //0(中文) | 1(英文)
  65. this.dateFormatStyle = "yyyy-MM-dd";
  66. if (beginYear != null && endYear != null){
  67.     this.beginYear = beginYear;
  68.     this.endYear = endYear;
  69. }
  70. if (lang != null){
  71.     this.lang = lang
  72. }
  73. if (dateFormatStyle != null){
  74.     this.dateFormatStyle = dateFormatStyle
  75. }
  76. this.dateControl = null;
  77. this.panel = this.getElementById("calendarPanel");
  78. this.form = null;
  79. this.date = new Date();
  80. this.year = this.date.getFullYear();
  81. this.month = this.date.getMonth();
  82. this.colors = {
  83. "cur_word"      : "#FFFFFF"//当日日期文字颜色
  84. "cur_bg"        : "#00FF00"//当日日期单元格背影色
  85. "sun_word"      : "#FF0000"//星期天文字颜色
  86. "sat_word"      : "#0000FF"//星期六文字颜色
  87. "td_word_light" : "#333333"//单元格文字颜色
  88. "td_word_dark" : "#CCCCCC"//单元格文字暗色
  89. "td_bg_out"     : "#EFEFEF"//单元格背影色
  90. "td_bg_over"    : "#FFCC00"//单元格背影色
  91. "tr_word"       : "#FFFFFF"//日历头文字颜色
  92. "tr_bg"         : "#666666"//日历头背影色
  93. "input_border" : "#CCCCCC"//input控件的边框颜色
  94. "input_bg"      : "#EFEFEF"   //input控件的背影色
  95. }
  96. this.draw();
  97. this.bindYear();
  98. this.bindMonth();
  99. this.changeSelect();
  100. this.bindData();
  101. }
  102. /**//**
  103. * 日历类属性(语言包,可自由扩展)
  104. */
  105. Calendar.language = {
  106. "year"   : [[""], [""]],
  107. "months" : [["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
  108.         ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"]
  109.          ],
  110. "weeks" : [["日","一","二","三","四","五","六"],
  111.         ["SUN","MON","TUR","WED","THU","FRI","SAT"]
  112.          ],
  113. "clear" : [["清空"], ["CLS"]],
  114. "today" : [["今天"], ["TODAY"]],
  115. "close" : [["关闭"], ["CLOSE"]]
  116. }
  117. Calendar.prototype.draw = function() {
  118. calendar = this;
  119. var mvAry = [];
  120. mvAry[mvAry.length] = ' <form name="calendarForm" style="margin: 0px;">';
  121. mvAry[mvAry.length] = '    <table width="100%" border="0" cellpadding="0" cellspacing="1">';
  122. mvAry[mvAry.length] = '      <tr>';
  123. mvAry[mvAry.length] = '        <th align="left" width="1%"><input style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:16px;height:20px;" name="prevMonth" type="button" id="prevMonth" value="<" /></th>';
  124. mvAry[mvAry.length] = '        <th align="center" width="98%" nowrap="nowrap"><select name="calendarYear" id="calendarYear" style="font-size:12px;"></select><select name="calendarMonth" id="calendarMonth" style="font-size:12px;"></select></th>';
  125. mvAry[mvAry.length] = '        <th align="right" width="1%"><input style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:16px;height:20px;" name="nextMonth" type="button" id="nextMonth" value=">" /></th>';
  126. mvAry[mvAry.length] = '      </tr>';
  127. mvAry[mvAry.length] = '    </table>';
  128. mvAry[mvAry.length] = '    <table id="calendarTable" width="100%" style="border:0px solid #CCCCCC;background-color:#FFFFFF" border="0" cellpadding="3" cellspacing="1">';
  129. mvAry[mvAry.length] = '      <tr>';
  130. for(var i = 0; i < 7; i++) {
  131.     mvAry[mvAry.length] = '      <th style="font-weight:normal;background-color:' + calendar.colors["tr_bg"] + ';color:' + calendar.colors["tr_word"] + ';">' + Calendar.language["weeks"][this.lang][i] + '</th>';
  132. }
  133. mvAry[mvAry.length] = '      </tr>';
  134. for(var i = 0; i < 6;i++){
  135.     mvAry[mvAry.length] = '    <tr align="center">';
  136.     for(var j = 0; j < 7; j++) {
  137.       if (j == 0){
  138.         mvAry[mvAry.length] = ' <td style="cursor:default;color:' + calendar.colors["sun_word"] + ';"></td>';
  139.       } else if(j == 6) {
  140.         mvAry[mvAry.length] = ' <td style="cursor:default;color:' + calendar.colors["sat_word"] + ';"></td>';
  141.       } else {
  142.         mvAry[mvAry.length] = ' <td style="cursor:default;"></td>';
  143.       }
  144.     }
  145.     mvAry[mvAry.length] = '    </tr>';
  146. }
  147. mvAry[mvAry.length] = '      <tr style="background-color:' + calendar.colors["input_bg"] + ';">';
  148. mvAry[mvAry.length] = '        <th colspan="2"><input name="calendarClear" type="button" id="calendarClear" value="' + Calendar.language["clear"][this.lang] + '" style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:100%;height:20px;font-size:12px;"/></th>';
  149. mvAry[mvAry.length] = '        <th colspan="3"><input name="calendarToday" type="button" id="calendarToday" value="' + Calendar.language["today"][this.lang] + '" style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:100%;height:20px;font-size:12px;"/></th>';
  150. mvAry[mvAry.length] = '        <th colspan="2"><input name="calendarClose" type="button" id="calendarClose" value="' + Calendar.language["close"][this.lang] + '" style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:100%;height:20px;font-size:12px;"/></th>';
  151. mvAry[mvAry.length] = '      </tr>';
  152. mvAry[mvAry.length] = '    </table>';
  153. mvAry[mvAry.length] = ' </form>';
  154. this.panel.innerHTML = mvAry.join("");
  155. this.form = document.forms["calendarForm"];
  156. this.form.prevMonth.onclick = function () {calendar.goPrevMonth(this);}
  157. this.form.nextMonth.onclick = function () {calendar.goNextMonth(this);}
  158. this.form.calendarClear.onclick = function () {calendar.dateControl.value = "";calendar.hide();}
  159. this.form.calendarClose.onclick = function () {calendar.hide();}
  160. this.form.calendarYear.onchange = function () {calendar.update(this);}
  161. this.form.calendarMonth.onchange = function () {calendar.update(this);}
  162. this.form.calendarToday.onclick = function () {
  163.     var today = new Date();
  164.     calendar.date = today;
  165.     calendar.year = today.getFullYear();
  166.     calendar.month = today.getMonth();
  167.     calendar.changeSelect();
  168.     calendar.bindData();
  169.     calendar.dateControl.value = today.format(calendar.dateFormatStyle);
  170.     calendar.hide();
  171. }
  172. }
  173. //年份下拉框绑定数据
  174. Calendar.prototype.bindYear = function() {
  175. var cy = this.form.calendarYear;
  176. cy.length = 0;
  177. for (var i = this.beginYear; i <= this.endYear; i++){
  178.     cy.options[cy.length] = new Option(i + Calendar.language["year"][this.lang], i);
  179. }
  180. }
  181. //月份下拉框绑定数据
  182. Calendar.prototype.bindMonth = function() {
  183. var cm = this.form.calendarMonth;
  184. cm.length = 0;
  185. for (var i = 0; i < 12; i++){
  186.     cm.options[cm.length] = new Option(Calendar.language["months"][this.lang][i], i);
  187. }
  188. }
  189. //向前一月
  190. Calendar.prototype.goPrevMonth = function(e){
  191. if (this.year == this.beginYear && this.month == 0){return;}
  192. this.month--;
  193. if (this.month == -1) {
  194.     this.year--;
  195.     this.month = 11;
  196. }
  197. this.date = new Date(this.year, this.month, 1);
  198. this.changeSelect();
  199. this.bindData();
  200. }
  201. //向后一月
  202. Calendar.prototype.goNextMonth = function(e){
  203. if (this.year == this.endYear && this.month == 11){return;}
  204. this.month++;
  205. if (this.month == 12) {
  206.     this.year++;
  207.     this.month = 0;
  208. }
  209. this.date = new Date(this.year, this.month, 1);
  210. this.changeSelect();
  211. this.bindData();
  212. }
  213. //改变SELECT选中状态
  214. Calendar.prototype.changeSelect = function() {
  215. var cy = this.form.calendarYear;
  216. var cm = this.form.calendarMonth;
  217. for (var i= 0; i < cy.length; i++){
  218.     if (cy.options[i].value == this.date.getFullYear()){
  219.       cy[i].selected = true;
  220.       break;
  221.     }
  222. }
  223. for (var i= 0; i < cm.length; i++){
  224.     if (cm.options[i].value == this.date.getMonth()){
  225.       cm[i].selected = true;
  226.       break;
  227.     }
  228. }
  229. }
  230. //更新年、月
  231. Calendar.prototype.update = function (e){
  232. this.year = e.form.calendarYear.options[e.form.calendarYear.selectedIndex].value;
  233. this.month = e.form.calendarMonth.options[e.form.calendarMonth.selectedIndex].value;
  234. this.date = new Date(this.year, this.month, 1);
  235. this.changeSelect();
  236. this.bindData();
  237. }
  238. //绑定数据到月视图
  239. Calendar.prototype.bindData = function () {
  240. var calendar = this;
  241. var dateArray = this.getMonthViewArray(this.date.getYear(), this.date.getMonth());
  242. var tds = this.getElementById("calendarTable").getElementsByTagName("td");
  243. for(var i = 0; i < tds.length; i++) {
  244. //tds[i].style.color = calendar.colors["td_word_light"];
  245. tds[i].style.backgroundColor = calendar.colors["td_bg_out"];
  246.     tds[i].onclick = function () {return;}
  247.     tds[i].onmouseover = function () {return;}
  248.     tds[i].onmouseout = function () {return;}
  249.     if (i > dateArray.length - 1) break;
  250.     tds[i].innerHTML = dateArray[i];
  251.     if (dateArray[i] != ""){
  252.       tds[i].onclick = function () {
  253.         if (calendar.dateControl != null){
  254.           calendar.dateControl.value = new Date(calendar.date.getFullYear(),
  255.                                                 calendar.date.getMonth(),
  256.                                                 this.innerHTML).format(calendar.dateFormatStyle);
  257.         }
  258.         calendar.hide();
  259.       }
  260.       tds[i].onmouseover = function () {
  261.         this.style.backgroundColor = calendar.colors["td_bg_over"];
  262.       }
  263.       tds[i].onmouseout = function () {
  264.         this.style.backgroundColor = calendar.colors["td_bg_out"];
  265.       }
  266.       if (new Date().format(calendar.dateFormatStyle) ==
  267.           new Date(calendar.date.getFullYear(),
  268.                    calendar.date.getMonth(),
  269.                    dateArray[i]).format(calendar.dateFormatStyle)) {
  270.         //tds[i].style.color = calendar.colors["cur_word"];
  271.         tds[i].style.backgroundColor = calendar.colors["cur_bg"];
  272.         tds[i].onmouseover = function () {
  273.           this.style.backgroundColor = calendar.colors["td_bg_over"];
  274.         }
  275.         tds[i].onmouseout = function () {
  276.           this.style.backgroundColor = calendar.colors["cur_bg"];
  277.         }
  278.       }//end if
  279.     }
  280. }
  281. }
  282. //根据年、月得到月视图数据(数组形式)
  283. Calendar.prototype.getMonthViewArray = function (y, m) {
  284. var mvArray = [];
  285. var dayOfFirstDay = new Date(y, m, 1).getDay();
  286. var daysOfMonth = new Date(y, m + 1, 0).getDate();
  287. for (var i = 0; i < 42; i++) {
  288.     mvArray[i] = "";
  289. }
  290. for (var i = 0; i < daysOfMonth; i++){
  291.     mvArray[i + dayOfFirstDay] = i + 1;
  292. }
  293. return mvArray;
  294. }
  295. //扩展 document.getElementById(id) 多浏览器兼容性 from meizz tree source
  296. Calendar.prototype.getElementById = function(id){
  297. if (typeof(id) != "string" || id == ""return null;
  298. if (document.getElementById) return document.getElementById(id);
  299. if (document.all) return document.all(id);
  300. try {return eval(id);} catch(e){ return null;}
  301. }
  302. //扩展 object.getElementsByTagName(tagName)
  303. Calendar.prototype.getElementsByTagName = function(object, tagName){
  304. if (document.getElementsByTagName) return document.getElementsByTagName(tagName);
  305. if (document.all) return document.all.tags(tagName);
  306. }
  307. //取得HTML控件绝对位置
  308. Calendar.prototype.getAbsPoint = function (e){
  309. var x = e.offsetLeft;
  310. var y = e.offsetTop;
  311. while(e = e.offsetParent){
  312.     x += e.offsetLeft;
  313.     y += e.offsetTop;
  314. }
  315. return {"x": x, "y": y};
  316. }
  317. //显示日历
  318. Calendar.prototype.show = function (dateControl, popControl) {
  319. if (dateControl == null){
  320.     throw new Error("arguments[0] is necessary")
  321. }
  322. this.dateControl = dateControl;
  323. if (dateControl.value.length > 0){
  324. this.date = new Date(dateControl.value.toDate());
  325. this.year = this.date.getFullYear();
  326. this.month = this.date.getMonth();
  327.     this.changeSelect();
  328.     this.bindData();
  329. }
  330. if (popControl == null){
  331.     popControl = dateControl;
  332. }
  333. var xy = this.getAbsPoint(popControl);
  334. this.panel.style.left = xy.x + "px";
  335. this.panel.style.top = (xy.y + dateControl.offsetHeight) + "px";
  336. this.setDisplayStyle("select""hidden");
  337. this.panel.style.visibility = "visible";
  338. }
  339. //隐藏日历
  340. Calendar.prototype.hide = function() {
  341. this.setDisplayStyle("select""visible");
  342. this.panel.style.visibility = "hidden";
  343. }
  344. //设置控件显示或隐藏
  345. Calendar.prototype.setDisplayStyle = function(tagName, style) {
  346. var tags = this.getElementsByTagName(null, tagName)
  347. for(var i = 0; i < tags.length; i++) {
  348.     if (tagName.toLowerCase() == "select" &
  349.        (tags[i].name == "calendarYear" ||
  350.       tags[i].name == "calendarMonth")){
  351.       continue;
  352.     }
  353.     tags[i].style.visibility = style;
  354. }
  355. }
  356. document.write('<div id="calendarPanel" style="position: absolute;visibility: hidden;z-index: 9999;background-color: #FFFFFF;border: 1px solid #CCCCCC;width:175px;font-size:12px;"></div>');
  357. var calendar = new Calendar();
  358. //调用calendar.show(dateControl, popControl);
  359. //-->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值