Calendar.js 16 KB

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