123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- /**
- * jQuery EasyUI 1.3.6
- *
- * Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
- *
- * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt
- * To use it on other terms please contact us at info@jeasyui.com
- *
- */
- /**
- * calendar - jQuery EasyUI
- *
- */
- (function($){
-
- function setSize(target){
- var opts = $.data(target, 'calendar').options;
- var t = $(target);
- // if (opts.fit == true){
- // var p = t.parent();
- // opts.width = p.width();
- // opts.height = p.height();
- // }
- opts.fit ? $.extend(opts, t._fit()) : t._fit(false);
- var header = t.find('.calendar-header');
- t._outerWidth(opts.width);
- t._outerHeight(opts.height);
- t.find('.calendar-body')._outerHeight(t.height() - header._outerHeight());
- }
-
- function init(target){
- $(target).addClass('calendar').html(
- '<div class="calendar-header">' +
- '<div class="calendar-prevmonth"></div>' +
- '<div class="calendar-nextmonth"></div>' +
- '<div class="calendar-prevyear"></div>' +
- '<div class="calendar-nextyear"></div>' +
- '<div class="calendar-title">' +
- '<span>Aprial 2010</span>' +
- '</div>' +
- '</div>' +
- '<div class="calendar-body">' +
- '<div class="calendar-menu">' +
- '<div class="calendar-menu-year-inner">' +
- '<span class="calendar-menu-prev"></span>' +
- '<span><input class="calendar-menu-year" type="text"></input></span>' +
- '<span class="calendar-menu-next"></span>' +
- '</div>' +
- '<div class="calendar-menu-month-inner">' +
- '</div>' +
- '</div>' +
- '</div>'
- );
-
- $(target).find('.calendar-title span').hover(
- function(){$(this).addClass('calendar-menu-hover');},
- function(){$(this).removeClass('calendar-menu-hover');}
- ).click(function(){
- var menu = $(target).find('.calendar-menu');
- if (menu.is(':visible')){
- menu.hide();
- } else {
- showSelectMenus(target);
- }
- });
-
- $('.calendar-prevmonth,.calendar-nextmonth,.calendar-prevyear,.calendar-nextyear', target).hover(
- function(){$(this).addClass('calendar-nav-hover');},
- function(){$(this).removeClass('calendar-nav-hover');}
- );
- $(target).find('.calendar-nextmonth').click(function(){
- showMonth(target, 1);
- });
- $(target).find('.calendar-prevmonth').click(function(){
- showMonth(target, -1);
- });
- $(target).find('.calendar-nextyear').click(function(){
- showYear(target, 1);
- });
- $(target).find('.calendar-prevyear').click(function(){
- showYear(target, -1);
- });
-
- $(target).bind('_resize', function(){
- var opts = $.data(target, 'calendar').options;
- if (opts.fit == true){
- setSize(target);
- }
- return false;
- });
- }
-
- /**
- * show the calendar corresponding to the current month.
- */
- function showMonth(target, delta){
- var opts = $.data(target, 'calendar').options;
- opts.month += delta;
- if (opts.month > 12){
- opts.year++;
- opts.month = 1;
- } else if (opts.month < 1){
- opts.year--;
- opts.month = 12;
- }
- show(target);
-
- var menu = $(target).find('.calendar-menu-month-inner');
- menu.find('td.calendar-selected').removeClass('calendar-selected');
- menu.find('td:eq(' + (opts.month-1) + ')').addClass('calendar-selected');
- }
-
- /**
- * show the calendar corresponding to the current year.
- */
- function showYear(target, delta){
- var opts = $.data(target, 'calendar').options;
- opts.year += delta;
- show(target);
-
- var menu = $(target).find('.calendar-menu-year');
- menu.val(opts.year);
- }
-
- /**
- * show the select menu that can change year or month, if the menu is not be created then create it.
- */
- function showSelectMenus(target){
- var opts = $.data(target, 'calendar').options;
- $(target).find('.calendar-menu').show();
-
- if ($(target).find('.calendar-menu-month-inner').is(':empty')){
- $(target).find('.calendar-menu-month-inner').empty();
- var t = $('<table class="calendar-mtable"></table>').appendTo($(target).find('.calendar-menu-month-inner'));
- var idx = 0;
- for(var i=0; i<3; i++){
- var tr = $('<tr></tr>').appendTo(t);
- for(var j=0; j<4; j++){
- $('<td class="calendar-menu-month"></td>').html(opts.months[idx++]).attr('abbr',idx).appendTo(tr);
- }
- }
-
- $(target).find('.calendar-menu-prev,.calendar-menu-next').hover(
- function(){$(this).addClass('calendar-menu-hover');},
- function(){$(this).removeClass('calendar-menu-hover');}
- );
- $(target).find('.calendar-menu-next').click(function(){
- var y = $(target).find('.calendar-menu-year');
- if (!isNaN(y.val())){
- y.val(parseInt(y.val()) + 1);
- setDate();
- }
- });
- $(target).find('.calendar-menu-prev').click(function(){
- var y = $(target).find('.calendar-menu-year');
- if (!isNaN(y.val())){
- y.val(parseInt(y.val() - 1));
- setDate();
- }
- });
-
- $(target).find('.calendar-menu-year').keypress(function(e){
- if (e.keyCode == 13){
- setDate(true);
- }
- });
-
- $(target).find('.calendar-menu-month').hover(
- function(){$(this).addClass('calendar-menu-hover');},
- function(){$(this).removeClass('calendar-menu-hover');}
- ).click(function(){
- var menu = $(target).find('.calendar-menu');
- menu.find('.calendar-selected').removeClass('calendar-selected');
- $(this).addClass('calendar-selected');
- setDate(true);
- });
- }
-
- function setDate(hideMenu){
- var menu = $(target).find('.calendar-menu');
- var year = menu.find('.calendar-menu-year').val();
- var month = menu.find('.calendar-selected').attr('abbr');
- if (!isNaN(year)){
- opts.year = parseInt(year);
- opts.month = parseInt(month);
- show(target);
- }
- if (hideMenu){menu.hide()}
- }
-
- var body = $(target).find('.calendar-body');
- var sele = $(target).find('.calendar-menu');
- var seleYear = sele.find('.calendar-menu-year-inner');
- var seleMonth = sele.find('.calendar-menu-month-inner');
-
- seleYear.find('input').val(opts.year).focus();
- seleMonth.find('td.calendar-selected').removeClass('calendar-selected');
- seleMonth.find('td:eq('+(opts.month-1)+')').addClass('calendar-selected');
-
- sele._outerWidth(body._outerWidth());
- sele._outerHeight(body._outerHeight());
- seleMonth._outerHeight(sele.height() - seleYear._outerHeight());
- }
-
- /**
- * get weeks data.
- */
- function getWeeks(target, year, month){
- var opts = $.data(target, 'calendar').options;
- var dates = [];
- var lastDay = new Date(year, month, 0).getDate();
- for(var i=1; i<=lastDay; i++) dates.push([year,month,i]);
-
- // group date by week
- var weeks = [], week = [];
- // var memoDay = 0;
- var memoDay = -1;
- while(dates.length > 0){
- var date = dates.shift();
- week.push(date);
- var day = new Date(date[0],date[1]-1,date[2]).getDay();
- if (memoDay == day){
- day = 0;
- } else if (day == (opts.firstDay==0 ? 7 : opts.firstDay) - 1){
- weeks.push(week);
- week = [];
- }
- memoDay = day;
- }
- if (week.length){
- weeks.push(week);
- }
-
- var firstWeek = weeks[0];
- if (firstWeek.length < 7){
- while(firstWeek.length < 7){
- var firstDate = firstWeek[0];
- var date = new Date(firstDate[0],firstDate[1]-1,firstDate[2]-1)
- firstWeek.unshift([date.getFullYear(), date.getMonth()+1, date.getDate()]);
- }
- } else {
- var firstDate = firstWeek[0];
- var week = [];
- for(var i=1; i<=7; i++){
- var date = new Date(firstDate[0], firstDate[1]-1, firstDate[2]-i);
- week.unshift([date.getFullYear(), date.getMonth()+1, date.getDate()]);
- }
- weeks.unshift(week);
- }
-
- var lastWeek = weeks[weeks.length-1];
- while(lastWeek.length < 7){
- var lastDate = lastWeek[lastWeek.length-1];
- var date = new Date(lastDate[0], lastDate[1]-1, lastDate[2]+1);
- lastWeek.push([date.getFullYear(), date.getMonth()+1, date.getDate()]);
- }
- if (weeks.length < 6){
- var lastDate = lastWeek[lastWeek.length-1];
- var week = [];
- for(var i=1; i<=7; i++){
- var date = new Date(lastDate[0], lastDate[1]-1, lastDate[2]+i);
- week.push([date.getFullYear(), date.getMonth()+1, date.getDate()]);
- }
- weeks.push(week);
- }
-
- return weeks;
- }
-
- /**
- * show the calendar day.
- */
- function show(target){
- var opts = $.data(target, 'calendar').options;
- if (opts.current && !opts.validator.call(target, opts.current)){
- opts.current = null;
- }
-
- var now = allGetServerTime();
- var todayInfo = now.getFullYear()+','+(now.getMonth()+1)+','+now.getDate();
- var currentInfo = opts.current ? (opts.current.getFullYear()+','+(opts.current.getMonth()+1)+','+opts.current.getDate()) : '';
- // calulate the saturday and sunday index
- var saIndex = 6 - opts.firstDay;
- var suIndex = saIndex + 1;
- if (saIndex >= 7) saIndex -= 7;
- if (suIndex >= 7) suIndex -= 7;
-
- $(target).find('.calendar-title span').html(opts.months[opts.month-1] + ' ' + opts.year);
-
- var body = $(target).find('div.calendar-body');
- body.children('table').remove();
-
- var data = ['<table class="calendar-dtable" cellspacing="0" cellpadding="0" border="0">'];
- data.push('<thead><tr>');
- for(var i=opts.firstDay; i<opts.weeks.length; i++){
- data.push('<th>'+opts.weeks[i]+'</th>');
- }
- for(var i=0; i<opts.firstDay; i++){
- data.push('<th>'+opts.weeks[i]+'</th>');
- }
- data.push('</tr></thead>');
-
- data.push('<tbody>');
- var weeks = getWeeks(target, opts.year, opts.month);
- for(var i=0; i<weeks.length; i++){
- var week = weeks[i];
- var cls = '';
- if (i == 0){cls = 'calendar-first';}
- else if (i == weeks.length - 1){cls = 'calendar-last';}
- data.push('<tr class="' + cls + '">');
- for(var j=0; j<week.length; j++){
- var day = week[j];
- var s = day[0]+','+day[1]+','+day[2];
- var dvalue = new Date(day[0], parseInt(day[1])-1, day[2]);
- var d = opts.formatter.call(target, dvalue);
- var css = opts.styler.call(target, dvalue);
- var classValue = '';
- var styleValue = '';
- if (typeof css == 'string'){
- styleValue = css;
- } else if (css){
- classValue = css['class'] || '';
- styleValue = css['style'] || '';
- }
-
- var cls = 'calendar-day';
- if (!(opts.year == day[0] && opts.month == day[1])){
- cls += ' calendar-other-month';
- }
- if (s == todayInfo){cls += ' calendar-today';}
- if (s == currentInfo){cls += ' calendar-selected';}
- if (j == saIndex){cls += ' calendar-saturday';}
- else if (j == suIndex){cls += ' calendar-sunday';}
- if (j == 0){cls += ' calendar-first';}
- else if (j == week.length-1){cls += ' calendar-last';}
-
- cls += ' ' + classValue;
- if (!opts.validator.call(target, dvalue)){
- cls += ' calendar-disabled';
- }
-
- data.push('<td class="' + cls + '" abbr="' + s + '" style="' + styleValue + '">' + d + '</td>');
- }
- data.push('</tr>');
- }
- data.push('</tbody>');
- data.push('</table>');
-
- body.append(data.join(''));
-
- var t = body.children('table.calendar-dtable').prependTo(body);
-
- t.find('td.calendar-day:not(.calendar-disabled)').hover(
- function(){$(this).addClass('calendar-hover');},
- function(){$(this).removeClass('calendar-hover');}
- ).click(function(){
- var oldValue = opts.current;
- t.find('.calendar-selected').removeClass('calendar-selected');
- $(this).addClass('calendar-selected');
- var parts = $(this).attr('abbr').split(',');
- opts.current = new Date(parts[0], parseInt(parts[1])-1, parts[2]);
- opts.onSelect.call(target, opts.current);
- if (!oldValue || oldValue.getTime() != opts.current.getTime()){
- opts.onChange.call(target, opts.current, oldValue);
- }
- });
- }
-
- $.fn.calendar = function(options, param){
- if (typeof options == 'string'){
- return $.fn.calendar.methods[options](this, param);
- }
-
- options = options || {};
- return this.each(function(){
- var state = $.data(this, 'calendar');
- if (state){
- $.extend(state.options, options);
- } else {
- state = $.data(this, 'calendar', {
- options:$.extend({}, $.fn.calendar.defaults, $.fn.calendar.parseOptions(this), options)
- });
- init(this);
- }
- if (state.options.border == false){
- $(this).addClass('calendar-noborder');
- }
- setSize(this);
- show(this);
- $(this).find('div.calendar-menu').hide(); // hide the calendar menu
- });
- };
-
- $.fn.calendar.methods = {
- options: function(jq){
- return $.data(jq[0], 'calendar').options;
- },
- resize: function(jq){
- return jq.each(function(){
- setSize(this);
- });
- },
- moveTo: function(jq, date){
- return jq.each(function(){
- var opts = $(this).calendar('options');
- if (opts.validator.call(this, date)){
- var oldValue = opts.current;
- $(this).calendar({
- year: date.getFullYear(),
- month: date.getMonth()+1,
- current: date
- });
- if (!oldValue || oldValue.getTime() != date.getTime()){
- opts.onChange.call(this, opts.current, oldValue);
- }
- }
- });
- }
- };
-
- $.fn.calendar.parseOptions = function(target){
- var t = $(target);
- return $.extend({}, $.parser.parseOptions(target, [
- 'width','height',{firstDay:'number',fit:'boolean',border:'boolean'}
- ]));
- };
-
- $.fn.calendar.defaults = {
- width:180,
- height:180,
- fit:false,
- border:true,
- firstDay:0,
- weeks:['S','M','T','W','T','F','S'],
- months:['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
- year:allGetServerTime().getFullYear(),
- month:allGetServerTime().getMonth()+1,
- current:(function(){
- var d = allGetServerTime();
- return new Date(d.getFullYear(), d.getMonth(), d.getDate());
- })(),
-
- formatter:function(date){return date.getDate()},
- styler:function(date){return ''},
- validator:function(date){return true},
-
- onSelect: function(date){},
- onChange: function(newDate, oldDate){}
- };
- })(jQuery);
|