73423b7449decab39b6bd95c6c5f26f4b38a184d.svn-base 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /**
  2. * jQuery EasyUI 1.3.6
  3. *
  4. * Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
  5. *
  6. * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt
  7. * To use it on other terms please contact us at info@jeasyui.com
  8. *
  9. */
  10. /**
  11. * calendar - jQuery EasyUI
  12. *
  13. */
  14. (function($){
  15. function setSize(target){
  16. var opts = $.data(target, 'calendar').options;
  17. var t = $(target);
  18. // if (opts.fit == true){
  19. // var p = t.parent();
  20. // opts.width = p.width();
  21. // opts.height = p.height();
  22. // }
  23. opts.fit ? $.extend(opts, t._fit()) : t._fit(false);
  24. var header = t.find('.calendar-header');
  25. t._outerWidth(opts.width);
  26. t._outerHeight(opts.height);
  27. t.find('.calendar-body')._outerHeight(t.height() - header._outerHeight());
  28. }
  29. function init(target){
  30. $(target).addClass('calendar').html(
  31. '<div class="calendar-header">' +
  32. '<div class="calendar-prevmonth"></div>' +
  33. '<div class="calendar-nextmonth"></div>' +
  34. '<div class="calendar-prevyear"></div>' +
  35. '<div class="calendar-nextyear"></div>' +
  36. '<div class="calendar-title">' +
  37. '<span>Aprial 2010</span>' +
  38. '</div>' +
  39. '</div>' +
  40. '<div class="calendar-body">' +
  41. '<div class="calendar-menu">' +
  42. '<div class="calendar-menu-year-inner">' +
  43. '<span class="calendar-menu-prev"></span>' +
  44. '<span><input class="calendar-menu-year" type="text"></input></span>' +
  45. '<span class="calendar-menu-next"></span>' +
  46. '</div>' +
  47. '<div class="calendar-menu-month-inner">' +
  48. '</div>' +
  49. '</div>' +
  50. '</div>'
  51. );
  52. $(target).find('.calendar-title span').hover(
  53. function(){$(this).addClass('calendar-menu-hover');},
  54. function(){$(this).removeClass('calendar-menu-hover');}
  55. ).click(function(){
  56. var menu = $(target).find('.calendar-menu');
  57. if (menu.is(':visible')){
  58. menu.hide();
  59. } else {
  60. showSelectMenus(target);
  61. }
  62. });
  63. $('.calendar-prevmonth,.calendar-nextmonth,.calendar-prevyear,.calendar-nextyear', target).hover(
  64. function(){$(this).addClass('calendar-nav-hover');},
  65. function(){$(this).removeClass('calendar-nav-hover');}
  66. );
  67. $(target).find('.calendar-nextmonth').click(function(){
  68. showMonth(target, 1);
  69. });
  70. $(target).find('.calendar-prevmonth').click(function(){
  71. showMonth(target, -1);
  72. });
  73. $(target).find('.calendar-nextyear').click(function(){
  74. showYear(target, 1);
  75. });
  76. $(target).find('.calendar-prevyear').click(function(){
  77. showYear(target, -1);
  78. });
  79. $(target).bind('_resize', function(){
  80. var opts = $.data(target, 'calendar').options;
  81. if (opts.fit == true){
  82. setSize(target);
  83. }
  84. return false;
  85. });
  86. }
  87. /**
  88. * show the calendar corresponding to the current month.
  89. */
  90. function showMonth(target, delta){
  91. var opts = $.data(target, 'calendar').options;
  92. opts.month += delta;
  93. if (opts.month > 12){
  94. opts.year++;
  95. opts.month = 1;
  96. } else if (opts.month < 1){
  97. opts.year--;
  98. opts.month = 12;
  99. }
  100. show(target);
  101. var menu = $(target).find('.calendar-menu-month-inner');
  102. menu.find('td.calendar-selected').removeClass('calendar-selected');
  103. menu.find('td:eq(' + (opts.month-1) + ')').addClass('calendar-selected');
  104. }
  105. /**
  106. * show the calendar corresponding to the current year.
  107. */
  108. function showYear(target, delta){
  109. var opts = $.data(target, 'calendar').options;
  110. opts.year += delta;
  111. show(target);
  112. var menu = $(target).find('.calendar-menu-year');
  113. menu.val(opts.year);
  114. }
  115. /**
  116. * show the select menu that can change year or month, if the menu is not be created then create it.
  117. */
  118. function showSelectMenus(target){
  119. var opts = $.data(target, 'calendar').options;
  120. $(target).find('.calendar-menu').show();
  121. if ($(target).find('.calendar-menu-month-inner').is(':empty')){
  122. $(target).find('.calendar-menu-month-inner').empty();
  123. var t = $('<table class="calendar-mtable"></table>').appendTo($(target).find('.calendar-menu-month-inner'));
  124. var idx = 0;
  125. for(var i=0; i<3; i++){
  126. var tr = $('<tr></tr>').appendTo(t);
  127. for(var j=0; j<4; j++){
  128. $('<td class="calendar-menu-month"></td>').html(opts.months[idx++]).attr('abbr',idx).appendTo(tr);
  129. }
  130. }
  131. $(target).find('.calendar-menu-prev,.calendar-menu-next').hover(
  132. function(){$(this).addClass('calendar-menu-hover');},
  133. function(){$(this).removeClass('calendar-menu-hover');}
  134. );
  135. $(target).find('.calendar-menu-next').click(function(){
  136. var y = $(target).find('.calendar-menu-year');
  137. if (!isNaN(y.val())){
  138. y.val(parseInt(y.val()) + 1);
  139. setDate();
  140. }
  141. });
  142. $(target).find('.calendar-menu-prev').click(function(){
  143. var y = $(target).find('.calendar-menu-year');
  144. if (!isNaN(y.val())){
  145. y.val(parseInt(y.val() - 1));
  146. setDate();
  147. }
  148. });
  149. $(target).find('.calendar-menu-year').keypress(function(e){
  150. if (e.keyCode == 13){
  151. setDate(true);
  152. }
  153. });
  154. $(target).find('.calendar-menu-month').hover(
  155. function(){$(this).addClass('calendar-menu-hover');},
  156. function(){$(this).removeClass('calendar-menu-hover');}
  157. ).click(function(){
  158. var menu = $(target).find('.calendar-menu');
  159. menu.find('.calendar-selected').removeClass('calendar-selected');
  160. $(this).addClass('calendar-selected');
  161. setDate(true);
  162. });
  163. }
  164. function setDate(hideMenu){
  165. var menu = $(target).find('.calendar-menu');
  166. var year = menu.find('.calendar-menu-year').val();
  167. var month = menu.find('.calendar-selected').attr('abbr');
  168. if (!isNaN(year)){
  169. opts.year = parseInt(year);
  170. opts.month = parseInt(month);
  171. show(target);
  172. }
  173. if (hideMenu){menu.hide()}
  174. }
  175. var body = $(target).find('.calendar-body');
  176. var sele = $(target).find('.calendar-menu');
  177. var seleYear = sele.find('.calendar-menu-year-inner');
  178. var seleMonth = sele.find('.calendar-menu-month-inner');
  179. seleYear.find('input').val(opts.year).focus();
  180. seleMonth.find('td.calendar-selected').removeClass('calendar-selected');
  181. seleMonth.find('td:eq('+(opts.month-1)+')').addClass('calendar-selected');
  182. sele._outerWidth(body._outerWidth());
  183. sele._outerHeight(body._outerHeight());
  184. seleMonth._outerHeight(sele.height() - seleYear._outerHeight());
  185. }
  186. /**
  187. * get weeks data.
  188. */
  189. function getWeeks(target, year, month){
  190. var opts = $.data(target, 'calendar').options;
  191. var dates = [];
  192. var lastDay = new Date(year, month, 0).getDate();
  193. for(var i=1; i<=lastDay; i++) dates.push([year,month,i]);
  194. // group date by week
  195. var weeks = [], week = [];
  196. // var memoDay = 0;
  197. var memoDay = -1;
  198. while(dates.length > 0){
  199. var date = dates.shift();
  200. week.push(date);
  201. var day = new Date(date[0],date[1]-1,date[2]).getDay();
  202. if (memoDay == day){
  203. day = 0;
  204. } else if (day == (opts.firstDay==0 ? 7 : opts.firstDay) - 1){
  205. weeks.push(week);
  206. week = [];
  207. }
  208. memoDay = day;
  209. }
  210. if (week.length){
  211. weeks.push(week);
  212. }
  213. var firstWeek = weeks[0];
  214. if (firstWeek.length < 7){
  215. while(firstWeek.length < 7){
  216. var firstDate = firstWeek[0];
  217. var date = new Date(firstDate[0],firstDate[1]-1,firstDate[2]-1)
  218. firstWeek.unshift([date.getFullYear(), date.getMonth()+1, date.getDate()]);
  219. }
  220. } else {
  221. var firstDate = firstWeek[0];
  222. var week = [];
  223. for(var i=1; i<=7; i++){
  224. var date = new Date(firstDate[0], firstDate[1]-1, firstDate[2]-i);
  225. week.unshift([date.getFullYear(), date.getMonth()+1, date.getDate()]);
  226. }
  227. weeks.unshift(week);
  228. }
  229. var lastWeek = weeks[weeks.length-1];
  230. while(lastWeek.length < 7){
  231. var lastDate = lastWeek[lastWeek.length-1];
  232. var date = new Date(lastDate[0], lastDate[1]-1, lastDate[2]+1);
  233. lastWeek.push([date.getFullYear(), date.getMonth()+1, date.getDate()]);
  234. }
  235. if (weeks.length < 6){
  236. var lastDate = lastWeek[lastWeek.length-1];
  237. var week = [];
  238. for(var i=1; i<=7; i++){
  239. var date = new Date(lastDate[0], lastDate[1]-1, lastDate[2]+i);
  240. week.push([date.getFullYear(), date.getMonth()+1, date.getDate()]);
  241. }
  242. weeks.push(week);
  243. }
  244. return weeks;
  245. }
  246. /**
  247. * show the calendar day.
  248. */
  249. function show(target){
  250. var opts = $.data(target, 'calendar').options;
  251. if (opts.current && !opts.validator.call(target, opts.current)){
  252. opts.current = null;
  253. }
  254. var now = allGetServerTime();
  255. var todayInfo = now.getFullYear()+','+(now.getMonth()+1)+','+now.getDate();
  256. var currentInfo = opts.current ? (opts.current.getFullYear()+','+(opts.current.getMonth()+1)+','+opts.current.getDate()) : '';
  257. // calulate the saturday and sunday index
  258. var saIndex = 6 - opts.firstDay;
  259. var suIndex = saIndex + 1;
  260. if (saIndex >= 7) saIndex -= 7;
  261. if (suIndex >= 7) suIndex -= 7;
  262. $(target).find('.calendar-title span').html(opts.months[opts.month-1] + ' ' + opts.year);
  263. var body = $(target).find('div.calendar-body');
  264. body.children('table').remove();
  265. var data = ['<table class="calendar-dtable" cellspacing="0" cellpadding="0" border="0">'];
  266. data.push('<thead><tr>');
  267. for(var i=opts.firstDay; i<opts.weeks.length; i++){
  268. data.push('<th>'+opts.weeks[i]+'</th>');
  269. }
  270. for(var i=0; i<opts.firstDay; i++){
  271. data.push('<th>'+opts.weeks[i]+'</th>');
  272. }
  273. data.push('</tr></thead>');
  274. data.push('<tbody>');
  275. var weeks = getWeeks(target, opts.year, opts.month);
  276. for(var i=0; i<weeks.length; i++){
  277. var week = weeks[i];
  278. var cls = '';
  279. if (i == 0){cls = 'calendar-first';}
  280. else if (i == weeks.length - 1){cls = 'calendar-last';}
  281. data.push('<tr class="' + cls + '">');
  282. for(var j=0; j<week.length; j++){
  283. var day = week[j];
  284. var s = day[0]+','+day[1]+','+day[2];
  285. var dvalue = new Date(day[0], parseInt(day[1])-1, day[2]);
  286. var d = opts.formatter.call(target, dvalue);
  287. var css = opts.styler.call(target, dvalue);
  288. var classValue = '';
  289. var styleValue = '';
  290. if (typeof css == 'string'){
  291. styleValue = css;
  292. } else if (css){
  293. classValue = css['class'] || '';
  294. styleValue = css['style'] || '';
  295. }
  296. var cls = 'calendar-day';
  297. if (!(opts.year == day[0] && opts.month == day[1])){
  298. cls += ' calendar-other-month';
  299. }
  300. if (s == todayInfo){cls += ' calendar-today';}
  301. if (s == currentInfo){cls += ' calendar-selected';}
  302. if (j == saIndex){cls += ' calendar-saturday';}
  303. else if (j == suIndex){cls += ' calendar-sunday';}
  304. if (j == 0){cls += ' calendar-first';}
  305. else if (j == week.length-1){cls += ' calendar-last';}
  306. cls += ' ' + classValue;
  307. if (!opts.validator.call(target, dvalue)){
  308. cls += ' calendar-disabled';
  309. }
  310. data.push('<td class="' + cls + '" abbr="' + s + '" style="' + styleValue + '">' + d + '</td>');
  311. }
  312. data.push('</tr>');
  313. }
  314. data.push('</tbody>');
  315. data.push('</table>');
  316. body.append(data.join(''));
  317. var t = body.children('table.calendar-dtable').prependTo(body);
  318. t.find('td.calendar-day:not(.calendar-disabled)').hover(
  319. function(){$(this).addClass('calendar-hover');},
  320. function(){$(this).removeClass('calendar-hover');}
  321. ).click(function(){
  322. var oldValue = opts.current;
  323. t.find('.calendar-selected').removeClass('calendar-selected');
  324. $(this).addClass('calendar-selected');
  325. var parts = $(this).attr('abbr').split(',');
  326. opts.current = new Date(parts[0], parseInt(parts[1])-1, parts[2]);
  327. opts.onSelect.call(target, opts.current);
  328. if (!oldValue || oldValue.getTime() != opts.current.getTime()){
  329. opts.onChange.call(target, opts.current, oldValue);
  330. }
  331. });
  332. }
  333. $.fn.calendar = function(options, param){
  334. if (typeof options == 'string'){
  335. return $.fn.calendar.methods[options](this, param);
  336. }
  337. options = options || {};
  338. return this.each(function(){
  339. var state = $.data(this, 'calendar');
  340. if (state){
  341. $.extend(state.options, options);
  342. } else {
  343. state = $.data(this, 'calendar', {
  344. options:$.extend({}, $.fn.calendar.defaults, $.fn.calendar.parseOptions(this), options)
  345. });
  346. init(this);
  347. }
  348. if (state.options.border == false){
  349. $(this).addClass('calendar-noborder');
  350. }
  351. setSize(this);
  352. show(this);
  353. $(this).find('div.calendar-menu').hide(); // hide the calendar menu
  354. });
  355. };
  356. $.fn.calendar.methods = {
  357. options: function(jq){
  358. return $.data(jq[0], 'calendar').options;
  359. },
  360. resize: function(jq){
  361. return jq.each(function(){
  362. setSize(this);
  363. });
  364. },
  365. moveTo: function(jq, date){
  366. return jq.each(function(){
  367. var opts = $(this).calendar('options');
  368. if (opts.validator.call(this, date)){
  369. var oldValue = opts.current;
  370. $(this).calendar({
  371. year: date.getFullYear(),
  372. month: date.getMonth()+1,
  373. current: date
  374. });
  375. if (!oldValue || oldValue.getTime() != date.getTime()){
  376. opts.onChange.call(this, opts.current, oldValue);
  377. }
  378. }
  379. });
  380. }
  381. };
  382. $.fn.calendar.parseOptions = function(target){
  383. var t = $(target);
  384. return $.extend({}, $.parser.parseOptions(target, [
  385. 'width','height',{firstDay:'number',fit:'boolean',border:'boolean'}
  386. ]));
  387. };
  388. $.fn.calendar.defaults = {
  389. width:180,
  390. height:180,
  391. fit:false,
  392. border:true,
  393. firstDay:0,
  394. weeks:['S','M','T','W','T','F','S'],
  395. months:['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  396. year:allGetServerTime().getFullYear(),
  397. month:allGetServerTime().getMonth()+1,
  398. current:(function(){
  399. var d = allGetServerTime();
  400. return new Date(d.getFullYear(), d.getMonth(), d.getDate());
  401. })(),
  402. formatter:function(date){return date.getDate()},
  403. styler:function(date){return ''},
  404. validator:function(date){return true},
  405. onSelect: function(date){},
  406. onChange: function(newDate, oldDate){}
  407. };
  408. })(jQuery);