WeekView.js 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  1. MWF.require("MWF.widget.Calendar", null, false);
  2. var MWFCalendarWeekView = MWF.xApplication.Calendar.WeekView = new Class({
  3. Extends: MWF.widget.Common,
  4. Implements: [Options, Events],
  5. options: {
  6. "style": "default",
  7. "date" : ""
  8. },
  9. initialize: function(node, app, options){
  10. this.setOptions(options);
  11. this.path = "../x_component_Calendar/$WeekView/";
  12. this.cssPath = "../x_component_Calendar/$WeekView/"+this.options.style+"/css.wcss";
  13. this._loadCss();
  14. this.app = app;
  15. this.container = $(node);
  16. this.weekBegin = this.app.calendarConfig.weekBegin || "0";
  17. this.load();
  18. },
  19. load: function(){
  20. this.node = new Element("div.node", {"styles": this.css.node}).inject(this.container);
  21. this.node.setStyle("position","relative");
  22. //this.loadSideBar();
  23. this.resetNodeSize();
  24. //this.app.addEvent("resize", this.resetNodeSize.bind(this));
  25. this.loadCalendar();
  26. },
  27. resetNodeSize: function(){
  28. //if( this.app.inContainer )return;
  29. var size = this.container.getSize();
  30. var y = size.y-50;
  31. this.node.setStyle("height", ""+y+"px");
  32. //this.node.setStyle("margin-top", "60px");
  33. if( this.calendar ){
  34. this.calendar.resetBodySize()
  35. }
  36. //var sideBarSize = this.app.sideBar ? this.app.sideBar.getSize() : { x : 0, y:0 };
  37. //this.node.setStyle("width", ""+(size.x - sideBarSize.x)+"px");
  38. //this.node.setStyle("margin-right", ""+sideBarSize.x+"px");
  39. //var size = this.container.getSize();
  40. //
  41. //this.scrollNode.setStyle("height", ""+(size.y-60)+"px");
  42. //this.scrollNode.setStyle("margin-top", "60px");
  43. //
  44. //if (this.contentWarpNode){
  45. // this.contentWarpNode.setStyles({
  46. // "width": (size.x - 50) +"px"
  47. // });
  48. //}
  49. },
  50. loadCalendar: function(){
  51. var date = "";
  52. if( this.options.date ){
  53. date = Date.parse( this.options.date )
  54. }else{
  55. date = new Date();
  56. }
  57. this.currentWeek = this.getWeekNumber( date );
  58. this.calendar = new MWFCalendarWeekView.Calendar(this, date );
  59. },
  60. hide: function(){
  61. var fx = new Fx.Morph(this.node, {
  62. "duration": "300",
  63. "transition": Fx.Transitions.Expo.easeOut
  64. });
  65. fx.start({
  66. "opacity": 0
  67. }).chain(function(){
  68. this.node.setStyle("display", "none");
  69. }.bind(this));
  70. },
  71. show: function(){
  72. this.node.setStyles(this.css.node);
  73. var fx = new Fx.Morph(this.node, {
  74. "duration": "800",
  75. "transition": Fx.Transitions.Expo.easeOut
  76. });
  77. //this.app.fireAppEvent("resize");
  78. fx.start({
  79. "opacity": 1
  80. //"left": MWFCalendar.LeftNaviWidth+"px"
  81. }).chain(function(){
  82. //this.node.setStyles({
  83. // "position": "static",
  84. // "width": "auto"
  85. //});
  86. if( this.calendar.dataTable_WholeDay ){
  87. this.calendar.dataTable_WholeDay.setStyle("display","");
  88. }
  89. }.bind(this));
  90. },
  91. reload: function(){
  92. if (this.calendar) this.calendar.reLoadCalendar();
  93. },
  94. recordStatus : function(){
  95. return {
  96. date : (this.calendar && this.calendar.baseDate) ? this.calendar.baseDate.format( "db" ) : ""
  97. };
  98. },
  99. destroy: function(){
  100. if (this.calendar){
  101. this.calendar.destroy();
  102. }
  103. this.node.destroy();
  104. //MWF.release( this );
  105. },
  106. getWeekNumber: function(d){
  107. // Create a copy of this date object
  108. var target = d.clone();
  109. // ISO week date weeks start on monday
  110. // so correct the day number
  111. //var dayNr = (d.getDay() + 6) % 7;
  112. var dayNr = ( 7 + d.getDay() - parseInt( this.weekBegin ) ) % 7;
  113. // ISO 8601 states that week 1 is the week
  114. // with the first thursday of that year.
  115. // Set the target date to the thursday in the target week
  116. target.setDate(target.getDate() - dayNr + 3);
  117. // Store the millisecond value of the target date
  118. var firstThursday = target.valueOf();
  119. // Set the target to the first thursday of the year
  120. // First set the target to january first
  121. target.setMonth(0, 1);
  122. // Not a thursday? Correct the date to the next thursday
  123. if (target.getDay() != 4) {
  124. target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
  125. }
  126. // The weeknumber is the number of weeks between the
  127. // first thursday of the year and the thursday in the target week
  128. return 1 + Math.ceil((firstThursday - target) / 604800000); // 604800000 = 7 * 24 * 3600 * 1000
  129. }
  130. });
  131. MWFCalendarWeekView.DayWidth;
  132. MWFCalendarWeekView.HourHeight = 48;
  133. MWFCalendarWeekView.DayHeight = 24*MWFCalendarWeekView.HourHeight;
  134. MWFCalendarWeekView.DayMsec = 3600 * 24 * 1000;
  135. MWFCalendarWeekView.WeekWidth;
  136. MWFCalendarWeekView.WeekMsec = MWFCalendarWeekView.DayMsec * 7;
  137. MWFCalendarWeekView.Calendar = new Class({
  138. Implements: [Events],
  139. initialize: function(view, date){
  140. this.view = view;
  141. this.css = this.view.css;
  142. this.container = this.view.node;
  143. this.app = this.view.app;
  144. this.weekBegin = this.app.calendarConfig.weekBegin || "0";
  145. this.baseDate = date || new Date();
  146. this.today = new Date();
  147. this.load();
  148. },
  149. load: function(){
  150. this.date = this.getWeekStartTime( this.baseDate );
  151. this.weekStartTime = new Date( this.date.get("year"), this.date.get("month"), this.date.get("date"), 0, 0, 0 );
  152. this.weekStartTimeStr = this.weekStartTime.format( "db" );
  153. var end = this.date.clone().increment( "day", 6 );
  154. this.weekEndTime = new Date( end.get("year"), end.get("month"), end.get("date"), 23, 59, 59 );
  155. this.weekEndTimeStr = this.weekEndTime.format( "db" );
  156. this.titleNode = new Element("div", {"styles": this.css.calendarTitleNode}).inject(this.container);
  157. this.titleTable = new Element("table.titleTable", {
  158. "styles": this.css.titleTable,
  159. "border": "0",
  160. "cellPadding": "0",
  161. "cellSpacing": "0"
  162. }).inject(this.container);
  163. this.scrollNode = new Element("div.scrollNode", {
  164. "styles": this.css.scrollNode //this.app.inContainer ? this.css.scrollNode_inContainer : this.css.scrollNode
  165. }).inject(this.container);
  166. this.contentWarpNode = new Element("div.contentWarpNode", {
  167. "styles": this.css.contentWarpNode
  168. }).inject(this.scrollNode);
  169. this.contentContainerNode = new Element("div.contentContainerNode",{
  170. "styles" : this.css.contentContainerNode
  171. }).inject(this.contentWarpNode);
  172. this.bodyNode = new Element("div.bodyNode", {
  173. "styles": this.css.contentNode
  174. }).inject(this.contentContainerNode);
  175. this.bodyNode.setStyle("position","relative");
  176. //this.bodyNode = new Element("div", {"styles": this.css.calendarBodyNode}).inject(this.container);
  177. this.setTitleNode();
  178. this.loadTitleTable();
  179. this.loadBodyTable();
  180. //this.resetBodySize();
  181. this.loadDataTable_WholeDay();
  182. this.loadDataTable();
  183. this.loadCalendar();
  184. //this.loadWholedayData( function(json){
  185. // this.loadWholeday(json);
  186. //}.bind(this));
  187. //
  188. //this.loadData( function( json ){
  189. // this.loadDataDay( json );
  190. //}.bind(this));
  191. //this.app.addEvent("resize", this.resetBodySize.bind(this));
  192. },
  193. getWeekStartTime: function( d ){
  194. var date = d.clone();
  195. //var week = date.getDay();
  196. //if( this.weekBegin == "1" ){
  197. // var decrementDay = ((week-1)<0) ? 6 : week-1;
  198. //}else{
  199. // var decrementDay = week;
  200. //}
  201. var decrementDay = ( 7 + d.getDay() - parseInt( this.weekBegin ) ) % 7;
  202. return date.decrement("day", decrementDay);
  203. },
  204. setTitleNode: function(){
  205. this.prevWeekNode = new Element("div.o2icon-triangle_left", {"styles": this.css.calendarPrevWeekNode}).inject(this.titleNode);
  206. // var text = this.baseDate.format(this.app.lp.dateFormatMonth)
  207. // + ",第" + this.view.getWeekNumber( this.baseDate ) + "周";
  208. var month = this.baseDate.format(this.app.lp.dateFormatMonth);
  209. var week = this.view.getWeekNumber( this.baseDate );
  210. var text = this.app.lp.weeklyViewTitle.replace("{month}", month ).replace("{week}",week);
  211. this.titleTextNode = new Element("div", {"styles": this.css.calendarTitleTextNode, "text": text}).inject(this.titleNode);
  212. this.nextWeekNode = new Element("div.o2icon-triangle_right", {"styles": this.css.calendarNextWeekNode}).inject(this.titleNode);
  213. this.prevWeekNode.addEvents({
  214. "mouseover": function(){
  215. this.prevWeekNode.setStyles(this.css.calendarPrevWeekNode_over);
  216. this.prevWeekNode.addClass("mainColor_color");
  217. }.bind(this),
  218. "mouseout": function(){
  219. this.prevWeekNode.setStyles(this.css.calendarPrevWeekNode);
  220. this.prevWeekNode.removeClass("mainColor_color");
  221. }.bind(this),
  222. "mousedown": function(){this.prevWeekNode.setStyles(this.css.calendarPrevWeekNode_down);}.bind(this),
  223. "mouseup": function(){this.prevWeekNode.setStyles(this.css.calendarPrevWeekNode_over);}.bind(this),
  224. "click": function(){this.changeWeekPrev();}.bind(this)
  225. });
  226. this.nextWeekNode.addEvents({
  227. "mouseover": function(){
  228. this.nextWeekNode.setStyles(this.css.calendarNextWeekNode_over);
  229. this.nextWeekNode.addClass("mainColor_color");
  230. }.bind(this),
  231. "mouseout": function(){
  232. this.nextWeekNode.setStyles(this.css.calendarNextWeekNode);
  233. this.nextWeekNode.removeClass("mainColor_color");
  234. }.bind(this),
  235. "mousedown": function(){this.nextWeekNode.setStyles(this.css.calendarNextWeekNode_down);}.bind(this),
  236. "mouseup": function(){this.nextWeekNode.setStyles(this.css.calendarNextWeekNode_over);}.bind(this),
  237. "click": function(){this.changeWeekNext();}.bind(this)
  238. });
  239. this.titleTextNode.addEvents({
  240. "mouseover": function(){
  241. this.titleTextNode.setStyles(this.css.calendarTitleTextNode_over);
  242. this.titleTextNode.addClass("mainColor_color");
  243. }.bind(this),
  244. "mouseout": function(){
  245. this.titleTextNode.setStyles(this.css.calendarTitleTextNode);
  246. this.titleTextNode.removeClass("mainColor_color");
  247. }.bind(this),
  248. "mousedown": function(){this.titleTextNode.setStyles(this.css.calendarTitleTextNode_down);}.bind(this),
  249. "mouseup": function(){this.titleTextNode.setStyles(this.css.calendarTitleTextNode_over);}.bind(this)
  250. //"click": function(){this.changeWeekSelect();}.bind(this)
  251. });
  252. this.createWeekSelector();
  253. },
  254. changeWeekPrev: function(){
  255. this.date.decrement("week", 1);
  256. this.baseDate = this.date;
  257. var month = this.baseDate.format(this.app.lp.dateFormatMonth);
  258. var week = this.view.getWeekNumber( this.baseDate );
  259. var text = this.app.lp.weeklyViewTitle.replace("{month}", month ).replace("{week}",week);
  260. this.titleTextNode.set("text", text);
  261. this.reLoadCalendar();
  262. },
  263. changeWeekNext: function(){
  264. this.date.increment("week", 1);
  265. this.baseDate = this.date;
  266. var month = this.baseDate.format(this.app.lp.dateFormatMonth);
  267. var week = this.view.getWeekNumber( this.baseDate );
  268. var text = this.app.lp.weeklyViewTitle.replace("{month}", month ).replace("{week}",week);
  269. this.titleTextNode.set("text", text);
  270. this.reLoadCalendar();
  271. },
  272. changeWeekSelect: function(){
  273. if (!this.monthSelector) this.createWeekSelector();
  274. //this.monthSelector.show();
  275. },
  276. createWeekSelector: function(){
  277. //this.monthSelector = new MWFCalendarWeekView.Calendar.WeekSelector(this.date, this);
  278. this.weekCalendar = new MWFCalendarWeekView.WeekCalendar(this.titleTextNode, {
  279. "style":"meeting_blue",
  280. "weekBegin" : this.weekBegin,
  281. "target": this.node,
  282. "baseDate" : this.baseDate,
  283. "onInit" : function(){
  284. this.options.dayPath = this.options.path+this.options.style+"/day_week.html";
  285. },
  286. "onQueryComplate": function(e, dv, date){
  287. var selectedDate = new Date.parse(dv);
  288. this.changeWeekTo(selectedDate);
  289. }.bind(this)
  290. });
  291. //this.weekCalendar.app = this.app;
  292. },
  293. changeWeekTo: function(d){
  294. this.baseDate = d;
  295. this.date = this.getWeekStartTime( d );
  296. //var text = this.date.format(this.app.lp.dateFormatWeek);
  297. //this.titleTextNode.set("text", text);
  298. var month = this.baseDate.format(this.app.lp.dateFormatMonth);
  299. var week = this.view.getWeekNumber( this.baseDate );
  300. var text = this.app.lp.weeklyViewTitle.replace("{month}", month ).replace("{week}",week);
  301. this.titleTextNode.set("text", text);
  302. this.reLoadCalendar();
  303. },
  304. isToday : function( d ){
  305. var today = new Date();
  306. if( today.get("year") != d.get("year") )return false;
  307. if( today.get("month") != d.get("month") )return false;
  308. if( today.get("date") != d.get("date") )return false;
  309. return true;
  310. },
  311. loadTitleTable: function(){
  312. var _self = this;
  313. if( !this.tableHead ){
  314. var d = this.date.clone();
  315. var head = this.tableHead = new Element("tr", {
  316. "styles" : this.css.calendarTableTitleTr
  317. }).inject( this.titleTable );
  318. new Element("th", {
  319. "styles" : this.css.calendarTableTh_hour
  320. }).inject(head);
  321. for( var i=0; i<7; i++ ) {
  322. var index = ( i + parseInt( this.weekBegin ) ) % 7;
  323. var th = new Element("th", {
  324. "styles": this.css.calendarTableTh,
  325. text: this.app.lp.weeks.arr[index] + "(" + d.format("%m.%d") + ")"
  326. }).inject(head);
  327. th.store("date", d.format("%Y-%m-%d") );
  328. th.addEvent( "click", function(){
  329. if( _self.app.topMenuNodeMap['toDay'] ){
  330. _self.app.switchTopMenuNode( _self.app.topMenuNodeMap['toDay'] );
  331. }
  332. _self.app.toDay( this.retrieve("date") )
  333. }.bind(th));
  334. d.increment("day", 1);
  335. }
  336. var tr = this.wholeDayTr = new Element( "tr").inject( this.titleTable );
  337. var td = new Element( "td.calendarTableCell", {
  338. "tdType" : "hour",
  339. styles : this.css.calendarTableCell_hour
  340. } ).inject( tr );
  341. td.setStyle("min-height","80px");
  342. var node = new Element("div",{
  343. text : "全天"
  344. }).inject( td );
  345. td.store("hour",i );
  346. this.wholeDayTdMap = {};
  347. var d = this.date.clone();
  348. for( var j=0; j<7; j++ ){
  349. td = this.wholeDayTdMap[j] = new Element( "td" , {
  350. "tdType" : "calendar",
  351. "styles" : this.css.calendarTableCell,
  352. "index" : j+1
  353. }).inject( tr );
  354. td.store("dateStr",d.format("%Y-%m-%d"));
  355. td.store("index",j);
  356. td.addEvent("click", function(ev){
  357. this.setCurrentTd( ev.target );
  358. }.bind(this));
  359. td.addEvent("dblclick", function(ev){
  360. this.cancelCurrentTd();
  361. var form = new MWF.xApplication.Calendar.EventForm(this,{}, {
  362. startTime : Date.parse( ev.target.retrieve("dateStr")) ,
  363. endTime : Date.parse( ev.target.retrieve("dateStr")),
  364. isWholeday : true
  365. }, {app:this.app});
  366. form.view = this;
  367. form.create();
  368. }.bind(this));
  369. new Drag(td, {
  370. "onStart": function(dragged, e){
  371. this.cancelCurrentTd();
  372. this.cellDragStart_wholeDay(dragged, e);
  373. }.bind(this),
  374. "onDrag": function(dragged, e){
  375. this.cellDrag_wholeDay(dragged, e);
  376. }.bind(this),
  377. "onComplete": function(dragged, e){
  378. this.completeDrag_wholeDay(dragged, e);
  379. }.bind(this)
  380. });
  381. d.increment("day", 1);
  382. }
  383. }else{
  384. var d = this.date.clone();
  385. this.tableHead.getElements("th").each( function( th, i ){
  386. if( i == 0 )return;
  387. var index = ( i - 1 + parseInt( this.weekBegin ) ) % 7;
  388. th.set("text", this.app.lp.weeks.arr[index] + "(" + d.format("%d") + ")");
  389. th.store("date", d.format("%Y-%m-%d") );
  390. d.increment("day", 1);
  391. }.bind(this));
  392. var d = this.date.clone();
  393. Object.each( this.wholeDayTdMap, function( td, i ){
  394. td.store("dateStr",d.format("%Y-%m-%d"));
  395. d.increment("day", 1);
  396. }.bind(this))
  397. }
  398. },
  399. loadBodyTable: function(){
  400. this.calendarTable = new Element("table.dragTable", {
  401. "styles": this.css.calendarTable,
  402. "height": "100%",
  403. "border": "0",
  404. "cellPadding": "0",
  405. "cellSpacing": "0"
  406. }).inject(this.bodyNode);
  407. this.hourTdMap = {};
  408. this.hourTrMap = {};
  409. for( var i=0; i<24; i++ ){
  410. var tr = new Element( "tr").inject( this.calendarTable );
  411. var td = new Element( "td.calendarTableCell", {
  412. "tdType" : "hour",
  413. styles : this.css.calendarTableCell_hour,
  414. valign : "top"
  415. } ).inject( tr );
  416. var node = new Element("div",{
  417. text : i + ":00"
  418. }).inject( td );
  419. td.store("hour",i );
  420. for( var j=0; j<7; j++ ){
  421. if( !this.hourTdMap[j] )this.hourTdMap[j] = {};
  422. var td = this.hourTdMap[j][i] = new Element( "td" , {
  423. "tdType" : "calendar",
  424. "styles" : this.css.calendarTableCell,
  425. "index" : j+1
  426. }).inject( tr );
  427. td.store("hour",i );
  428. td.store("index",j );
  429. td.addEvent("click", function(ev){
  430. this.setCurrentTd( ev.target );
  431. }.bind(this));
  432. td.addEvent("dblclick", function(ev){
  433. this.cancelCurrentTd();
  434. var hour = ev.target.retrieve("hour");
  435. var index = ev.target.retrieve("index");
  436. var dateStr = this.getDateByIndex( index );
  437. var form = new MWF.xApplication.Calendar.EventForm(this,{}, {
  438. startTime : Date.parse( dateStr + " " +hour+":00") ,
  439. endTime : Date.parse( dateStr + " " + (hour+1)+":00" )
  440. }, {app:this.app});
  441. form.view = this;
  442. form.create();
  443. }.bind(this));
  444. new Drag(td, {
  445. "onStart": function(dragged, e){
  446. this.cancelCurrentTd();
  447. this.cellDragStart(dragged, e);
  448. }.bind(this),
  449. "onDrag": function(dragged, e){
  450. this.cellDrag(dragged, e);
  451. }.bind(this),
  452. "onComplete": function(dragged, e){
  453. this.completeDrag(dragged, e);
  454. }.bind(this)
  455. });
  456. }
  457. this.hourTrMap[ i ] = tr;
  458. }
  459. },
  460. setCurrentTd : function(td){
  461. td.setStyle("background-color","#fffdf2");
  462. if( this.currentSelectedTd ){
  463. var flag = this.currentSelectedTd.retrieve("index") == this.todayIndex;
  464. this.currentSelectedTd.setStyle("background-color",flag?"#F8FBFF":"#fff");
  465. }
  466. this.currentSelectedTd = td;
  467. },
  468. cancelCurrentTd : function(){
  469. if( this.currentSelectedTd ){
  470. var flag = this.currentSelectedTd.retrieve("index") == this.todayIndex;
  471. this.currentSelectedTd.setStyle("background-color",flag?"#F8FBFF":"#fff");
  472. }
  473. this.currentSelectedTd = null;
  474. },
  475. reLoadCalendar: function(){
  476. this.weekStartTime = new Date( this.date.get("year"), this.date.get("month"), this.date.get("date"), 0, 0, 0 );
  477. this.weekStartTimeStr = this.weekStartTime.format( "db" );
  478. var end = this.date.clone().increment( "day", 6 );
  479. this.weekEndTime = new Date( end.get("year"), end.get("month"), end.get("date"), 23, 59, 59 );
  480. this.weekEndTimeStr = this.weekEndTime.format( "db" );
  481. Object.each(this.dayMap || {}, function(day){
  482. day.destroy();
  483. }.bind(this));
  484. this.dayMap = {};
  485. if( this.wholeday ){
  486. this.wholeday.destroy();
  487. }
  488. this.wholeday = null;
  489. //this.calendarTable.getElements("td[tdType='calendar']").each( function(td){
  490. // td.empty();
  491. //}.bind(this));
  492. this.loadTitleTable();
  493. this.loadCalendar();
  494. },
  495. loadCalendar : function(){
  496. this.app.currentDate = this.baseDate.clone();
  497. this.dateIndexMap = null;
  498. this.titleTable.getElement("td:nth-child(1)").setStyle("height", "auto") ;
  499. this.loadData( function(){
  500. this.loadWholeday(this.wholeDayData);
  501. this.loadDataDay( this.inOneDayEvents );
  502. this.resetBodySize();
  503. this.setTodayTds();
  504. this.cancelCurrentTd();
  505. }.bind(this));
  506. },
  507. setTodayTds : function(){
  508. var now = new Date();
  509. var index = this.todayIndex = this.getDateNumOfWeek( now.format("%Y-%m-%d") );
  510. var hour = now.get("hours");
  511. var mintues = now.get("minutes");
  512. mintues = mintues < 2 ? 2 : mintues;
  513. if( index > -1 ){
  514. this.todayTds = [];
  515. var td = this.wholeDayTdMap[index];
  516. td.setStyle("background-color","#f8fbff");
  517. this.todayTds.push( td );
  518. var tds = this.hourTdMap[index];
  519. Object.each( tds, function( td, i ){
  520. td.setStyle("background-color","#f8fbff");
  521. this.todayTds.push( td );
  522. }.bind(this));
  523. this.nowTd = tds[hour];
  524. var nowTdPosition = this.nowTd.getPosition(this.bodyNode);
  525. this.nowTimeNode = new Element("div",{
  526. styles : {
  527. "position" : "absolute",
  528. "left" : nowTdPosition.x,
  529. "top" : nowTdPosition.y + ( (mintues - 2) / 60 ) * MWFCalendarWeekView.HourHeight,
  530. "height" : "2px",
  531. "width" : MWFCalendarWeekView.DayWidth,
  532. "background-color" : "#ff3333"
  533. }
  534. }).inject(this.bodyNode);
  535. }else if( this.todayTds && this.todayTds.length ){
  536. while( this.todayTds.length > 0 ){
  537. this.todayTds.pop().setStyle("background-color","#fff");
  538. }
  539. if(this.nowTd)this.nowTd = null;
  540. if( this.nowTimeNode )this.nowTimeNode.destroy();
  541. }
  542. },
  543. loadData : function( callback ){
  544. this.app.actions.listEventWithFilter( {
  545. calendarIds : this.app.getSelectedCalendarId(),
  546. startTime : this.weekStartTimeStr,
  547. endTime : this.weekEndTimeStr //,
  548. //createPerson : this.app.userName
  549. }, function(json){
  550. this.wholeDayData = ( json.data && json.data.wholeDayEvents ) ? json.data.wholeDayEvents : [];
  551. this.inOneDayEvents = [];
  552. (( json.data && json.data.inOneDayEvents) ? json.data.inOneDayEvents : []).each( function( d ){
  553. if(d.inOneDayEvents.length > 0 ){
  554. this.inOneDayEvents.push( d );
  555. }
  556. }.bind(this));
  557. //json.data.each( function(d){
  558. // var flag = false;
  559. // if( d.isAllDayEvent ){
  560. // flag = true;
  561. // }else if( d.startTime.split(" ")[0] != d.endTime.split(" ")[0] ){
  562. // flag = true;
  563. // }
  564. // if( flag ){
  565. // this.wholeDayData.push( d )
  566. // }else{
  567. // this.inOneDayEvents.push(d)
  568. // }
  569. //}.bind(this));
  570. if(callback)callback();
  571. }.bind(this))
  572. },
  573. loadDataTable_WholeDay: function( json ){
  574. this.dataTable_WholeDay = new Element("table.dataTable", {
  575. "styles": this.css.calendarTable,
  576. "border": "0",
  577. "cellPadding": "0",
  578. "height" : "0",
  579. "cellSpacing": "0"
  580. }).inject(this.container);
  581. this.dataTable_WholeDay.setStyles({
  582. "display" : "none",
  583. "position":"absolute",
  584. "top" : "92px",
  585. "left" : "0px",
  586. "margin": "0px auto 0px 12px"
  587. });
  588. var tr = new Element("tr").inject(this.dataTable_WholeDay);
  589. new Element( "td" , {
  590. "styles" : {"height":"0px" ,"position":"relative"}
  591. }).inject( tr );
  592. this.dataTd_WholeDay = new Element( "td" , {
  593. "valign" : "top",
  594. "styles" : {"height":"0px","position":"relative"}
  595. }).inject( tr );
  596. //this.dataTdMap_WholeDay = {};
  597. //for( var i=0 ;i <7; i++ ){
  598. // this.dataTdMap_WholeDay[ i ] = new Element( "td" , {
  599. // "valign" : "top",
  600. // "styles" : {"height":"0px"}, //,"position":"relative"},
  601. // "index" : i
  602. // }).inject( tr );
  603. //}
  604. },
  605. loadWholeday : function( data ){
  606. this.wholeday = new MWFCalendarWeekView.Calendar.WholeDay( this, data, this.date);
  607. },
  608. loadDataTable: function( json ){
  609. this.dataTable = new Element("table.dataTable", {
  610. "styles": this.css.calendarTable,
  611. "height": "0",
  612. "border": "0",
  613. "cellPadding": "0",
  614. "cellSpacing": "0"
  615. }).inject(this.bodyNode);
  616. this.dataTable.setStyles({
  617. "position":"absolute",
  618. "top" : "0px",
  619. "left" : "0px"
  620. });
  621. var tr = new Element("tr").inject(this.dataTable);
  622. new Element( "td" , {
  623. "styles" : {"height":"0px","position":"relative"}
  624. }).inject( tr );
  625. this.dataTdMap = {};
  626. for( var i=0 ;i <7; i++ ){
  627. this.dataTdMap[ i ] = new Element( "td" , {
  628. "styles" : {"height":"0px","position":"relative"},
  629. "index" : i
  630. }).inject( tr );
  631. }
  632. },
  633. getDateIndexMap : function(){
  634. if( !this.dateIndexMap ){
  635. var date = this.getWeekStartTime( this.baseDate );
  636. this.dateIndexMap = {};
  637. for( var i=0 ;i <7; i++ ){
  638. var dateStr = date.format("%Y-%m-%d");
  639. this.dateIndexMap[ dateStr ] = i;
  640. date.increment();
  641. }
  642. }
  643. return this.dateIndexMap;
  644. },
  645. getDateByIndex : function( index ){
  646. var dateStr;
  647. var dateIndexMap = this.getDateIndexMap();
  648. for( var key in dateIndexMap ){
  649. if( dateIndexMap[key] == index ){
  650. return key;
  651. }
  652. }
  653. },
  654. getDateNumOfWeek : function( dateString ){
  655. var dateIndexMap = this.getDateIndexMap();
  656. return this.dateIndexMap[ dateString ]
  657. },
  658. loadDataDay : function(data){
  659. this.dayMap = {};
  660. data.each( function(d){
  661. var key = d.eventDate;
  662. var container = this.dataTdMap[ this.getDateNumOfWeek( key ) ];
  663. if(container){
  664. this.loadDay(container, key, d.inOneDayEvents );
  665. }
  666. }.bind(this));
  667. //var dataMap = {};
  668. //data.each( function( d ){
  669. // var date2 = Date.parse( d.startTime );
  670. // var dateStr = date2.format("%Y-%m-%d");
  671. // if( !dataMap[dateStr] )dataMap[dateStr] = [];
  672. // dataMap[dateStr].push( d )
  673. //}.bind(this));
  674. //
  675. //for( var key in dataMap ){
  676. // var container = this.dataTdMap[ this.getDateNumOfWeek( key ) ];
  677. // if(container){
  678. // this.loadDay(container, key, dataMap[key]);
  679. // }
  680. //}
  681. },
  682. loadDay: function( container, dateStr, array){
  683. var date = Date.parse(dateStr);
  684. var m = date.get("month");
  685. var y = date.get("year");
  686. var d = date.get("date");
  687. var startTime = new Date( y, m, d, 0, 0, 0 );
  688. var endTime = new Date( y, m, d, 23, 59, 59 );
  689. //if(dayArray.length>0){
  690. this.dayMap[dateStr] = new MWFCalendarWeekView.Calendar.Day( container, date, this, array);
  691. //}
  692. //if(wholeDayArray.length>0){
  693. // this.wholedayMap[dateStr] = new MWFCalendarWeekView.Calendar.WholeDay( container, date, this, type, wholeDayArray);
  694. //}
  695. },
  696. resetBodySize: function(){
  697. //if( this.app.inContainer )return;
  698. var size = this.container.getSize();
  699. var titleSize = this.titleNode.getSize();
  700. var titleTableSize = this.titleTable.getSize();
  701. var y = size.y-titleSize.y-titleTableSize.y;
  702. //this.bodyNode.setStyle("height", ""+y+"px");
  703. if (this.contentWarpNode){
  704. this.contentWarpNode.setStyles({
  705. "width": (size.x - 40) +"px"
  706. });
  707. }
  708. this.scrollNode.setStyle("height", ""+y+"px");
  709. var hourTdX = 60;
  710. MWFCalendarWeekView.WeekWidth = size.x - 40 - hourTdX;
  711. var tdX = MWFCalendarWeekView.DayWidth = Math.floor( (MWFCalendarWeekView.WeekWidth-8) / 7);
  712. if(this.calendarTable){
  713. this.calendarTable.setStyles({
  714. "width": (size.x - 40) +"px"
  715. });
  716. var tr =this.calendarTable.getElement("tr:nth-child(1)");
  717. tr.getElements("td").each( function( td, i ){
  718. td.setStyle("width", (i==0 ? hourTdX : tdX )+"px");
  719. })
  720. }
  721. if(this.titleTable){
  722. this.titleTable.setStyles({
  723. "width": (size.x - 40) +"px"
  724. });
  725. var tr =this.titleTable.getElement("tr:nth-child(1)");
  726. tr.getElements("th").each( function( td, i ){
  727. td.setStyle("width", (i==0 ? hourTdX : tdX )+"px");
  728. })
  729. }
  730. if( this.dataTable ){
  731. this.dataTable.setStyles({
  732. "width": (size.x - 40) +"px"
  733. });
  734. var tr =this.dataTable.getElement("tr:nth-child(1)");
  735. tr.getElements("td").each( function( td, i ){
  736. td.setStyle("width", (i==0 ? hourTdX : tdX )+"px");
  737. })
  738. }
  739. for( var key in this.dayMap ){
  740. this.dayMap[key].resize();
  741. }
  742. if( this.dataTable_WholeDay ){
  743. this.dataTable_WholeDay.setStyles({
  744. "width": (size.x - 40) +"px"
  745. });
  746. var tr =this.dataTable_WholeDay.getElement("tr:nth-child(1)");
  747. var contendTdWidth = size.x - 40 - hourTdX - 2;
  748. tr.getElements("td").each( function( td, i ){
  749. td.setStyle("width", (i==0 ? hourTdX : contendTdWidth )+"px");
  750. })
  751. }
  752. if(this.wholeday)this.wholeday.resize();
  753. if(this.nowTimeNode){
  754. this.nowTimeNode.setStyle("width",tdX);
  755. if(this.nowTd)this.nowTimeNode.setStyle("left", this.nowTd.getPosition(this.bodyNode).x );
  756. }
  757. //for( var key in this.dayMap_WholeDay ){
  758. // this.dayMap_WholeDay[key].resize();
  759. //}
  760. },
  761. reload : function(){
  762. this.view.reload();
  763. },
  764. destroy: function(){
  765. Object.each(this.dayMap || {}, function(day){
  766. day.destroy();
  767. }.bind(this));
  768. if( this.wholeday ){
  769. this.wholeday.destroy();
  770. }
  771. this.container.empty();
  772. },
  773. getIndexByPage: function( page ){
  774. var pos = this.calendarTable.getPosition();
  775. if( !this.calendarTableFirstTd ){
  776. this.calendarTableFirstTd = this.calendarTable.getElement("td");
  777. }
  778. pos.x = pos.x + this.calendarTableFirstTd.getSize().x;
  779. var col = (page.x - pos.x ) / (MWFCalendarWeekView.DayWidth + 1);
  780. if( col < 0 || col > 7 )return null;
  781. this.pageOffsetHeight = page.y - pos.y;
  782. var row = ( page.y - pos.y ) / MWFCalendarWeekView.HourHeight;
  783. if( row < 0 || row > 24 )return null;
  784. return {
  785. row : Math.floor(row),
  786. col : Math.floor(col)
  787. }
  788. },
  789. getIndexListByRange : function( index1, index2 ){
  790. var minIndex, maxIndex;
  791. if( index1.col == index2.col ){
  792. if( index1.row <= index2.row ){
  793. minIndex = index1;
  794. maxIndex = index2;
  795. }else{
  796. minIndex = index2;
  797. maxIndex = index1;
  798. }
  799. }else if( index1.col < index2.col ){
  800. minIndex = index1;
  801. maxIndex = index2;
  802. }else{
  803. minIndex = index2;
  804. maxIndex = index1;
  805. }
  806. var beginRow, endRow;
  807. var result = [];
  808. for( var i = minIndex.col; i<=maxIndex.col; i++ ){
  809. beginRow = i == minIndex.col ? minIndex.row : 0;
  810. endRow = i == maxIndex.col ? maxIndex.row : 23;
  811. for( var j = beginRow; j<= endRow; j++ ){
  812. result.push( i+"_"+j );
  813. }
  814. }
  815. return result;
  816. },
  817. getTdByIndexString : function(index){
  818. var indexList = index.split("_");
  819. var col = indexList[0];
  820. var row = indexList[1];
  821. return this.hourTdMap[col][row];
  822. },
  823. cellDragStart: function(td, e){
  824. td.store("index", this.getIndexByPage(e.page ) );
  825. this.scrollNodeHeight = this.scrollNode.getSize().y;
  826. },
  827. cellDrag: function(td, e){
  828. var orgIndex = td.retrieve( "index" );
  829. var curIndex = this.getIndexByPage( e.page );
  830. if( !curIndex )return;
  831. var indexs = this.getIndexListByRange( orgIndex, curIndex );
  832. var flag = this.todayIndex > -1;
  833. if( this.selectedIndexRange ){
  834. var oldIndex = this.selectedIndexRange;
  835. this.selectedIndexRange.each( function( index ){
  836. if( !indexs.contains(index) ){
  837. this.getTdByIndexString( index ).setStyle("background-color", flag && ( index.split("_")[0] ==this.todayIndex ) ? "#F8FBFF" : "#fff");
  838. }
  839. }.bind(this));
  840. indexs.each( function( index ){
  841. if( !this.selectedIndexRange.contains(index) ){
  842. this.getTdByIndexString( index ).setStyle("background-color", "#fffdf2")
  843. }
  844. }.bind(this))
  845. }else{
  846. for( var i=0; i<indexs.length; i++ ){
  847. this.getTdByIndexString( indexs[i] ).setStyle("background-color", "#fffdf2")
  848. }
  849. }
  850. this.selectedIndexRange = indexs;
  851. var scrollNodeTop = this.scrollNode.getScroll().y;
  852. if(( this.pageOffsetHeight + MWFCalendarWeekView.HourHeight * 1.5) > ( this.scrollNodeHeight + scrollNodeTop )){
  853. window.setTimeout( function(){
  854. this.scrollNode.scrollTo(0, scrollNodeTop + MWFCalendarWeekView.HourHeight )
  855. }.bind(this), 200)
  856. }else if( this.pageOffsetHeight - MWFCalendarWeekView.HourHeight * 1.5 < scrollNodeTop ){
  857. window.setTimeout( function(){
  858. this.scrollNode.scrollTo(0, scrollNodeTop - MWFCalendarWeekView.HourHeight )
  859. }.bind(this), 200)
  860. }
  861. },
  862. completeDrag: function(td, e){
  863. var flag = this.todayIndex > -1;
  864. if( this.selectedIndexRange && this.selectedIndexRange.length ){
  865. this.selectedIndexRange.each( function( index ){
  866. this.getTdByIndexString( index ).setStyle("background-color", flag && ( index.split("_")[0] ==this.todayIndex ) ? "#F8FBFF" : "#fff");
  867. }.bind(this));
  868. var beginIndex = this.selectedIndexRange[0].split("_");
  869. var endIndex = this.selectedIndexRange.getLast().split("_");
  870. var beginTime = this.getDateByIndex( beginIndex[0] ) + " " + beginIndex[1]+":00";
  871. var endTime = this.getDateByIndex( endIndex[0] ) + " " + endIndex[1]+":59";
  872. var form = new MWF.xApplication.Calendar.EventForm(this,{}, {
  873. startTime : beginTime ,
  874. endTime : endTime
  875. }, {app:this.app});
  876. form.view = this;
  877. form.create();
  878. this.selectedIndexRange = null;
  879. }
  880. },
  881. getIndexByPage_wholeDay: function( page ){
  882. var pos = this.wholeDayTr.getPosition();
  883. if( !this.wholeDayFirstTd ){
  884. this.wholeDayFirstTd = this.wholeDayTr.getElement("td");
  885. }
  886. pos.x = pos.x + this.wholeDayFirstTd.getSize().x;
  887. var col = (page.x - pos.x ) / (MWFCalendarWeekView.DayWidth + 1);
  888. if( col < 0 || col > 7 )return null;
  889. return Math.floor(col);
  890. },
  891. getIndexListByRange_wholeDay : function( index1, index2 ){
  892. var minIndex = Math.min( index1, index2 );
  893. var maxIndex = Math.max( index1, index2 );
  894. var result = [];
  895. for( var i = minIndex; i<=maxIndex; i++ ){
  896. result.push( i );
  897. }
  898. return result;
  899. },
  900. cellDragStart_wholeDay: function(td, e){
  901. td.store("index", this.getIndexByPage_wholeDay(e.page ) );
  902. },
  903. cellDrag_wholeDay: function(td, e){
  904. var orgIndex = td.retrieve( "index" );
  905. var curIndex = this.getIndexByPage_wholeDay( e.page );
  906. if( !curIndex )return;
  907. var indexs = this.getIndexListByRange_wholeDay( orgIndex, curIndex );
  908. var flag = this.todayIndex > -1;
  909. if( this.selectedIndexRange_wholeDay ){
  910. var oldIndex = this.selectedIndexRange_wholeDay;
  911. this.selectedIndexRange_wholeDay.each( function( index ){
  912. if( !indexs.contains(index) ){
  913. this.wholeDayTdMap[index].setStyle("background-color", flag && ( index == this.todayIndex ) ? "#F8FBFF" : "#fff");
  914. }
  915. }.bind(this));
  916. indexs.each( function( index ){
  917. if( !this.selectedIndexRange_wholeDay.contains(index) ){
  918. this.wholeDayTdMap[index].setStyle("background-color", "#fffdf2")
  919. }
  920. }.bind(this))
  921. }else{
  922. for( var i=0; i<indexs.length; i++ ){
  923. this.wholeDayTdMap[indexs[i]].setStyle("background-color", "#fffdf2")
  924. }
  925. }
  926. this.selectedIndexRange_wholeDay = indexs;
  927. },
  928. completeDrag_wholeDay: function(td, e){
  929. var flag = this.todayIndex > -1;
  930. if( this.selectedIndexRange_wholeDay && this.selectedIndexRange_wholeDay.length ){
  931. this.selectedIndexRange_wholeDay.each( function( index ){
  932. this.wholeDayTdMap[index].setStyle("background-color", flag && ( index == this.todayIndex ) ? "#F8FBFF" : "#fff");
  933. }.bind(this));
  934. var beginIndex = this.selectedIndexRange_wholeDay[0];
  935. var endIndex = this.selectedIndexRange_wholeDay.getLast();
  936. var beginTime = this.getDateByIndex( beginIndex );
  937. var endTime = this.getDateByIndex( endIndex );
  938. var form = new MWF.xApplication.Calendar.EventForm(this,{}, {
  939. startTime : beginTime ,
  940. endTime : endTime,
  941. isWholeday : true
  942. }, {app:this.app});
  943. form.view = this;
  944. form.create();
  945. this.selectedIndexRange_wholeDay = null;
  946. }
  947. }
  948. });
  949. MWFCalendarWeekView.Calendar.WholeDay = new Class({
  950. Implements: [Events],
  951. initialize: function( calendar, data, date){
  952. this.calendar = calendar;
  953. this.view = this.calendar.view;
  954. this.css = this.calendar.css;
  955. this.app = this.calendar.app;
  956. this.date = date.clone();
  957. this.data = data;
  958. this.load();
  959. },
  960. load: function(){
  961. //var start = this.calendar.getWeekStartTime( this.date );
  962. //this.weekStartTime = new Date( start.get("year"), start.get("month"), start.get("date"), 0, 0, 0 );
  963. //
  964. //var end = start.clone().increment( "day", 6 );
  965. //this.weekEndTime = new Date( end.get("year"), end.get("month"), end.get("date"), 23, 59, 59 );
  966. this.weekStartTime = this.calendar.weekStartTime;
  967. this.weekEndTime = this.calendar.weekEndTime;
  968. this.rangeList = [];
  969. this.rangeObject = {};
  970. this.data.each( function(d , i){
  971. var range = this.getTimeRange( d.startTime, d.endTime );
  972. if( !range )return null;
  973. d.range = range;
  974. d.range.id = d.id;
  975. d.range.data = d;
  976. this.rangeList.push( range );
  977. this.rangeObject[d.id] = range;
  978. }.bind(this));
  979. var itemCountOfDay = {};
  980. this.rangeList.each( function(r){
  981. r.days.each( function( d ){
  982. itemCountOfDay[ d ] = itemCountOfDay[ d ] ? itemCountOfDay[ d ]+1 : 1
  983. }.bind(this))
  984. }.bind(this));
  985. this.maxDayLength = 0;
  986. for(var key in itemCountOfDay){
  987. if( itemCountOfDay[key] > this.maxDayLength )this.maxDayLength = itemCountOfDay[key];
  988. }
  989. if( this.maxDayLength ){
  990. this.calendar.titleTable.getElement("td:nth-child(1)").setStyle("height", 24*this.maxDayLength);
  991. }
  992. this.usefulTdFlagArray = [];
  993. for( var i=0; i<this.maxDayLength; i++ ){
  994. var array = [];
  995. for( var j=0; j<7; j++ ){
  996. array.push( true );
  997. }
  998. this.usefulTdFlagArray.push( array )
  999. }
  1000. this.sortRange();
  1001. //this.getIntersectedPath2();
  1002. this.documentList = [];
  1003. this.rangeList.each( function(r , i){
  1004. var d = r.data;
  1005. if( !d )return null;
  1006. this.documentList.push( new MWFCalendarWeekView.Calendar.WholeDayDocument( this, d, r ) );
  1007. }.bind(this))
  1008. },
  1009. sortRange : function(){
  1010. this.rangeList.sort( function( range1, range2 ){
  1011. if( range1.days[0] > range2.days[0] )return 1;
  1012. if( range1.days[0] < range2.days[0] )return -1;
  1013. return range2.diff - range1.diff;
  1014. }.bind(this));
  1015. },
  1016. getTimeRange : function( startTime, endTime ){
  1017. var start = Date.parse(startTime );
  1018. var end = Date.parse(endTime );
  1019. if( end < this.weekStartTime || start > this.weekEndDate )return null;
  1020. if( start < this.weekStartTime )start = this.weekStartTime.clone();
  1021. if( this.weekEndTime < end )end = this.weekEndTime.clone();
  1022. var end = new Date( end.get("year"), end.get("month"), end.get("date"), 23, 59, 59 );
  1023. var days = [];
  1024. while( start < end ){
  1025. days.push( start.clone().format("%Y-%m-%d") );
  1026. start.increment()
  1027. }
  1028. start = Date.parse(startTime );
  1029. end = Date.parse(endTime );
  1030. return {
  1031. start : start,
  1032. end : end,
  1033. days : days,
  1034. diff : end - start
  1035. }
  1036. },
  1037. resize: function(){
  1038. if(!this.documentList)return;
  1039. this.documentList.each( function(d){
  1040. d.resize();
  1041. }.bind(this))
  1042. },
  1043. destroy : function(){
  1044. if(!this.documentList)return;
  1045. while( this.documentList.length ){
  1046. this.documentList.pop().destroy()
  1047. }
  1048. this.calendar.dataTd_WholeDay.empty();
  1049. }
  1050. });
  1051. MWFCalendarWeekView.Calendar.WholeDayDocument = new Class({
  1052. initialize: function(day, data, range ){
  1053. this.day = day;
  1054. this.calendar = day.calendar;
  1055. this.view = this.calendar.view;
  1056. this.css = this.calendar.css;
  1057. this.app = this.calendar.app;
  1058. this.date = day.date.clone();
  1059. this.data = data;
  1060. this.range = range;
  1061. this.load();
  1062. },
  1063. load: function(){
  1064. this.container = this.calendar.dataTd_WholeDay;
  1065. var items = this.items = [];
  1066. this.data.timeStart = Date.parse( this.data.startTime );
  1067. this.data.timeEnd = Date.parse( this.data.endTime );
  1068. this.getUsefulTdYIndex();
  1069. this.createNode();
  1070. //this.range.days.each( function( d, i ){
  1071. // items.push( new MWFCalendarWeekView.Calendar.WholeDayItem( this, d, i ) )
  1072. //}.bind(this))
  1073. },
  1074. getUsefulTdYIndex : function(){
  1075. for( var i=0; i<this.day.maxDayLength; i++ ){
  1076. var flag = true;
  1077. for( var j=0; j<this.range.days.length; j++ ){
  1078. var idx = this.calendar.getDateNumOfWeek( this.range.days[j] );
  1079. if( !this.day.usefulTdFlagArray[i][idx] ){
  1080. flag = false;
  1081. break;
  1082. }
  1083. }
  1084. if( flag ){
  1085. this.yIndex = i;
  1086. for( var j=0; j<this.range.days.length; j++ ){
  1087. var idx = this.calendar.getDateNumOfWeek( this.range.days[j] );
  1088. this.day.usefulTdFlagArray[i][idx] = false;
  1089. }
  1090. break;
  1091. }
  1092. }
  1093. },
  1094. createNode : function(){
  1095. var lightColor = this.lightColor = MWFCalendar.ColorOptions.getLightColor( this.data.color );
  1096. var node = this.node = new Element("div",{
  1097. styles : {
  1098. position : "absolute",
  1099. //border : "1px solid #cae2ff",
  1100. "background-color" : "#cae2ff",
  1101. "overflow" : "hidden",
  1102. "height" : "20px",
  1103. "line-height" : "20px",
  1104. "border-top" : "1px solid " + lightColor,
  1105. "border-bottom" : "1px solid " + lightColor
  1106. },
  1107. events : {
  1108. click : function(){
  1109. var form = new MWF.xApplication.Calendar.EventForm(this, this.data, {
  1110. isFull : true
  1111. }, {app:this.app});
  1112. form.view = this.view;
  1113. this.calendar.app.isEventEditable(this.data) ? form.edit() : form.open();
  1114. }.bind(this),
  1115. "mouseover" : function () {
  1116. this.node.setStyle("border-color", this.data.color );
  1117. }.bind(this),
  1118. "mouseout" : function () {
  1119. this.node.setStyle("border-color", this.lightColor );
  1120. }.bind(this)
  1121. }
  1122. }).inject( this.container );
  1123. node.setStyles(this.getCoordinate());
  1124. if( !this.startTimeOutRange ){
  1125. node.setStyles({
  1126. "border-left" : "1px solid " + lightColor,
  1127. "border-top-left-radius" : "10px",
  1128. "border-bottom-left-radius" : "10px"
  1129. })
  1130. }
  1131. if( !this.endTimeOutRange ){
  1132. node.setStyles({
  1133. "border-right" : "1px solid " + lightColor,
  1134. "border-top-right-radius" : "10px",
  1135. "border-bottom-right-radius" : "10px"
  1136. })
  1137. }
  1138. //if( this.isFirst ){
  1139. var timeNode = new Element("div",{
  1140. styles : {
  1141. "font-size" : "10px",
  1142. "padding-left" : "2px",
  1143. "float" : "left"
  1144. },
  1145. text : this.data.timeStart.format("%m-%d %H:%M") + MWF.xApplication.Calendar.LP.to + this.data.timeEnd.format("%m-%d %H:%M")
  1146. }).inject( node );
  1147. var titleNode = new Element("div",{
  1148. styles : {
  1149. "padding-left" : "5px",
  1150. "font-size" : "12px",
  1151. "float" : "left"
  1152. },
  1153. text : this.data.title
  1154. }).inject( node );
  1155. //}
  1156. this.tooltip = new MWF.xApplication.Calendar.EventTooltip(this.app.content, this.node, this.app, this.data, {
  1157. axis : "y", "delay" : 350
  1158. });
  1159. this.tooltip.view = this.view;
  1160. },
  1161. getCoordinate : function(){
  1162. var data = this.data;
  1163. var range = this.range;
  1164. var top = this.yIndex * 24;
  1165. var timeStart = this.day.weekStartTime;
  1166. var timeEnd = this.day.weekEndTime;
  1167. if( this.data.timeStart < timeStart ){
  1168. this.startTimeOutRange = true;
  1169. }else{
  1170. this.startTimeOutRange = false;
  1171. timeStart = this.data.timeStart;
  1172. }
  1173. if( this.data.timeEnd > timeEnd ){
  1174. this.endTimeOutRange = true;
  1175. }else{
  1176. this.endTimeOutRange = false;
  1177. timeEnd = this.data.timeEnd;
  1178. }
  1179. var diff = timeEnd - timeStart;
  1180. var width = ( diff / MWFCalendarWeekView.WeekMsec ) * MWFCalendarWeekView.WeekWidth - 2;
  1181. var left = ( ( timeStart - this.day.weekStartTime ) / MWFCalendarWeekView.WeekMsec ) * MWFCalendarWeekView.WeekWidth + 3;
  1182. //var marginRight = ( ( this.weekTimeEnd - timeEnd ) / MWFCalendarWeekView.DayMsec) * MWFCalendarWeekView.DayWidth;
  1183. return {
  1184. top : top + 2,
  1185. left : left,
  1186. width : width // + ( !this.isLast ? 2 : -2 ) //,
  1187. //"margin-left" : marginLeft,
  1188. //"margin-right" : marginRight
  1189. }
  1190. },
  1191. resize : function(){
  1192. this.node.setStyles(this.getCoordinate());
  1193. },
  1194. reload: function(){
  1195. if( this.tooltip )this.tooltip.destroy();
  1196. this.view.reload();
  1197. },
  1198. destroy : function(){
  1199. if( this.tooltip )this.tooltip.destroy();
  1200. this.node.destroy();
  1201. }
  1202. });
  1203. MWFCalendarWeekView.Calendar.Day = new Class({
  1204. Implements: [Events],
  1205. initialize: function(container, date, calendar, data){
  1206. this.container = container;
  1207. this.calendar = calendar;
  1208. this.view = this.calendar.view;
  1209. this.css = this.calendar.css;
  1210. this.app = this.calendar.app;
  1211. this.date = date.clone();
  1212. this.data = data;
  1213. this.calendars = [];
  1214. this.load();
  1215. },
  1216. load: function(){
  1217. this.day = this.date.getDate();
  1218. this.month = this.date.getMonth();
  1219. this.year = this.date.getFullYear();
  1220. this.startTime = new Date( this.year, this.month, this.day, 0, 0, 0 );
  1221. this.endTime = new Date( this.year, this.month, this.day, 23, 59, 59 );
  1222. this.rangeList = [];
  1223. this.rangeObject = {};
  1224. this.data.each( function(d , i){
  1225. var range = this.getTimeRange( d.startTime, d.endTime );
  1226. if( !range )return null;
  1227. d.range = range;
  1228. d.range.id = d.id;
  1229. this.rangeList.push( range );
  1230. this.rangeObject[d.id] = range;
  1231. }.bind(this));
  1232. this.sortRange();
  1233. var length = this.data.length;
  1234. this.documentList = [];
  1235. this.data.each( function(d , i){
  1236. var range = d.range;
  1237. range.dayRangeCount = length;
  1238. range.index = this.rangeList.indexOf( range );
  1239. if( !range )return null;
  1240. //var coordinate = this.getCoordinate( d, range );
  1241. this.documentList.push( new MWFCalendarWeekView.Calendar.Document(this.container, this, d, range ) );
  1242. }.bind(this))
  1243. },
  1244. sortRange : function(){
  1245. this.rangeList.sort( function(range1, range2){
  1246. return range1.startTime - range2.startTime;
  1247. });
  1248. },
  1249. getTimeRange: function( bDate1, eDate1 ){
  1250. var bDate = typeOf(bDate1) == "string" ? Date.parse(bDate1) : bDate1;
  1251. var eDate = typeOf(eDate1) == "string" ? Date.parse(eDate1) : eDate1;
  1252. if( eDate <= this.startTime ){ //比当天天12点更晚
  1253. return null;
  1254. }
  1255. if( this.endTime <= bDate ){
  1256. return null;
  1257. }
  1258. var range = {
  1259. start: ( bDate <= this.startTime ) ? [0, 0, 0] : [bDate.get("hr"), bDate.get("min"), bDate.get("sec")],
  1260. end: ( this.endTime <= eDate ) ? [23, 59, 59] : [eDate.get("hr"), eDate.get("min"), eDate.get("sec")]
  1261. };
  1262. range.startTime = new Date( this.year, this.month, this.day, range.start[0], range.start[1], range.start[2] );
  1263. range.endTime = new Date( this.year, this.month, this.day, range.end[0], range.end[1], range.end[2] );
  1264. range.diff = range.endTime - range.startTime;
  1265. return range;
  1266. },
  1267. resize : function(){
  1268. if(!this.documentList)return;
  1269. this.documentList.each( function( doc ){
  1270. doc.resize();
  1271. }.bind(this))
  1272. },
  1273. reload: function(){
  1274. this.view.reload();
  1275. },
  1276. destroy : function(){
  1277. while( this.documentList.length > 0 ){
  1278. this.documentList.pop().destroy();
  1279. }
  1280. this.container.empty();
  1281. }
  1282. });
  1283. MWFCalendarWeekView.Calendar.Document = new Class({
  1284. initialize: function(container, day, data, range, coordinate ){
  1285. this.container = container;
  1286. this.day = day;
  1287. this.calendar = day.calendar;
  1288. this.view = this.calendar.view;
  1289. this.css = this.calendar.css;
  1290. this.app = this.calendar.app;
  1291. this.date = day.date.clone();
  1292. this.data = data;
  1293. this.range = range;
  1294. this.coordinate = coordinate; //today, otherMonth, thisMonth
  1295. this.load();
  1296. },
  1297. load: function(){
  1298. var lightColor = this.lightColor = MWFCalendar.ColorOptions.getLightColor( this.data.color );
  1299. var node = this.node = new Element("div",{
  1300. styles : {
  1301. position : "absolute",
  1302. border : "1px solid "+lightColor,
  1303. "background-color" : lightColor,
  1304. "overflow" : "hidden",
  1305. "border-radius" : "5px"
  1306. },
  1307. events : {
  1308. click : function(){
  1309. var form = new MWF.xApplication.Calendar.EventForm(this, this.data, {
  1310. isFull : true
  1311. }, {app:this.app});
  1312. form.view = this.view;
  1313. this.calendar.app.isEventEditable(this.data) ? form.edit() : form.open();
  1314. }.bind(this),
  1315. "mouseover" : function () {
  1316. this.node.setStyle("border-color", this.data.color );
  1317. }.bind(this),
  1318. "mouseout" : function () {
  1319. this.node.setStyle("border-color", this.lightColor );
  1320. }.bind(this)
  1321. }
  1322. }).inject( this.container );
  1323. node.setStyles(this.getCoordinate());
  1324. var timeNode = new Element("div",{
  1325. styles : {
  1326. "font-size" : "10px",
  1327. "padding-top" : "2px",
  1328. "padding-left" : "2px"
  1329. },
  1330. text : this.range.startTime.format("%H:%M") + "-" + this.range.endTime.format("%H:%M")
  1331. }).inject( node );
  1332. var titleNode = new Element("div",{
  1333. styles : {
  1334. "padding-top" : "7px",
  1335. "padding-left" : "5px",
  1336. "font-size" : "12px"
  1337. },
  1338. text : this.data.title
  1339. }).inject( node );
  1340. this.tooltip = new MWF.xApplication.Calendar.EventTooltip(this.app.content, this.node, this.app, this.data, {
  1341. axis : "x", "delay" : 350
  1342. });
  1343. this.tooltip.view = this.view;
  1344. },
  1345. resize : function(){
  1346. this.node.setStyles(this.getCoordinate());
  1347. },
  1348. getCoordinate : function(){
  1349. var data = this.data;
  1350. var range = this.range;
  1351. var width_div = 4;
  1352. var top_div = 0;
  1353. if(Browser.name === "ie" ){
  1354. width_div = 4;
  1355. top_div = -2
  1356. }
  1357. var height = Math.floor( ( ( range.endTime - range.startTime ) / MWFCalendarWeekView.DayMsec ) * MWFCalendarWeekView.DayHeight)-width_div;
  1358. if(height < 16) height = 16;
  1359. var top = Math.floor( ( ( range.startTime - this.day.startTime ) / MWFCalendarWeekView.DayMsec ) * MWFCalendarWeekView.DayHeight)-top_div;
  1360. //var width = Math.floor( MWFCalendarWeekView.DayWidth / length )-2;
  1361. //var left = ( width + 2)* index + 1;
  1362. //var width = Math.floor( MWFCalendarWeekView.DayWidth / range.path.length )-5;
  1363. //var left = ( width + 5)* range.path.indexOf( data.id ) + 3;
  1364. var width = Math.floor( MWFCalendarWeekView.DayWidth / this.range.dayRangeCount )-5;
  1365. var left = ( width + 5)* this.range.index + 3;
  1366. return {
  1367. top : top,
  1368. left : left,
  1369. width : width,
  1370. height : height
  1371. }
  1372. },
  1373. reload: function(){
  1374. if( this.tooltip )this.tooltip.destroy();
  1375. this.view.reload();
  1376. },
  1377. destroy: function(){
  1378. if( this.tooltip )this.tooltip.destroy();
  1379. this.node.destroy()
  1380. }
  1381. });
  1382. MWFCalendarWeekView.WeekCalendar = new Class({
  1383. Extends : MWF.widget.Calendar,
  1384. initialize: function(node, options){
  1385. this.options.weekBegin = "0";
  1386. Locale.use("zh-CHS");
  1387. this.options.defaultTime = ""+this.options.baseDate.getHours()+":"+this.options.baseDate.getMinutes()+":"+this.options.baseDate.getSeconds();
  1388. this.setOptions(options);
  1389. this.path = MWF.defaultPath+"/widget/$Calendar/";
  1390. this.cssPath = MWF.defaultPath+"/widget/$Calendar/"+this.options.style+"/css.wcss";
  1391. this._loadCss();
  1392. // this.options.containerPath = this.path+this.style+"/container.html";
  1393. // this.options.dayPath = this.path+this.style+"/day.html";
  1394. // this.options.monthPath = this.path+this.style+"/month.html";
  1395. // this.options.yearPath = this.path+this.style+"/year.html";
  1396. // this.options.timePath = this.path+this.style+"/time.html";
  1397. if (!this.options.format){
  1398. if (this.options.isTime){
  1399. //this.options.format = Locale.get("Date").shortDate + " " + Locale.get("Date").shortTime;
  1400. if(this.options.timeOnly){
  1401. this.options.format="%H:%M";
  1402. }
  1403. else{
  1404. this.options.format = Locale.get("Date").shortDate + " " + "%H:%M";
  1405. }
  1406. }else{
  1407. this.options.format = Locale.get("Date").shortDate;
  1408. }
  1409. }
  1410. this.options.containerPath = this.options.path+this.options.style+"/container.html";
  1411. this.options.dayPath = this.options.path+this.options.style+"/day_week.html";
  1412. this.options.monthPath = this.options.path+this.options.style+"/month.html";
  1413. this.options.yearPath = this.options.path+this.options.style+"/year.html";
  1414. this.options.timePath = this.options.path+this.options.style+"/time.html";
  1415. this.today = new Date();
  1416. this.currentView = this.options.defaultView;
  1417. this.node = $(node);
  1418. this.visible = false;
  1419. this.container = this.createContainer();
  1420. this.container.inject((this.options.target) || $(document.body));
  1421. this.contentTable = this.createContentTable();
  1422. this.contentTable.inject(this.contentDateNode);
  1423. this.addEvents();
  1424. this.container.set({
  1425. styles: {
  1426. "display": "none",
  1427. "opacity": 1
  1428. }
  1429. });
  1430. this.fireEvent("init");
  1431. //this.move = true;
  1432. //this.containerDrag = new Drag.Move(this.container);
  1433. },
  1434. showDay: function(year, month){
  1435. this._setDayTitle(null, year, month);
  1436. this._setDayWeekTitleTh();
  1437. this._setDayDate(null, year, month);
  1438. // if (!this.move){
  1439. // this.move = true;
  1440. // this.containerDrag = new Drag.Move(this.container);
  1441. // }
  1442. },
  1443. _setDayTitle: function(node, year, month){
  1444. var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear();
  1445. var thisMonth = (month!=undefined) ? month : this.options.baseDate.getMonth();
  1446. thisMonth++;
  1447. var text;
  1448. if( MWF.language.substr(0, 2) === "zh" ){
  1449. text = thisYear+ MWF.xApplication.Calendar.LP.year +thisMonth+ MWF.xApplication.Calendar.LP.month;
  1450. }else{
  1451. text = thisYear+ "-" +thisMonth;
  1452. }
  1453. var thisNode = node || this.currentTextNode;
  1454. thisNode.set("text", text);
  1455. thisNode.store("year", thisYear);
  1456. thisNode.store("month", thisMonth);
  1457. },
  1458. _setDayWeekTitleTh: function(table){
  1459. var dayTable = table || this.contentTable;
  1460. var thead = dayTable.getElement("thead");
  1461. var cells = thead.getElements("th");
  1462. if (this.css.calendarDaysContentTh) cells.setStyles(this.css.calendarDaysContentTh);
  1463. //var days_abbr = Locale.get("Date").days_abbr;
  1464. var days_abbr = MWF.LP.widget.days_abbr;
  1465. cells.each(function(item, idx){
  1466. if( idx == 0 ){
  1467. item.set("text", MWF.xApplication.Calendar.LP.week);
  1468. }else{
  1469. //var index;
  1470. //if( this.options.weekBegin == "0" ){
  1471. // index = idx-1;
  1472. //}else{
  1473. // index = idx == 7 ? 0 : idx;
  1474. //}
  1475. var index = ( idx - 1 + parseInt( this.options.weekBegin ) ) % 7;
  1476. item.set("text", days_abbr[index]);
  1477. }
  1478. }.bind(this));
  1479. return cells;
  1480. },
  1481. _setDayDate: function(table, year, month){
  1482. var dayTable = table || this.contentTable;
  1483. var baseDate = this.options.baseDate;
  1484. if ((year!=undefined) && (month!=undefined)){
  1485. baseDate = new Date();
  1486. baseDate.setDate(1);
  1487. baseDate.setFullYear(year);
  1488. baseDate.setMonth(month);
  1489. }
  1490. var tbody = dayTable.getElement("tbody");
  1491. var tds = tbody.getElements("td");
  1492. var firstDate = baseDate.clone();
  1493. firstDate.setDate(1);
  1494. //if( this.options.weekBegin == "0" ){
  1495. // var day = firstDate.getDay()+1;
  1496. //}else{
  1497. //
  1498. //}
  1499. var day = ( 7 + firstDate.getDay() - parseInt( this.options.weekBegin ) ) % 7 + 1;
  1500. var tmpDate = firstDate.clone();
  1501. for (var i=day-1; i>=0; i--){
  1502. if( i % 8 == 0 ){ //设置周数
  1503. var week = this.getWeekNumber( tmpDate );
  1504. tds[i].set("text", week);
  1505. tds[i].setStyles(this.css.week);
  1506. tds[i].store("weekValue", week.toString());
  1507. tds[i].store("dateValue", tmpDate.toString());
  1508. i--;
  1509. if( i<0 )break;
  1510. }
  1511. tmpDate.increment("day", -1);
  1512. tds[i].set("text", tmpDate.getDate());
  1513. tds[i].addClass("gray_"+this.options.style);
  1514. tds[i].setStyles(this.css["gray_"+this.options.style]);
  1515. tds[i].store("dateValue", tmpDate.toString())
  1516. }
  1517. for (var i=day; i<tds.length; i++){
  1518. if( i % 8 == 0 ){ //设置周数
  1519. var week = this.getWeekNumber( firstDate );
  1520. tds[i].set("text", week );
  1521. //tds[i].addClass("gray_"+this.options.style);
  1522. tds[i].setStyles(this.css.week);
  1523. tds[i].store("weekValue", week.toString());
  1524. tds[i].store("dateValue", firstDate.toString());
  1525. i++;
  1526. if( i>=tds.length )break;
  1527. }
  1528. tds[i].set("text", firstDate.getDate());
  1529. if (firstDate.toString() == this.options.baseDate.toString()){
  1530. tds[i].addClass("current_"+this.options.style);
  1531. tds[i].setStyles(this.css["current_"+this.options.style]);
  1532. tds[i].removeClass("gray_"+this.options.style);
  1533. tds[i].setStyle("border", "1px solid #FFF");
  1534. }else if (firstDate.getMonth()!=baseDate.getMonth()){
  1535. tds[i].addClass("gray_"+this.options.style);
  1536. tds[i].setStyles(this.css["gray_"+this.options.style]);
  1537. tds[i].removeClass("current_"+this.options.style);
  1538. tds[i].setStyle("border", "1px solid #FFF");
  1539. }else{
  1540. tds[i].setStyles(this.css["normal_"+this.options.style]);
  1541. tds[i].removeClass("current_"+this.options.style);
  1542. tds[i].removeClass("gray_"+this.options.style);
  1543. tds[i].setStyle("border", "1px solid #FFF");
  1544. }
  1545. var tmp = firstDate.clone();
  1546. if (tmp.clearTime().toString() == this.today.clearTime().toString()){
  1547. //tds[i].addClass("today_"+this.options.style);
  1548. tds[i].setStyles(this.css["today_"+this.options.style]);
  1549. tds[i].setStyle("border", "0px solid #AAA");
  1550. tds[i].addClass("mainColor_bg");
  1551. }
  1552. tds[i].store("dateValue", firstDate.toString());
  1553. firstDate.increment("day", 1);
  1554. }
  1555. },
  1556. getWeekNumber: function( d ){
  1557. // Create a copy of this date object
  1558. var target = d.clone();
  1559. // ISO week date weeks start on monday
  1560. // so correct the day number
  1561. //if( this.options.weekBegin == "1" ){
  1562. // var dayNr = (d.getDay() + 6) % 7;
  1563. //}else{
  1564. // var dayNr = (d.getDay() + 7) % 7;
  1565. //}
  1566. var dayNr = ( 7 + d.getDay() - parseInt( this.options.weekBegin ) ) % 7;
  1567. // ISO 8601 states that week 1 is the week
  1568. // with the first thursday of that year.
  1569. // Set the target date to the thursday in the target week
  1570. target.setDate(target.getDate() - dayNr + 3);
  1571. // Store the millisecond value of the target date
  1572. var firstThursday = target.valueOf();
  1573. // Set the target to the first thursday of the year
  1574. // First set the target to january first
  1575. target.setMonth(0, 1);
  1576. // Not a thursday? Correct the date to the next thursday
  1577. if (target.getDay() != 4) {
  1578. target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
  1579. }
  1580. // The weeknumber is the number of weeks between the
  1581. // first thursday of the year and the thursday in the target week
  1582. return 1 + Math.ceil((firstThursday - target) / 604800000); // 604800000 = 7 * 24 * 3600 * 1000
  1583. }
  1584. });