blob: cac5ec8265a278220c763e7c2699822376357862 [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) {
Rushabh Mehta5eb37642013-02-14 17:34:51 +053024 wn.ui.make_app_page({
25 parent: wrapper,
26 single_column: true,
27 title: 'Calendar'
28 });
Rushabh Mehtab73fa492012-02-24 15:07:39 +053029
Rushabh Mehta5eb37642013-02-14 17:34:51 +053030 wn.require('lib/js/lib/fullcalendar/fullcalendar.css');
31 wn.require('lib/js/lib/fullcalendar/fullcalendar.js');
Rushabh Mehtab73fa492012-02-24 15:07:39 +053032}
33
Rushabh Mehta5eb37642013-02-14 17:34:51 +053034pscript.update_event = function(event) {
35 wn.model.remove_from_locals("Event", event.id);
36 wn.call({
37 module: "utilities",
38 page: "calendar",
39 method: "update_event",
40 args: {
41 "start": wn.datetime.get_datetime_as_string(event.start),
42 "end": wn.datetime.get_datetime_as_string(event.end),
43 "name": event.id
44 },
45 callback: function(r) {
46 if(r.exc) {
47 show_alert("Unable to update event.")
Rushabh Mehtab73fa492012-02-24 15:07:39 +053048 }
49 }
Rushabh Mehta5eb37642013-02-14 17:34:51 +053050 });
Rushabh Mehtab73fa492012-02-24 15:07:39 +053051}
52
Rushabh Mehtab73fa492012-02-24 15:07:39 +053053
Rushabh Mehta5eb37642013-02-14 17:34:51 +053054pscript.onshow_calendar = function(wrapper) {
55 if(!wrapper.setup_complete) {
56 $('<div id="fullcalendar">').appendTo($(wrapper).find('.layout-main')).fullCalendar({
57 header: {
58 left: 'prev,next today',
59 center: 'title',
60 right: 'month,agendaWeek,agendaDay'
61 },
62 editable: true,
63 events: function(start, end, callback) {
64 wn.call({
65 method: 'utilities.page.calendar.calendar.get_events',
66 type: "GET",
67 args: {
68 start: dateutil.obj_to_str(start),
69 end: dateutil.obj_to_str(end)
70 },
71 callback: function(r) {
72 var events = r.message;
73 $.each(events, function(i, d) {
74 d.editable = d.owner==user;
75 d.allDay = false;
76 });
77 callback(events);
Rushabh Mehtab73fa492012-02-24 15:07:39 +053078 }
Rushabh Mehta5eb37642013-02-14 17:34:51 +053079 })
80 },
81 dayClick: function(date, allDay, jsEvent, view) {
82 // if current date, show popup to create a new event
83 var ev = wn.model.create('Event')
84 ev.doc.set('start', date);
85 ev.doc.set('end', new Date(date));
86 ev.doc.set('all_day', 1);
Rushabh Mehtab73fa492012-02-24 15:07:39 +053087
Rushabh Mehta5eb37642013-02-14 17:34:51 +053088 },
89 eventClick: function(calEvent, jsEvent, view) {
90 // edit event description or delete
91 wn.set_route("Form", "Event", calEvent.id);
92 },
93 eventDrop: function(event, dayDelta, minuteDelta, allDay, revertFunc) {
94 pscript.update_event(event);
95 },
96 eventResize: function(event, dayDelta, minuteDelta, allDay, revertFunc) {
97 pscript.update_event(event);
Rushabh Mehtab73fa492012-02-24 15:07:39 +053098 }
Rushabh Mehta5eb37642013-02-14 17:34:51 +053099 });
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530100
Rushabh Mehta5eb37642013-02-14 17:34:51 +0530101 wrapper.setup_complete = true;
102 } else {
103 $("#fullcalendar").fullCalendar("refetchEvents");
Rushabh Mehtab73fa492012-02-24 15:07:39 +0530104 }
105}
106