YUI.add('gallery-calendar', function(Y) { //YUI.add('g-calendar', function (Y) { /** * calendar.js * autohr:lijing00333@163.com jayli * @class Y.Calendar * @param { string } hook or trigger * @param { object } option * @return { object } a new calendar * @requires { 'node' } * @requires { calendar-skin-default } skin * * Y.Calenar * info calendar constructor * useage new Y.Calendar(id,options); * param id:{string} container id * confgi selected {date} selected date * mindate:{date} min date * maxdate:{date} max date * popup:{boolean} pupup or not,false by default * closeable:{boolean} close the calendar by select a date when popup,false by default * range_select:{boolean} range select,false by default * range:{start:date,end:date} default range * multi_select:{number} pages,default number is 1 * withtime:{boolean} select date with time,false by default * date:{date} default date * navigator:{boolean} can jump to custom month,true by default useShim:{boolean} use iframe startDay:{number} start weekday offset,0 by default * Y.Calendar 's method * init * render,rewrite old params * hide * show * */ YUI.namespace('Y.Calendar'); if(typeof Y.Node.prototype.queryAll == 'undefined'){ Y.Node.prototype.queryAll = Y.Node.prototype.all; Y.Node.prototype.query = Y.Node.prototype.one; Y.Node.get = Y.Node.one; Y.get = Y.one; } Y.Calendar = function(){ this.init.apply(this,arguments); }; Y.mix(Y.Calendar,{ init:function(id,config){ var that = this; that.id = that.C_Id = id; //alert(id); that.buildParam(config); //形成con /* that.con,日历的容器 that.id 传进来的id that.C_Id 永远代表日历容器的ID */ if(!that.popup){ that.con = Y.one('#'+id); } else { var trigger = Y.one('#'+id); that.trigger = trigger; //that.C_Id = 'C_'+Math.random().toString().replace(/.\./i,''); that.C_Id = 'C_'+id; that.con = Y.Node.create('
'); Y.one('body').appendChild(that.con); /* var _x = trigger.getXY()[0]; var _y = trigger.getXY()[1]+trigger.get('region').height; that.con.setStyle('left',_x.toString()+'px'); that.con.setStyle('top',_y.toString()+'px'); */ that.con.setStyle('top','0px'); that.con.setStyle('position','absolute'); that.con.setStyle('zIndex','1000'); that.con.setStyle('background','white'); that.con.setStyle('visibility','hidden'); if(that.useShim){ that.shim = Y.Node.create(''); that.shim.setStyle('position','absolute'); that.con.insert(that.shim, 'before'); } } that.buildEventCenter(); that.render(); that.buildEvent(); return this; }, /** * 日历的事件中心 */ buildEventCenter:function(){ var that = this; var EventFactory = function(){ this.publish("select"); this.publish("switch"); this.publish("rangeselect"); this.publish("timeselect"); this.publish("selectcomplete"); this.publish("hide");//later this.publish("show");//later }; Y.augment(EventFactory, Y.Event.Target); that.EventCenter = new EventFactory(); return this; }, /** * 绑定函数 */ on:function(type,foo){ var that = this; that.EventCenter.subscribe(type,foo); return this; }, render:function(o){ var that = this; var o = o || {}; that.parseParam(o); that.ca = []; that.con.addClass('c-call clearfix multi-'+that.multi_page); that.con.set('innerHTML',''); for(var i = 0,_oym = [that.year,that.month]; i that.range.end.getTime()){ var __t = that.range.start; that.range.start = that.range.end; that.range.end = __t; } that.EventCenter.fire('rangeselect',that.range); that.render(); } return this; }, //constructor /** * TimeSelector只支持选择,不支持初始化 */ TimeSelector:function(ft,fathor){ //属性------------------ this.fathor = fathor; this.fcon = ft.ancestor('.c-box');//cc容器 this.popupannel = this.fcon.query('.selectime');//点选时间的弹出层 if(typeof fathor._time == 'undefined'){//确保初始值和当前时间一致 fathor._time = new Date(); } this.time = fathor._time; this.status = 's';//当前选择的状态,'h','m','s'依次判断更新哪个值 this.ctime = Y.Node.create('
Time: h:m:s
'); //this.button = Y.Node.create(''); //小时 this.h_a = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23']; //分钟 this.m_a = ['00','10','20','30','40','50']; //秒 this.s_a = ['00','10','20','30','40','50']; //接口---------------- /** * 创建相应的容器html,值均包含在a中 * 参数:要拼装的数组 * 返回:拼好的innerHTML,结尾还要带一个关闭的a * */ this.parseSubHtml = function(a){ var in_str = ''; for(var i = 0;i'; } in_str += 'x'; return in_str; }; /** * 显示selectime容器 * 参数,构造好的innerHTML */ this.showPopup= function(instr){ var that = this; this.popupannel.set('innerHTML',instr); this.popupannel.removeClass('hidden'); var status = that.status; var _con = that.ctime; that.ctime.queryAll('span').removeClass('on'); switch(status){ case 'h': that.ctime.query('.h').addClass('on'); break; case 'm': that.ctime.query('.m').addClass('on'); break; case 's': that.ctime.query('.s').addClass('on'); break; } }; /** * 隐藏selectime容器 */ this.hidePopup= function(){ this.popupannel.addClass('hidden'); }; /** * 不对其做更多的上下文假设,仅仅根据time显示出来 */ this.render = function(){ var that = this; var h = that.get('h'); var m = that.get('m'); var s = that.get('s'); if(h < 10) { h = "0"+h; } if(m < 10) { m = "0"+m; } if(s < 10) { s = "0"+s; } that.fathor._time = that.time; that.ctime.query('.h').set('innerHTML',h); that.ctime.query('.m').set('innerHTML',m); that.ctime.query('.s').set('innerHTML',s); return that; }; //这里的set和get都只是对time的操作,并不对上下文做过多假设 /** * set(status,v) * h:2,'2' */ this.set = function(status,v){ var that = this; var v = Number(v); switch(status){ case 'h': that.time.setHours(v); break; case 'm': that.time.setMinutes(v); break; case 's': that.time.setSeconds(v); break; } that.render(); }; /** * get(status) */ this.get = function(status){ var that = this; var time = that.time; switch(status){ case 'h': return time.getHours(); break; case 'm': return time.getMinutes(); break; case 's': return time.getSeconds(); break; } }; /** * add() * 状态值代表的变量增1 */ this.add = function(){ var that = this; var status = that.status; var v = that.get(status); v++; /* */ that.set(status,v); }; /** * minus() * 状态值代表的变量增1 */ this.minus= function(){ var that = this; var status = that.status; var v = that.get(status); v--; that.set(status,v); }; //构造--------- this.init = function(){ var that = this; ft.set('innerHTML','').append(that.ctime); //ft.append(that.button); that.render(); that.popupannel.on('click',function(e){ var el = e.target; if(el.hasClass('x')){//关闭 that.hidePopup(); return; }else if(el.hasClass('item')){//点选一个值 var v = Number(el.get('innerHTML')); that.set(that.status,v); that.hidePopup(); return; } }); //确定的动作 /* that.button.on('click',function(e){ var d = typeof that.fathor.dt_date == 'undefined'?that.fathor.date:that.fathor.dt_date; d.setHours(that.get('h')); d.setMinutes(that.get('m')); d.setSeconds(that.get('s')); that.fathor.EventCenter.fire('timeselect',d); if(that.fathor.popup && that.fathor.closeable){ that.fathor.hide(); } }); */ //ctime上的键盘事件,上下键,左右键的监听 //TODO 考虑是否去掉 that.ctime.on('keyup',function(e){ if(e.keyCode == 38 || e.keyCode == 37){//up or left e.halt(); that.add(); } if(e.keyCode == 40 || e.keyCode == 39){//down or right e.halt(); that.minus(); } }); //上的箭头动作 that.ctime.query('.u').on('click',function(e){ that.hidePopup(); that.add(); }); //下的箭头动作 that.ctime.query('.d').on('click',function(e){ that.hidePopup(); that.minus(); }); //弹出选择小时 that.ctime.query('.h').on('click',function(e){ var in_str = that.parseSubHtml(that.h_a); that.status = 'h'; that.showPopup(in_str); }); //弹出选择分钟 that.ctime.query('.m').on('click',function(e){ var in_str = that.parseSubHtml(that.m_a); that.status = 'm'; that.showPopup(in_str); }); //弹出选择秒 that.ctime.query('.s').on('click',function(e){ var in_str = that.parseSubHtml(that.s_a); that.status = 's'; that.showPopup(in_str); }); }; this.init(); }, //constructor /** * 子日历构造器 * @constructor Y.Calendar.prototype.Call * @param {object} config ,参数列表,需要指定子日历所需的年月 * @param {object} fathor,指向Y.Calendar实例的指针,需要共享父框的参数 * @return 子日历的实例 */ Call:function(config,fathor){ //属性 this.fathor = fathor; this.month = Number(config.month); this.year = Number(config.year); this.prev_arrow = config.prev_arrow; this.next_arrow = config.next_arrow; this.node = null; this.timmer = null;//时间选择的实例 this.id = ''; this.EV = []; this.html = [ '
', '
', '', '{$title}', '', '
', '
', '
', /* '', '', '', '', '', '', '', */ fathor.handleOffset().day_html, '
', '
', '{$ds}', /* 1 3 1 1 1 */ '
', '
', '', '
', '
', '时间:00:00 ♥', '
', '
', '', '
' ].join(""); this.nav_html = [ '

', 'Month', '', '

', '

', 'Year', '', '

', '

', '', '

' ].join(""); //常用的数据格式的验证 this.Verify = function() { var isDay = function(n) { if (!/^\d+$/i.test(n))return false; n = Number(n); return !(n < 1 || n > 31); }, isYear = function(n) { if (!/^\d+$/i.test(n))return false; n = Number(n); return !(n < 100 || n > 10000); }, isMonth = function(n) { if (!/^\d+$/i.test(n))return false; n = Number(n); return !(n < 1 || n > 12); }; return { isDay:isDay, isYear:isYear, isMonth:isMonth }; }; //方法 /** * 渲染子日历的UI */ this.renderUI = function(){ var cc = this; cc.HTML = ''; var _o = {}; _o.prev = ''; _o.next = ''; _o.title = ''; _o.ds = ''; if(!cc.prev_arrow){ _o.prev = 'hidden'; } if(!cc.next_arrow){ _o.next = 'hidden'; } if(!cc.fathor.showtime){ _o.showtime = 'hidden'; } _o.id = cc.id = 'cc-'+Math.random().toString().replace(/.\./i,''); _o.title = cc.fathor.getHeadStr(cc.year,cc.month); cc.createDS(); _o.ds = cc.ds; //cc.node = Y.Node.create(cc.fathor.templetShow(cc.html,_o)); cc.fathor.con.appendChild(Y.Node.create(cc.fathor.templetShow(cc.html,_o))); cc.node = Y.one('#'+cc.id); if(cc.fathor.withtime){ var ft = cc.node.query('.c-ft'); ft.removeClass('hidden'); cc.timmer = new cc.fathor.TimeSelector(ft,cc.fathor); } return this; }; /** * 创建子日历的事件 */ this.buildEvent = function(){ var cc = this; var con = Y.one('#'+cc.id); //flush event for(var i = 0;i'; }else if(cc.fathor.maxdate instanceof Date && new Date(cc.year+'/'+(cc.month+1)+'/'+(i+1-startweekday)).getTime() > cc.fathor.maxdate.getTime() ){//disabled s+= ''+(i - startweekday + 1)+''; }else if((cc.fathor.range.start != null && cc.fathor.range.end != null) //日期选择范围 && ( _td_s.getTime()>=cc.fathor.showdate(1,cc.fathor.range.start).getTime() && _td_e.getTime() < cc.fathor.showdate(1,cc.fathor.range.end).getTime()) ){ //alert(Y.dump(_td_s.getDate())); if(i == (startweekday + (new Date()).getDate() - 1) && (new Date()).getFullYear() == cc.year && (new Date()).getMonth() == cc.month){//今天并被选择 s+=''+(i - startweekday + 1)+''; }else{ s+= ''+(i - startweekday + 1)+''; } }else if(i == (startweekday + (new Date()).getDate() - 1) && (new Date()).getFullYear() == cc.year && (new Date()).getMonth() == cc.month){//today s += ''+(i - startweekday + 1)+''; }else if(i == (startweekday + cc.fathor.selected.getDate() - 1) && cc.month == cc.fathor.selected.getMonth() && cc.year == cc.fathor.selected.getFullYear()){//selected s += ''+(i - startweekday + 1)+''; }else{//other s += ''+(i - startweekday + 1)+''; } } if(k%7 != 0){ for(var i = 0;i<(7-k%7);i++){ s += '0'; } } cc.ds = s; return this; }; /** * 渲染 */ this.render = function(){ var cc = this; cc.renderUI(); cc.buildEvent(); return this; }; }//Call constructor over },0,null,4); }, '@VERSION@' ,{requires:['node']});