jquery-rockcalendar.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. (function ($) {
  2. var jierixiuxi='',jierishangban='';
  3. function calendarclass(element, options){
  4. var obj = element;
  5. var can = options;
  6. var me = this,
  7. rand= js.getrand();
  8. jierixiuxi=can.jierixiuxi;jierishangban=can.jierishangban;
  9. this.onclick = can.onclick;
  10. this.mid = '';
  11. this.week = ['日','一','二','三','四','五','六'];
  12. this.obj = [];
  13. this.Y = 2014;
  14. this.m = 1;
  15. this.nY = 2014;
  16. this.nm = 1;
  17. this.nd = 29;
  18. this.marr = [31,28,31,30,31,30,31,31,30,31,30,31];
  19. this.max = 0;
  20. this.w = 0;
  21. this.nobj = null;
  22. this.sterma = {};
  23. this.dayobj = [];
  24. this.feastarr = {'0101':'元旦','0214':'情人节','一月初一':'春节','一月十五':'元宵节','七月初七':'七夕','五月初五':'端午节','0501':'劳动节','0601':'儿童节','1001':'国庆节','1111':'光棍日','八月十五':'中秋节','十二月三十':'除夕','十二月初八':'腊八','0801':'建军节','0701':'建党日','0401':'愚人节','0504':'五四青年节','0308':'妇女节','1225':'圣诞节','1224':'平安夜','1031':'万圣节','九月初九':'重阳节','0910':'教师节','0504':'青年节','0312':'植树节','0314':'白色情人节','0315':'消费者权益日','十二月廿三':'小年','0305':'学雷锋日','0422':'世界地球日','0920':'国际爱牙日','0918':'九一八事变','1213':'南京大屠杀日','1201':'世界艾滋病日','1203':'国际残疾人日','0815':'日本投降日'};
  25. this.tsarr = {'1144':'感恩节','0520':'母亲节','0630':'父亲节'};
  26. var tgString = '甲乙丙丁戊己庚辛壬癸';
  27. var dzString = '子丑寅卯辰巳午未申酉戌亥';
  28. var bool = false;
  29. this.showYue=function(y,m)
  30. {
  31. var g1=4,g2=0;
  32. var y1=1901,y2=1;
  33. var jy = y-y1;
  34. var jm = jy*12+m;
  35. var a1 = (jm+g1)%10,a2 = (jm+g2)%12;
  36. return tgString.charAt(a1)+dzString.charAt(a2);
  37. }
  38. this.showDay=function(y,m,d)
  39. {
  40. var g1=5,g2=3;
  41. var y1=1901,y2=1;
  42. var jm=0;
  43. var jd1 = js.now('time','1901-01-01');
  44. var jd2 = js.now('time',''+y+'-'+m+'-'+d+'');
  45. jm = (jd2-jd1)/1000/24/3600;
  46. var a1 = (jm+g1)%10,a2 = (jm+g2)%12;
  47. return tgString.charAt(a1)+dzString.charAt(a2);
  48. }
  49. this.init = function(){
  50. var id = obj.attr('id');
  51. this.mid = id;
  52. var s = '',bo1r='0';
  53. if(can.bordercolor!='')bo1r='1';
  54. s+='<div id="calmain_div'+this.mid+'" style="height:'+can.height+'px" class="jquery-calendar">';
  55. s+='<table width="100%" border="'+bo1r+'" style="border-collapse:collapse;border-color:'+can.bordercolor+'" height="100%" cellspacing="0" cellpadding="0">';
  56. s+='<tr>';
  57. for(var i=0;i<7;i++){
  58. s+='<td class="thtext" style="background-color:'+can.headerbgcolor+'" align="center">'+this.week[i]+'</td>';
  59. }
  60. s+='</tr>';
  61. var x = 0,w = 100/7;
  62. var h = can.height-26;
  63. for(var j=1;j<=6;j++){
  64. s+='<tr>';
  65. for(var i=0;i<7;i++){
  66. x++;
  67. s+='<td align="'+can.align+'" height="'+(h/6)+'px" valign="'+can.valign+'" width="'+w+'%" class="tdtext" id="calcontabc'+x+'_'+id+'" temp="'+x+'"></td>';
  68. }
  69. s+='</tr>';
  70. }
  71. s+='</table></div>';
  72. obj.html(s);
  73. var me = this;
  74. for(var i=1;i<=42;i++){
  75. var no = get('calcontabc'+i+'_'+id+'');
  76. this.obj[i]=no;
  77. no.onclick=function(){me.click(this)}
  78. }
  79. this.nowmonth(can.month);//当月
  80. }
  81. this.nowmonth=function(nmot)
  82. {
  83. var dt = js.now().split('-');
  84. this.Y = parseFloat(dt[0]);
  85. this.m = parseFloat(dt[1]);
  86. if(nmot){
  87. var nmots = nmot.split('-');
  88. this.Y = parseFloat(nmots[0]);
  89. this.m = parseFloat(nmots[1]);
  90. }
  91. this.nd = parseFloat(dt[2]);
  92. this.nY = this.Y;
  93. this.nm = this.m;
  94. this.setcalend();
  95. return false;
  96. }
  97. this.jieqishow = function()
  98. {
  99. var sterma={};
  100. for(var j=1900;j<=2099;j++){
  101. var gY = j;
  102. var str = '';
  103. for(var i=1;i<=12;i++){
  104. gm = i;
  105. var tmp1=lunar.sTerm(gY, gm*2-2);
  106. var tmp2=lunar.sTerm(gY, gm*2-1);
  107. sterma[''+this.sa(gm)+''+this.sa(tmp1)+'']=lunar.solarTerm[gm*2-2];
  108. sterma[''+this.sa(gm)+''+this.sa(tmp2)+'']=lunar.solarTerm[gm*2-1];
  109. str+='@'+tmp1+','+tmp2+'';
  110. }
  111. str = str.substr(1);
  112. str = "jieResel["+(j-1900)+"] = \""+str+"\";";
  113. $('body').append(str);
  114. }
  115. }
  116. this.setcalend=function()
  117. {
  118. can.changemonthbefore(this.Y, this.m, this);
  119. var gY = this.Y,gm = this.m,j1=0;
  120. this.max=this.marr[gm-1];
  121. if(gY%4==0&&gm==2)this.max=29;
  122. this.w = parseFloat(js.now('w',''+gY+'-'+gm+'-01'));
  123. var mx = this.max+this.w;
  124. var offstaa = $('#calmain_div'+this.mid+'').offset();
  125. $("span[temp='showcaentt5eaee"+this.mid+"']").remove();
  126. //当月24节气名称
  127. var tmp1=lunar.sTerm(gY, gm*2-2);
  128. var tmp2=lunar.sTerm(gY, gm*2-1);
  129. this.sterma={};
  130. //this.sterma[''+this.sa(gm)+''+this.sa(tmp1)+'']=lunar.solarTerm[gm*2-2];
  131. //this.sterma[''+this.sa(gm)+''+this.sa(tmp2)+'']=lunar.solarTerm[gm*2-1];
  132. this.nobj=null;
  133. var xqarr=[0,0,0,0,0,0,0];
  134. var oci = this.w;
  135. var savt = '';
  136. for(var i1=1+this.w;i1<=mx; i1++){
  137. xqarr[oci]++;
  138. j1++;
  139. var col1='',col2='#aaaaaa';
  140. var day = ''+gY+'-'+this.sa(gm)+'-'+this.sa(j1)+'';
  141. var lun = lunar.iconv(gY,gm,j1);
  142. var s2 = lun[2];
  143. var sn = s2;
  144. if(s2=='初一'){
  145. s2 =lun[1];
  146. col2 = '#419900';
  147. }
  148. var jie = '';
  149. var jiec = this.getJie(gY,gm,j1,lun[1]+sn);
  150. //var jiec1 = this.sterma[''+this.sa(gm)+''+this.sa(j1)+''];
  151. var jiec1 = lun[3];
  152. var jiec2 = this.tsarr[''+this.sa(gm)+''+xqarr[oci]+''+oci+''];//年第几个星期
  153. if(j1==this.nd){
  154. this.nobj=this.obj[i1];
  155. this.obj[i1].style.backgroundColor=can.selbgcolor;
  156. this.changetoday(j1);
  157. }else{
  158. this.obj[i1].style.backgroundColor='';
  159. }
  160. if(jiec!='')jie+=','+jiec+'';
  161. //节气
  162. if(jiec1){
  163. if(jiec1!='清明'){
  164. col2='#006699';
  165. }
  166. jie+=','+jiec1+'';
  167. }
  168. if(jiec2){
  169. this.sterma[''+this.sa(gm)+''+this.sa(j1)+'']=jiec2;
  170. jie+=','+jiec2+'';
  171. }
  172. if(jie!=''){
  173. jie = jie.substr(1);
  174. s2 = jie;
  175. if(col2=='#aaaaaa')col2= '#ff0000';
  176. savt+='|'+gY+'-'+gm+'-'+j1+':'+jie+'';
  177. }
  178. if(i1%7==0)col1='#ff6600';
  179. if((i1-1)%7==0)col1='#ff6600';
  180. this.obj[i1].innerHTML=this.getSpanAcc(j1,s2,col1,col2,day, i1, false);
  181. //是不是休息的
  182. var xiuval = '';
  183. if(jierixiuxi.indexOf(day)>=0)xiuval='休';
  184. if(jierishangban.indexOf(day)>=0)xiuval='班';
  185. if(xiuval!=''){
  186. var coac = '#419900';
  187. if(xiuval=='班')coac='#888888';
  188. var nest = '<span temp="showcaentt5eaee'+this.mid+'" style="font-size:12px;left:1px;top:1px;background-color:'+coac+';filter:Alpha(Opacity=100);opacity:1;padding:2px;color:#ffffff;position:absolute">'+xiuval+'</span>';
  189. $(this.obj[i1]).append(nest);
  190. }
  191. oci++;
  192. if(oci==7)oci=0;
  193. };
  194. if(!can.fillot){
  195. can.changemonth(this.Y, this.m, this);
  196. return;
  197. }
  198. //填充其余的
  199. var lY = gY,lm = gm-1,lx=0,lxu=0;
  200. if(lm==0){
  201. lY = lY-1;
  202. lm = 12;
  203. }
  204. lx = this.marr[lm-1];
  205. if(lY%4==0 && lm==2)lx++;//闰年2月29天
  206. for(var i=this.w;i>=1;i--){
  207. var day = ''+lY+'-'+this.sa(lm)+'-'+this.sa(lx)+'';
  208. var lun = lunar.iconv(lY,lm,lx);
  209. var s2 = lun[2];
  210. this.obj[i].innerHTML=this.getSpanAcc(lx,s2,'#cccccc','#cccccc', day,i, true);
  211. this.obj[i].style.backgroundColor='';
  212. //是不是休息的
  213. var xiuval = '';
  214. if(jierixiuxi.indexOf(day)>=0)xiuval='休';
  215. if(jierishangban.indexOf(day)>=0)xiuval='班';
  216. if(xiuval!=''){
  217. var coac = '#419900';
  218. if(xiuval=='班')coac='#888888';
  219. var nest = '<span temp="showcaentt5eaee'+this.mid+'" style="font-size:12px;left:1px;top:1px;background-color:'+coac+';filter:Alpha(Opacity=50);opacity:0.5;padding:2px;color:#ffffff;position:absolute">'+xiuval+'</span>';
  220. $(this.obj[i]).append(nest);
  221. }
  222. lx--;
  223. }
  224. lm = gm+1;lY = gY;
  225. if(lm==13){
  226. lY = lY+1;
  227. lm = 1;
  228. }
  229. for(var i=j1+1+this.w;i<=42; i++){
  230. lxu++;
  231. var day = ''+lY+'-'+this.sa(lm)+'-'+this.sa(lxu)+'';
  232. var lun = lunar.iconv(lY,lm,lxu);
  233. var s2 = lun[2];
  234. this.obj[i].innerHTML=this.getSpanAcc(lxu,s2,'#cccccc','#cccccc', day,i, true);
  235. this.obj[i].style.backgroundColor='';
  236. //是不是休息的
  237. var xiuval = '';
  238. if(jierixiuxi.indexOf(day)>=0)xiuval='休';
  239. if(jierishangban.indexOf(day)>=0)xiuval='班';
  240. if(xiuval!=''){
  241. var coac = '#419900';
  242. if(xiuval=='班')coac='#888888';
  243. var nest = '<span temp="showcaentt5eaee'+this.mid+'" style="font-size:12px;position:absolute;left:1px;top:1px;background-color:'+coac+';filter:Alpha(Opacity=50);opacity:0.5;padding:2px;color:#ffffff;">'+xiuval+'</span>';
  244. $(this.obj[i]).append(nest);
  245. }
  246. }
  247. can.changemonth(this.Y, this.m, this);
  248. }
  249. this.changetoday=function(d)
  250. {
  251. var gY = this.Y,gm = this.m;
  252. var day = js.now('Y年m月d日 星期W',''+gY+'-'+gm+'-'+d+'');
  253. var lun = lunar.iconv(gY, gm, d);
  254. //alert(day);
  255. $('#leftday').html(this.sa(d));
  256. $('#changedate').html(''+day+'');
  257. $('#lunanday').html(''+gY+'年 农历 '+lun[1]+''+lun[2]+'');
  258. //什么月
  259. var yue1 = this.showYue(gY,gm);
  260. //什么日
  261. var day1 = this.showDay(gY,gm,d);
  262. $('#lunanday1').html(''+lun[0]+' '+yue1+'月 '+day1+'日');
  263. var jie = '';
  264. var jiec = this.getJie(gY,gm,d,lun[1]+lun[2]);
  265. var jiec1 = this.sterma[''+this.sa(gm)+''+this.sa(d)+''];
  266. if(jiec!='')jie+=','+jiec+'';
  267. if(jiec1)jie+=','+jiec1+'';
  268. if(jie!=''){
  269. jie = jie.substr(1);
  270. }else{jie='&nbsp;'}
  271. this.onclick(gY,gm,d, day, lun, jie);
  272. };
  273. this.click=function(o1)
  274. {
  275. var d = parseFloat($(o1).attr('temp'));
  276. var dc = d-this.w;
  277. var da = $(this.obj[d]).find('span[dt]:eq(0)').html();
  278. if(!da)return;
  279. da = da.split(',');
  280. this.nd = parseFloat(da[0]);
  281. if(dc<=0){
  282. if(can.boofan)this.fanmonth(-1);
  283. return ;
  284. }
  285. if(dc>this.max){
  286. if(can.boofan)this.fanmonth(1);
  287. return;
  288. }
  289. if(this.nobj!=null)this.nobj.style.backgroundColor='';
  290. this.obj[d].style.backgroundColor=can.selbgcolor;
  291. this.nobj = this.obj[d];
  292. this.changetoday(dc);
  293. };
  294. this.getSpanAcc=function(s1,s2,col1,col2, day, oi, lbo)
  295. {
  296. if(s2.indexOf('国际')==0 || s2.indexOf('世界')==0)col2='#419900';
  297. var s = '<div><font color='+col1+'>'+s1+'</font><font style="font-size:11px" color='+col2+'>,'+s2+'</font></div>';
  298. var sq = can.renderer(day, s, s1,s2,col1,col2, oi,this);
  299. if(sq)s = sq;
  300. s+= '<span style="display:none" dt="'+s1+'">'+s1+','+day+'</span>';
  301. this.dayobj[oi]={day:day,d:s1};
  302. if(!can.overShow && lbo)return '';
  303. return s;
  304. };
  305. this.getFistdt = function(){
  306. var d = this.dayobj[1];
  307. return d.day;
  308. };
  309. this.getLastdt = function(){
  310. var d = this.dayobj[42];
  311. return d.day;
  312. };
  313. this.fanyear = function(oi)
  314. {
  315. this.Y=this.Y+oi;
  316. this.setcalend();
  317. return false;
  318. };
  319. this.showjie = function(o,yeas)
  320. {
  321. var val = o.value;
  322. if(val=='')return;
  323. this.m = parseFloat(val);
  324. this.Y = yeas;
  325. this.setcalend();
  326. };
  327. this.fanmonth = function(oi)
  328. {
  329. oi = parseFloat(oi);
  330. this.m = this.m+oi;
  331. if(this.m==0){
  332. this.m = 12;
  333. this.Y = this.Y-1;
  334. }
  335. if(this.m==13){
  336. this.m = 1;
  337. this.Y = this.Y+1;
  338. }
  339. this.setcalend();
  340. return false;
  341. };
  342. this.nextMonth = function(){
  343. this.fanmonth(1);
  344. };
  345. this.lastMonth = function(){
  346. this.fanmonth(-11);
  347. };
  348. this.getJie = function(y,m,d,nr)
  349. {
  350. var s1 = this.sa(m)+this.sa(d),s2 = nr;
  351. var a = this.feastarr;
  352. var s = '';
  353. if(a[s1]){
  354. var s3 = a[s1];
  355. if(y<1949&&m==10&&d==1)s3='';
  356. if(s3)s+=','+s3+'';
  357. }
  358. if(a[s2]){
  359. s+=','+a[s2]+'';
  360. }
  361. if(s!='')s=s.substr(1);
  362. return s;
  363. };
  364. this.sa = function(s)
  365. {
  366. s = ''+s+'';
  367. if(s.length<=1)s='0'+s+'';
  368. return s;
  369. };
  370. this.changemonth = function(y1,m1)
  371. {
  372. this.Y = parseFloat(y1);
  373. this.m = parseFloat(m1);
  374. this.setcalend();
  375. return false;
  376. };
  377. this.setbgcolor = function(oi, col){
  378. if(!col)col='';
  379. this.obj[oi].style.backgroundColor=col;
  380. };
  381. this.setMonth = function(mon)
  382. {
  383. var a = mon.split('-');
  384. this.changemonth(a[0], a[1]);
  385. }
  386. }
  387. $.fn.rockcalendar = function(options){
  388. var defaultVal = {
  389. height:400,selbgcolor:'#D3FFF6',month:'',
  390. fillot:true,renderer:function(){return ''},align:'left',valign:'top',
  391. changemonth:function(){},boofan:true,onclick:function(){},jierixiuxi:'',jierishangban:'',headerbgcolor:'',
  392. bordercolor:'',
  393. overShow:true,
  394. changemonthbefore:function(){}
  395. };
  396. var can = $.extend({}, defaultVal, options);
  397. var clsa = new calendarclass($(this), can);
  398. clsa.init();
  399. return clsa;
  400. };
  401. })(jQuery);