FlexoCalendar.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. AUTHOR : ELEANOR MAO
  3. EMAIL : danningmao@outlook.com
  4. 2016-02-5
  5. */
  6. +(function($){
  7. 'use strict';
  8. //↓可选参数
  9. var CALENDAR_OPTION = {
  10. type : 'normal',//'normal','weekly','monthly'
  11. id : '',//日历的id
  12. className : '',//日历的样式
  13. dayNames : ['日', '一', '二', '三', '四', '五', '六'],//周名
  14. numbers : ['一', '二', '三', '四', '五', '六'],//数字标示
  15. today : true,//是否标志今天
  16. select : true,//是否单击选中
  17. multi : false,//是否单击可多选
  18. disOtherMonth : false,//其他月日期是否可选
  19. setMonth : null,//设定月
  20. setYear : null,//设定年
  21. selectDate : null, //format 'yyyy-mm-dd' or 'yyyy-mm-weekn' every element can be set to 'each';
  22. allowDate : null, //format [yyyy-mm-dd,yyyy-mm-dd]
  23. prev: 'icon-arrow-left',//向前的按钮
  24. next: 'icon-arrow-right',//向后的按钮
  25. onselect: function(){},//当选中的回调函数
  26. ongoprev: function(){},//当往前翻的回调函数
  27. ongonext: function(){} //当往后翻的回调函数
  28. }
  29. var Calendar = function( element, options){
  30. this.el = $(element);
  31. this.opt = null;
  32. this.date = null;
  33. this.type = null;
  34. this.calendar = '';
  35. this.init(element, options);
  36. }
  37. Calendar.prototype.init = function(element, options){
  38. this.date = this.getDate();
  39. var options = this.opt = this.getOptions( options, this.date );
  40. this.type = options.type;
  41. options.target = $(element);
  42. this.calendar = '<table class="flexoCalendar '+ options.className +'" id="'+ options.id +'" cellspacing="0">';
  43. this.calendar += this.bulidCH(options);
  44. if( this.type == 'normal' ){
  45. this.calendar += this.bulidWeekBar(options);
  46. this.calendar += '</thead>';
  47. this.calendar += '<tbody>';
  48. this.calendar += this.bulidFull(options);
  49. }
  50. else if(this.type == 'weekly' ){
  51. this.calendar += '</thead><tbody>';
  52. this.calendar += this.bulidWeekly(options);
  53. }
  54. else if(this.type == 'monthly' ){
  55. this.calendar += '</thead><tbody>';
  56. this.calendar += this.bulidMonthly(options);
  57. }
  58. this.calendar +='</tbody></table>';
  59. $(element).append(this.calendar).trigger('click');
  60. $(element).on('click', ".prev, .next", options, function(){
  61. var month = $(this).data('month').split('-')[1];
  62. var year = $(this).data('month').split('-')[0];
  63. var allowDate = options.allowDate;
  64. var calendar = '';
  65. if ( $.isArray(allowDate) && (( year == allowDate[0].split('-')[0] && month == allowDate[0].split('-')[1] ) || ( year == allowDate[1].split('-')[0] && month == allowDate[1].split('-')[1])) ) return;
  66. if(options.type == 'normal'){
  67. calendar = Calendar.prototype.bulidFull(options, year, month);
  68. }
  69. else if(options.type == 'monthly'){
  70. calendar = Calendar.prototype.bulidMonthly(options, year, month);
  71. }
  72. else if(options.type == 'weekly'){
  73. calendar = Calendar.prototype.bulidWeekly(options, year, month);
  74. }
  75. $(options.target).find('table.flexoCalendar tbody').empty().append(calendar).trigger('el.create');
  76. Calendar.prototype.changeCH(this,options);
  77. if( this.className == 'prev' && options.ongoprev ){
  78. options.ongoprev.call(options.target, $(this).next('.current-year').data('year'), $(this).data('month'));
  79. }
  80. if( this.className == 'next' && options.ongonext ){
  81. options.ongonext.call(options.target, $(this).next('.current-year').data('year'), $(this).data('month'));
  82. }
  83. })
  84. if( options.select ){
  85. $(element).on('click', 'table>tbody>tr>td', options, function(){
  86. if( !options.multi ){
  87. $(this).parents("tbody").find("td.selected").removeClass('selected');
  88. }
  89. if( options.disOtherMonth ){
  90. if( this.className.split(' ')[0] != 'other-month'){ $(this).addClass('selected'); };
  91. }
  92. else{
  93. $(this).addClass('selected');
  94. }
  95. if( options.onselect ){
  96. options.onselect.call(options.target, $(this).data('time'));
  97. }
  98. })
  99. }
  100. }
  101. Calendar.prototype.bulidCH = function(options){
  102. var calendar='';
  103. var year = options.setYear;
  104. var month = options.setMonth;
  105. var prevdate = this.getPrev(year, month, this.type);
  106. var nextdate = this.getNext(year, month, this.type);
  107. var prev = options.prev;
  108. var next = options.next;
  109. if( this.type == 'normal' ){
  110. calendar +='<thead><tr class="calendar-hd"><th class="prev" data-month="'+prevdate+'"><i class="'+prev+'"></i></th><th class="current-year" data-year="'+year+'-'+this.dispose(month)+'" colspan="5">'+year+'年'+this.dispose(month)+'月</th><th class="next" data-month="'+nextdate+'"><i class="'+next+'"></i></th></tr>';
  111. }
  112. else if(this.type == 'weekly' ){
  113. var prevMonth = prevdate.split('-')[1];
  114. var prevYear = prevdate.split('-')[0];
  115. var nextMonth = nextdate.split('-')[1];
  116. var nextYear = nextdate.split('-')[0];
  117. var prevWeek = this.forWeek(prevYear ,prevMonth);
  118. var nextWeek = this.forWeek(nextYear ,nextMonth);
  119. calendar +='<thead><tr class="calendar-hd"><th class="prev" data-month="'+prevdate+'" data-week="'+prevWeek+'"><i class="'+prev+'"></i></th><th class="current-year" data-year="'+year+'-'+this.dispose(month)+'" colspan="5">'+year+'年'+this.dispose(month)+'月</th><th class="next" data-month="'+nextdate+'" data-week="'+nextWeek+'"><i class="'+next+'"></i></th></tr></thead>';
  120. }
  121. else if(this.type == 'monthly' ){
  122. calendar +='<thead><tr class="calendar-hd"><th class="prev" data-month="'+prevdate+'" colspan="2"><i class="'+prev+'"></i></th><th class="current-year" data-year="'+year+'-'+this.dispose(month)+'" colspan="4">'+year+'年</th><th class="next" data-month="'+nextdate+'" colspan="2"><i class="'+next+'"></i></th></tr></thead>';
  123. }
  124. return calendar;
  125. }
  126. Calendar.prototype.changeCH = function(element,options){
  127. var month = $(element).data('month').split('-')[1];
  128. var year = $(element).data('month').split('-')[0];
  129. month = parseInt(month);
  130. year = parseInt(year);
  131. var type = options.type;
  132. var prevdate = this.getPrev(year, month, type);
  133. var nextdate = this.getNext(year, month, type);
  134. var crtdate = year + '-' + this.dispose(month);
  135. var header = $(options.target).find(".current-year");
  136. if(type == 'weekly'){
  137. var prevMonth = prevdate.split('-')[1];
  138. var prevYear = prevdate.split('-')[0];
  139. var nextMonth = nextdate.split('-')[1];
  140. var nextYear = nextdate.split('-')[0];
  141. var prevWeek = this.forWeek(prevYear ,prevMonth);
  142. var nextWeek = this.forWeek(nextYear ,nextMonth);
  143. $(options.target).find(".prev").data('month', prevdate).attr('data-month', prevdate).data('week', prevWeek).attr('data-week', prevWeek);
  144. $(options.target).find(".next").data('month', nextdate).attr('data-month', nextdate).data('week', nextWeek).attr('data-week', nextWeek);
  145. }else{
  146. $(options.target).find(".prev").data('month', prevdate).attr('data-month', prevdate);
  147. $(options.target).find(".next").data('month', nextdate).attr('data-month', nextdate);
  148. }
  149. $(options.target).find(".current-year").data('year', crtdate).attr('data-year', crtdate);
  150. if(type == 'monthly' ){
  151. header.text(year+'年');
  152. }else{
  153. header.text(year+'年'+ this.dispose(month) +'月');
  154. }
  155. }
  156. Calendar.prototype.bulidWeekBar = function(options){
  157. var calendar = '<tr class="weekday">';
  158. for(var i = 0; i < 7; i++){
  159. calendar += '<th data-day=day'+i+'>'+options.dayNames[i]+'</th>';
  160. }
  161. calendar += '</tr></thead>';
  162. return calendar;
  163. }
  164. Calendar.prototype.bulidFull = function(options, year, month){
  165. var calendar = '';
  166. var month = month ? parseInt(month) : parseInt(options.setMonth);
  167. var year = year ? parseInt(year) : parseInt(options.setYear);
  168. var prevdate = this.getPrev(year, month, 'normal');
  169. var nextdate = this.getNext(year, month, 'normal');
  170. var monthLen = eli.getMonthLen(year, month);
  171. var prevMonthLen = eli.getMonthLen(prevdate.split('-')[0], prevdate.split('-')[1]);
  172. var firDay = new Date(year,month-1,1).getDay();
  173. var selectDate = options.selectDate;
  174. selectDate = typeof selectDate == 'string' ? selectDate.indexOf('-') != -1 ? selectDate.split('-') : null : null;
  175. if( selectDate ){
  176. selectDate[0] = selectDate[0] == 'each' ? year : parseInt(selectDate[0]);
  177. selectDate[1] = selectDate[1] == 'each' ? month : parseInt(selectDate[1]);
  178. selectDate[2] = selectDate[2] == 'each' ? 1 : parseInt(selectDate[2]);
  179. }
  180. for(var j = 0; j < 42; j++){
  181. if( j % 7 == 0 && j != 41 ){
  182. if( j != 0){
  183. calendar +="</tr><tr>";
  184. }
  185. else{
  186. calendar +="<tr>";
  187. }
  188. }
  189. //当前月
  190. if( j >= firDay && ( j < (firDay + monthLen)) ){
  191. if( options.today ){
  192. if( j == parseInt(options.crtdate) + firDay - 1 && options.realyear == year && parseInt(options.realmonth) == month){
  193. if( selectDate && selectDate[0] == year && selectDate[1] == month && selectDate[2] == parseInt(options.crtdate) ){
  194. calendar += '<td class="current-month current-day selected-day tdday day'+(j % 7)+'" data-time="'+year+'-'+this.dispose(month)+'-'+this.dispose((j - firDay + 1))+'"><span class="day">'+(j-firDay+1)+'</span></td>';
  195. }else{
  196. calendar += '<td class="current-month current-day tdday day'+(j % 7)+'" data-time="'+year+'-'+this.dispose(month)+'-'+this.dispose((j - firDay + 1))+'"><span class="day">'+(j-firDay+1)+'</span></td>';
  197. }
  198. }else{
  199. if( selectDate && selectDate[0] == year && selectDate[1] == month && selectDate[2] == ( j -firDay + 1 )){
  200. calendar +='<td class="current-month selected-day tdday day'+(j % 7)+'" data-time="'+year+'-'+this.dispose(month)+'-'+this.dispose((j - firDay + 1))+'"><span class="day">'+(j-firDay+1)+'</span></td>';
  201. }else{
  202. calendar +='<td class="current-month tdday day'+(j % 7)+'" data-time="'+year+'-'+this.dispose(month)+'-'+this.dispose((j - firDay + 1))+'"><span class="day">'+(j-firDay+1)+'</span></td>';
  203. }
  204. }
  205. }else{
  206. if( selectDate && selectDate[0] == year && selectDate[1] == month && selectDate[2] == ( j -firDay + 1 )){
  207. calendar +='<td class="current-month selected-day tdday day'+(j % 7)+'" data-time="'+year+'-'+this.dispose(month)+'-'+this.dispose((j - firDay + 1))+'"><span class="day">'+(j-firDay+1)+'</span></td>';
  208. }else{
  209. calendar +='<td class="current-month tdday day'+(j % 7)+'" data-time="'+year+'-'+this.dispose(month)+'-'+this.dispose((j - firDay + 1))+'"><span class="day">'+(j-firDay+1)+'</span></td>';
  210. }
  211. }
  212. }
  213. //上个月
  214. else if( j < firDay ){
  215. if( selectDate && ( selectDate[0] + '-' + selectDate[1] ) == prevdate && selectDate[2] == (prevMonthLen - (firDay - j - 1)) ){
  216. calendar +='<td class="other-month selected-day prev-month day'+(j % 7)+'" data-time="'+prevdate+'-'+this.dispose((prevMonthLen - (firDay - j - 1)))+'"><span class="day">'+(prevMonthLen - (firDay - j - 1))+'</span></td>';
  217. }else{
  218. calendar +='<td class="other-month prev-month day'+(j % 7)+'" data-time="'+prevdate+'-'+this.dispose((prevMonthLen - (firDay - j - 1)))+'"><span class="day">'+(prevMonthLen - (firDay - j - 1))+'</span></td>';
  219. }
  220. }
  221. //下个月
  222. else if( j >= (firDay + monthLen)){
  223. if( selectDate && ( selectDate[0] + '-' + selectDate[1] ) == nextdate && selectDate[2] == (j - monthLen - firDay + 1) ){
  224. calendar += '<td class="other-month selected-day next-month day'+(j % 7)+'" data-time="'+nextdate+'-'+this.dispose((j - monthLen - firDay + 1))+'"><span class="day">'+(j - monthLen - firDay + 1)+'</span></td>';
  225. }else{
  226. calendar += '<td class="other-month next-month day'+(j % 7)+'" data-time="'+nextdate+'-'+this.dispose((j - monthLen - firDay + 1))+'"><span class="day">'+(j - monthLen - firDay + 1)+'</span></td>';
  227. }
  228. }
  229. }
  230. calendar +='</tr>';
  231. return calendar;
  232. }
  233. Calendar.prototype.bulidWeekly = function(options, year ,month){
  234. var calendar = '';
  235. var numbers = options.numbers;
  236. var month = month ? parseInt(month) : parseInt(options.setMonth);
  237. var year = year ? parseInt(year) : parseInt(options.setYear);
  238. var prevdate = this.getPrev(year, month, 'weekly');
  239. var nextdate = this.getNext(year, month, 'weekly');
  240. var monthLen = eli.getMonthLen(year, month);
  241. var prevMonth = prevdate.split('-')[1];
  242. var nextMonth = nextdate.split('-')[1];
  243. var prevMonthLen = eli.getMonthLen(prevdate.split('-')[0], prevMonth);
  244. var firDay = new Date(year,month-1,1).getDay();
  245. firDay = firDay == 0 ? 7 : firDay;
  246. var startDay = firDay != 1 ? prevMonthLen - firDay + 2 : 1 ;
  247. var startMonth = startDay != 1? prevMonth : month ;
  248. var endDay = parseInt(startDay) + 6 > parseInt(prevMonthLen)? parseInt(startDay) + 6 - parseInt(prevMonthLen) : parseInt(startDay) + 6;
  249. var startYear = startMonth > month ? year - 1 : year ;
  250. var endYear = startMonth == 12 ? startYear + 1 : startYear ;
  251. var selectDate = options.selectDate;
  252. selectDate = typeof selectDate == 'string' ? selectDate.indexOf('-') != -1 ? selectDate.split('-') : null : null;
  253. if( selectDate ){
  254. selectDate[0] = selectDate[0] == 'each' ? year : parseInt(selectDate[0]);
  255. selectDate[1] = selectDate[1] == 'each' ? month : parseInt(selectDate[1]);
  256. selectDate[2] = selectDate[2] == 'each' ? 'week1' : selectDate[2].indexOf('week') != -1 ? selectDate[2] : parseInt(selectDate[2]);
  257. if( typeof selectDate[2] == 'number'){ selectDate[2] = eli.getMonthWeek(selectDate[0], selectDate[1], selectDate[2])}
  258. }
  259. var crt;
  260. for(var j = 0; j < 5; j++ ){
  261. if( j != 0 && parseInt(startDay) >= parseInt(endDay) ){
  262. if( selectDate ){
  263. crt = 'week' + ( j + 1 );
  264. if( ($.isArray(selectDate[2]) && (year == selectDate[2][0][0] && month == selectDate[2][0][1] && selectDate[2][0][2] == crt)) || ( year == selectDate[0] && month == selectDate[1] && selectDate[2] == crt ) ){
  265. calendar += '<tr><td class="selected-week tweek week'+(j + 1)+'" data-time="'+startYear+'-'+this.dispose(startMonth)+'-'+this.dispose(startDay)+','+endYear+'-'+this.dispose(month)+'-'+this.dispose(endDay)+'" colspan="7"><div><span class="week">第'+ numbers[j] +'周</span><span class="process">'+this.dispose(startMonth)+'/'+this.dispose(startDay)+'~'+this.dispose(nextMonth)+'/'+this.dispose(endDay)+'</span></div></td></tr>';
  266. }
  267. else{
  268. calendar += '<tr><td class="tweek week'+(j + 1)+'" data-time="'+startYear+'-'+this.dispose(startMonth)+'-'+this.dispose(startDay)+','+endYear+'-'+this.dispose(nextMonth)+'-'+this.dispose(endDay)+'" colspan="7"><div><span class="week">第'+ numbers[j] +'周</span><span class="process">'+this.dispose(startMonth)+'/'+this.dispose(startDay)+'~'+this.dispose(nextMonth)+'/'+this.dispose(endDay)+'</span></div></td></tr>';
  269. }
  270. }else{
  271. calendar += '<tr><td class="tweek week'+(j + 1)+'" data-time="'+startYear+'-'+this.dispose(startMonth)+'-'+this.dispose(startDay)+','+endYear+'-'+this.dispose(nextMonth)+'-'+this.dispose(endDay)+'" colspan="7"><div><span class="week">第'+ numbers[j] +'周</span><span class="process">'+this.dispose(startMonth)+'/'+this.dispose(startDay)+'~'+this.dispose(nextMonth)+'/'+this.dispose(endDay)+'</span></div></td></tr>';
  272. }
  273. }
  274. else if( options.today && ( (parseInt(options.realmonth) == startMonth && startDay <= parseInt(options.crtdate)) || ( parseInt(options.realmonth) != startMonth && startDay >= parseInt(options.crtdate)) ) && parseInt(options.crtdate) <= endDay && options.realyear == year && options.realmonth == month){
  275. if( selectDate ){
  276. crt = 'week' + ( j + 1 );
  277. if( ($.isArray(selectDate[2]) && ((year == selectDate[2][0][0] && month == selectDate[2][0][1] && selectDate[2][0][2] == crt) || ( selectDate[2][1] && year == selectDate[2][1][0] && month == selectDate[2][1][1] && selectDate[2][1][2] == crt))) || ( year == selectDate[0] && month == selectDate[1] && selectDate[2] == crt ) ){
  278. calendar += '<tr><td class="current-week current-day selected-week tweek week'+(j + 1)+'" data-time="'+startYear+'-'+this.dispose(startMonth)+'-'+this.dispose(startDay)+','+endYear+'-'+this.dispose(month)+'-'+this.dispose(endDay)+'" colspan="7"><div><span class="week">第'+ numbers[j] +'周</span><span class="process">'+this.dispose(startMonth)+'/'+this.dispose(startDay)+'~'+this.dispose(month)+'/'+this.dispose(endDay)+'</span></div></td></tr>';
  279. }else{
  280. calendar += '<tr><td class="current-week current-day tweek week'+(j + 1)+'" data-time="'+startYear+'-'+this.dispose(startMonth)+'-'+this.dispose(startDay)+','+endYear+'-'+this.dispose(month)+'-'+this.dispose(endDay)+'" colspan="7"><div><span class="week">第'+ numbers[j] +'周</span><span class="process">'+this.dispose(startMonth)+'/'+this.dispose(startDay)+'~'+this.dispose(month)+'/'+this.dispose(endDay)+'</span></div></td></tr>';
  281. }
  282. }else{
  283. calendar += '<tr><td class="current-week current-day tweek week'+(j + 1)+'" data-time="'+startYear+'-'+this.dispose(startMonth)+'-'+this.dispose(startDay)+','+endYear+'-'+this.dispose(month)+'-'+this.dispose(endDay)+'" colspan="7"><div><span class="week">第'+ numbers[j] +'周</span><span class="process">'+this.dispose(startMonth)+'/'+this.dispose(startDay)+'~'+this.dispose(month)+'/'+this.dispose(endDay)+'</span></div></td></tr>';
  284. }
  285. }
  286. else if( selectDate ){
  287. crt = 'week' + ( j + 1 );
  288. if( ($.isArray(selectDate[2]) && ((year == selectDate[2][0][0] && month == selectDate[2][0][1] && selectDate[2][0][2] == crt) || ( selectDate[2][1] && year == selectDate[2][1][0] && month == selectDate[2][1][1] && selectDate[2][1][2] == crt))) || ( year == selectDate[0] && month == selectDate[1] && selectDate[2] == crt ) ){
  289. calendar += '<tr><td class="selected-week tweek week'+(j + 1)+'" data-time="'+startYear+'-'+this.dispose(startMonth)+'-'+this.dispose(startDay)+','+endYear+'-'+this.dispose(month)+'-'+this.dispose(endDay)+'" colspan="7"><div><span class="week">第'+ numbers[j] +'周</span><span class="process">'+this.dispose(startMonth)+'/'+this.dispose(startDay)+'~'+this.dispose(month)+'/'+this.dispose(endDay)+'</span></div></td></tr>';
  290. }else{
  291. calendar += '<tr><td class="tweek week'+(j + 1)+'" data-time="'+startYear+'-'+this.dispose(startMonth)+'-'+this.dispose(startDay)+','+endYear+'-'+this.dispose(month)+'-'+this.dispose(endDay)+'" colspan="7"><div><span class="week">第'+ numbers[j] +'周</span><span class="process">'+this.dispose(startMonth)+'/'+this.dispose(startDay)+'~'+this.dispose(month)+'/'+this.dispose(endDay)+'</span></div></td></tr>';
  292. }
  293. }
  294. else{
  295. calendar += '<tr><td class="tweek week'+(j + 1)+'" data-time="'+startYear+'-'+this.dispose(startMonth)+'-'+this.dispose(startDay)+','+endYear+'-'+this.dispose(month)+'-'+this.dispose(endDay)+'" colspan="7"><div><span class="week">第'+ numbers[j] +'周</span><span class="process">'+this.dispose(startMonth)+'/'+this.dispose(startDay)+'~'+this.dispose(month)+'/'+this.dispose(endDay)+'</span></div></td></tr>';
  296. }
  297. if( j == 0 && firDay != 1){
  298. startMonth = parseInt(startMonth) + 1 > 12 ? '1' : parseInt(startMonth) + 1 ;
  299. startDay = parseInt(endDay) + 1;
  300. }else{
  301. startDay += 7;
  302. }
  303. endDay = startDay + 6 > monthLen ? startDay + 6 - monthLen : startDay + 6;
  304. startYear = startMonth > month ? year - 1 : year ;
  305. endYear = startMonth == 12 && parseInt(endDay)<7? startYear +1 : startYear ;
  306. }
  307. return calendar;
  308. }
  309. Calendar.prototype.forWeek = function (year ,month){
  310. var endMonth = parseInt(month) ;
  311. var year = parseInt(year) ;
  312. var prevdate = this.getPrev(year, month, 'weekly');
  313. var monthLen = eli.getMonthLen(year, month);
  314. var prevMonth = prevdate.split('-')[1];
  315. var prevMonthLen = eli.getMonthLen(prevdate.split('-')[0], prevMonth);
  316. var firDay = new Date(year, month - 1, 1).getDay();
  317. firDay = firDay == 0 ? 7 : firDay;
  318. var startDay = firDay != 1 ? prevMonthLen - firDay + 2 : 1 ;
  319. var startMonth = startDay != 1? prevMonth : month ;
  320. var endDay = parseInt(startDay) + 6 > parseInt(prevMonthLen)? parseInt(startDay) + 6 - parseInt(prevMonthLen) : parseInt(startDay) + 6;
  321. var startYear = parseInt(startMonth) > month ? year - 1 : year ;
  322. var endYear = startMonth == 12 ? startYear + 1 : startYear ;
  323. var data = startYear +'-'+ this.dispose(startMonth) + '-' +this.dispose(startDay) +',' + endYear +'-' + this.dispose(month) + '-'+ this.dispose(endDay);
  324. return data;
  325. }
  326. Calendar.prototype.bulidMonthly = function(options, year ,month){
  327. var calendar = '';
  328. var month = month ? parseInt(month) : parseInt(options.setMonth);
  329. var year = year ? parseInt(year) : parseInt(options.setYear);
  330. var selectDate = options.selectDate;
  331. selectDate = typeof selectDate == 'string' ? selectDate.indexOf('-') != -1 && selectDate.indexOf('week') == -1 ? selectDate.split('-') : null : null;
  332. if(selectDate){
  333. selectDate[0] = selectDate[0] == 'each' ? year : parseInt(selectDate[0]);
  334. selectDate[1] = selectDate[1] == 'each' ? 1 : parseInt(selectDate[1]);
  335. }
  336. for(var j = 0; j < 12; j++){
  337. if( j % 4 == 0 && j !=12 ){
  338. if( j != 0){
  339. calendar +="</tr><tr>";
  340. }
  341. else{
  342. calendar +="<tr>";
  343. }
  344. }
  345. if( options.today && (j + 1) == options.realmonth && options.realyear == year){
  346. if( selectDate && selectDate[0] == year && selectDate[1] == (j + 1)){
  347. calendar += '<td colspan=2 class="current-month current-day selected-month selected-day tmonth month'+this.dispose(( j + 1 ))+'" data-time="'+year+'-'+this.dispose(( j + 1 ))+'"><span class="month">'+(j + 1)+'月</span></td>';
  348. }else{
  349. calendar += '<td colspan=2 class="current-month current-day tmonth month'+this.dispose(( j + 1 ))+'" data-time="'+year+'-'+this.dispose(( j + 1 ))+'"><span class="month">'+(j + 1)+'月</span></td>';
  350. }
  351. }else if( selectDate && selectDate[0] == year && selectDate[1] == (j + 1)){
  352. calendar += '<td colspan=2 class="selected-month selected-day tmonth month'+this.dispose(( j + 1 ))+'" data-time="'+year+'-'+this.dispose(( j + 1 ))+'"><span class="month">'+(j + 1)+'月</span></td>'
  353. }else{
  354. calendar += '<td colspan=2 class="tmonth month'+this.dispose(( j + 1 ))+'" data-time="'+year+'-'+this.dispose(( j + 1 ))+'"><span class="month">'+(j + 1)+'月</span></td>'
  355. }
  356. }
  357. calendar +='</tr>';
  358. return calendar;
  359. }
  360. Calendar.prototype.getNext = function(year, month, type){
  361. var nextdate;
  362. if(type == 'monthly'){
  363. nextdate = parseInt(year) + 1 + '-';
  364. }else{
  365. nextdate = parseInt(month) == 12 ? parseInt(year) + 1 + '-01' : year + '-' + this.dispose((parseInt(month) + 1));
  366. }
  367. return nextdate;
  368. }
  369. Calendar.prototype.getPrev = function(year, month, type){
  370. var prevdate;
  371. if(type == 'monthly'){
  372. prevdate = parseInt(year) - 1 + '-';
  373. }else{
  374. prevdate = parseInt(month) == 1 ? parseInt(year) - 1 + '-12' : year + '-' + this.dispose((parseInt(month) - 1));
  375. }
  376. return prevdate;
  377. }
  378. Calendar.prototype.getDate = function(){
  379. var now = new Date();
  380. var crtdate = now.getDate();
  381. var setMonth = now.getMonth() + 1 ;
  382. var setYear = now.getYear() + 1900;
  383. crtdate = this.dispose(crtdate);
  384. setMonth = this.dispose(setMonth);
  385. var date = {
  386. crtdate : crtdate,
  387. realmonth : setMonth,
  388. setMonth : setMonth,
  389. realyear : setYear,
  390. setYear : setYear
  391. }
  392. return date;
  393. }
  394. Calendar.prototype.dispose = function(val){
  395. var value = (parseInt(val)+100);
  396. value=value.toString();
  397. value=value.substring(1);
  398. return value;
  399. }
  400. Calendar.prototype.getOptions = function(options, date){
  401. if(options && typeof options == 'object'){
  402. $.each(options , function(key, value){
  403. if(value == void 0){
  404. delete options[key];
  405. }
  406. })
  407. }
  408. var option = $.extend({target : null }, CALENDAR_OPTION, date, options);
  409. return option;
  410. }
  411. var Plugin = function( option ){
  412. return this.each(function(){
  413. var $this = $(this);
  414. var data = $this.data( 'ellie.calendar' );
  415. var options = typeof option == 'object' && option;
  416. if( !data ) $this.data( 'ellie.calendar', ( data = new Calendar(this, option) ) )
  417. })
  418. }
  419. $.fn.flexoCalendar = Plugin;
  420. $.fn.flexoCalendar.Constructor = Calendar;
  421. var old = $.fn.flexoCalendar;
  422. // calendarWidget no conflict
  423. $.fn.flexoCalendar.noConflict = function () {
  424. $.fn.flexoCalendar = old;
  425. return this;
  426. }
  427. })(jQuery)
  428. +(function($){
  429. var root=this;
  430. var eli = function(obj) {
  431. if (obj instanceof eli) return obj;
  432. if (!(this instanceof eli)) return new eli(obj);
  433. this.eliwrapped = obj;
  434. };
  435. root.eli = eli;
  436. eli.getMonthLen = function(year, month){
  437. var year = parseInt(year);
  438. var month = parseInt(month);
  439. var monthLen=[,31,28,31,30,31,30,31,31,30,31,30,31];
  440. if ((month == 2)&&(year % 4 == 0)&&((year % 100 != 0)||(year % 400 == 0))){
  441. return 29;
  442. }else{
  443. return monthLen[month];
  444. }
  445. }
  446. eli.getMonthWeek = function(year, month, day){
  447. var year = parseInt(year),
  448. month = parseInt(month),
  449. day = parseInt(day);
  450. var that = [year, month,],
  451. firDay = new Date(year, month - 1, 1).getDay(),
  452. start = 1,
  453. next = 8,
  454. monthLen = eli.getMonthLen(year, month),
  455. nextfirDay = new Date(year , month, 1 ).getDay(),
  456. another;
  457. firDay = firDay == 0 ? 7 : firDay;
  458. next = firDay == 1 ? next : ( 9 - firDay );
  459. for ( var i = 0; i < 5 ; i++){
  460. if( start <= day && day < next){
  461. that[2] = 'week' + ( i + 1 );
  462. }
  463. start = next ;
  464. next += 7;
  465. next = next > monthLen ? monthLen : next;
  466. }
  467. if( nextfirDay !=1 ){
  468. another = [,,'week1'];
  469. another[1] = month + 1 == 13 ? 1 : month + 1;
  470. another[0] = another[1] == 1 ? year + 1 : year;
  471. }
  472. var output = [that, another];
  473. return output
  474. }
  475. })(jQuery)