blob: 033e906b1fabd7e2a0ca4176d0d1c23157a11656 [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
Rushabh Mehta62275272013-02-14 20:42:33 +053023wn.provide("erpnext.calendar");
24
Rushabh Mehtab73fa492012-02-24 15:07:39 +053025pscript.onload_calendar = function(wrapper) {
Rushabh Mehta1a9f0842013-02-15 12:09:05 +053026
27
28
Rushabh Mehta5eb37642013-02-14 17:34:51 +053029 wn.ui.make_app_page({
30 parent: wrapper,
31 single_column: true,
32 title: 'Calendar'
33 });
Rushabh Mehtab73fa492012-02-24 15:07:39 +053034
Rushabh Mehta5eb37642013-02-14 17:34:51 +053035 wn.require('lib/js/lib/fullcalendar/fullcalendar.css');
36 wn.require('lib/js/lib/fullcalendar/fullcalendar.js');
Rushabh Mehtab73fa492012-02-24 15:07:39 +053037}
38
Rushabh Mehta62275272013-02-14 20:42:33 +053039pscript.onshow_calendar = function(wrapper) {
40 if(!wrapper.setup_complete) {
41 erpnext.calendar.setup(wrapper);
42 } else {
43 $("#fullcalendar").fullCalendar("refetchEvents");
44 }
45}
46
47erpnext.calendar.setup = function(wrapper) {
48 wn.model.with_doctype("Event", function() {
49 $('<div id="fullcalendar">').appendTo($(wrapper).find('.layout-main')).fullCalendar({
50 header: {
51 left: 'prev,next today',
52 center: 'title',
53 right: 'month,agendaWeek,agendaDay'
54 },
55 editable: true,
56 selectable: true,
57 selectHelper: true,
58 events: function(start, end, callback) {
59 wn.call({
60 method: 'utilities.page.calendar.calendar.get_events',
61 type: "GET",
62 args: {
63 start: dateutil.obj_to_str(start),
64 end: dateutil.obj_to_str(end),
65 company: wn.user.get_default("company")[0],
66 employee: wn.user.get_default("employee")[0]
67 },
68 callback: function(r) {
69 var events = r.message;
70 $.each(events, function(i, d) {
71 d.editable = d.owner==user;
72 var options = erpnext.calendar.event_options[d.doctype];
73 if(options && options.prepare)
74 options.prepare(d);
75 });
76 callback(events);
77 }
78 })
79 },
80 eventClick: function(event, jsEvent, view) {
81 // edit event description or delete
82 var options = erpnext.calendar.event_options[event.doctype];
83 if(options && options.click)
84 options.click(event);
85 },
86 eventDrop: function(event, dayDelta, minuteDelta, allDay, revertFunc) {
87 erpnext.calendar.update_event(event);
88 },
89 eventResize: function(event, dayDelta, minuteDelta, allDay, revertFunc) {
90 erpnext.calendar.update_event(event);
91 },
92 select: function(startDate, endDate, allDay, jsEvent, view) {
93 if(jsEvent.day_clicked && view.name=="month")
94 return;
95 var event = wn.model.get_new_doc("Event");
96 event.starts_on = wn.datetime.get_datetime_as_string(startDate);
97 event.ends_on = wn.datetime.get_datetime_as_string(endDate);
98 event.all_day = allDay ? 1 : 0;
99 wn.set_route("Form", "Event", event.name);
100 },
101 dayClick: function(date, allDay, jsEvent, view) {
102 jsEvent.day_clicked = true;
103 $("#fullcalendar").fullCalendar("gotoDate", date)
104 return false;
105 }
106 });
107 });
108
109 wrapper.setup_complete = true;
110
111}
112
113erpnext.calendar.update_event = function(event) {
Rushabh Mehta5eb37642013-02-14 17:34:51 +0530114 wn.model.remove_from_locals("Event", event.id);
115 wn.call({
116 module: "utilities",
117 page: "calendar",
118 method: "update_event",
119 args: {
120 "start": wn.datetime.get_datetime_as_string(event.start),
121 "end": wn.datetime.get_datetime_as_string(event.end),
Rushabh Mehta62275272013-02-14 20:42:33 +0530122 "all_day": event.allDay,
Rushabh Mehta5eb37642013-02-14 17:34:51 +0530123 "name": event.id
124 },
125 callback: function(r) {
126 if(r.exc) {
127 show_alert("Unable to update event.")
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530128 }
129 }
Rushabh Mehta5eb37642013-02-14 17:34:51 +0530130 });
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530131}
132
Rushabh Mehta62275272013-02-14 20:42:33 +0530133erpnext.calendar.event_options = {
134 "Leave Block List Date": {
135 prepare: function(d) {
136 d.color = "#aaa";
137 }
138 },
139 "Event": {
140 prepare: function(d) {
141 if(d.event_type=="Public") {
142 d.color = "#57AF5B";
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530143 }
Rushabh Mehta62275272013-02-14 20:42:33 +0530144 },
145 click: function(event) {
146 wn.set_route("Form", "Event", event.id);
147 }
148 },
149 "Leave Application": {
150 prepare: function(d) {
151 d.color = "#4F9F96";
152 },
153 click: function(event) {
154 if(event.employee==wn.user.get_default("employee")[0]) {
155 wn.set_route("Form", "Leave Application", event.id);
156 }
157 }
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530158 }
159}
160