Calendar.js 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. o2.widget = o2.widget || {};
  2. o2.widget.Calendar = o2.Calendar = new Class({
  3. Implements: [Options, Events],
  4. Extends: o2.widget.Common,
  5. options: {
  6. "style": "default",
  7. "path": o2.session.path+"/widget/$Calendar/" ,
  8. "defaultView": "day", //day, month, year
  9. "baseDate": new Date(),
  10. "secondEnable" : false,
  11. "isTime": false,
  12. "isMulti": false,
  13. "before": null,
  14. "after": null,
  15. "timeOnly": false,
  16. "defaultDate": new Date(),
  17. "beforeCurrent": true,
  18. "range": false,
  19. "rangeNodes": [],
  20. "rangeRule": "asc", //asc + , des -
  21. "target": null
  22. },
  23. initialize: function(node, options){
  24. Locale.use("zh-CHS");
  25. this.options.defaultTime = ""+this.options.baseDate.getHours()+":"+this.options.baseDate.getMinutes()+":"+this.options.baseDate.getSeconds();
  26. this.setOptions(options);
  27. this.path = o2.session.path+"/widget/$Calendar/";
  28. this.cssPath = o2.session.path+"/widget/$Calendar/"+this.options.style+"/css.wcss";
  29. this._loadCss();
  30. // this.options.containerPath = this.path+this.style+"/container.html";
  31. // this.options.dayPath = this.path+this.style+"/day.html";
  32. // this.options.monthPath = this.path+this.style+"/month.html";
  33. // this.options.yearPath = this.path+this.style+"/year.html";
  34. // this.options.timePath = this.path+this.style+"/time.html";
  35. if (!this.options.format){
  36. if (this.options.isTime){
  37. //this.options.format = Locale.get("Date").shortDate + " " + Locale.get("Date").shortTime;
  38. if(this.options.timeOnly){
  39. this.options.format="%H:%M";
  40. }
  41. else{
  42. this.options.format = Locale.get("Date").shortDate + " " + "%H:%M";
  43. }
  44. }else{
  45. this.options.format = Locale.get("Date").shortDate;
  46. }
  47. }
  48. this.options.containerPath = this.options.path+this.options.style+"/container.html";
  49. this.options.dayPath = this.options.path+this.options.style+"/day.html";
  50. this.options.monthPath = this.options.path+this.options.style+"/month.html";
  51. this.options.yearPath = this.options.path+this.options.style+"/year.html";
  52. this.options.timePath = this.options.path+this.options.style+"/time.html";
  53. this.today = new Date();
  54. this.currentView = this.options.defaultView;
  55. this.node = $(node);
  56. this.visible = false;
  57. this.container = this.createContainer();
  58. this.container.inject((this.options.target) || $(document.body));
  59. this.contentTable = this.createContentTable();
  60. this.contentTable.inject(this.contentDateNode);
  61. this.addEvents();
  62. this.container.set({
  63. styles: {
  64. "display": "none",
  65. "opacity": 1
  66. }
  67. });
  68. this.fireEvent("init");
  69. //this.move = true;
  70. //this.containerDrag = new Drag.Move(this.container);
  71. },
  72. addEvents: function(){
  73. this.node.addEvent("focus", function(){
  74. this.show();
  75. }.bind(this));
  76. this.node.addEvent("click", function(){
  77. this.show();
  78. }.bind(this));
  79. this.prevNode.addEvent("click", function(){
  80. this.getPrev();
  81. }.bind(this));
  82. this.nextNode.addEvent("click", function(){
  83. this.getNext();
  84. }.bind(this));
  85. this.currentTextNode.addEvent("click", function(){
  86. this.changeView();
  87. }.bind(this));
  88. this.titleNode.addEvent("mousedown", function(){
  89. this.move();
  90. }.bind(this));
  91. this.titleNode.addEvent("mouseup", function(){
  92. this.unmove();
  93. }.bind(this));
  94. document.addEvent('mousedown', this.outsideClick.bind(this));
  95. },
  96. move: function(){
  97. this.containerDrag = new Drag.Move(this.container, {
  98. "onDrag": function(e){
  99. if (this.iframe){
  100. var p = this.container.getPosition();
  101. this.iframe.setStyles({
  102. "top": ""+p.y+"px",
  103. "left": ""+p.x+"px"
  104. });
  105. }
  106. }.bind(this)
  107. });
  108. },
  109. unmove: function(){
  110. this.container.removeEvents("mousedown");
  111. this.titleNode.addEvent("mousedown", function(){
  112. this.move();
  113. }.bind(this));
  114. },
  115. changeView: function(){
  116. var view = "day";
  117. switch (this.currentView) {
  118. case "day" :
  119. this.changeViewToMonth();
  120. break;
  121. case "month" :
  122. this.changeViewToYear();
  123. break;
  124. case "year" :
  125. this.changeViewToDay();
  126. break;
  127. case "time" :
  128. this.changeViewToDay();
  129. //this.changeViewToDay();
  130. break;
  131. default :
  132. //nothing;
  133. }
  134. },
  135. changeViewToMonth: function(year){
  136. this.currentView = "month";
  137. if (!this.contentMonthTable){
  138. this.contentMonthTable = this.createContentTable();
  139. this.contentMonthTable.inject(this.contentDateNode);
  140. }
  141. if (this.contentTable) this.contentTable.setStyle("display", "none");
  142. if (this.contentYearTable) this.contentYearTable.setStyle("display", "none");
  143. if (this.contentTimeTable) this.contentTimeTable.setStyle("display", "none");
  144. // if (this.contentMonthTable) this.contentMonthTable.setStyle("display", "block");
  145. if (this.contentMonthTable) this.contentMonthTable.setStyle("display", "table");
  146. var year = (year!=undefined) ? year : this.currentTextNode.retrieve("year");
  147. var month = this.currentTextNode.retrieve("month");
  148. this.showMonth(year, month);
  149. },
  150. changeViewToYear: function(year){
  151. this.currentView = "year";
  152. if (!this.contentYearTable){
  153. this.contentYearTable = this.createContentTable();
  154. this.contentYearTable.inject(this.contentDateNode);
  155. }
  156. if (this.contentTable) this.contentTable.setStyle("display", "none");
  157. if (this.contentMonthTable) this.contentMonthTable.setStyle("display", "none");
  158. if (this.contentTimeTable) this.contentTimeTable.setStyle("display", "none");
  159. // if (this.contentYearTable) this.contentYearTable.setStyle("display", "block");
  160. if (this.contentYearTable) this.contentYearTable.setStyle("display", "table");
  161. this.showYear(year);
  162. },
  163. changeViewToDay: function(year, month){
  164. this.currentView = "day";
  165. if (!this.contentTable){
  166. this.contentTable = this.createContentTable();
  167. this.contentTable.inject(this.contentDateNode);
  168. }
  169. if (this.contentMonthTable) this.contentMonthTable.setStyle("display", "none");
  170. if (this.contentYearTable) this.contentYearTable.setStyle("display", "none");
  171. if (this.contentTimeTable) this.contentTimeTable.setStyle("display", "none");
  172. // if (this.contentTable) this.contentTable.setStyle("display", "block");
  173. if (this.contentTable) this.contentTable.setStyle("display", "table");
  174. this.showDay(year, month);
  175. },
  176. getNext: function(){
  177. switch (this.currentView) {
  178. case "time" :
  179. this.getNextDate();
  180. break;
  181. case "day" :
  182. this.getNextDay();
  183. break;
  184. case "month" :
  185. this.getNextMonth();
  186. break;
  187. case "year" :
  188. this.getNextYear();
  189. break;
  190. default :
  191. //nothing
  192. }
  193. },
  194. getPrev: function(){
  195. switch (this.currentView) {
  196. case "time" :
  197. this.getPrevDate();
  198. break;
  199. case "day" :
  200. this.getPrevDay();
  201. break;
  202. case "month" :
  203. this.getPrevMonth();
  204. break;
  205. case "year" :
  206. this.getPrevYear();
  207. break;
  208. default :
  209. //nothing
  210. }
  211. },
  212. getNextDate: function(){
  213. var date = this.currentTextNode.retrieve("date");
  214. // var year = this.currentTextNode.retrieve("year");
  215. // var month = this.currentTextNode.retrieve("month");
  216. // month--;
  217. // var day = this.currentTextNode.retrieve("day");
  218. // var date = new Date(year, month, day);
  219. date.increment("day", 1);
  220. this._setTimeTitle(null, date);
  221. },
  222. getPrevDate: function(){
  223. var date = this.currentTextNode.retrieve("date");
  224. date.increment("day", -1);
  225. this._setTimeTitle(null, date);
  226. },
  227. getNextDay: function(){
  228. var year = this.currentTextNode.retrieve("year");
  229. var month = this.currentTextNode.retrieve("month");
  230. month--;
  231. var date = new Date(year, month, 1);
  232. date.increment("month", 1);
  233. var thisYear = date.getFullYear();
  234. var thisMonth = date.getMonth();
  235. this._setDayTitle(null, thisYear, thisMonth);
  236. this._setDayDate(null,thisYear, thisMonth);
  237. },
  238. getPrevDay: function(){
  239. var year = this.currentTextNode.retrieve("year");
  240. var month = this.currentTextNode.retrieve("month");
  241. month--;
  242. var date = new Date(year, month, 1);
  243. date.increment("month", -1)
  244. var thisYear = date.getFullYear();
  245. var thisMonth = date.getMonth();
  246. this._setDayTitle(null, thisYear, thisMonth);
  247. this._setDayDate(null,thisYear, thisMonth);
  248. },
  249. getNextMonth: function(){
  250. var year = this.currentTextNode.retrieve("year");
  251. var date = new Date(year, 1, 1);
  252. date.increment("year", 1)
  253. var thisYear = date.getFullYear();
  254. this.showMonth(thisYear);
  255. },
  256. getPrevMonth: function(){
  257. var year = this.currentTextNode.retrieve("year");
  258. var date = new Date(year, 1, 1);
  259. date.increment("year", -1)
  260. var thisYear = date.getFullYear();
  261. this.showMonth(thisYear);
  262. },
  263. getNextYear: function(){
  264. var year = this.currentTextNode.retrieve("year");
  265. var date = new Date(year, 1, 1);
  266. date.increment("year", this.yearLength)
  267. var thisYear = date.getFullYear();
  268. this.showYear(thisYear);
  269. },
  270. getPrevYear: function(){
  271. var year = this.currentTextNode.retrieve("year");
  272. var date = new Date(year, 1, 1);
  273. date.increment("year", 0-this.yearLength)
  274. var thisYear = date.getFullYear();
  275. this.showYear(thisYear);
  276. },
  277. outsideClick: function(e) {
  278. if(this.visible) {
  279. var elementCoords = this.container.getCoordinates();
  280. var targetCoords = this.node.getCoordinates();
  281. if(((e.page.x < elementCoords.left || e.page.x > (elementCoords.left + elementCoords.width)) ||
  282. (e.page.y < elementCoords.top || e.page.y > (elementCoords.top + elementCoords.height))) &&
  283. ((e.page.x < targetCoords.left || e.page.x > (targetCoords.left + targetCoords.width)) ||
  284. (e.page.y < targetCoords.top || e.page.y > (targetCoords.top + targetCoords.height))) ) this.hide();
  285. }
  286. },
  287. hide: function(){
  288. if (this.visible){
  289. // if (!this.morph){
  290. // this.morph = new Fx.Morph(this.container, {"duration": 200});
  291. // }
  292. this.visible = false;
  293. // this.changeViewToDay();
  294. // this.morph.start({"opacity": 0}).chain(function(){
  295. this.container.setStyle("display", "none");
  296. if (this.iframe) this.iframe.destroy();
  297. if (layout.desktop.offices){
  298. Object.each(layout.desktop.offices, function(office){
  299. office.show();
  300. });
  301. }
  302. // }.bind(this));
  303. this.fireEvent("hide");
  304. }
  305. },
  306. show: function(){
  307. debugger;
  308. if (!this.visible){
  309. var dStr = this.node.get("value");
  310. if (dStr && Date.isValid(dStr)){
  311. this.options.baseDate = Date.parse(dStr.substr(0,10));
  312. }
  313. if(this.options.timeOnly){
  314. this.currentView = "time";
  315. }
  316. else{
  317. this.currentView = this.options.defaultView;
  318. }
  319. switch (this.currentView) {
  320. case "day" :
  321. this.changeViewToDay();
  322. break;
  323. case "month" :
  324. this.showMonth();
  325. break;
  326. case "year" :
  327. this.showYear();
  328. break;
  329. case "time" :
  330. //this.showTime(this.options.baseDate);
  331. this.changeViewToTime(this.options.defaultDate);
  332. //this.changeViewToTime(this.options.baseDate);
  333. break;
  334. default :
  335. this.showDay();
  336. }
  337. // if (!this.morph){
  338. // this.morph = new Fx.Morph(this.container, {"duration": 200});
  339. // }
  340. this.container.setStyle("display", "block");
  341. if (this.container.position){
  342. this.container.position({
  343. relativeTo: this.node,
  344. position: 'bottomLeft',
  345. edge: 'upperLeft'
  346. });
  347. // var offsetPNode = this.node.getOffsetParent();
  348. var cp = this.container.getPosition(this.options.target || null);
  349. var cSize = this.container.getSize();
  350. //var fp = (this.options.target) ? this.options.target.getPosition() : $(document.body).getPosition()
  351. var fsize = (this.options.target) ? this.options.target.getSize() : $(document.body).getSize();
  352. //if (cp.y+cSize.y>fsize.y+fp.y){
  353. if (cp.y+cSize.y>fsize.y){
  354. this.container.position({
  355. relativeTo: this.node,
  356. position: 'upperLeft',
  357. edge: 'bottomLeft'
  358. });
  359. }
  360. }else{
  361. var p = this.node.getPosition(this.options.target || null);
  362. var size = this.node.getSize();
  363. var containerSize = this.container.getSize();
  364. var bodySize = $(document.body).getSize();
  365. var left = p.x;
  366. if ((left + containerSize.x) > bodySize.x){
  367. left = bodySize.x - containerSize.x;
  368. }
  369. this.container.setStyle("top", p.y+size.y+2);
  370. this.container.setStyle("left", left);
  371. }
  372. // var p = this.container.getPosition();
  373. // var s = this.container.getSize();
  374. // var zidx = this.container.getStyle("z-index");
  375. // this.iframe = new Element("iframe", {"styles":{
  376. // "border": "0px",
  377. // "margin": "0px",
  378. // "padding": "0px",
  379. // "opacity": 0,
  380. // "z-index": (zidx) ? zidx-1 : 0,
  381. // "top": ""+p.y+"px",
  382. // "left": ""+p.x+"px",
  383. // "width": ""+s.x+"px",
  384. // "height": ""+s.y+"px",
  385. // "position": "absolute"
  386. // }}).inject(this.container, "before");
  387. if (layout.desktop.offices){
  388. Object.each(layout.desktop.offices, function(office){
  389. if (this.container.isOverlap(office.officeNode)){
  390. office.hide();
  391. }
  392. }.bind(this));
  393. }
  394. // this.morph.start({"opacity": 1}).chain(function(){
  395. this.visible = true;
  396. // }.bind(this));
  397. this.fireEvent("show");
  398. }
  399. },
  400. showYear: function(year){
  401. var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear();
  402. var date = new Date(thisYear, 1, 1);
  403. date.increment("year", -2);
  404. var beginYear = date.getFullYear();
  405. date.increment("year", this.yearLength-1);
  406. var endYear = date.getFullYear();
  407. this._setYearTitle(null, beginYear, endYear, thisYear);
  408. this._setYearDate(null, beginYear, endYear, thisYear);
  409. // if (!this.move){
  410. // this.move = true;
  411. // this.containerDrag = new Drag.Move(this.container);
  412. // }
  413. },
  414. _setYearTitle:function(node, beginYear, endYear, thisYear){
  415. var thisNode = node || this.currentTextNode;
  416. thisNode.set("text", beginYear+"-"+endYear);
  417. thisNode.store("year", thisYear);
  418. },
  419. _setYearDate: function(table, beginYear, endYear, year){
  420. var yearTable = table || this.contentYearTable;
  421. var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear();
  422. var tbody = yearTable.getElement("tbody");
  423. var tds = tbody.getElements("td");
  424. tds.each(function(item, idx){
  425. var y = beginYear+idx;
  426. item.set("text", y);
  427. item.store("year", y);
  428. if (y==this.options.baseDate.getFullYear()){
  429. item.addClass("current_"+this.options.style);
  430. }else{
  431. item.removeClass("current_"+this.options.style);
  432. }
  433. }.bind(this));
  434. },
  435. showMonth: function(year, month){
  436. var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear();
  437. var thisMonth = (month!=undefined) ? month : this.options.baseDate.getMonth();
  438. this._setMonthTitle(null, thisYear, thisMonth);
  439. this._setMonthDate(null, thisYear, thisMonth);
  440. // if (!this.move){
  441. // this.move = true;
  442. // this.containerDrag = new Drag.Move(this.container);
  443. // }
  444. },
  445. _setMonthTitle:function(node, year){
  446. var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear();
  447. var thisNode = node || this.currentTextNode;
  448. thisNode.set("text", thisYear);
  449. thisNode.store("year", thisYear);
  450. },
  451. _setMonthDate: function(table, year, month){
  452. //var months = Locale.get("Date").months;
  453. var months = o2.LP.widget.months;
  454. var monthTable = table || this.contentMonthTable;
  455. var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear();
  456. var thisMonth = (month!=undefined) ? month : this.options.baseDate.getMonth();
  457. var tbody = monthTable.getElement("tbody");
  458. var tds = tbody.getElements("td");
  459. tds.each(function(item, idx){
  460. item.set("text", months[idx].substr(0,2));
  461. item.store("year", thisYear);
  462. item.store("month", idx);
  463. if ((thisYear==this.options.baseDate.getFullYear()) && (idx==this.options.baseDate.getMonth())){
  464. item.addClass("current_"+this.options.style);
  465. }else{
  466. item.removeClass("current_"+this.options.style);
  467. }
  468. }.bind(this));
  469. },
  470. showDay: function(year, month){
  471. this._setDayTitle(null, year, month);
  472. this._setDayWeekTitleTh();
  473. this._setDayDate(null, year, month);
  474. // if (!this.move){
  475. // this.move = true;
  476. // this.containerDrag = new Drag.Move(this.container);
  477. // }
  478. },
  479. _setDayTitle: function(node, year, month){
  480. var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear();
  481. var thisMonth = (month!=undefined) ? month : this.options.baseDate.getMonth();
  482. thisMonth++;
  483. var text = thisYear+"年"+thisMonth+"月";
  484. var thisNode = node || this.currentTextNode;
  485. thisNode.set("text", text);
  486. thisNode.store("year", thisYear);
  487. thisNode.store("month", thisMonth);
  488. },
  489. _setDayDate: function(table, year, month){
  490. var dayTable = table || this.contentTable;
  491. var baseDate = this.options.baseDate;
  492. if ((year!=undefined) && (month!=undefined)){
  493. baseDate = new Date();
  494. baseDate.setDate(1);
  495. baseDate.setFullYear(year);
  496. baseDate.setMonth(month);
  497. }
  498. var tbody = dayTable.getElement("tbody");
  499. var tds = tbody.getElements("td");
  500. var firstDate = baseDate.clone();
  501. firstDate.setDate(1);
  502. var day = firstDate.getDay();
  503. var tmpDate = firstDate.clone();
  504. for (var i=day-1; i>=0; i--){
  505. tmpDate.increment("day", -1);
  506. tds[i].set("text", tmpDate.getDate());
  507. tds[i].addClass("gray_"+this.options.style);
  508. tds[i].setStyles(this.css["gray_"+this.options.style]);
  509. tds[i].store("dateValue", tmpDate.toString())
  510. }
  511. for (var i=day; i<tds.length; i++){
  512. tds[i].set("text", firstDate.getDate());
  513. if (firstDate.toString() == this.options.baseDate.toString()){
  514. tds[i].addClass("current_"+this.options.style);
  515. tds[i].setStyles(this.css["current_"+this.options.style]);
  516. tds[i].removeClass("gray_"+this.options.style);
  517. tds[i].setStyle("border", "1px solid #FFF");
  518. }else if (firstDate.getMonth()!=baseDate.getMonth()){
  519. tds[i].addClass("gray_"+this.options.style);
  520. tds[i].setStyles(this.css["gray_"+this.options.style]);
  521. tds[i].removeClass("current_"+this.options.style);
  522. tds[i].setStyle("border", "1px solid #FFF");
  523. }else{
  524. tds[i].setStyles(this.css["normal_"+this.options.style]);
  525. tds[i].removeClass("current_"+this.options.style);
  526. tds[i].removeClass("gray_"+this.options.style);
  527. tds[i].setStyle("border", "1px solid #FFF");
  528. }
  529. var tmp = firstDate.clone();
  530. if (tmp.clearTime().toString() == this.today.clearTime().toString()){
  531. //tds[i].addClass("today_"+this.options.style);
  532. tds[i].setStyles(this.css["today_"+this.options.style]);
  533. tds[i].setStyle("border", "0px solid #AAA");
  534. }
  535. tds[i].store("dateValue", firstDate.toString())
  536. firstDate.increment("day", 1);
  537. }
  538. },
  539. changeViewToTime: function(date){
  540. this.currentView = "time";
  541. if (!this.contentTimeTable){
  542. this.contentTimeTable = this.createContentTable();
  543. this.contentTimeTable.inject(this.contentDateNode);
  544. }
  545. if (this.contentTable) this.contentTable.setStyle("display", "none");
  546. if (this.contentYearTable) this.contentYearTable.setStyle("display", "none");
  547. if (this.contentMonthTable) this.contentMonthTable.setStyle("display", "none");
  548. if (this.contentTimeTable) this.contentTimeTable.setStyle("display", "block");
  549. // if (this.contentTimeTable) this.contentTimeTable.setStyle("display", "table");
  550. var thisDate = date || this.options.baseDate;
  551. this.showTime(thisDate);
  552. },
  553. showTime: function(date){
  554. // var thisHour = this.options.baseDate.getHours();
  555. // var thisMinutes = this.options.baseDate.getMinutes();
  556. // var thisSeconds = this.options.baseDate.getSeconds();
  557. var times = this.options.defaultTime.split(":");
  558. var thisHour = (times[0]) ? times[0] : "0";
  559. var thisMinutes = (times[1]) ? times[1] : "0";
  560. var thisSeconds = (times[2]) ? times[2] : "0";
  561. this._setTimeTitle(null, date);
  562. this._setTimeDate(null, thisHour, thisMinutes, thisSeconds);
  563. // if (this.move){
  564. // this.move = false;
  565. // this.container.removeEvents("mousedown");
  566. // }
  567. },
  568. _setTimeTitle: function(node, date){
  569. var thisDate = date || this.options.baseDate;
  570. var thisNode = node || this.currentTextNode;
  571. var y = thisDate.getFullYear();
  572. var m = thisDate.getMonth()+1;
  573. var d = thisDate.getDate();
  574. var text = "" + y + "年" + m + "月" + d + "日";
  575. if (this.options.timeOnly){
  576. thisNode.hide();
  577. if (this.prevNode) this.prevNode.hide();
  578. if (this.nextNode) this.nextNode.hide();
  579. }
  580. thisNode.set("text", text);
  581. thisNode.store("date", date);
  582. },
  583. _setTimeDate: function(node, h, m, s){
  584. if( !this.options.secondEnable ){
  585. var div = this.contentTimeTable.getElement(".MWF_calendar_time_s");
  586. if( div )div.hide();
  587. div = this.contentTimeTable.getElement(".MWF_calendar_time_show_s");
  588. if( div )div.hide();
  589. }
  590. this.itmeHNode = this.contentTimeTable.getElement(".MWF_calendar_time_h_slider");
  591. this.itmeMNode = this.contentTimeTable.getElement(".MWF_calendar_time_m_slider");
  592. this.itmeSNode = this.contentTimeTable.getElement(".MWF_calendar_time_s_slider");
  593. this.timeShowNode = this.contentTimeTable.getElement(".MWF_calendar_time_show");
  594. this.timeShowNode.addEvent("click", function(){
  595. this._selectTime();
  596. }.bind(this));
  597. this.showHNode = this.contentTimeTable.getElement(".MWF_calendar_time_show_h");
  598. this.showMNode = this.contentTimeTable.getElement(".MWF_calendar_time_show_m");
  599. this.showSNode = this.contentTimeTable.getElement(".MWF_calendar_time_show_s");
  600. this.showActionNode = this.contentTimeTable.getElement(".MWF_calendar_action_show");
  601. var calendar = this;
  602. if (COMMON.Browser.Platform.isMobile){
  603. this.itmeHNode.empty();
  604. this.itmeHNode.removeClass("calendarTimeSlider");
  605. this.itmeHNode.setStyles(this.css.calendarTimeSliderNoStyle);
  606. var sel = new Element("select").inject(this.itmeHNode);
  607. for (i=0; i<=23; i++){
  608. var v = (i<10) ? "0"+i: i;
  609. var o = new Element("option", {
  610. "value": v,
  611. "text": v
  612. }).inject(sel);
  613. if (h==i) o.set("selected", true);
  614. }
  615. sel.addEvent("change", function(){
  616. calendar.showHNode.set("text", this.options[this.selectedIndex].get("value"));
  617. });
  618. this.showHNode.set("text", sel.options[sel.selectedIndex].get("value"));
  619. this.itmeMNode.empty();
  620. this.itmeMNode.removeClass("calendarTimeSlider");
  621. this.itmeMNode.setStyles(this.css.calendarTimeSliderNoStyle);
  622. sel = new Element("select").inject(this.itmeMNode);
  623. for (i=0; i<=59; i++){
  624. var v = (i<10) ? "0"+i: i;
  625. var o = new Element("option", {
  626. "value": v,
  627. "text": v
  628. }).inject(sel);
  629. if (m==i) o.set("selected", true);
  630. }
  631. sel.addEvent("change", function(){
  632. calendar.showMNode.set("text", this.options[this.selectedIndex].get("value"));
  633. });
  634. this.showMNode.set("text", sel.options[sel.selectedIndex].get("value"));
  635. }else{
  636. var hSlider = new Slider(this.itmeHNode, this.itmeHNode.getFirst(), {
  637. range: [0, 23],
  638. initialStep: h.toInt(),
  639. onChange: function(value){
  640. var tmp = (value.toInt().toString());
  641. if (tmp.length<2){
  642. tmp = "0"+tmp
  643. }
  644. this.showHNode.set("text", tmp);
  645. this.itmeHNode.getFirst().set("text", tmp);
  646. }.bind(this)
  647. });
  648. var mSlider = new Slider(this.itmeMNode, this.itmeMNode.getFirst(), {
  649. range: [0, 59],
  650. initialStep: m.toInt(),
  651. onChange: function(value){
  652. var tmp = (value.toInt().toString());
  653. if (tmp.length<2){
  654. tmp = "0"+tmp
  655. }
  656. this.showMNode.set("text", tmp);
  657. this.itmeMNode.getFirst().set("text", tmp);
  658. }.bind(this)
  659. });
  660. if( this.options.secondEnable && this.itmeSNode ){
  661. var sSlider = new Slider(this.itmeSNode, this.itmeSNode.getFirst(), {
  662. range: [0, 59],
  663. initialStep: parseInt(s),
  664. onChange: function(value){
  665. var tmp = (value.toInt().toString());
  666. if (tmp.length<2){
  667. tmp = "0"+tmp
  668. }
  669. this.showSNode.set("text", tmp);
  670. this.itmeSNode.getFirst().set("text", tmp);
  671. }.bind(this)
  672. });
  673. }
  674. }
  675. this.showHNode.set("text", this.addZero( h.toInt(), 2) );
  676. this.showMNode.set("text", this.addZero( m.toInt(), 2));
  677. if( this.options.secondEnable && this.showSNode ){
  678. this.showSNode.set("text", this.addZero( s.toInt(), 2) );
  679. }
  680. if (!this.okButton){
  681. this.okButton = new Element("button", {"text": "确定"}).inject(this.showActionNode);
  682. this.okButton.addEvent("click", function(){
  683. this._selectTime();
  684. this.hide();
  685. }.bind(this));
  686. this.okButton.setStyles(this.css.calendarActionShowButton);
  687. }
  688. if (!this.clearButton){
  689. this.clearButton = new Element("button", {"text": "清除"}).inject(this.showActionNode);
  690. this.clearButton.addEvent("click", function(){
  691. this.node.set("value", "");
  692. this.fireEvent("clear");
  693. this.hide();
  694. }.bind(this));
  695. this.clearButton.setStyles(this.css.calendarActionShowButton);
  696. }
  697. },
  698. addZero : function( str, length ){
  699. var zero = "";
  700. str = str.toString();
  701. for( var i=0; i<length; i++ ){
  702. zero = zero + "0";
  703. }
  704. var s = zero + str;
  705. return s.substr(s.length - length, length );
  706. },
  707. _selectTime: function(){
  708. var date = this.currentTextNode.retrieve("date");
  709. var h = this.showHNode.get("text");
  710. var m = this.showMNode.get("text");
  711. date.setHours(h);
  712. date.setMinutes(m);
  713. if( this.options.secondEnable && this.showSNode ){
  714. var s = this.showSNode.get("text");
  715. date.setSeconds(s);
  716. }
  717. if (!this.options.beforeCurrent){
  718. var now = new Date();
  719. if (date.getTime()-now.getTime()<0){
  720. alert("选择的日期必须大于当前日期!");
  721. this.node.focus();
  722. return false;
  723. }
  724. }
  725. var dv = date.format(this.options.format);
  726. if (this.fireEvent("queryComplate", [dv, date])){
  727. var t = this.node.get("value");
  728. this.options.defaultTime = ""+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
  729. this.node.set("value", dv);
  730. // this.node.focus();
  731. this.hide();
  732. if (t!=dv) this.fireEvent("change", [dv, date, t]);
  733. this.fireEvent("complate", [dv, date]);
  734. }
  735. },
  736. _selectDate: function(dateStr){
  737. var date = new Date(dateStr);
  738. var dv = date.format(this.options.format);
  739. if (this.options.isTime){
  740. this.changeViewToTime(date);
  741. }else{
  742. if (!this.options.beforeCurrent){
  743. var now = new Date();
  744. date.setHours(23,59,59);
  745. if (date.getTime()-now.getTime()<0){
  746. alert("选择的日期必须大于当前日期!");
  747. this.node.focus();
  748. return false;
  749. }
  750. }
  751. if (this.fireEvent("queryComplate", [dv, date])){
  752. var t = this.node.get("value");
  753. this.node.set("value", dv);
  754. this.hide();
  755. if (t!=dv) this.fireEvent("change", [dv, date, t]);
  756. this.fireEvent("complate", [dv, date, t]);
  757. }
  758. }
  759. },
  760. _setDayWeekTitleTh: function(table){
  761. var dayTable = table || this.contentTable;
  762. var thead = dayTable.getElement("thead");
  763. var cells = thead.getElements("th");
  764. if (this.css.calendarDaysContentTh) cells.setStyles(this.css.calendarDaysContentTh);
  765. //var days_abbr = Locale.get("Date").days_abbr;
  766. var days_abbr = o2.LP.widget.days_abbr;
  767. cells.each(function(item, idx){
  768. item.set("text", days_abbr[idx]);
  769. });
  770. return cells;
  771. },
  772. createContainer: function(){
  773. var div = null;
  774. var request = new Request.HTML({
  775. url: this.options.containerPath,
  776. method: "GET",
  777. async: false,
  778. onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
  779. div = responseTree[0];
  780. }
  781. });
  782. request.send();
  783. //this.containerNode = div.getElement(".MWF_calendar_container");
  784. this.titleNode = div.getElement(".MWF_calendar_title");
  785. this.prevNode = div.getElement(".MWF_calendar_prev");
  786. this.currentNode = div.getElement(".MWF_calendar_current");
  787. this.currentTextNode = div.getElement(".MWF_calendar_currentText");
  788. this.nextNode = div.getElement(".MWF_calendar_next");
  789. this.contentNode = div.getElement(".MWF_calendar_content");
  790. this.contentDateNode = div.getElement(".MWF_calendar_content_date");
  791. this.contentTimeNode = div.getElement(".MWF_calendar_content_time");
  792. this.bottomNode = div.getElement(".MWF_calendar_bottom");
  793. div.setStyles(this.css.container);
  794. this.titleNode.setStyles(this.css.dateTitle);
  795. this.prevNode.setStyles(this.css.datePrev);
  796. this.currentNode.setStyles(this.css.dateCurrent);
  797. this.currentTextNode.setStyles(this.css.dateCurrentText);
  798. this.nextNode.setStyles(this.css.dateNext);
  799. this.contentNode.setStyles(this.css.calendarContent);
  800. this.bottomNode.setStyles(this.css.dateBottom);
  801. return div;
  802. },
  803. createContentTable: function(){
  804. var table = null;
  805. var request = new Request.HTML({
  806. url: this.options[this.currentView+"Path"],
  807. method: "GET",
  808. async: false,
  809. onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
  810. table = responseTree[0];
  811. }
  812. });
  813. request.send();
  814. var tbody = table.getElement("tbody");
  815. if (tbody){
  816. var tds = tbody.getElements("td");
  817. var calendar = this;
  818. tds.addEvent("click", function(){
  819. switch (calendar.currentView) {
  820. case "day" :
  821. calendar._selectDate(this.retrieve("dateValue"), this);
  822. break;
  823. case "month" :
  824. calendar.changeViewToDay(this.retrieve("year"), this.retrieve("month"));
  825. break;
  826. case "year" :
  827. calendar.changeViewToMonth(this.retrieve("year"))
  828. break;
  829. case "time" :
  830. //nothing
  831. break;
  832. default :
  833. //nothing;
  834. }
  835. });
  836. switch (this.currentView) {
  837. case "day" :
  838. if (!table.display) table.display="";
  839. if (!table.style.display) table.style.display="";
  840. table.setStyles(this.css.calendarDaysContent);
  841. tds.setStyles(this.css.calendarDaysContentTd);
  842. break;
  843. case "month" :
  844. table.setStyles(this.css.calendarMonthsContent);
  845. tds.setStyles(this.css.calendarMonthsContentTd);
  846. break;
  847. case "year" :
  848. this.yearLength = tds.length;
  849. table.setStyles(this.css.calendarYearsContent);
  850. tds.setStyles(this.css.calendarYearsContentTd);
  851. break;
  852. case "time" :
  853. var nodes = table.getElements(".calendarTimeArea");
  854. if (nodes.length) nodes.setStyles(this.css.calendarTimeArea);
  855. nodes = table.getElements(".calendarTimeSlider");
  856. if (nodes.length) nodes.setStyles(this.css.calendarTimeSlider);
  857. nodes = table.getElements(".calendarTimeSliderKnob");
  858. if (nodes.length) nodes.setStyles(this.css.calendarTimeSliderKnob);
  859. nodes = table.getElements(".calendarTimeShow");
  860. if (nodes.length) nodes.setStyles(this.css.calendarTimeShow);
  861. nodes = table.getElements(".calendarTimeShowItem");
  862. if (nodes.length) nodes.setStyles(this.css.calendarTimeShowItem);
  863. var node = table.getElement(".MWF_calendar_action_show");
  864. if (node){
  865. node.setStyles(this.css.calendarActionShow);
  866. var buttons = node.getElements("button");
  867. buttons.setStyles(this.css.calendarActionShowButton);
  868. }
  869. break;
  870. default :
  871. //nothing;
  872. }
  873. tds.addEvent("mouseover", function(){
  874. this.setStyle("border", "1px solid #999999");
  875. });
  876. tds.addEvent("mouseout", function(){
  877. this.setStyle("border", "1px solid #FFF");
  878. });
  879. }else{
  880. switch (this.currentView) {
  881. case "day" :
  882. table.setStyles(this.css.calendarDaysContent);
  883. tds.setStyles(this.css.calendarDaysContentTd);
  884. break;
  885. case "month" :
  886. table.setStyles(this.css.calendarMonthsContent);
  887. tds.setStyles(this.css.calendarMonthsContentTd);
  888. break;
  889. case "year" :
  890. this.yearLength = tds.length;
  891. table.setStyles(this.css.calendarYearsContent);
  892. tds.setStyles(this.css.calendarYearsContentTd);
  893. break;
  894. case "time" :
  895. var nodes = table.getElements(".calendarTimeArea");
  896. if (nodes.length) nodes.setStyles(this.css.calendarTimeArea);
  897. nodes = table.getElements(".calendarTimeSlider");
  898. if (nodes.length) nodes.setStyles(this.css.calendarTimeSlider);
  899. nodes = table.getElements(".calendarTimeSliderKnob");
  900. if (nodes.length) nodes.setStyles(this.css.calendarTimeSliderKnob);
  901. nodes = table.getElements(".calendarTimeShow");
  902. if (nodes.length) nodes.setStyles(this.css.calendarTimeShow);
  903. nodes = table.getElements(".calendarTimeShowItem");
  904. if (nodes.length) nodes.setStyles(this.css.calendarTimeShowItem);
  905. var node = table.getElement(".MWF_calendar_action_show");
  906. if (node){
  907. node.setStyles(this.css.calendarActionShow);
  908. var buttons = node.getElements("button");
  909. buttons.setStyles(this.css.calendarActionShowButton);
  910. }
  911. break;
  912. default :
  913. //nothing;
  914. }
  915. }
  916. return table;
  917. }
  918. });