blob: 5e59f1f4c8e11db9133849f09389ee29625ded9e [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;
Rushabh Mehtab73fa492012-02-24 15:07:39 +053097
98 this.widgets['Heading'].innerHTML =
99 '<div style="text-align: center; padding:4px; font-size: 14px">'
100 + erpnext.calendar.weekdays[c.getDay()] + ', ' + c.getDate() + ' ' + month_list_full[c.getMonth()] + ' ' + c.getFullYear()
Nabin Haitd11e9d62013-01-16 16:51:58 +0530101 + ' - <b>'+this.ev.event_hour+'</b></div>';
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530102
103 // set
104 this.widgets['Description'].value = cstr(this.ev.description);
105
106 this.widgets['Public Event'].checked = false;
Rushabh Mehta291449b2012-12-13 12:53:21 +0530107 this.widgets['Cancelled Event'].checked = false;
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530108
109 if(this.ev.event_type=='Public')
110 this.widgets['Public Event'].checked = true;
111
112 this.widgets['Event Link'].innerHTML = '';
Rushabh Mehta586c6f32012-08-07 16:36:40 +0530113 this.widgets['Ref Link'].innerHTML = '';
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530114
Rushabh Mehta586c6f32012-08-07 16:36:40 +0530115 if(this.ev.ref_type) {
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530116 $(repl('<table style="width: 100%;"><tr>\
117 <td style="width: 30%"><b>Reference:</b></td>\
118 <td><a href="#Form/%(ref_type)s/%(ref_name)s" \
119 onclick="cur_dialog.hide()">%(ref_type)s: %(ref_name)s</a></td>\
120 </tr></table>', this.ev))
Rushabh Mehta586c6f32012-08-07 16:36:40 +0530121 .appendTo(this.widgets['Ref Link'])
122 }
123
124 $(repl('<a href="#Form/Event/%(name)s" \
125 onclick="cur_dialog.hide()">More Options</a>', this.ev))
126 .appendTo(this.widgets['Event Link'])
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530127 }
128
129 // event save
130 d.widgets['Save'].onclick = function() {
131 var d = me.event_dialog;
132
133 // save values
134 d.ev.description = d.widgets['Description'].value;
Rushabh Mehta291449b2012-12-13 12:53:21 +0530135 if(d.widgets['Cancelled Event'].checked)
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530136 d.ev.event_type='Cancel';
137 else if(d.widgets['Public Event'].checked)
138 d.ev.event_type='Public';
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530139
140 me.event_dialog.hide();
141
142 // if new event
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530143 me.save_event(d.ev);
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530144 }
145 this.event_dialog = d;
146 }
147 this.event_dialog.ev = ev;
148 this.event_dialog.cal_ev = cal_ev ? cal_ev : null;
149 this.event_dialog.show();
150
151}
152
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530153Calendar.prototype.save_event = function(doc) {
154 var me = this;
Rushabh Mehta291449b2012-12-13 12:53:21 +0530155 var doclist = new wn.model.DocList("Event", doc.name);
156 doclist.save("Save", function(r) {
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530157 var doc = locals['Event'][r.docname];
158 var cal = erpnext.calendar;
159 cal.cur_view.refresh();
160
161 // if cancelled, hide
162 if(doc.event_type=='Cancel') {
163 $(cal.events_by_name[doc.name].body).toggle(false);
Rushabh Mehta291449b2012-12-13 12:53:21 +0530164 }
165 })
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530166}
167
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530168//------------------------------------------------------
169
170Calendar.prototype.add_event = function() {
171
Rushabh Mehta291449b2012-12-13 12:53:21 +0530172 var ev = wn.model.make_new_doc_and_get_name('Event');
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530173 ev = locals['Event'][ev];
174
175 ev.event_date = dateutil.obj_to_str(this.selected_date);
Nabin Haitd11e9d62013-01-16 16:51:58 +0530176 ev.event_hour = this.selected_hour+':00:00';
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530177 ev.event_type = 'Private';
178
179 this.show_event(ev);
180}
181//------------------------------------------------------
182
183Calendar.prototype.get_month_events = function(call_back) {
184 // ret fn
185 var me = this;
186 var f = function(r, rt) {
187 if(me.cur_view) me.cur_view.refresh();
188 if(call_back)call_back();
189 }
190
191 //load
192 var y=this.selected_date.getFullYear(); var m = this.selected_date.getMonth();
193 if(!this.events[y] || !this.events[y][m]) {
194 $c('webnotes.widgets.event.load_month_events', args = {
195 'month': m + 1,
196 'year' : y},
197 f);
198 }
199}
200//------------------------------------------------------
201
202Calendar.prototype.get_daily_event_list=function(day) {
203 var el = [];
204 var d = day.getDate(); var m = day.getMonth(); var y = day.getFullYear()
205 if(this.events[y] && this.events[y][m] &&
206 this.events[y][m][d]) {
207 var l = this.events[y][m][d]
208 for(var i in l) {
209 for(var j in l[i]) el[el.length] = l[i][j];
210 }
211 return el;
212 }
213 else return [];
214}
215//------------------------------------------------------
216
217Calendar.prototype.set_event = function(ev) {
218 // don't duplicate
219 if(this.events_by_name[ev.name]) {
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530220 return this.events_by_name[ev.name];
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530221 }
222
223 var dt = dateutil.str_to_obj(ev.event_date);
224 var m = dt.getMonth();
225 var d = dt.getDate();
226 var y = dt.getFullYear();
227
228 if(!this.events[y]) this.events[y] = [];
229 if(!this.events[y][m]) this.events[y][m] = [];
230 if(!this.events[y][m][d]) this.events[y][m][d] = [];
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530231 if(!this.events[y][m][d][cint(ev.event_hour)])
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530232 this.events[y][m][d][cint(ev.event_hour)] = [];
233
234 var cal_ev = new Calendar.CalEvent(ev, this);
235 this.events[y][m][d][cint(ev.event_hour)].push(cal_ev);
236 this.events_by_name[ev.name] = cal_ev;
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530237
238 return cal_ev;
239}
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530240
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530241//------------------------------------------------------
242
Rushabh Mehta17db39d2012-11-24 14:45:31 +0530243Calendar.prototype.clear = function() {
244 this.events = {};
245 this.events_by_name = {};
246 locals.Event = {};
247}
248
249Calendar.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 +0530250 if(viewtype)
251 this.viewtype = viewtype;
Rushabh Mehta17db39d2012-11-24 14:45:31 +0530252
253 if(clear_events)
254 this.clear();
255
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530256 // switch view if reqd
257 if(this.cur_view.viewtype!=this.viewtype) {
258 this.cur_view.hide();
259 this.cur_view = this.views[this.viewtype];
260 this.cur_view.in_home = false; // for home page
261 this.cur_view.show();
262 }
263 else{
Rushabh Mehta17db39d2012-11-24 14:45:31 +0530264 this.cur_view.get_events();
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530265 this.cur_view.refresh(this);
266 }
267}
268
269//------------------------------------------------------
270
271Calendar.CalEvent= function(doc, cal) {
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530272 var me = this;
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530273 me.doc = doc;
274
275 this.body = $("<div class='label cal_event'></div>")
276 .html(doc.description)
Rushabh Mehtaa44b66e2012-11-23 16:34:42 +0530277 .attr("title", doc.description)
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530278 .css({"cursor":"pointer"})
279 .attr("data-event", doc.name)
280 .click(function() {
281 var doc = locals["Event"][$(this).attr("data-event")];
282 cal.show_event(doc, me);
283 })
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530284
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530285 this.show = function(vu) {
286 me.body
287 .html(me.doc.description)
288 .css({"width": ($(vu.body).width()-10)})
289 .appendTo(vu.body)
290 .removeClass("label-success").removeClass("label-info")
291 .addClass(me.doc.event_type=="Public" ? "label-success" : "label-info")
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530292 }
293}
294
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530295
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530296// ----------
297
298Calendar.View =function() { this.daystep = 0; this.monthstep = 0; }
299Calendar.View.prototype.init=function(cal) {
300 this.cal = cal;
301 this.body = $a(cal.body, 'div', 'cal_view_body');
302 this.body.style.display = 'none';
303 this.create_table();
304}
305
306
307Calendar.View.prototype.show=function() {
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530308 this.body.style.display = 'block';
309 this.get_events(); this.refresh();
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530310}
311
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530312Calendar.View.prototype.hide=function() {
313 this.body.style.display = 'none';
314}
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530315
316Calendar.View.prototype.next = function() {
317 var s = this.cal.selected_date;
318 this.cal.selected_date = new Date(s.getFullYear(), s.getMonth() + this.monthstep, s.getDate() + this.daystep);
319 this.get_events(); this.refresh();
320}
321
322Calendar.View.prototype.prev = function() {
323 var s = this.cal.selected_date;
324 this.cal.selected_date = new Date(s.getFullYear(), s.getMonth() - this.monthstep, s.getDate() - this.daystep);
325 this.get_events(); this.refresh();
326}
327
328Calendar.View.prototype.get_events = function() {
329 this.cal.get_month_events();
330}
331Calendar.View.prototype.add_unit = function(vu) {
332 this.viewunits[this.viewunits.length] = vu;
333}
334Calendar.View.prototype.refresh_units = function() {
335 // load the events
336 if(locals['Event']) {
337 for(var name in locals['Event']) {
338 this.cal.set_event(locals['Event'][name]);
339 }
340 }
341
342
343 for(var r in this.table.rows) {
344 for(var c in this.table.rows[r].cells) {
345 if(this.table.rows[r].cells[c].viewunit) {
346 this.table.rows[r].cells[c].viewunit.refresh();
347 }
348 }
349 }
350}
351
352// ................. Month View..........................
353Calendar.MonthView = function(cal) { this.init(cal); this.monthstep = 1; this.rows = 5; this.cells = 7; }
354Calendar.MonthView.prototype=new Calendar.View();
355Calendar.MonthView.prototype.create_table = function() {
356
357 // create head
358 this.head_wrapper = $a(this.body, 'div', 'cal_month_head');
359
360 // create headers
361 this.headtable = $a(this.head_wrapper, 'table', 'cal_month_headtable');
362 var r = this.headtable.insertRow(0);
363 for(var j=0;j<7;j++) {
364 var cell = r.insertCell(j);
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530365 cell.innerHTML = erpnext.calendar.weekdays[j];
366 $w(cell, (100 / 7) + '%');
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530367 }
368
369 this.main = $a(this.body, 'div', 'cal_month_body');
370 this.table = $a(this.main, 'table', 'cal_month_table');
371 var me = this;
372
373 // create body
374 for(var i=0;i<5;i++) {
375 var r = this.table.insertRow(i);
376 for(var j=0;j<7;j++) {
377 var cell = r.insertCell(j);
378 cell.viewunit = new Calendar.MonthViewUnit(cell);
379 }
380 }
381}
382
383Calendar.MonthView.prototype.refresh = function() {
384 var c =this.cal.selected_date;
385 var me=this;
386 // fill other days
387
388 var cur_row = 0;
389
390 var cur_month = c.getMonth();
391 var cur_year = c.getFullYear();
392
393 var d = new Date(cur_year, cur_month, 1);
394 var day = 1 - d.getDay();
395
396
397 // set day headers
398 var d = new Date(cur_year, cur_month, day);
399
400 this.cal.view_title.innerHTML = month_list_full[cur_month] + ' ' + cur_year;
401
402 for(var i=0;i<6;i++) {
403 if((i<5) || cur_month==d.getMonth()) { // if this month
404 for(var j=0;j<7;j++) {
405 var cell = this.table.rows[cur_row].cells[j];
406
407 if((i<5) || cur_month==d.getMonth()) { // if this month
408 cell.viewunit.day = d;
409 cell.viewunit.hour = 8;
410 if(cur_month == d.getMonth()) {
411 cell.viewunit.is_disabled = false;
412
413 if(same_day(this.cal.todays_date, d))
414 cell.viewunit.is_today = true;
415 else
416 cell.viewunit.is_today = false;
417
418 } else {
419 cell.viewunit.is_disabled = true;
420 }
421 }
422 // new date
423 day++;
424 d = new Date(cur_year, cur_month, day);
425 }
426 }
427 cur_row++;
428 if(cur_row == 5) {cur_row = 0;} // back to top
429 }
430 this.refresh_units();
431
432}
433 // ................. Daily View..........................
434Calendar.DayView=function(cal){ this.init(cal); this.daystep = 1; }
435Calendar.DayView.prototype=new Calendar.View();
436Calendar.DayView.prototype.create_table = function() {
437
438 // create body
439 this.main = $a(this.body, 'div', 'cal_day_body');
440 this.table = $a(this.main, 'table', 'cal_day_table');
441 var me = this;
442
443 for(var i=0;i<24;i++) {
444 var r = this.table.insertRow(i);
445 for(var j=0;j<2;j++) {
446 var cell = r.insertCell(j);
447 if(j==0) {
Nabin Haitd11e9d62013-01-16 16:51:58 +0530448 cell.innerHTML = i+':00:00';
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530449 $w(cell, '10%');
450 } else {
451 cell.viewunit = new Calendar.DayViewUnit(cell);
452 cell.viewunit.hour = i;
453 $w(cell, '90%');
454 if((i>=7)&&(i<=20)) {
455 cell.viewunit.is_daytime = true;
456 }
457 }
458 }
459 }
460 }
461
462Calendar.DayView.prototype.refresh = function() {
463 var c =this.cal.selected_date;
464
465 // fill other days
466 var me=this;
467
468 this.cal.view_title.innerHTML = erpnext.calendar.weekdays[c.getDay()] + ', '
469 + c.getDate() + ' ' + month_list_full[c.getMonth()] + ' ' + c.getFullYear();
470
471 // headers
472 var d = c;
473
474 for(var i=0;i<24;i++) {
475 var cell = this.table.rows[i].cells[1];
476 if(same_day(this.cal.todays_date, d)) cell.viewunit.is_today = true;
477 else cell.viewunit.is_today = false;
478 cell.viewunit.day = d;
479 }
480 this.refresh_units();
481}
482
483// ................. Weekly View..........................
484Calendar.WeekView=function(cal) { this.init(cal); this.daystep = 7; }
485Calendar.WeekView.prototype=new Calendar.View();
486Calendar.WeekView.prototype.create_table = function() {
487
488 // create head
489 this.head_wrapper = $a(this.body, 'div', 'cal_month_head');
490
491 // day headers
492 this.headtable = $a(this.head_wrapper, 'table', 'cal_month_headtable');
493 var r = this.headtable.insertRow(0);
494 for(var j=0;j<8;j++) {
495 var cell = r.insertCell(j);
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530496 }
497
498 // hour header
499
500 // create body
501 this.main = $a(this.body, 'div', 'cal_week_body');
502 this.table = $a(this.main, 'table', 'cal_week_table');
503 var me = this;
504
505 for(var i=0;i<24;i++) {
506 var r = this.table.insertRow(i);
507 for(var j=0;j<8;j++) {
508 var cell = r.insertCell(j);
509 if(j==0) {
Nabin Haitd11e9d62013-01-16 16:51:58 +0530510 cell.innerHTML = i+':00:00';
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530511 $w(cell, '10%');
512 } else {
513 cell.viewunit = new Calendar.WeekViewUnit(cell);
514 cell.viewunit.hour = i;
515 if((i>=7)&&(i<=20)) {
516 cell.viewunit.is_daytime = true;
517 }
518 }
519 }
520 }
521}
522
523Calendar.WeekView.prototype.refresh = function() {
524 var c =this.cal.selected_date;
525 // fill other days
526 var me=this;
527
528 this.cal.view_title.innerHTML = month_list_full[c.getMonth()] + ' ' + c.getFullYear();
529
530 // headers
531 var d = new Date(c.getFullYear(), c.getMonth(), c.getDate() - c.getDay());
532
533 for (var k=1;k<8;k++) {
534 this.headtable.rows[0].cells[k].innerHTML = erpnext.calendar.weekdays[d.getDay()] + ' ' + d.getDate();
535
536 for(var i=0;i<24;i++) {
537 var cell = this.table.rows[i].cells[k];
538 if(same_day(this.cal.todays_date, d))
539 cell.viewunit.is_today = true;
540 else cell.viewunit.is_today = false;
541
542 cell.viewunit.day = d;
543 //cell.viewunit.refresh();
544 }
545 d=new Date(d.getFullYear(),d.getMonth(),d.getDate() + 1);
546
547 }
548
549 this.refresh_units();
550}
551
552//------------------------------------------------------.
553
554Calendar.ViewUnit = function() {}
555Calendar.ViewUnit.prototype.init = function(parent) {
556 parent.style.border = "1px solid #CCC" ;
557 this.body = $a(parent, 'div', this.default_class);
558 this.parent = parent;
559
560 var me = this;
561 this.body.onclick = function() {
562 erpnext.calendar.selected_date = me.day;
563 erpnext.calendar.selected_hour = me.hour;
564
565 if(erpnext.calendar.cur_vu && erpnext.calendar.cur_vu!=me){
566 erpnext.calendar.cur_vu.deselect();
567 me.select();
568 erpnext.calendar.cur_vu = me;
569 }
570 }
571 this.body.ondblclick = function() {
572 erpnext.calendar.add_event();
573 }
574}
575
576Calendar.ViewUnit.prototype.set_header=function(v) {
577 this.header.innerHTML = v;
578}
579
580Calendar.ViewUnit.prototype.set_today = function() {
581 this.is_today = true;
582 this.set_display();
583}
584
585Calendar.ViewUnit.prototype.clear = function() {
586 if(this.header)this.header.innerHTML = '';
587
588 // clear body
589 while(this.body.childNodes.length)
590 this.body.removeChild(this.body.childNodes[0]);
591}
592
593Calendar.ViewUnit.prototype.set_display = function() {
594 var cn = '#FFF';
595
596 // colors
597 var col_tod_sel = '#EEE';
598 var col_tod = '#FFF';
599 var col_sel = '#EEF';
600
601 if(this.is_today) {
602 if(this.selected) cn = col_tod_sel;
603 else cn = col_tod;
604 } else
605 if(this.selected) cn = col_sel;
606
607 if(this.header) {
608 if(this.is_disabled) {
609 this.body.className = this.default_class + ' cal_vu_disabled';
610 this.header.style.color = '#BBB';
611 } else {
612 this.body.className = this.default_class;
613 this.header.style.color = '#000';
614 }
615
616 if(this.day&&this.day.getDay()==0)
617 this.header.style.backgroundColor = '#FEE';
618 else
619 this.header.style.backgroundColor = '';
620 }
621 this.parent.style.backgroundColor = cn;
622}
623
624Calendar.ViewUnit.prototype.is_selected = function() {
625 return (same_day(this.day, erpnext.calendar.selected_date)
626 && this.hour==erpnext.calendar.selected_hour)
627}
628
629Calendar.ViewUnit.prototype.get_event_list = function() {
630 var y = this.day.getFullYear();
631 var m = this.day.getMonth();
632 var d = this.day.getDate();
633 if(erpnext.calendar.events[y] && erpnext.calendar.events[y][m] &&
634 erpnext.calendar.events[y][m][d] &&
635 erpnext.calendar.events[y][m][d][this.hour]) {
636 return erpnext.calendar.events[y][m][d][this.hour];
637 } else
638 return [];
639}
640
641Calendar.ViewUnit.prototype.refresh = function() {
642 this.clear();
643
644 if(this.is_selected()) {
645 if(erpnext.calendar.cur_vu)erpnext.calendar.cur_vu.deselect();
646 this.selected = true;
647 erpnext.calendar.cur_vu = this;
648 }
649 this.set_display();
650 this.el = this.get_event_list();
651 if(this.onrefresh)this.onrefresh();
652
653 for(var i in this.el) {
654 this.el[i].show(this);
655 }
656
657 var me = this;
658}
659
660Calendar.ViewUnit.prototype.select=function() { this.selected = true; this.set_display(); }
661Calendar.ViewUnit.prototype.deselect=function() { this.selected = false; this.set_display(); }
662Calendar.ViewUnit.prototype.setevent=function() { }
663
664Calendar.MonthViewUnit=function(parent) {
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530665 var me = this;
666 this.header = $("<div class='cal_month_date'></div>")
667 .appendTo(parent)
668 .css({"cursor":"pointer"})
669 .click(function() {
670 me.body.onclick();
671 })
672 .bind("dblclick", function() {
673 me.body.ondblclick();
674 })
675 .get(0);
676
677 this.default_class = "cal_month_unit";
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530678 this.init(parent);
679
680 this.onrefresh = function() {
681 this.header.innerHTML = this.day.getDate();
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530682 }
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530683}
684Calendar.MonthViewUnit.prototype = new Calendar.ViewUnit();
685Calendar.MonthViewUnit.prototype.is_selected = function() {
686 return same_day(this.day, erpnext.calendar.selected_date)
687}
688
689Calendar.MonthViewUnit.prototype.get_event_list = function() {
690 return erpnext.calendar.get_daily_event_list(this.day);
691}
692
693Calendar.DayViewUnit= function(parent) {
694 this.default_class = "cal_day_unit"; this.init(parent);
695}
696Calendar.DayViewUnit.prototype = new Calendar.ViewUnit();
697Calendar.DayViewUnit.prototype.onrefresh = function() {
698 if(this.el.length<3)
699 this.body.style.height = '30px';
700 else this.body.style.height = '';
701}
702
703Calendar.WeekViewUnit=function(parent) {
704 this.default_class = "cal_week_unit"; this.init(parent);
705}
706Calendar.WeekViewUnit.prototype = new Calendar.ViewUnit();
707Calendar.WeekViewUnit.prototype.onrefresh = function() {
708 if(this.el.length<3) this.body.style.height = '30px';
709 else this.body.style.height = '';
710}