blob: e7abbabb63ca80750e9e424dea01e10289996a1f [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']
88 ,['Check', 'Cancel Event']
89 ,['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;
109 this.widgets['Cancel Event'].checked = false;
110
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 Mehtaaa999df2012-02-27 12:54:04 +0530137 if(d.widgets['Cancel Event'].checked)
138 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;
157 save_doclist('Event', doc.name, 'Save', function(r) {
158 var doc = locals['Event'][r.docname];
159 var cal = erpnext.calendar;
160 cal.cur_view.refresh();
161
162 // if cancelled, hide
163 if(doc.event_type=='Cancel') {
164 $(cal.events_by_name[doc.name].body).toggle(false);
165 }
166 });
167}
168
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530169//------------------------------------------------------
170
171Calendar.prototype.add_event = function() {
172
173 var ev = LocalDB.create('Event');
174 ev = locals['Event'][ev];
175
176 ev.event_date = dateutil.obj_to_str(this.selected_date);
177 ev.event_hour = this.selected_hour+':00';
178 ev.event_type = 'Private';
179
180 this.show_event(ev);
181}
182//------------------------------------------------------
183
184Calendar.prototype.get_month_events = function(call_back) {
185 // ret fn
186 var me = this;
187 var f = function(r, rt) {
188 if(me.cur_view) me.cur_view.refresh();
189 if(call_back)call_back();
190 }
191
192 //load
193 var y=this.selected_date.getFullYear(); var m = this.selected_date.getMonth();
194 if(!this.events[y] || !this.events[y][m]) {
195 $c('webnotes.widgets.event.load_month_events', args = {
196 'month': m + 1,
197 'year' : y},
198 f);
199 }
200}
201//------------------------------------------------------
202
203Calendar.prototype.get_daily_event_list=function(day) {
204 var el = [];
205 var d = day.getDate(); var m = day.getMonth(); var y = day.getFullYear()
206 if(this.events[y] && this.events[y][m] &&
207 this.events[y][m][d]) {
208 var l = this.events[y][m][d]
209 for(var i in l) {
210 for(var j in l[i]) el[el.length] = l[i][j];
211 }
212 return el;
213 }
214 else return [];
215}
216//------------------------------------------------------
217
218Calendar.prototype.set_event = function(ev) {
219 // don't duplicate
220 if(this.events_by_name[ev.name]) {
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530221 return this.events_by_name[ev.name];
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530222 }
223
224 var dt = dateutil.str_to_obj(ev.event_date);
225 var m = dt.getMonth();
226 var d = dt.getDate();
227 var y = dt.getFullYear();
228
229 if(!this.events[y]) this.events[y] = [];
230 if(!this.events[y][m]) this.events[y][m] = [];
231 if(!this.events[y][m][d]) this.events[y][m][d] = [];
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530232 if(!this.events[y][m][d][cint(ev.event_hour)])
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530233 this.events[y][m][d][cint(ev.event_hour)] = [];
234
235 var cal_ev = new Calendar.CalEvent(ev, this);
236 this.events[y][m][d][cint(ev.event_hour)].push(cal_ev);
237 this.events_by_name[ev.name] = cal_ev;
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530238
239 return cal_ev;
240}
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530241
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530242//------------------------------------------------------
243
Rushabh Mehta17db39d2012-11-24 14:45:31 +0530244Calendar.prototype.clear = function() {
245 this.events = {};
246 this.events_by_name = {};
247 locals.Event = {};
248}
249
250Calendar.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 +0530251 if(viewtype)
252 this.viewtype = viewtype;
Rushabh Mehta17db39d2012-11-24 14:45:31 +0530253
254 if(clear_events)
255 this.clear();
256
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530257 // switch view if reqd
258 if(this.cur_view.viewtype!=this.viewtype) {
259 this.cur_view.hide();
260 this.cur_view = this.views[this.viewtype];
261 this.cur_view.in_home = false; // for home page
262 this.cur_view.show();
263 }
264 else{
Rushabh Mehta17db39d2012-11-24 14:45:31 +0530265 this.cur_view.get_events();
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530266 this.cur_view.refresh(this);
267 }
268}
269
270//------------------------------------------------------
271
272Calendar.CalEvent= function(doc, cal) {
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530273 var me = this;
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530274 me.doc = doc;
275
276 this.body = $("<div class='label cal_event'></div>")
277 .html(doc.description)
Rushabh Mehtaa44b66e2012-11-23 16:34:42 +0530278 .attr("title", doc.description)
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530279 .css({"cursor":"pointer"})
280 .attr("data-event", doc.name)
281 .click(function() {
282 var doc = locals["Event"][$(this).attr("data-event")];
283 cal.show_event(doc, me);
284 })
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530285
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530286 this.show = function(vu) {
287 me.body
288 .html(me.doc.description)
289 .css({"width": ($(vu.body).width()-10)})
290 .appendTo(vu.body)
291 .removeClass("label-success").removeClass("label-info")
292 .addClass(me.doc.event_type=="Public" ? "label-success" : "label-info")
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530293 }
294}
295
Rushabh Mehtaaa999df2012-02-27 12:54:04 +0530296
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530297// ----------
298
299Calendar.View =function() { this.daystep = 0; this.monthstep = 0; }
300Calendar.View.prototype.init=function(cal) {
301 this.cal = cal;
302 this.body = $a(cal.body, 'div', 'cal_view_body');
303 this.body.style.display = 'none';
304 this.create_table();
305}
306
307
308Calendar.View.prototype.show=function() {
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530309 this.body.style.display = 'block';
310 this.get_events(); this.refresh();
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530311}
312
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530313Calendar.View.prototype.hide=function() {
314 this.body.style.display = 'none';
315}
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530316
317Calendar.View.prototype.next = function() {
318 var s = this.cal.selected_date;
319 this.cal.selected_date = new Date(s.getFullYear(), s.getMonth() + this.monthstep, s.getDate() + this.daystep);
320 this.get_events(); this.refresh();
321}
322
323Calendar.View.prototype.prev = function() {
324 var s = this.cal.selected_date;
325 this.cal.selected_date = new Date(s.getFullYear(), s.getMonth() - this.monthstep, s.getDate() - this.daystep);
326 this.get_events(); this.refresh();
327}
328
329Calendar.View.prototype.get_events = function() {
330 this.cal.get_month_events();
331}
332Calendar.View.prototype.add_unit = function(vu) {
333 this.viewunits[this.viewunits.length] = vu;
334}
335Calendar.View.prototype.refresh_units = function() {
336 // load the events
337 if(locals['Event']) {
338 for(var name in locals['Event']) {
339 this.cal.set_event(locals['Event'][name]);
340 }
341 }
342
343
344 for(var r in this.table.rows) {
345 for(var c in this.table.rows[r].cells) {
346 if(this.table.rows[r].cells[c].viewunit) {
347 this.table.rows[r].cells[c].viewunit.refresh();
348 }
349 }
350 }
351}
352
353// ................. Month View..........................
354Calendar.MonthView = function(cal) { this.init(cal); this.monthstep = 1; this.rows = 5; this.cells = 7; }
355Calendar.MonthView.prototype=new Calendar.View();
356Calendar.MonthView.prototype.create_table = function() {
357
358 // create head
359 this.head_wrapper = $a(this.body, 'div', 'cal_month_head');
360
361 // create headers
362 this.headtable = $a(this.head_wrapper, 'table', 'cal_month_headtable');
363 var r = this.headtable.insertRow(0);
364 for(var j=0;j<7;j++) {
365 var cell = r.insertCell(j);
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530366 cell.innerHTML = erpnext.calendar.weekdays[j];
367 $w(cell, (100 / 7) + '%');
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530368 }
369
370 this.main = $a(this.body, 'div', 'cal_month_body');
371 this.table = $a(this.main, 'table', 'cal_month_table');
372 var me = this;
373
374 // create body
375 for(var i=0;i<5;i++) {
376 var r = this.table.insertRow(i);
377 for(var j=0;j<7;j++) {
378 var cell = r.insertCell(j);
379 cell.viewunit = new Calendar.MonthViewUnit(cell);
380 }
381 }
382}
383
384Calendar.MonthView.prototype.refresh = function() {
385 var c =this.cal.selected_date;
386 var me=this;
387 // fill other days
388
389 var cur_row = 0;
390
391 var cur_month = c.getMonth();
392 var cur_year = c.getFullYear();
393
394 var d = new Date(cur_year, cur_month, 1);
395 var day = 1 - d.getDay();
396
397
398 // set day headers
399 var d = new Date(cur_year, cur_month, day);
400
401 this.cal.view_title.innerHTML = month_list_full[cur_month] + ' ' + cur_year;
402
403 for(var i=0;i<6;i++) {
404 if((i<5) || cur_month==d.getMonth()) { // if this month
405 for(var j=0;j<7;j++) {
406 var cell = this.table.rows[cur_row].cells[j];
407
408 if((i<5) || cur_month==d.getMonth()) { // if this month
409 cell.viewunit.day = d;
410 cell.viewunit.hour = 8;
411 if(cur_month == d.getMonth()) {
412 cell.viewunit.is_disabled = false;
413
414 if(same_day(this.cal.todays_date, d))
415 cell.viewunit.is_today = true;
416 else
417 cell.viewunit.is_today = false;
418
419 } else {
420 cell.viewunit.is_disabled = true;
421 }
422 }
423 // new date
424 day++;
425 d = new Date(cur_year, cur_month, day);
426 }
427 }
428 cur_row++;
429 if(cur_row == 5) {cur_row = 0;} // back to top
430 }
431 this.refresh_units();
432
433}
434 // ................. Daily View..........................
435Calendar.DayView=function(cal){ this.init(cal); this.daystep = 1; }
436Calendar.DayView.prototype=new Calendar.View();
437Calendar.DayView.prototype.create_table = function() {
438
439 // create body
440 this.main = $a(this.body, 'div', 'cal_day_body');
441 this.table = $a(this.main, 'table', 'cal_day_table');
442 var me = this;
443
444 for(var i=0;i<24;i++) {
445 var r = this.table.insertRow(i);
446 for(var j=0;j<2;j++) {
447 var cell = r.insertCell(j);
448 if(j==0) {
449 var tmp = time_to_ampm((i)+':00');
450 cell.innerHTML = tmp[0]+':'+tmp[1]+' '+tmp[2];
451 $w(cell, '10%');
452 } else {
453 cell.viewunit = new Calendar.DayViewUnit(cell);
454 cell.viewunit.hour = i;
455 $w(cell, '90%');
456 if((i>=7)&&(i<=20)) {
457 cell.viewunit.is_daytime = true;
458 }
459 }
460 }
461 }
462 }
463
464Calendar.DayView.prototype.refresh = function() {
465 var c =this.cal.selected_date;
466
467 // fill other days
468 var me=this;
469
470 this.cal.view_title.innerHTML = erpnext.calendar.weekdays[c.getDay()] + ', '
471 + c.getDate() + ' ' + month_list_full[c.getMonth()] + ' ' + c.getFullYear();
472
473 // headers
474 var d = c;
475
476 for(var i=0;i<24;i++) {
477 var cell = this.table.rows[i].cells[1];
478 if(same_day(this.cal.todays_date, d)) cell.viewunit.is_today = true;
479 else cell.viewunit.is_today = false;
480 cell.viewunit.day = d;
481 }
482 this.refresh_units();
483}
484
485// ................. Weekly View..........................
486Calendar.WeekView=function(cal) { this.init(cal); this.daystep = 7; }
487Calendar.WeekView.prototype=new Calendar.View();
488Calendar.WeekView.prototype.create_table = function() {
489
490 // create head
491 this.head_wrapper = $a(this.body, 'div', 'cal_month_head');
492
493 // day headers
494 this.headtable = $a(this.head_wrapper, 'table', 'cal_month_headtable');
495 var r = this.headtable.insertRow(0);
496 for(var j=0;j<8;j++) {
497 var cell = r.insertCell(j);
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530498 }
499
500 // hour header
501
502 // create body
503 this.main = $a(this.body, 'div', 'cal_week_body');
504 this.table = $a(this.main, 'table', 'cal_week_table');
505 var me = this;
506
507 for(var i=0;i<24;i++) {
508 var r = this.table.insertRow(i);
509 for(var j=0;j<8;j++) {
510 var cell = r.insertCell(j);
511 if(j==0) {
512 var tmp = time_to_ampm(i+':00');
513 cell.innerHTML = tmp[0]+':'+tmp[1]+' '+tmp[2];
514
515 $w(cell, '10%');
516 } else {
517 cell.viewunit = new Calendar.WeekViewUnit(cell);
518 cell.viewunit.hour = i;
519 if((i>=7)&&(i<=20)) {
520 cell.viewunit.is_daytime = true;
521 }
522 }
523 }
524 }
525}
526
527Calendar.WeekView.prototype.refresh = function() {
528 var c =this.cal.selected_date;
529 // fill other days
530 var me=this;
531
532 this.cal.view_title.innerHTML = month_list_full[c.getMonth()] + ' ' + c.getFullYear();
533
534 // headers
535 var d = new Date(c.getFullYear(), c.getMonth(), c.getDate() - c.getDay());
536
537 for (var k=1;k<8;k++) {
538 this.headtable.rows[0].cells[k].innerHTML = erpnext.calendar.weekdays[d.getDay()] + ' ' + d.getDate();
539
540 for(var i=0;i<24;i++) {
541 var cell = this.table.rows[i].cells[k];
542 if(same_day(this.cal.todays_date, d))
543 cell.viewunit.is_today = true;
544 else cell.viewunit.is_today = false;
545
546 cell.viewunit.day = d;
547 //cell.viewunit.refresh();
548 }
549 d=new Date(d.getFullYear(),d.getMonth(),d.getDate() + 1);
550
551 }
552
553 this.refresh_units();
554}
555
556//------------------------------------------------------.
557
558Calendar.ViewUnit = function() {}
559Calendar.ViewUnit.prototype.init = function(parent) {
560 parent.style.border = "1px solid #CCC" ;
561 this.body = $a(parent, 'div', this.default_class);
562 this.parent = parent;
563
564 var me = this;
565 this.body.onclick = function() {
566 erpnext.calendar.selected_date = me.day;
567 erpnext.calendar.selected_hour = me.hour;
568
569 if(erpnext.calendar.cur_vu && erpnext.calendar.cur_vu!=me){
570 erpnext.calendar.cur_vu.deselect();
571 me.select();
572 erpnext.calendar.cur_vu = me;
573 }
574 }
575 this.body.ondblclick = function() {
576 erpnext.calendar.add_event();
577 }
578}
579
580Calendar.ViewUnit.prototype.set_header=function(v) {
581 this.header.innerHTML = v;
582}
583
584Calendar.ViewUnit.prototype.set_today = function() {
585 this.is_today = true;
586 this.set_display();
587}
588
589Calendar.ViewUnit.prototype.clear = function() {
590 if(this.header)this.header.innerHTML = '';
591
592 // clear body
593 while(this.body.childNodes.length)
594 this.body.removeChild(this.body.childNodes[0]);
595}
596
597Calendar.ViewUnit.prototype.set_display = function() {
598 var cn = '#FFF';
599
600 // colors
601 var col_tod_sel = '#EEE';
602 var col_tod = '#FFF';
603 var col_sel = '#EEF';
604
605 if(this.is_today) {
606 if(this.selected) cn = col_tod_sel;
607 else cn = col_tod;
608 } else
609 if(this.selected) cn = col_sel;
610
611 if(this.header) {
612 if(this.is_disabled) {
613 this.body.className = this.default_class + ' cal_vu_disabled';
614 this.header.style.color = '#BBB';
615 } else {
616 this.body.className = this.default_class;
617 this.header.style.color = '#000';
618 }
619
620 if(this.day&&this.day.getDay()==0)
621 this.header.style.backgroundColor = '#FEE';
622 else
623 this.header.style.backgroundColor = '';
624 }
625 this.parent.style.backgroundColor = cn;
626}
627
628Calendar.ViewUnit.prototype.is_selected = function() {
629 return (same_day(this.day, erpnext.calendar.selected_date)
630 && this.hour==erpnext.calendar.selected_hour)
631}
632
633Calendar.ViewUnit.prototype.get_event_list = function() {
634 var y = this.day.getFullYear();
635 var m = this.day.getMonth();
636 var d = this.day.getDate();
637 if(erpnext.calendar.events[y] && erpnext.calendar.events[y][m] &&
638 erpnext.calendar.events[y][m][d] &&
639 erpnext.calendar.events[y][m][d][this.hour]) {
640 return erpnext.calendar.events[y][m][d][this.hour];
641 } else
642 return [];
643}
644
645Calendar.ViewUnit.prototype.refresh = function() {
646 this.clear();
647
648 if(this.is_selected()) {
649 if(erpnext.calendar.cur_vu)erpnext.calendar.cur_vu.deselect();
650 this.selected = true;
651 erpnext.calendar.cur_vu = this;
652 }
653 this.set_display();
654 this.el = this.get_event_list();
655 if(this.onrefresh)this.onrefresh();
656
657 for(var i in this.el) {
658 this.el[i].show(this);
659 }
660
661 var me = this;
662}
663
664Calendar.ViewUnit.prototype.select=function() { this.selected = true; this.set_display(); }
665Calendar.ViewUnit.prototype.deselect=function() { this.selected = false; this.set_display(); }
666Calendar.ViewUnit.prototype.setevent=function() { }
667
668Calendar.MonthViewUnit=function(parent) {
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530669 var me = this;
670 this.header = $("<div class='cal_month_date'></div>")
671 .appendTo(parent)
672 .css({"cursor":"pointer"})
673 .click(function() {
674 me.body.onclick();
675 })
676 .bind("dblclick", function() {
677 me.body.ondblclick();
678 })
679 .get(0);
680
681 this.default_class = "cal_month_unit";
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530682 this.init(parent);
683
684 this.onrefresh = function() {
685 this.header.innerHTML = this.day.getDate();
Rushabh Mehta2e593fa2012-11-23 16:24:56 +0530686 }
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530687}
688Calendar.MonthViewUnit.prototype = new Calendar.ViewUnit();
689Calendar.MonthViewUnit.prototype.is_selected = function() {
690 return same_day(this.day, erpnext.calendar.selected_date)
691}
692
693Calendar.MonthViewUnit.prototype.get_event_list = function() {
694 return erpnext.calendar.get_daily_event_list(this.day);
695}
696
697Calendar.DayViewUnit= function(parent) {
698 this.default_class = "cal_day_unit"; this.init(parent);
699}
700Calendar.DayViewUnit.prototype = new Calendar.ViewUnit();
701Calendar.DayViewUnit.prototype.onrefresh = function() {
702 if(this.el.length<3)
703 this.body.style.height = '30px';
704 else this.body.style.height = '';
705}
706
707Calendar.WeekViewUnit=function(parent) {
708 this.default_class = "cal_week_unit"; this.init(parent);
709}
710Calendar.WeekViewUnit.prototype = new Calendar.ViewUnit();
711Calendar.WeekViewUnit.prototype.onrefresh = function() {
712 if(this.el.length<3) this.body.style.height = '30px';
713 else this.body.style.height = '';
714}