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