/** rockdatepicker 时间选择插件 caratename:chenxihu caratetime:2014-05-13 21:40:00 email:qqqq2900@126.com homepage:www.xh829.com */ (function ($) { js.onchangedate = function(){}; //选择时间回调 function rockdatepicker(elet, options){ var obj = $(elet); var can = options; var rand = js.getrand(); var me = this; var timeas = null; this.rand = rand; this.nY = 2014;//当前月份 this.nm = 5; this.marr = [31,28,31,30,31,30,31,31,30,31,30,31]; this.weekarr= ['日','一','二','三','四','五','六']; //初始化 this.init = function(){ this.initdevvalue(); obj[can.trigger](function(){ me.setcontent(); return false; }); if(can.initshow){ me.setcontent(); }; obj.attr('rockdatepickerbool','true'); if(!can.editable)elet.readOnly =true; }; this.initdevvalue = function(){ if(can.view=='date')can.format='Y-m-d'; if(can.view=='year')can.format='Y'; if(can.view=='month')can.format='Y-m'; if(can.view=='datetime')can.format='Y-m-d H:i:s'; if(can.view=='time')can.format='H:i:s'; if(can.formats)can.format = can.formats; var lx = can.format; if(lx=='H:i:00'||lx=='H:i'||lx=='i:s')can.view='time'; var minv = can.mindate; if(isempt(minv))minv= obj.attr('mindate'); if(isempt(minv))minv= '1930-01-01 00:00:00';//最小时间 can.mindate = minv; var maxv = can.maxdate; if(isempt(maxv))maxv= obj.attr('maxdate'); if(isempt(maxv))maxv= '2050-12-31 23:59:59';//最大时间 can.maxdate = maxv; this.max = this.shijienges(can.maxdate) this.min = this.shijienges(can.mindate); }; this.showtoday = function(){ this.todatetext = this.formdt('now'); this.todate = this.shijienges(this.todatetext); var val = obj.val(); if(can.inputid!='')val=$('#'+can.inputid+'').val(); if(can.view.indexOf('date')>-1 && val){ if(!this.isdate(val))val = this.todatetext; } if(can.view=='time'){ if(val==''){val=js.now('now');}else{val=js.now('Y-m-d')+' '+val;} } if(val=='')val = this.formdt(can.view=='datetime' ? 'Y-m-d H:i:00' : can.format, val); this.nowtext= val; this.now = this.shijienges(val); }; this.shijienges = function(sj){ var Y=2014,m=1,d=1,H=0,i=0,s=0,ss1,ss2,ss3,total=0; ss1 = sj.split(' '); ss2 = ss1[0].split('-'); Y = parseFloat(ss2[0]); if(ss2.length>1)m= parseFloat(ss2[1]); if(ss2.length>2)d= parseFloat(ss2[2]); if(ss1[1]){ ss3 = ss1[1].split(':'); H = parseFloat(ss3[0]); i = parseFloat(ss3[1]); if(ss3.length>2)s= parseFloat(ss3[2]); } total = parseFloat(''+Y+''+this.sa(m)+''+this.sa(d)+''+this.sa(H)+''+this.sa(i)+''+this.sa(s)+''); return {Y:Y,m:m,d:d,H:H,i:i,s:s,total:total}; }; this.sa = function(v){ v = parseFloat(v); var v1 = ''+v+''; if(v<10)v1='0'+v+''; return v1; }; this.createbasic = function(w, h){ var s= ''; s+= ''; $('body').prepend(s); var oac = $('#rockdatepicker_'+rand+''); oac.show(); this.setweizhi(); setTimeout(function(){js.addbody(rand, 'remove', 'rockdatepicker_'+rand+''); },100); return oac; }; this.setView = function(vis){ can.view = vis; this.initdevvalue(); }; this.setcontent = function(){ this.showtoday(); $("div[class='rockdatepicker']").remove(); if(can.view =='month' || can.format=='Y-m'){ this.createmontview(1); return false; } if(can.view =='year' || can.format=='Y'){ this.createmontview(0); return false; } var s= '',oi=0,w=270,h=278; if(can.view!='time'){ s+='
'; s+=' '; s+='
'; s+=' 2014'; s+=' '; s+=' '; s+=' '; s+=' 06'; s+=' '; s+='  '; s+='  当月 '; s+='
'; s+='
'; s+='
'; s+=' '; s+=' '; for(var d=0; d<7; d++){ s+=''; } s+=' '; for(var r=1; r<=6; r++){ s+=''; for(var d=1; d<=7; d++){ oi++; s+=''; } s+=''; } s+='
'+this.weekarr[d]+'
'+oi+'
'; s+='
'; }else{ s+='
'; s+='
'; w = 220;h=200; } s+='
  选择:'; s+=' :'; s+=' :'; s+=' '; s+= '
'; s+='
'; s+=' 清空  '; s+=' 现在  '; s+=' 确定  '; s+=' 关闭  '; s+='
'; var oac = this.createbasic(w,h); oac.html(s); this.objtd = oac.find("td[temp='nr']"); oac.find("td[tdaddclick]").click(function(){ me.changedatec($(this)); }); this.objtd.click(function(){ me.tdclick(this); }); this.setcontentinit(); $('#rockdatepicker_close'+rand+'').click(function(){ me.hidemenu(); }); $('#rockdatepicker_queding'+rand+'').click(function(){ me.queding(); }); $('#rockdatepicker_now'+rand+'').click(function(){ me.quenow(); }); $('#rockdatepicker_clear'+rand+'').click(function(){ me.queclear(); }); $('#rockdatepicker_table'+rand+'').dblclick(function(){ me.queding(); }); $('#rockdatepicker_table'+rand+'').mouseover(function(){ me.hidefudong(); }); if(can.view!='time'){ this.setcontentinit(); this.addcale(this.now.Y, this.now.m); }else{ $('#rockdatepicker_span'+rand+'').hide(); } this.shetispannvel(0); }; this.shetispannvel = function(lx){ $('#rockdatepicker_span'+rand+'').html(''+this.now.Y+'-'+this.sa(this.now.m)+'-'+this.sa(this.now.d)+''); var ho = $('#rockdatepicker_input_h'+rand+''); var io = $('#rockdatepicker_input_i'+rand+''); var so = $('#rockdatepicker_input_s'+rand+''); ho.val(this.sa(this.now.H)); io.val(this.sa(this.now.i)); so.val(this.sa(this.now.s)); if(can.format.indexOf('H')<0){ get('rockdatepicker_input_h'+rand+'').disabled=true; }else{ if(lx==0)this.shetispannvelbulr('h'); } if(can.format.indexOf('i')<0){ get('rockdatepicker_input_i'+rand+'').disabled=true; }else{ if(lx==0)this.shetispannvelbulr('i'); } if(can.format.indexOf('s')<0){ get('rockdatepicker_input_s'+rand+'').disabled=true; }else{ if(lx==0)this.shetispannvelbulr('s'); } }; this.shetispannvelbulr = function(lx){ var o = $('#rockdatepicker_input_'+lx+''+rand+''); o.blur(function(){ me.blurnum(this); }); o.focus(function(){ me.foucsnum(this); }); }; this.setcontentinit = function(){ $('#rockdatepicker_year'+rand+'').parent().click(function(){ me.changeyear(this); }); $('#rockdatepicker_month'+rand+'').parent().click(function(){ me.changemonth(this); }); }; //选择年的 this.changeyear=function(o1){ this.hidefudong(); var o = $(o1); var off = o.offset(); var s='
'; var xuoi = 0,oi=0; for(var i=this.max.Y; i>=this.min.Y; i--){ oi++; var cls= ''; if(i==this.Y){ cls='div01'; xuoi = oi; } s+='
'+i+'
'; } s+='
'; $('#rockdatepicker_'+rand+'').append(s); $('#rockdatepicker_fudong'+rand+'').scrollTop(xuoi*20); $('#rockdatepicker_fudong'+rand+'').find('div').click(function(){ me.changeyeara(this); }); }; //选择年的 this.changemonth=function(o1){ this.hidefudong(); var o = $(o1); var off = o.offset(); var s='
'; var xuoi = 0,oi=0; for(var i=1; i<=12; i++){ oi++; var cls= ''; if(i==this.m){ cls='div01'; xuoi = oi; } s+='
'+i+'
'; } s+='
'; $('#rockdatepicker_'+rand+'').append(s); $('#rockdatepicker_fudong'+rand+'').scrollTop(xuoi*20); $('#rockdatepicker_fudong'+rand+'').find('div').click(function(){ me.changemontha(this); }); }; this.hidefudong = function(){ $('#rockdatepicker_fudong'+rand+'').remove(); }; this.changeyeara = function(o1){ $('#rockdatepicker_year'+rand+'').html($(o1).html()); this.selchagnge(); }; this.changemontha = function(o1){ $('#rockdatepicker_month'+rand+'').html($(o1).html()); this.selchagnge(); }; this.selchagnge=function(){ var Y=parseFloat($('#rockdatepicker_year'+rand+'').html()); var m=parseFloat($('#rockdatepicker_month'+rand+'').html()); this.addcale(Y,m); me.hidefudong(); }; this.setweizhi = function(){ var off = obj.offset();; if(can.inputid != '')off = $('#'+can.inputid+'').offset(); var o = $('#rockdatepicker_'+rand+''); var nh = get('rockdatepicker_'+rand+'').clientHeight, nw = get('rockdatepicker_'+rand+'').clientWidth, t = off.top+can.top, dy = t+nh-winHb()-$(document).scrollTop(), l = off.left+can.left, jl = l+nw-winWb(), jl1 = 5; if($('body,html').height()>winHb())jl1=22; jl=jl+jl1; if(dy>0)t=t-dy-5; if(jl>0)l=l-jl; this.mleft = l; this.mtop = t; o.css({'left':''+l+'px','top':''+t+'px'}); }; //单元格单击 this.tdclick = function(o){ var o1 = $(o); var d = o1.text(); if(isempt(d))return; this.now.Y = parseFloat(this.Y); this.now.m = parseFloat(this.m); this.now.d = parseFloat(d); this.objtd.removeClass(); this.objtd.addClass('td00'); o.className='td01'; this.shetispannvel(1); }; //确定 this.queding = function(){ var jg = $('#rockdatepicker_span'+rand+'').html(); if(can.view=='time')jg=js.now('Y-m-d'); var ho = get('rockdatepicker_input_h'+rand+''); var io = get('rockdatepicker_input_i'+rand+''); var so = get('rockdatepicker_input_s'+rand+''); if(ho)if(!ho.disabled)jg+=' '+ho.value+''; if(io)if(!io.disabled)jg+=':'+io.value+''; if(so)if(!so.disabled)jg+=':'+so.value+''; var val = jg; if(this.isdate(val)){ val=this.formdt(can.format, val); } this.setValue(val); }; this.quenow = function(){ var val = this.formdt(can.format); this.setValue(val); }; this.setValue = function(v){ var nobj = false; if(can.inputid!=''){ nobj = get(can.inputid); $('#'+can.inputid+'').val(v).focus();; }else{ nobj = elet; obj.val(v).focus();; } if(nobj){ js.onchangedate(nobj.name, nobj, v, this); } can.itemsclick(v, this); this.hidemenu(); }; this.getValue = function(){ var s = ''; if(can.inputid!=''){ s = $('#'+can.inputid+'').val();; }else{ s = obj.val(); } return s; }; this.queclear = function(){ this.setValue(''); }; //单击 this.itemsclick = function(o,event){ }; this.hidemenu = function(){ $('#rockdatepicker_'+rand+'').remove(); }; this.changedatec= function(o){ var lx = o.attr('tdaddclick'); if(lx=='m'){ this.plftmonth(1); } if(lx=='-m'){ this.plftmonth(-1); } if(lx=='y'){ this.plftyear(1); } if(lx=='-y'){ this.plftyear(-1); } if(lx =='today'){ this.addcale(this.todate.Y,this.todate.m); } }; //上个月 this.plftmonth=function(lx) { var Y=parseFloat(this.Y),m=parseFloat(this.m); m=m+lx; if(m==0)m=12; if(m==13)m=1; if(m==12&&lx==-1)Y--; if(m==1&&lx==1)Y++; this.addcale(Y,m); }; this.plftyear=function(lx){ var Y=parseFloat(this.Y)+lx; this.addcale(Y,this.m); }; this.formdt=function(type,sj){ return js.now(type, sj); }; this.isdate = function(sj){ if(!sj)return false; var bo = /^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/.test(sj); return bo; }; this.addcale = function(Y,m){ this.objtd.removeClass(); this.objtd.html(''); me.Y=parseFloat(Y); me.m=parseFloat(m); var first = ''+Y+'-'+m+'-01'; var stuat = me.formdt('Y-m-w',first); stuat=stuat.split('-'); var year = parseFloat(stuat[0]); var month = parseFloat(stuat[1]); var maxday = me.marr[month-1];//这个月最大天数 if(year%4==0&&month==2)maxday=29;//判断是不是轮年 if(month<10)month='0'+month; var ic=parseFloat(stuat[2]); var maic=1; var xq = 0,nic=ic-1; var xqarr=[0,0,0,0,0,0,0]; var cls = ''; for(var i=0;ima)val=ma; o.value=this.sa(val); this.setoutshow=setTimeout("$('#rockdatepicker_spanselfaei"+rand+"').remove();",200); }; this.foucsnum=function(o){ clearTimeout(this.setoutshow); this.focusval = o.value; var o1 = $(o); var mi = parseFloat(o1.attr('min')); var ma = parseFloat(o1.attr('max')); o.select(); $('#rockdatepicker_spanselfaei'+rand+'').remove(); var s='
'; this.inputhis=o; for(var a=mi;a<=ma;a++){ var ai = this.sa(a); if(ai==o.value)ai=''+ai+''; s+=''+ai+''; if((a+1)%10==0)s+='
'; } s+='
'; $('#rockdatepicker_'+rand+'').append(s); $('#rockdatepicker_spanselfaei'+rand+'').find('font').click(function(){ var x = $(this).text(); o.value = x; }); }; this.createmontview = function(lx){ var w = 220,w1=109; if(lx==0){ w=130; w1=w; } var oac = this.createbasic(w,270); var s = ''; s+=''; s+=' '; if(lx == 1){ s+=' '; s+=' '; } s+='
  年份 
月份
'; s+='
  选择:'+this.now.Y+'-0'+this.now.d+'
'; s+='
'; s+=' 清空'; s+=' 现在'; s+=' 确定'; s+=' 关闭'; s+='
'; oac.html(s); $('#rockdatepicker_close'+rand+'').click(function(){ me.hidemenu(); }); $('#rockdatepicker_queding'+rand+'').click(function(){ me.queding(); }); $('#rockdatepicker_now'+rand+'').click(function(){ me.quenow(); }); $('#rockdatepicker_clear'+rand+'').click(function(){ me.queclear(); }); $('#rockdatepicker_yearshang'+rand+'').click(function(){ me.montviewyear(me.montviewyearmin-1,1); }); $('#rockdatepicker_yearxia'+rand+'').click(function(){ me.montviewyear(me.montviewyearax+1,2); }); $('#rockdatepicker_table'+rand+'').dblclick(function(){ me.queding(); }); if(lx == 1)this.montviewmonth(); this.montviewyear(this.now.Y,0); this.showviewffwfwe(); }; this.montviewmonth = function(){ var s = ''; for(var i=1; i<=12; i++){ var oi = ''+i+''; if(i<10)oi = '0'+i+''; var cls = 'a02'; if(i==this.now.m)cls='a03'; var deval = parseFloat(''+this.now.Y+''+this.sa(i)+'01000000'); if(devalthis.max.total)cls+=' not'; s+=''+oi+'月 '; if(i%2==0)s+='
'; } if(s=='')return false; var oss = $('#rockdatepicker_monthlist'+rand+''); oss.html(s); oss.find('a').click(function(){ me.montviewyearcheng(this,1); }); }; this.montviewyear = function(y,lx){ var min = y - 5; var max = y + 6; if(lx==1){ max = y; min = y-11; } if(lx==2){ min = y; max = y+11; } if(minthis.max.Y)max = this.max.Y; var oi = 0,s='',cls=''; for(var i=min; i<=max; i++){ if(oi==0)this.montviewyearmin = i; this.montviewyearax = i; cls = 'a02'; if(i==this.now.Y)cls='a03'; oi++; s+=''+i+' '; if(oi%2==0)s+='
'; } if(s=='')return false; var oss = $('#rockdatepicker_yearlist'+rand+''); oss.html(s); oss.find('a').click(function(){ me.montviewyearcheng(this,0); }); }; this.montviewyearcheng = function(o1,lx){ if(o1.className.indexOf('not')>-1)return false; var ossa = $(o1).parent().find('a'); for(var i=0;i