o2.widget = o2.widget || {}; o2.widget.Calendar = o2.Calendar = new Class({ Implements: [Options, Events], Extends: o2.widget.Common, options: { "style": "default", "path": o2.session.path+"/widget/$Calendar/" , "defaultView": "day", //day, month, year "baseDate": new Date(), "isTime": false, "isMulti": false, "before": null, "after": null, "timeOnly": false, "defaultDate": new Date(), "beforeCurrent": true, "range": false, "rangeNodes": [], "rangeRule": "asc", //asc + , des - "target": null }, initialize: function(node, options){ Locale.use("zh-CHS"); this.options.defaultTime = ""+this.options.baseDate.getHours()+":"+this.options.baseDate.getMinutes()+":"+this.options.baseDate.getSeconds(); this.setOptions(options); this.path = o2.session.path+"/widget/$Calendar/"; this.cssPath = o2.session.path+"/widget/$Calendar/"+this.options.style+"/css.wcss"; this._loadCss(); // this.options.containerPath = this.path+this.style+"/container.html"; // this.options.dayPath = this.path+this.style+"/day.html"; // this.options.monthPath = this.path+this.style+"/month.html"; // this.options.yearPath = this.path+this.style+"/year.html"; // this.options.timePath = this.path+this.style+"/time.html"; if (!this.options.format){ if (this.options.isTime){ //this.options.format = Locale.get("Date").shortDate + " " + Locale.get("Date").shortTime; if(this.options.timeOnly){ this.options.format="%H:%M"; } else{ this.options.format = Locale.get("Date").shortDate + " " + "%H:%M"; } }else{ this.options.format = Locale.get("Date").shortDate; } } this.options.containerPath = this.options.path+this.options.style+"/container.html"; this.options.dayPath = this.options.path+this.options.style+"/day.html"; this.options.monthPath = this.options.path+this.options.style+"/month.html"; this.options.yearPath = this.options.path+this.options.style+"/year.html"; this.options.timePath = this.options.path+this.options.style+"/time.html"; this.today = new Date(); this.currentView = this.options.defaultView; this.node = $(node); this.visible = false; this.container = this.createContainer(); this.container.inject((this.options.target) || $(document.body)); this.contentTable = this.createContentTable(); this.contentTable.inject(this.contentDateNode); this.addEvents(); this.container.set({ styles: { "display": "none", "opacity": 1 } }); this.fireEvent("init"); //this.move = true; //this.containerDrag = new Drag.Move(this.container); }, addEvents: function(){ this.node.addEvent("focus", function(){ this.show(); }.bind(this)); this.node.addEvent("click", function(){ this.show(); }.bind(this)); this.prevNode.addEvent("click", function(){ this.getPrev(); }.bind(this)); this.nextNode.addEvent("click", function(){ this.getNext(); }.bind(this)); this.currentTextNode.addEvent("click", function(){ this.changeView(); }.bind(this)); this.titleNode.addEvent("mousedown", function(){ this.move(); }.bind(this)); this.titleNode.addEvent("mouseup", function(){ this.unmove(); }.bind(this)); document.addEvent('mousedown', this.outsideClick.bind(this)); }, move: function(){ this.containerDrag = new Drag.Move(this.container, { "onDrag": function(e){ if (this.iframe){ var p = this.container.getPosition(); this.iframe.setStyles({ "top": ""+p.y+"px", "left": ""+p.x+"px" }); } }.bind(this) }); }, unmove: function(){ this.container.removeEvents("mousedown"); this.titleNode.addEvent("mousedown", function(){ this.move(); }.bind(this)); }, changeView: function(){ var view = "day"; switch (this.currentView) { case "day" : this.changeViewToMonth(); break; case "month" : this.changeViewToYear(); break; case "year" : this.changeViewToDay(); break; case "time" : this.changeViewToDay(); //this.changeViewToDay(); break; default : //nothing; } }, changeViewToMonth: function(year){ this.currentView = "month"; if (!this.contentMonthTable){ this.contentMonthTable = this.createContentTable(); this.contentMonthTable.inject(this.contentDateNode); } if (this.contentTable) this.contentTable.setStyle("display", "none"); if (this.contentYearTable) this.contentYearTable.setStyle("display", "none"); if (this.contentTimeTable) this.contentTimeTable.setStyle("display", "none"); // if (this.contentMonthTable) this.contentMonthTable.setStyle("display", "block"); if (this.contentMonthTable) this.contentMonthTable.setStyle("display", "table"); var year = (year!=undefined) ? year : this.currentTextNode.retrieve("year"); var month = this.currentTextNode.retrieve("month"); this.showMonth(year, month); }, changeViewToYear: function(year){ this.currentView = "year"; if (!this.contentYearTable){ this.contentYearTable = this.createContentTable(); this.contentYearTable.inject(this.contentDateNode); } if (this.contentTable) this.contentTable.setStyle("display", "none"); if (this.contentMonthTable) this.contentMonthTable.setStyle("display", "none"); if (this.contentTimeTable) this.contentTimeTable.setStyle("display", "none"); // if (this.contentYearTable) this.contentYearTable.setStyle("display", "block"); if (this.contentYearTable) this.contentYearTable.setStyle("display", "table"); this.showYear(year); }, changeViewToDay: function(year, month){ this.currentView = "day"; if (!this.contentTable){ this.contentTable = this.createContentTable(); this.contentTable.inject(this.contentDateNode); } if (this.contentMonthTable) this.contentMonthTable.setStyle("display", "none"); if (this.contentYearTable) this.contentYearTable.setStyle("display", "none"); if (this.contentTimeTable) this.contentTimeTable.setStyle("display", "none"); // if (this.contentTable) this.contentTable.setStyle("display", "block"); if (this.contentTable) this.contentTable.setStyle("display", "table"); this.showDay(year, month); }, getNext: function(){ switch (this.currentView) { case "time" : this.getNextDate(); break; case "day" : this.getNextDay(); break; case "month" : this.getNextMonth(); break; case "year" : this.getNextYear(); break; default : //nothing } }, getPrev: function(){ switch (this.currentView) { case "time" : this.getPrevDate(); break; case "day" : this.getPrevDay(); break; case "month" : this.getPrevMonth(); break; case "year" : this.getPrevYear(); break; default : //nothing } }, getNextDate: function(){ var date = this.currentTextNode.retrieve("date"); // var year = this.currentTextNode.retrieve("year"); // var month = this.currentTextNode.retrieve("month"); // month--; // var day = this.currentTextNode.retrieve("day"); // var date = new Date(year, month, day); date.increment("day", 1); this._setTimeTitle(null, date); }, getPrevDate: function(){ var date = this.currentTextNode.retrieve("date"); date.increment("day", -1); this._setTimeTitle(null, date); }, getNextDay: function(){ var year = this.currentTextNode.retrieve("year"); var month = this.currentTextNode.retrieve("month"); month--; var date = new Date(year, month, 1); date.increment("month", 1); var thisYear = date.getFullYear(); var thisMonth = date.getMonth(); this._setDayTitle(null, thisYear, thisMonth); this._setDayDate(null,thisYear, thisMonth); }, getPrevDay: function(){ var year = this.currentTextNode.retrieve("year"); var month = this.currentTextNode.retrieve("month"); month--; var date = new Date(year, month, 1); date.increment("month", -1) var thisYear = date.getFullYear(); var thisMonth = date.getMonth(); this._setDayTitle(null, thisYear, thisMonth); this._setDayDate(null,thisYear, thisMonth); }, getNextMonth: function(){ var year = this.currentTextNode.retrieve("year"); var date = new Date(year, 1, 1); date.increment("year", 1) var thisYear = date.getFullYear(); this.showMonth(thisYear); }, getPrevMonth: function(){ var year = this.currentTextNode.retrieve("year"); var date = new Date(year, 1, 1); date.increment("year", -1) var thisYear = date.getFullYear(); this.showMonth(thisYear); }, getNextYear: function(){ var year = this.currentTextNode.retrieve("year"); var date = new Date(year, 1, 1); date.increment("year", this.yearLength) var thisYear = date.getFullYear(); this.showYear(thisYear); }, getPrevYear: function(){ var year = this.currentTextNode.retrieve("year"); var date = new Date(year, 1, 1); date.increment("year", 0-this.yearLength) var thisYear = date.getFullYear(); this.showYear(thisYear); }, outsideClick: function(e) { if(this.visible) { var elementCoords = this.container.getCoordinates(); var targetCoords = this.node.getCoordinates(); if(((e.page.x < elementCoords.left || e.page.x > (elementCoords.left + elementCoords.width)) || (e.page.y < elementCoords.top || e.page.y > (elementCoords.top + elementCoords.height))) && ((e.page.x < targetCoords.left || e.page.x > (targetCoords.left + targetCoords.width)) || (e.page.y < targetCoords.top || e.page.y > (targetCoords.top + targetCoords.height))) ) this.hide(); } }, hide: function(){ if (this.visible){ // if (!this.morph){ // this.morph = new Fx.Morph(this.container, {"duration": 200}); // } this.visible = false; // this.changeViewToDay(); // this.morph.start({"opacity": 0}).chain(function(){ this.container.setStyle("display", "none"); if (this.iframe) this.iframe.destroy(); if (layout.desktop.offices){ Object.each(layout.desktop.offices, function(office){ office.show(); }); } // }.bind(this)); this.fireEvent("hide"); } }, show: function(){ if (!this.visible){ var dStr = this.node.get("value"); if (dStr && Date.isValid(dStr)){ this.options.baseDate = Date.parse(dStr.substr(0,10)); } if(this.options.timeOnly){ this.currentView = "time"; } else{ this.currentView = this.options.defaultView; } switch (this.currentView) { case "day" : this.changeViewToDay(); break; case "month" : this.showMonth(); break; case "year" : this.showYear(); break; case "time" : //this.showTime(this.options.baseDate); this.changeViewToTime(this.options.defaultDate); //this.changeViewToTime(this.options.baseDate); break; default : this.showDay(); } // if (!this.morph){ // this.morph = new Fx.Morph(this.container, {"duration": 200}); // } this.container.setStyle("display", "block"); if (this.container.position){ this.container.position({ relativeTo: this.node, position: 'bottomLeft', edge: 'upperLeft' }); // var offsetPNode = this.node.getOffsetParent(); var cp = this.container.getPosition(this.options.target || null); var cSize = this.container.getSize(); //var fp = (this.options.target) ? this.options.target.getPosition() : $(document.body).getPosition() var fsize = (this.options.target) ? this.options.target.getSize() : $(document.body).getSize(); //if (cp.y+cSize.y>fsize.y+fp.y){ if (cp.y+cSize.y>fsize.y){ this.container.position({ relativeTo: this.node, position: 'upperLeft', edge: 'bottomLeft' }); } }else{ var p = this.node.getPosition(this.options.target || null); var size = this.node.getSize(); var containerSize = this.container.getSize(); var bodySize = $(document.body).getSize(); var left = p.x; if ((left + containerSize.x) > bodySize.x){ left = bodySize.x - containerSize.x; } this.container.setStyle("top", p.y+size.y+2); this.container.setStyle("left", left); } // var p = this.container.getPosition(); // var s = this.container.getSize(); // var zidx = this.container.getStyle("z-index"); // this.iframe = new Element("iframe", {"styles":{ // "border": "0px", // "margin": "0px", // "padding": "0px", // "opacity": 0, // "z-index": (zidx) ? zidx-1 : 0, // "top": ""+p.y+"px", // "left": ""+p.x+"px", // "width": ""+s.x+"px", // "height": ""+s.y+"px", // "position": "absolute" // }}).inject(this.container, "before"); if (layout.desktop.offices){ Object.each(layout.desktop.offices, function(office){ if (this.container.isOverlap(office.officeNode)){ office.hide(); } }.bind(this)); } // this.morph.start({"opacity": 1}).chain(function(){ this.visible = true; // }.bind(this)); this.fireEvent("show"); } }, showYear: function(year){ var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear(); var date = new Date(thisYear, 1, 1); date.increment("year", -2); var beginYear = date.getFullYear(); date.increment("year", this.yearLength-1); var endYear = date.getFullYear(); this._setYearTitle(null, beginYear, endYear, thisYear); this._setYearDate(null, beginYear, endYear, thisYear); // if (!this.move){ // this.move = true; // this.containerDrag = new Drag.Move(this.container); // } }, _setYearTitle:function(node, beginYear, endYear, thisYear){ var thisNode = node || this.currentTextNode; thisNode.set("text", beginYear+"-"+endYear); thisNode.store("year", thisYear); }, _setYearDate: function(table, beginYear, endYear, year){ var yearTable = table || this.contentYearTable; var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear(); var tbody = yearTable.getElement("tbody"); var tds = tbody.getElements("td"); tds.each(function(item, idx){ var y = beginYear+idx; item.set("text", y); item.store("year", y); if (y==this.options.baseDate.getFullYear()){ item.addClass("current_"+this.options.style); }else{ item.removeClass("current_"+this.options.style); } }.bind(this)); }, showMonth: function(year, month){ var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear(); var thisMonth = (month!=undefined) ? month : this.options.baseDate.getMonth(); this._setMonthTitle(null, thisYear, thisMonth); this._setMonthDate(null, thisYear, thisMonth); // if (!this.move){ // this.move = true; // this.containerDrag = new Drag.Move(this.container); // } }, _setMonthTitle:function(node, year){ var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear(); var thisNode = node || this.currentTextNode; thisNode.set("text", thisYear); thisNode.store("year", thisYear); }, _setMonthDate: function(table, year, month){ //var months = Locale.get("Date").months; var months = o2.LP.widget.months; var monthTable = table || this.contentMonthTable; var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear(); var thisMonth = (month!=undefined) ? month : this.options.baseDate.getMonth(); var tbody = monthTable.getElement("tbody"); var tds = tbody.getElements("td"); tds.each(function(item, idx){ item.set("text", months[idx].substr(0,2)); item.store("year", thisYear); item.store("month", idx); if ((thisYear==this.options.baseDate.getFullYear()) && (idx==this.options.baseDate.getMonth())){ item.addClass("current_"+this.options.style); }else{ item.removeClass("current_"+this.options.style); } }.bind(this)); }, showDay: function(year, month){ this._setDayTitle(null, year, month); this._setDayWeekTitleTh(); this._setDayDate(null, year, month); // if (!this.move){ // this.move = true; // this.containerDrag = new Drag.Move(this.container); // } }, _setDayTitle: function(node, year, month){ var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear(); var thisMonth = (month!=undefined) ? month : this.options.baseDate.getMonth(); thisMonth++; var text = thisYear+"年"+thisMonth+"月"; var thisNode = node || this.currentTextNode; thisNode.set("text", text); thisNode.store("year", thisYear); thisNode.store("month", thisMonth); }, _setDayDate: function(table, year, month){ var dayTable = table || this.contentTable; var baseDate = this.options.baseDate; if ((year!=undefined) && (month!=undefined)){ baseDate = new Date(); baseDate.setDate(1); baseDate.setFullYear(year); baseDate.setMonth(month); } var tbody = dayTable.getElement("tbody"); var tds = tbody.getElements("td"); var firstDate = baseDate.clone(); firstDate.setDate(1); var day = firstDate.getDay(); var tmpDate = firstDate.clone(); for (var i=day-1; i>=0; i--){ tmpDate.increment("day", -1); tds[i].set("text", tmpDate.getDate()); tds[i].addClass("gray_"+this.options.style); tds[i].setStyles(this.css["gray_"+this.options.style]); tds[i].store("dateValue", tmpDate.toString()) } for (var i=day; i