blob: d62dc68a866044370a8b743a440d63333ad9a745 [file] [log] [blame]
Rushabh Mehtab73fa492012-02-24 15:07:39 +05301// Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
2//
3// MIT License (MIT)
4//
5// Permission is hereby granted, free of charge, to any person obtaining a
6// copy of this software and associated documentation files (the "Software"),
7// to deal in the Software without restriction, including without limitation
8// the rights to use, copy, modify, merge, publish, distribute, sublicense,
9// and/or sell copies of the Software, and to permit persons to whom the
10// Software is furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
17// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21//
22
23pscript.onload_calendar = function(wrapper) {
24 if(!erpnext.calendar) {
25 erpnext.calendar = new Calendar();
26 erpnext.calendar.init(wrapper);
Rushabh Mehta9b1afe12012-03-20 14:37:44 +053027
28 var me = this;
29 $(document).bind('rename', function(event, dt, old_name, new_name) {
30 erpnext.calendar.rename_notify(dt, old_name, new_name)
31 });
Rushabh Mehtab73fa492012-02-24 15:07:39 +053032 }
33}
34
35///// CALENDAR
36
37Calendar=function() {
38 this.views=[];
39 this.events = {};
Rushabh Mehtab73fa492012-02-24 15:07:39 +053040 this.events_by_name = {};
41 this.weekdays = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
42}
43
44Calendar.prototype.init=function (parent) {
45
46 this.wrapper = parent;
47 this.body = $('.cal_body').get(0);
48
49 //this.make_head_buttons();
50 //this.make_header();
51 this.view_title = $('.cal_view_title').get(0);
52
53 this.todays_date = new Date();
54 this.selected_date = this.todays_date;
55 this.selected_hour = 8;
56
57 // Create views
58 this.views['Month'] = new Calendar.MonthView(this);
59 this.views['Week'] = new Calendar.WeekView(this);
60 this.views['Day'] = new Calendar.DayView(this);
61
62 // Month view as initial
Rushabh Mehtaaa999df2012-02-27 12:54:04 +053063 this.cur_view = this.views['Month'];
64 this.views['Month'].show();
Rushabh Mehtab73fa492012-02-24 15:07:39 +053065
66}
67
68Calendar.prototype.rename_notify = function(dt, old_name, new_name) {
69 // calendar
Rushabh Mehta9b1afe12012-03-20 14:37:44 +053070 if(dt = 'Event'){
Rushabh Mehtaaa999df2012-02-27 12:54:04 +053071 if(this.events_by_name[old_name]) {
72 delete this.events_by_name[old_name];
73 }
74 }
Rushabh Mehtab73fa492012-02-24 15:07:39 +053075}
76
77//------------------------------------------------------
78
79Calendar.prototype.show_event = function(ev, cal_ev) {
80 var me = this;
81 if(!this.event_dialog) {
82 var d = new Dialog(400, 400, 'Calendar Event');
83 d.make_body([
84 ['HTML','Heading']
85 ,['Text','Description']
Rushabh Mehta586c6f32012-08-07 16:36:40 +053086 ,['HTML', 'Ref Link']
Rushabh Mehtab73fa492012-02-24 15:07:39 +053087 ,['Check', 'Public Event']
Rushabh Mehta291449b2012-12-13 12:53:21 +053088 ,['Check', 'Cancelled Event']
Rushabh Mehtab73fa492012-02-24 15:07:39 +053089 ,['HTML', 'Event Link']
90 ,['Button', 'Save']
91 ])
92
93 // show the event when the dialog opens
94 d.onshow = function() {
95 // heading
96 var c = me.selected_date;
97 var tmp = time_to_ampm(this.ev.event_hour);
98 tmp = tmp[0]+':'+tmp[1]+' '+tmp[2];
99
100 this.widgets['Heading'].innerHTML =
101 '<div style="text-align: center; padding:4px; font-size: 14px">'
102 + erpnext.calendar.weekdays[c.getDay()] + ', ' + c.getDate() + ' ' + month_list_full[c.getMonth()] + ' ' + c.getFullYear()
103 + ' - <b>'+tmp+'</b></div>';
104
105 // set
106 this.widgets['Description'].value = cstr(this.ev.description);
107
108 this.widgets['Public Event'].checked = false;
Rushabh Mehta291449b2012-12-13 12:53:21 +0530109 this.widgets['Cancelled Event'].checked = false;
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530110
111 if(this.ev.event_type=='Public')
112 this.widgets['Public Event'].checked = true;
113
114 this.widgets['Event Link'].innerHTML = '';
Rushabh Mehta586c6f32012-08-07 16:36:40 +0530115 this.widgets['Ref Link'].innerHTML = '';
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530116
Rushabh Mehta586c6f32012-08-07 16:36:40 +0530117 if(this.ev.ref_type) {
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530118 $(repl('<table style="width: 100%;"><tr>\
119 <td style="width: 30%"><b>Reference:</b></td>\
120 <td><a href="#Form/%(ref_type)s/%(ref_name)s" \
121 onclick="cur_dialog.hide()">%(ref_type)s: %(ref_name)s</a></td>\
122 </tr></table>', this.ev))
Rushabh Mehta586c6f32012-08-07 16:36:40 +0530123 .appendTo(this.widgets['Ref Link'])
124 }
125
126 $(repl('<a href="#Form/Event/%(name)s" \
127 onclick="cur_dialog.hide()">More Options</a>', this.ev))
128 .appendTo(this.widgets['Event Link'])
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530129 }
130
131 // event save
132 d.widgets['Save'].onclick = function() {
133 var d = me.event_dialog;
134
135 // save values
136 d.ev.description = d.widgets['Description'].value;
Rushabh Mehta291449b2012-12-13 12:53:21 +0530137 if(d.widgets['Cancelled Event'].checked)
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530138 d.ev.event_type='Cancel';
139 else if(d.widgets['Public Event'].checked)
140 d.ev.event_type='Public';
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530141
142 me.event_dialog.hide();
143
144 // if new event
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530145 me.save_event(d.ev);
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530146 }
147 this.event_dialog = d;
148 }
149 this.event_dialog.ev = ev;
150 this.event_dialog.cal_ev = cal_ev ? cal_ev : null;
151 this.event_dialog.show();
152
153}
154
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530155Calendar.prototype.save_event = function(doc) {
156 var me = this;
Rushabh Mehta291449b2012-12-13 12:53:21 +0530157 var doclist = new wn.model.DocList("Event", doc.name);
158 doclist.save("Save", function(r) {
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530159 var doc = locals['Event'][r.docname];
160 var cal = erpnext.calendar;
161 cal.cur_view.refresh();
162
163 // if cancelled, hide
164 if(doc.event_type=='Cancel') {
165 $(cal.events_by_name[doc.name].body).toggle(false);
Rushabh Mehta291449b2012-12-13 12:53:21 +0530166 }
167 })
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530168}
169
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530170//------------------------------------------------------
171
172Calendar.prototype.add_event = function() {
173
Rushabh Mehta291449b2012-12-13 12:53:21 +0530174 var ev = wn.model.make_new_doc_and_get_name('Event');
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530175 ev = locals['Event'][ev];
176
177 ev.event_date = dateutil.obj_to_str(this.selected_date);
178 ev.event_hour = this.selected_hour+':00';
179 ev.event_type = 'Private';
180
181 this.show_event(ev);
182}
183//------------------------------------------------------
184
185Calendar.prototype.get_month_events = function(call_back) {
186 // ret fn
187 var me = this;
188 var f = function(r, rt) {
189 if(me.cur_view) me.cur_view.refresh();
190 if(call_back)call_back();
191 }
192
193 //load
194 var y=this.selected_date.getFullYear(); var m = this.selected_date.getMonth();
195 if(!this.events[y] || !this.events[y][m]) {
196 $c('webnotes.widgets.event.load_month_events', args = {
197 'month': m + 1,
198 'year' : y},
199 f);
200 }
201}
202//------------------------------------------------------
203
204Calendar.prototype.get_daily_event_list=function(day) {
205 var el = [];
206 var d = day.getDate(); var m = day.getMonth(); var y = day.getFullYear()
207 if(this.events[y] && this.events[y][m] &&
208 this.events[y][m][d]) {
209 var l = this.events[y][m][d]
210 for(var i in l) {
211 for(var j in l[i]) el[el.length] = l[i][j];
212 }
213 return el;
214 }
215 else return [];
216}
217//------------------------------------------------------
218
219Calendar.prototype.set_event = function(ev) {
220 // don't duplicate
221 if(this.events_by_name[ev.name]) {
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530222 return this.events_by_name[ev.name];
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530223 }
224
225 var dt = dateutil.str_to_obj(ev.event_date);
226 var m = dt.getMonth();
227 var d = dt.getDate();
228 var y = dt.getFullYear();
229
230 if(!this.events[y]) this.events[y] = [];
231 if(!this.events[y][m]) this.events[y][m] = [];
232 if(!this.events[y][m][d]) this.events[y][m][d] = [];
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530233 if(!this.events[y][m][d][cint(ev.event_hour)])
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530234 this.events[y][m][d][cint(ev.event_hour)] = [];
235
236 var cal_ev = new Calendar.CalEvent(ev, this);
237 this.events[y][m][d][cint(ev.event_hour)].push(cal_ev);
238 this.events_by_name[ev.name] = cal_ev;
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530239
240 return cal_ev;
241}
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530242
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530243//------------------------------------------------------
244
Rushabh Mehta17db39d2012-11-24 14:45:31 +0530245Calendar.prototype.clear = function() {
246 this.events = {};
247 this.events_by_name = {};
248 locals.Event = {};
249}
250
251Calendar.prototype.refresh = function(viewtype, clear_events){//Sets the viewtype of the Calendar and Calls the View class based on the viewtype
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530252 if(viewtype)
253 this.viewtype = viewtype;
Rushabh Mehta17db39d2012-11-24 14:45:31 +0530254
255 if(clear_events)
256 this.clear();
257
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530258 // switch view if reqd
259 if(this.cur_view.viewtype!=this.viewtype) {
260 this.cur_view.hide();
261 this.cur_view = this.views[this.viewtype];
262 this.cur_view.in_home = false; // for home page
263 this.cur_view.show();
264 }
265 else{
Rushabh Mehta17db39d2012-11-24 14:45:31 +0530266 this.cur_view.get_events();
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530267 this.cur_view.refresh(this);
268 }
269}
270
271//------------------------------------------------------
272
273Calendar.CalEvent= function(doc, cal) {
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530274 var me = this;
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530275 me.doc = doc;
276
277 this.body = $("<div class='label cal_event'></div>")
278 .html(doc.description)
Rushabh Mehtaa44b66e2012-11-23 16:34:42 +0530279 .attr("title", doc.description)
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530280 .css({"cursor":"pointer"})
281 .attr("data-event", doc.name)
282 .click(function() {
283 var doc = locals["Event"][$(this).attr("data-event")];
284 cal.show_event(doc, me);
285 })
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530286
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530287 this.show = function(vu) {
288 me.body
289 .html(me.doc.description)
290 .css({"width": ($(vu.body).width()-10)})
291 .appendTo(vu.body)
292 .removeClass("label-success").removeClass("label-info")
293 .addClass(me.doc.event_type=="Public" ? "label-success" : "label-info")
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530294 }
295}
296
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530297
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530298// ----------
299
300Calendar.View =function() { this.daystep = 0; this.monthstep = 0; }
301Calendar.View.prototype.init=function(cal) {
302 this.cal = cal;
303 this.body = $a(cal.body, 'div', 'cal_view_body');
304 this.body.style.display = 'none';
305 this.create_table();
306}
307
308
309Calendar.View.prototype.show=function() {
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530310 this.body.style.display = 'block';
311 this.get_events(); this.refresh();
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530312}
313
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530314Calendar.View.prototype.hide=function() {
315 this.body.style.display = 'none';
316}
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530317
318Calendar.View.prototype.next = function() {
319 var s = this.cal.selected_date;
320 this.cal.selected_date = new Date(s.getFullYear(), s.getMonth() + this.monthstep, s.getDate() + this.daystep);
321 this.get_events(); this.refresh();
322}
323
324Calendar.View.prototype.prev = function() {
325 var s = this.cal.selected_date;
326 this.cal.selected_date = new Date(s.getFullYear(), s.getMonth() - this.monthstep, s.getDate() - this.daystep);
327 this.get_events(); this.refresh();
328}
329
330Calendar.View.prototype.get_events = function() {
331 this.cal.get_month_events();
332}
333Calendar.View.prototype.add_unit = function(vu) {
334 this.viewunits[this.viewunits.length] = vu;
335}
336Calendar.View.prototype.refresh_units = function() {
337 // load the events
338 if(locals['Event']) {
339 for(var name in locals['Event']) {
340 this.cal.set_event(locals['Event'][name]);
341 }
342 }
343
344
345 for(var r in this.table.rows) {
346 for(var c in this.table.rows[r].cells) {
347 if(this.table.rows[r].cells[c].viewunit) {
348 this.table.rows[r].cells[c].viewunit.refresh();
349 }
350 }
351 }
352}
353
354// ................. Month View..........................
355Calendar.MonthView = function(cal) { this.init(cal); this.monthstep = 1; this.rows = 5; this.cells = 7; }
356Calendar.MonthView.prototype=new Calendar.View();
357Calendar.MonthView.prototype.create_table = function() {
358
359 // create head
360 this.head_wrapper = $a(this.body, 'div', 'cal_month_head');
361
362 // create headers
363 this.headtable = $a(this.head_wrapper, 'table', 'cal_month_headtable');
364 var r = this.headtable.insertRow(0);
365 for(var j=0;j<7;j++) {
366 var cell = r.insertCell(j);
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530367 cell.innerHTML = erpnext.calendar.weekdays[j];
368 $w(cell, (100 / 7) + '%');
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530369 }
370
371 this.main = $a(this.body, 'div', 'cal_month_body');
372 this.table = $a(this.main, 'table', 'cal_month_table');
373 var me = this;
374
375 // create body
376 for(var i=0;i<5;i++) {
377 var r = this.table.insertRow(i);
378 for(var j=0;j<7;j++) {
379 var cell = r.insertCell(j);
380 cell.viewunit = new Calendar.MonthViewUnit(cell);
381 }
382 }
383}
384
385Calendar.MonthView.prototype.refresh = function() {
386 var c =this.cal.selected_date;
387 var me=this;
388 // fill other days
389
390 var cur_row = 0;
391
392 var cur_month = c.getMonth();
393 var cur_year = c.getFullYear();
394
395 var d = new Date(cur_year, cur_month, 1);
396 var day = 1 - d.getDay();
397
398
399 // set day headers
400 var d = new Date(cur_year, cur_month, day);
401
402 this.cal.view_title.innerHTML = month_list_full[cur_month] + ' ' + cur_year;
403
404 for(var i=0;i<6;i++) {
405 if((i<5) || cur_month==d.getMonth()) { // if this month
406 for(var j=0;j<7;j++) {
407 var cell = this.table.rows[cur_row].cells[j];
408
409 if((i<5) || cur_month==d.getMonth()) { // if this month
410 cell.viewunit.day = d;
411 cell.viewunit.hour = 8;
412 if(cur_month == d.getMonth()) {
413 cell.viewunit.is_disabled = false;
414
415 if(same_day(this.cal.todays_date, d))
416 cell.viewunit.is_today = true;
417 else
418 cell.viewunit.is_today = false;
419
420 } else {
421 cell.viewunit.is_disabled = true;
422 }
423 }
424 // new date
425 day++;
426 d = new Date(cur_year, cur_month, day);
427 }
428 }
429 cur_row++;
430 if(cur_row == 5) {cur_row = 0;} // back to top
431 }
432 this.refresh_units();
433
434}
435 // ................. Daily View..........................
436Calendar.DayView=function(cal){ this.init(cal); this.daystep = 1; }
437Calendar.DayView.prototype=new Calendar.View();
438Calendar.DayView.prototype.create_table = function() {
439
440 // create body
441 this.main = $a(this.body, 'div', 'cal_day_body');
442 this.table = $a(this.main, 'table', 'cal_day_table');
443 var me = this;
444
445 for(var i=0;i<24;i++) {
446 var r = this.table.insertRow(i);
447 for(var j=0;j<2;j++) {
448 var cell = r.insertCell(j);
449 if(j==0) {
450 var tmp = time_to_ampm((i)+':00');
451 cell.innerHTML = tmp[0]+':'+tmp[1]+' '+tmp[2];
452 $w(cell, '10%');
453 } else {
454 cell.viewunit = new Calendar.DayViewUnit(cell);
455 cell.viewunit.hour = i;
456 $w(cell, '90%');
457 if((i>=7)&&(i<=20)) {
458 cell.viewunit.is_daytime = true;
459 }
460 }
461 }
462 }
463 }
464
465Calendar.DayView.prototype.refresh = function() {
466 var c =this.cal.selected_date;
467
468 // fill other days
469 var me=this;
470
471 this.cal.view_title.innerHTML = erpnext.calendar.weekdays[c.getDay()] + ', '
472 + c.getDate() + ' ' + month_list_full[c.getMonth()] + ' ' + c.getFullYear();
473
474 // headers
475 var d = c;
476
477 for(var i=0;i<24;i++) {
478 var cell = this.table.rows[i].cells[1];
479 if(same_day(this.cal.todays_date, d)) cell.viewunit.is_today = true;
480 else cell.viewunit.is_today = false;
481 cell.viewunit.day = d;
482 }
483 this.refresh_units();
484}
485
486// ................. Weekly View..........................
487Calendar.WeekView=function(cal) { this.init(cal); this.daystep = 7; }
488Calendar.WeekView.prototype=new Calendar.View();
489Calendar.WeekView.prototype.create_table = function() {
490
491 // create head
492 this.head_wrapper = $a(this.body, 'div', 'cal_month_head');
493
494 // day headers
495 this.headtable = $a(this.head_wrapper, 'table', 'cal_month_headtable');
496 var r = this.headtable.insertRow(0);
497 for(var j=0;j<8;j++) {
498 var cell = r.insertCell(j);
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530499 }
500
501 // hour header
502
503 // create body
504 this.main = $a(this.body, 'div', 'cal_week_body');
505 this.table = $a(this.main, 'table', 'cal_week_table');
506 var me = this;
507
508 for(var i=0;i<24;i++) {
509 var r = this.table.insertRow(i);
510 for(var j=0;j<8;j++) {
511 var cell = r.insertCell(j);
512 if(j==0) {
513 var tmp = time_to_ampm(i+':00');
514 cell.innerHTML = tmp[0]+':'+tmp[1]+' '+tmp[2];
515
516 $w(cell, '10%');
517 } else {
518 cell.viewunit = new Calendar.WeekViewUnit(cell);
519 cell.viewunit.hour = i;
520 if((i>=7)&&(i<=20)) {
521 cell.viewunit.is_daytime = true;
522 }
523 }
524 }
525 }
526}
527
528Calendar.WeekView.prototype.refresh = function() {
529 var c =this.cal.selected_date;
530 // fill other days
531 var me=this;
532
533 this.cal.view_title.innerHTML = month_list_full[c.getMonth()] + ' ' + c.getFullYear();
534
535 // headers
536 var d = new Date(c.getFullYear(), c.getMonth(), c.getDate() - c.getDay());
537
538 for (var k=1;k<8;k++) {
539 this.headtable.rows[0].cells[k].innerHTML = erpnext.calendar.weekdays[d.getDay()] + ' ' + d.getDate();
540
541 for(var i=0;i<24;i++) {
542 var cell = this.table.rows[i].cells[k];
543 if(same_day(this.cal.todays_date, d))
544 cell.viewunit.is_today = true;
545 else cell.viewunit.is_today = false;
546
547 cell.viewunit.day = d;
548 //cell.viewunit.refresh();
549 }
550 d=new Date(d.getFullYear(),d.getMonth(),d.getDate() + 1);
551
552 }
553
554 this.refresh_units();
555}
556
557//------------------------------------------------------.
558
559Calendar.ViewUnit = function() {}
560Calendar.ViewUnit.prototype.init = function(parent) {
561 parent.style.border = "1px solid #CCC" ;
562 this.body = $a(parent, 'div', this.default_class);
563 this.parent = parent;
564
565 var me = this;
566 this.body.onclick = function() {
567 erpnext.calendar.selected_date = me.day;
568 erpnext.calendar.selected_hour = me.hour;
569
570 if(erpnext.calendar.cur_vu && erpnext.calendar.cur_vu!=me){
571 erpnext.calendar.cur_vu.deselect();
572 me.select();
573 erpnext.calendar.cur_vu = me;
574 }
575 }
576 this.body.ondblclick = function() {
577 erpnext.calendar.add_event();
578 }
579}
580
581Calendar.ViewUnit.prototype.set_header=function(v) {
582 this.header.innerHTML = v;
583}
584
585Calendar.ViewUnit.prototype.set_today = function() {
586 this.is_today = true;
587 this.set_display();
588}
589
590Calendar.ViewUnit.prototype.clear = function() {
591 if(this.header)this.header.innerHTML = '';
592
593 // clear body
594 while(this.body.childNodes.length)
595 this.body.removeChild(this.body.childNodes[0]);
596}
597
598Calendar.ViewUnit.prototype.set_display = function() {
599 var cn = '#FFF';
600
601 // colors
602 var col_tod_sel = '#EEE';
603 var col_tod = '#FFF';
604 var col_sel = '#EEF';
605
606 if(this.is_today) {
607 if(this.selected) cn = col_tod_sel;
608 else cn = col_tod;
609 } else
610 if(this.selected) cn = col_sel;
611
612 if(this.header) {
613 if(this.is_disabled) {
614 this.body.className = this.default_class + ' cal_vu_disabled';
615 this.header.style.color = '#BBB';
616 } else {
617 this.body.className = this.default_class;
618 this.header.style.color = '#000';
619 }
620
621 if(this.day&&this.day.getDay()==0)
622 this.header.style.backgroundColor = '#FEE';
623 else
624 this.header.style.backgroundColor = '';
625 }
626 this.parent.style.backgroundColor = cn;
627}
628
629Calendar.ViewUnit.prototype.is_selected = function() {
630 return (same_day(this.day, erpnext.calendar.selected_date)
631 && this.hour==erpnext.calendar.selected_hour)
632}
633
634Calendar.ViewUnit.prototype.get_event_list = function() {
635 var y = this.day.getFullYear();
636 var m = this.day.getMonth();
637 var d = this.day.getDate();
638 if(erpnext.calendar.events[y] && erpnext.calendar.events[y][m] &&
639 erpnext.calendar.events[y][m][d] &&
640 erpnext.calendar.events[y][m][d][this.hour]) {
641 return erpnext.calendar.events[y][m][d][this.hour];
642 } else
643 return [];
644}
645
646Calendar.ViewUnit.prototype.refresh = function() {
647 this.clear();
648
649 if(this.is_selected()) {
650 if(erpnext.calendar.cur_vu)erpnext.calendar.cur_vu.deselect();
651 this.selected = true;
652 erpnext.calendar.cur_vu = this;
653 }
654 this.set_display();
655 this.el = this.get_event_list();
656 if(this.onrefresh)this.onrefresh();
657
658 for(var i in this.el) {
659 this.el[i].show(this);
660 }
661
662 var me = this;
663}
664
665Calendar.ViewUnit.prototype.select=function() { this.selected = true; this.set_display(); }
666Calendar.ViewUnit.prototype.deselect=function() { this.selected = false; this.set_display(); }
667Calendar.ViewUnit.prototype.setevent=function() { }
668
669Calendar.MonthViewUnit=function(parent) {
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530670 var me = this;
671 this.header = $("<div class='cal_month_date'></div>")
672 .appendTo(parent)
673 .css({"cursor":"pointer"})
674 .click(function() {
675 me.body.onclick();
676 })
677 .bind("dblclick", function() {
678 me.body.ondblclick();
679 })
680 .get(0);
681
682 this.default_class = "cal_month_unit";
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530683 this.init(parent);
684
685 this.onrefresh = function() {
686 this.header.innerHTML = this.day.getDate();
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530687 }
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530688}
689Calendar.MonthViewUnit.prototype = new Calendar.ViewUnit();
690Calendar.MonthViewUnit.prototype.is_selected = function() {
691 return same_day(this.day, erpnext.calendar.selected_date)
692}
693
694Calendar.MonthViewUnit.prototype.get_event_list = function() {
695 return erpnext.calendar.get_daily_event_list(this.day);
696}
697
698Calendar.DayViewUnit= function(parent) {
699 this.default_class = "cal_day_unit"; this.init(parent);
700}
701Calendar.DayViewUnit.prototype = new Calendar.ViewUnit();
702Calendar.DayViewUnit.prototype.onrefresh = function() {
703 if(this.el.length<3)
704 this.body.style.height = '30px';
705 else this.body.style.height = '';
706}
707
708Calendar.WeekViewUnit=function(parent) {
709 this.default_class = "cal_week_unit"; this.init(parent);
710}
711Calendar.WeekViewUnit.prototype = new Calendar.ViewUnit();
712Calendar.WeekViewUnit.prototype.onrefresh = function() {
713 if(this.el.length<3) this.body.style.height = '30px';
714 else this.body.style.height = '';
715}