Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 1 | // 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 | |
| 23 | pscript.onload_calendar = function(wrapper) { |
| 24 | if(!erpnext.calendar) { |
| 25 | erpnext.calendar = new Calendar(); |
| 26 | erpnext.calendar.init(wrapper); |
Rushabh Mehta | 9b1afe1 | 2012-03-20 14:37:44 +0530 | [diff] [blame] | 27 | |
| 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 Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 35 | ///// CALENDAR |
| 36 | |
| 37 | Calendar=function() { |
| 38 | this.views=[]; |
| 39 | this.events = {}; |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 40 | this.events_by_name = {}; |
| 41 | this.weekdays = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat"); |
| 42 | } |
| 43 | |
| 44 | Calendar.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 Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 63 | this.cur_view = this.views['Month']; |
| 64 | this.views['Month'].show(); |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 65 | |
| 66 | } |
| 67 | |
| 68 | Calendar.prototype.rename_notify = function(dt, old_name, new_name) { |
| 69 | // calendar |
Rushabh Mehta | 9b1afe1 | 2012-03-20 14:37:44 +0530 | [diff] [blame] | 70 | if(dt = 'Event'){ |
Rushabh Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 71 | if(this.events_by_name[old_name]) { |
| 72 | delete this.events_by_name[old_name]; |
| 73 | } |
| 74 | } |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | //------------------------------------------------------ |
| 78 | |
| 79 | Calendar.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'] |
| 86 | ,['Check', 'Public Event'] |
| 87 | ,['Check', 'Cancel Event'] |
| 88 | ,['HTML', 'Event Link'] |
| 89 | ,['Button', 'Save'] |
| 90 | ]) |
| 91 | |
| 92 | // show the event when the dialog opens |
| 93 | d.onshow = function() { |
| 94 | // heading |
| 95 | var c = me.selected_date; |
| 96 | var tmp = time_to_ampm(this.ev.event_hour); |
| 97 | tmp = tmp[0]+':'+tmp[1]+' '+tmp[2]; |
| 98 | |
| 99 | this.widgets['Heading'].innerHTML = |
| 100 | '<div style="text-align: center; padding:4px; font-size: 14px">' |
| 101 | + erpnext.calendar.weekdays[c.getDay()] + ', ' + c.getDate() + ' ' + month_list_full[c.getMonth()] + ' ' + c.getFullYear() |
| 102 | + ' - <b>'+tmp+'</b></div>'; |
| 103 | |
| 104 | // set |
| 105 | this.widgets['Description'].value = cstr(this.ev.description); |
| 106 | |
| 107 | this.widgets['Public Event'].checked = false; |
| 108 | this.widgets['Cancel Event'].checked = false; |
| 109 | |
| 110 | if(this.ev.event_type=='Public') |
| 111 | this.widgets['Public Event'].checked = true; |
| 112 | |
| 113 | this.widgets['Event Link'].innerHTML = ''; |
| 114 | |
| 115 | // link |
| 116 | var div = $a(this.widgets['Event Link'], 'div', 'link_type', {margin:'4px 0px'}); |
| 117 | div.onclick = function() { me.event_dialog.hide(); loaddoc('Event', me.event_dialog.ev.name); } |
| 118 | div.innerHTML = 'View Event details, add or edit participants'; |
| 119 | |
| 120 | } |
| 121 | |
| 122 | // event save |
| 123 | d.widgets['Save'].onclick = function() { |
| 124 | var d = me.event_dialog; |
| 125 | |
| 126 | // save values |
| 127 | d.ev.description = d.widgets['Description'].value; |
Rushabh Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 128 | if(d.widgets['Cancel Event'].checked) |
| 129 | d.ev.event_type='Cancel'; |
| 130 | else if(d.widgets['Public Event'].checked) |
| 131 | d.ev.event_type='Public'; |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 132 | |
| 133 | me.event_dialog.hide(); |
| 134 | |
| 135 | // if new event |
Rushabh Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 136 | me.save_event(d.ev); |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 137 | } |
| 138 | this.event_dialog = d; |
| 139 | } |
| 140 | this.event_dialog.ev = ev; |
| 141 | this.event_dialog.cal_ev = cal_ev ? cal_ev : null; |
| 142 | this.event_dialog.show(); |
| 143 | |
| 144 | } |
| 145 | |
Rushabh Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 146 | Calendar.prototype.save_event = function(doc) { |
| 147 | var me = this; |
| 148 | save_doclist('Event', doc.name, 'Save', function(r) { |
| 149 | var doc = locals['Event'][r.docname]; |
| 150 | var cal = erpnext.calendar; |
| 151 | cal.cur_view.refresh(); |
| 152 | |
| 153 | // if cancelled, hide |
| 154 | if(doc.event_type=='Cancel') { |
| 155 | $(cal.events_by_name[doc.name].body).toggle(false); |
| 156 | } |
| 157 | }); |
| 158 | } |
| 159 | |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 160 | //------------------------------------------------------ |
| 161 | |
| 162 | Calendar.prototype.add_event = function() { |
| 163 | |
| 164 | var ev = LocalDB.create('Event'); |
| 165 | ev = locals['Event'][ev]; |
| 166 | |
| 167 | ev.event_date = dateutil.obj_to_str(this.selected_date); |
| 168 | ev.event_hour = this.selected_hour+':00'; |
| 169 | ev.event_type = 'Private'; |
| 170 | |
| 171 | this.show_event(ev); |
| 172 | } |
| 173 | //------------------------------------------------------ |
| 174 | |
| 175 | Calendar.prototype.get_month_events = function(call_back) { |
| 176 | // ret fn |
| 177 | var me = this; |
| 178 | var f = function(r, rt) { |
| 179 | if(me.cur_view) me.cur_view.refresh(); |
| 180 | if(call_back)call_back(); |
| 181 | } |
| 182 | |
| 183 | //load |
| 184 | var y=this.selected_date.getFullYear(); var m = this.selected_date.getMonth(); |
| 185 | if(!this.events[y] || !this.events[y][m]) { |
| 186 | $c('webnotes.widgets.event.load_month_events', args = { |
| 187 | 'month': m + 1, |
| 188 | 'year' : y}, |
| 189 | f); |
| 190 | } |
| 191 | } |
| 192 | //------------------------------------------------------ |
| 193 | |
| 194 | Calendar.prototype.get_daily_event_list=function(day) { |
| 195 | var el = []; |
| 196 | var d = day.getDate(); var m = day.getMonth(); var y = day.getFullYear() |
| 197 | if(this.events[y] && this.events[y][m] && |
| 198 | this.events[y][m][d]) { |
| 199 | var l = this.events[y][m][d] |
| 200 | for(var i in l) { |
| 201 | for(var j in l[i]) el[el.length] = l[i][j]; |
| 202 | } |
| 203 | return el; |
| 204 | } |
| 205 | else return []; |
| 206 | } |
| 207 | //------------------------------------------------------ |
| 208 | |
| 209 | Calendar.prototype.set_event = function(ev) { |
| 210 | // don't duplicate |
| 211 | if(this.events_by_name[ev.name]) { |
Rushabh Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 212 | return this.events_by_name[ev.name]; |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | var dt = dateutil.str_to_obj(ev.event_date); |
| 216 | var m = dt.getMonth(); |
| 217 | var d = dt.getDate(); |
| 218 | var y = dt.getFullYear(); |
| 219 | |
| 220 | if(!this.events[y]) this.events[y] = []; |
| 221 | if(!this.events[y][m]) this.events[y][m] = []; |
| 222 | if(!this.events[y][m][d]) this.events[y][m][d] = []; |
Rushabh Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 223 | if(!this.events[y][m][d][cint(ev.event_hour)]) |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 224 | this.events[y][m][d][cint(ev.event_hour)] = []; |
| 225 | |
| 226 | var cal_ev = new Calendar.CalEvent(ev, this); |
| 227 | this.events[y][m][d][cint(ev.event_hour)].push(cal_ev); |
| 228 | this.events_by_name[ev.name] = cal_ev; |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 229 | |
| 230 | return cal_ev; |
| 231 | } |
Rushabh Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 232 | |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 233 | //------------------------------------------------------ |
| 234 | |
| 235 | Calendar.prototype.refresh = function(viewtype){//Sets the viewtype of the Calendar and Calls the View class based on the viewtype |
| 236 | if(viewtype) |
| 237 | this.viewtype = viewtype; |
| 238 | // switch view if reqd |
| 239 | if(this.cur_view.viewtype!=this.viewtype) { |
| 240 | this.cur_view.hide(); |
| 241 | this.cur_view = this.views[this.viewtype]; |
| 242 | this.cur_view.in_home = false; // for home page |
| 243 | this.cur_view.show(); |
| 244 | } |
| 245 | else{ |
| 246 | this.cur_view.refresh(this); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | //------------------------------------------------------ |
| 251 | |
| 252 | Calendar.CalEvent= function(doc, cal) { |
| 253 | this.body = document.createElement('div'); |
Rushabh Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 254 | this.link = $a(this.body, 'a', '', {}, locals['Event'][doc.name].description || ''); |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 255 | this.doc = doc; |
| 256 | var me = this; |
| 257 | |
Rushabh Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 258 | this.link.onclick = function() { |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 259 | if(me.doc.name) { |
| 260 | cal.show_event(me.doc, me); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | Calendar.CalEvent.prototype.show = function(vu) { |
| 266 | |
| 267 | var t = this.doc.event_type; |
Rushabh Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 268 | this.my_class = 'cal_event_'+ t; |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 269 | |
| 270 | if(this.body.parentNode) |
| 271 | this.body.parentNode.removeChild(this.body); |
| 272 | vu.body.appendChild(this.body); |
| 273 | |
| 274 | // refresh |
Rushabh Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 275 | this.link.innerHTML = this.doc.description || ''; |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 276 | this.body.className = this.my_class; |
| 277 | } |
| 278 | |
Rushabh Mehta | aa999df | 2012-02-27 12:54:04 +0530 | [diff] [blame] | 279 | |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 280 | // ---------- |
| 281 | |
| 282 | Calendar.View =function() { this.daystep = 0; this.monthstep = 0; } |
| 283 | Calendar.View.prototype.init=function(cal) { |
| 284 | this.cal = cal; |
| 285 | this.body = $a(cal.body, 'div', 'cal_view_body'); |
| 286 | this.body.style.display = 'none'; |
| 287 | this.create_table(); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | Calendar.View.prototype.show=function() { |
| 292 | this.get_events(); this.refresh(); this.body.style.display = 'block'; |
| 293 | } |
| 294 | |
| 295 | Calendar.View.prototype.hide=function() { this.body.style.display = 'none';} |
| 296 | |
| 297 | Calendar.View.prototype.next = function() { |
| 298 | var s = this.cal.selected_date; |
| 299 | this.cal.selected_date = new Date(s.getFullYear(), s.getMonth() + this.monthstep, s.getDate() + this.daystep); |
| 300 | this.get_events(); this.refresh(); |
| 301 | } |
| 302 | |
| 303 | Calendar.View.prototype.prev = function() { |
| 304 | var s = this.cal.selected_date; |
| 305 | this.cal.selected_date = new Date(s.getFullYear(), s.getMonth() - this.monthstep, s.getDate() - this.daystep); |
| 306 | this.get_events(); this.refresh(); |
| 307 | } |
| 308 | |
| 309 | Calendar.View.prototype.get_events = function() { |
| 310 | this.cal.get_month_events(); |
| 311 | } |
| 312 | Calendar.View.prototype.add_unit = function(vu) { |
| 313 | this.viewunits[this.viewunits.length] = vu; |
| 314 | } |
| 315 | Calendar.View.prototype.refresh_units = function() { |
| 316 | // load the events |
| 317 | if(locals['Event']) { |
| 318 | for(var name in locals['Event']) { |
| 319 | this.cal.set_event(locals['Event'][name]); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | |
| 324 | for(var r in this.table.rows) { |
| 325 | for(var c in this.table.rows[r].cells) { |
| 326 | if(this.table.rows[r].cells[c].viewunit) { |
| 327 | this.table.rows[r].cells[c].viewunit.refresh(); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // ................. Month View.......................... |
| 334 | Calendar.MonthView = function(cal) { this.init(cal); this.monthstep = 1; this.rows = 5; this.cells = 7; } |
| 335 | Calendar.MonthView.prototype=new Calendar.View(); |
| 336 | Calendar.MonthView.prototype.create_table = function() { |
| 337 | |
| 338 | // create head |
| 339 | this.head_wrapper = $a(this.body, 'div', 'cal_month_head'); |
| 340 | |
| 341 | // create headers |
| 342 | this.headtable = $a(this.head_wrapper, 'table', 'cal_month_headtable'); |
| 343 | var r = this.headtable.insertRow(0); |
| 344 | for(var j=0;j<7;j++) { |
| 345 | var cell = r.insertCell(j); |
| 346 | cell.innerHTML = erpnext.calendar.weekdays[j]; $w(cell, (100 / 7) + '%'); |
| 347 | } |
| 348 | |
| 349 | this.main = $a(this.body, 'div', 'cal_month_body'); |
| 350 | this.table = $a(this.main, 'table', 'cal_month_table'); |
| 351 | var me = this; |
| 352 | |
| 353 | // create body |
| 354 | for(var i=0;i<5;i++) { |
| 355 | var r = this.table.insertRow(i); |
| 356 | for(var j=0;j<7;j++) { |
| 357 | var cell = r.insertCell(j); |
| 358 | cell.viewunit = new Calendar.MonthViewUnit(cell); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | Calendar.MonthView.prototype.refresh = function() { |
| 364 | var c =this.cal.selected_date; |
| 365 | var me=this; |
| 366 | // fill other days |
| 367 | |
| 368 | var cur_row = 0; |
| 369 | |
| 370 | var cur_month = c.getMonth(); |
| 371 | var cur_year = c.getFullYear(); |
| 372 | |
| 373 | var d = new Date(cur_year, cur_month, 1); |
| 374 | var day = 1 - d.getDay(); |
| 375 | |
| 376 | |
| 377 | // set day headers |
| 378 | var d = new Date(cur_year, cur_month, day); |
| 379 | |
| 380 | this.cal.view_title.innerHTML = month_list_full[cur_month] + ' ' + cur_year; |
| 381 | |
| 382 | for(var i=0;i<6;i++) { |
| 383 | if((i<5) || cur_month==d.getMonth()) { // if this month |
| 384 | for(var j=0;j<7;j++) { |
| 385 | var cell = this.table.rows[cur_row].cells[j]; |
| 386 | |
| 387 | if((i<5) || cur_month==d.getMonth()) { // if this month |
| 388 | cell.viewunit.day = d; |
| 389 | cell.viewunit.hour = 8; |
| 390 | if(cur_month == d.getMonth()) { |
| 391 | cell.viewunit.is_disabled = false; |
| 392 | |
| 393 | if(same_day(this.cal.todays_date, d)) |
| 394 | cell.viewunit.is_today = true; |
| 395 | else |
| 396 | cell.viewunit.is_today = false; |
| 397 | |
| 398 | } else { |
| 399 | cell.viewunit.is_disabled = true; |
| 400 | } |
| 401 | } |
| 402 | // new date |
| 403 | day++; |
| 404 | d = new Date(cur_year, cur_month, day); |
| 405 | } |
| 406 | } |
| 407 | cur_row++; |
| 408 | if(cur_row == 5) {cur_row = 0;} // back to top |
| 409 | } |
| 410 | this.refresh_units(); |
| 411 | |
| 412 | } |
| 413 | // ................. Daily View.......................... |
| 414 | Calendar.DayView=function(cal){ this.init(cal); this.daystep = 1; } |
| 415 | Calendar.DayView.prototype=new Calendar.View(); |
| 416 | Calendar.DayView.prototype.create_table = function() { |
| 417 | |
| 418 | // create body |
| 419 | this.main = $a(this.body, 'div', 'cal_day_body'); |
| 420 | this.table = $a(this.main, 'table', 'cal_day_table'); |
| 421 | var me = this; |
| 422 | |
| 423 | for(var i=0;i<24;i++) { |
| 424 | var r = this.table.insertRow(i); |
| 425 | for(var j=0;j<2;j++) { |
| 426 | var cell = r.insertCell(j); |
| 427 | if(j==0) { |
| 428 | var tmp = time_to_ampm((i)+':00'); |
| 429 | cell.innerHTML = tmp[0]+':'+tmp[1]+' '+tmp[2]; |
| 430 | $w(cell, '10%'); |
| 431 | } else { |
| 432 | cell.viewunit = new Calendar.DayViewUnit(cell); |
| 433 | cell.viewunit.hour = i; |
| 434 | $w(cell, '90%'); |
| 435 | if((i>=7)&&(i<=20)) { |
| 436 | cell.viewunit.is_daytime = true; |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | Calendar.DayView.prototype.refresh = function() { |
| 444 | var c =this.cal.selected_date; |
| 445 | |
| 446 | // fill other days |
| 447 | var me=this; |
| 448 | |
| 449 | this.cal.view_title.innerHTML = erpnext.calendar.weekdays[c.getDay()] + ', ' |
| 450 | + c.getDate() + ' ' + month_list_full[c.getMonth()] + ' ' + c.getFullYear(); |
| 451 | |
| 452 | // headers |
| 453 | var d = c; |
| 454 | |
| 455 | for(var i=0;i<24;i++) { |
| 456 | var cell = this.table.rows[i].cells[1]; |
| 457 | if(same_day(this.cal.todays_date, d)) cell.viewunit.is_today = true; |
| 458 | else cell.viewunit.is_today = false; |
| 459 | cell.viewunit.day = d; |
| 460 | } |
| 461 | this.refresh_units(); |
| 462 | } |
| 463 | |
| 464 | // ................. Weekly View.......................... |
| 465 | Calendar.WeekView=function(cal) { this.init(cal); this.daystep = 7; } |
| 466 | Calendar.WeekView.prototype=new Calendar.View(); |
| 467 | Calendar.WeekView.prototype.create_table = function() { |
| 468 | |
| 469 | // create head |
| 470 | this.head_wrapper = $a(this.body, 'div', 'cal_month_head'); |
| 471 | |
| 472 | // day headers |
| 473 | this.headtable = $a(this.head_wrapper, 'table', 'cal_month_headtable'); |
| 474 | var r = this.headtable.insertRow(0); |
| 475 | for(var j=0;j<8;j++) { |
| 476 | var cell = r.insertCell(j); |
| 477 | $w(cell, (100 / 8) + '%'); |
| 478 | } |
| 479 | |
| 480 | // hour header |
| 481 | |
| 482 | // create body |
| 483 | this.main = $a(this.body, 'div', 'cal_week_body'); |
| 484 | this.table = $a(this.main, 'table', 'cal_week_table'); |
| 485 | var me = this; |
| 486 | |
| 487 | for(var i=0;i<24;i++) { |
| 488 | var r = this.table.insertRow(i); |
| 489 | for(var j=0;j<8;j++) { |
| 490 | var cell = r.insertCell(j); |
| 491 | if(j==0) { |
| 492 | var tmp = time_to_ampm(i+':00'); |
| 493 | cell.innerHTML = tmp[0]+':'+tmp[1]+' '+tmp[2]; |
| 494 | |
| 495 | $w(cell, '10%'); |
| 496 | } else { |
| 497 | cell.viewunit = new Calendar.WeekViewUnit(cell); |
| 498 | cell.viewunit.hour = i; |
| 499 | if((i>=7)&&(i<=20)) { |
| 500 | cell.viewunit.is_daytime = true; |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | Calendar.WeekView.prototype.refresh = function() { |
| 508 | var c =this.cal.selected_date; |
| 509 | // fill other days |
| 510 | var me=this; |
| 511 | |
| 512 | this.cal.view_title.innerHTML = month_list_full[c.getMonth()] + ' ' + c.getFullYear(); |
| 513 | |
| 514 | // headers |
| 515 | var d = new Date(c.getFullYear(), c.getMonth(), c.getDate() - c.getDay()); |
| 516 | |
| 517 | for (var k=1;k<8;k++) { |
| 518 | this.headtable.rows[0].cells[k].innerHTML = erpnext.calendar.weekdays[d.getDay()] + ' ' + d.getDate(); |
| 519 | |
| 520 | for(var i=0;i<24;i++) { |
| 521 | var cell = this.table.rows[i].cells[k]; |
| 522 | if(same_day(this.cal.todays_date, d)) |
| 523 | cell.viewunit.is_today = true; |
| 524 | else cell.viewunit.is_today = false; |
| 525 | |
| 526 | cell.viewunit.day = d; |
| 527 | //cell.viewunit.refresh(); |
| 528 | } |
| 529 | d=new Date(d.getFullYear(),d.getMonth(),d.getDate() + 1); |
| 530 | |
| 531 | } |
| 532 | |
| 533 | this.refresh_units(); |
| 534 | } |
| 535 | |
| 536 | //------------------------------------------------------. |
| 537 | |
| 538 | Calendar.ViewUnit = function() {} |
| 539 | Calendar.ViewUnit.prototype.init = function(parent) { |
| 540 | parent.style.border = "1px solid #CCC" ; |
| 541 | this.body = $a(parent, 'div', this.default_class); |
| 542 | this.parent = parent; |
| 543 | |
| 544 | var me = this; |
| 545 | this.body.onclick = function() { |
| 546 | erpnext.calendar.selected_date = me.day; |
| 547 | erpnext.calendar.selected_hour = me.hour; |
| 548 | |
| 549 | if(erpnext.calendar.cur_vu && erpnext.calendar.cur_vu!=me){ |
| 550 | erpnext.calendar.cur_vu.deselect(); |
| 551 | me.select(); |
| 552 | erpnext.calendar.cur_vu = me; |
| 553 | } |
| 554 | } |
| 555 | this.body.ondblclick = function() { |
| 556 | erpnext.calendar.add_event(); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | Calendar.ViewUnit.prototype.set_header=function(v) { |
| 561 | this.header.innerHTML = v; |
| 562 | } |
| 563 | |
| 564 | Calendar.ViewUnit.prototype.set_today = function() { |
| 565 | this.is_today = true; |
| 566 | this.set_display(); |
| 567 | } |
| 568 | |
| 569 | Calendar.ViewUnit.prototype.clear = function() { |
| 570 | if(this.header)this.header.innerHTML = ''; |
| 571 | |
| 572 | // clear body |
| 573 | while(this.body.childNodes.length) |
| 574 | this.body.removeChild(this.body.childNodes[0]); |
| 575 | } |
| 576 | |
| 577 | Calendar.ViewUnit.prototype.set_display = function() { |
| 578 | var cn = '#FFF'; |
| 579 | |
| 580 | // colors |
| 581 | var col_tod_sel = '#EEE'; |
| 582 | var col_tod = '#FFF'; |
| 583 | var col_sel = '#EEF'; |
| 584 | |
| 585 | if(this.is_today) { |
| 586 | if(this.selected) cn = col_tod_sel; |
| 587 | else cn = col_tod; |
| 588 | } else |
| 589 | if(this.selected) cn = col_sel; |
| 590 | |
| 591 | if(this.header) { |
| 592 | if(this.is_disabled) { |
| 593 | this.body.className = this.default_class + ' cal_vu_disabled'; |
| 594 | this.header.style.color = '#BBB'; |
| 595 | } else { |
| 596 | this.body.className = this.default_class; |
| 597 | this.header.style.color = '#000'; |
| 598 | } |
| 599 | |
| 600 | if(this.day&&this.day.getDay()==0) |
| 601 | this.header.style.backgroundColor = '#FEE'; |
| 602 | else |
| 603 | this.header.style.backgroundColor = ''; |
| 604 | } |
| 605 | this.parent.style.backgroundColor = cn; |
| 606 | } |
| 607 | |
| 608 | Calendar.ViewUnit.prototype.is_selected = function() { |
| 609 | return (same_day(this.day, erpnext.calendar.selected_date) |
| 610 | && this.hour==erpnext.calendar.selected_hour) |
| 611 | } |
| 612 | |
| 613 | Calendar.ViewUnit.prototype.get_event_list = function() { |
| 614 | var y = this.day.getFullYear(); |
| 615 | var m = this.day.getMonth(); |
| 616 | var d = this.day.getDate(); |
| 617 | if(erpnext.calendar.events[y] && erpnext.calendar.events[y][m] && |
| 618 | erpnext.calendar.events[y][m][d] && |
| 619 | erpnext.calendar.events[y][m][d][this.hour]) { |
| 620 | return erpnext.calendar.events[y][m][d][this.hour]; |
| 621 | } else |
| 622 | return []; |
| 623 | } |
| 624 | |
| 625 | Calendar.ViewUnit.prototype.refresh = function() { |
| 626 | this.clear(); |
| 627 | |
| 628 | if(this.is_selected()) { |
| 629 | if(erpnext.calendar.cur_vu)erpnext.calendar.cur_vu.deselect(); |
| 630 | this.selected = true; |
| 631 | erpnext.calendar.cur_vu = this; |
| 632 | } |
| 633 | this.set_display(); |
| 634 | this.el = this.get_event_list(); |
| 635 | if(this.onrefresh)this.onrefresh(); |
| 636 | |
| 637 | for(var i in this.el) { |
| 638 | this.el[i].show(this); |
| 639 | } |
| 640 | |
| 641 | var me = this; |
| 642 | } |
| 643 | |
| 644 | Calendar.ViewUnit.prototype.select=function() { this.selected = true; this.set_display(); } |
| 645 | Calendar.ViewUnit.prototype.deselect=function() { this.selected = false; this.set_display(); } |
| 646 | Calendar.ViewUnit.prototype.setevent=function() { } |
| 647 | |
| 648 | Calendar.MonthViewUnit=function(parent) { |
| 649 | this.header = $a(parent, 'div' , "cal_month_date"); |
| 650 | this.default_class = "cal_month_unit"; |
| 651 | |
| 652 | this.init(parent); |
| 653 | |
| 654 | this.onrefresh = function() { |
| 655 | this.header.innerHTML = this.day.getDate(); |
| 656 | } |
| 657 | } |
| 658 | Calendar.MonthViewUnit.prototype = new Calendar.ViewUnit(); |
| 659 | Calendar.MonthViewUnit.prototype.is_selected = function() { |
| 660 | return same_day(this.day, erpnext.calendar.selected_date) |
| 661 | } |
| 662 | |
| 663 | Calendar.MonthViewUnit.prototype.get_event_list = function() { |
| 664 | return erpnext.calendar.get_daily_event_list(this.day); |
| 665 | } |
| 666 | |
| 667 | Calendar.DayViewUnit= function(parent) { |
| 668 | this.default_class = "cal_day_unit"; this.init(parent); |
| 669 | } |
| 670 | Calendar.DayViewUnit.prototype = new Calendar.ViewUnit(); |
| 671 | Calendar.DayViewUnit.prototype.onrefresh = function() { |
| 672 | if(this.el.length<3) |
| 673 | this.body.style.height = '30px'; |
| 674 | else this.body.style.height = ''; |
| 675 | } |
| 676 | |
| 677 | Calendar.WeekViewUnit=function(parent) { |
| 678 | this.default_class = "cal_week_unit"; this.init(parent); |
| 679 | } |
| 680 | Calendar.WeekViewUnit.prototype = new Calendar.ViewUnit(); |
| 681 | Calendar.WeekViewUnit.prototype.onrefresh = function() { |
| 682 | if(this.el.length<3) this.body.style.height = '30px'; |
| 683 | else this.body.style.height = ''; |
| 684 | } |