blob: 89c1ed8024bcb3504f406ff2989680e4742f32a1 [file] [log] [blame]
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +05301// ERPNext - web based ERP (http://erpnext.com)
2// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
Rushabh Mehta2886b952012-02-24 11:26:31 +053017wn.provide('erpnext.todo');
18
19erpnext.todo.refresh = function() {
20 wn.call({
21 method: 'utilities.page.todo.todo.get',
22 callback: function(r,rt) {
Anand Doshi47afacc2012-06-13 18:02:43 +053023 var todo_list = $('#todo-list div.todo-content');
24 var assigned_todo_list = $('#assigned-todo-list div.todo-content');
25 todo_list.empty();
26 assigned_todo_list.empty();
27
28 var nothing_to_do = function() {
29 $('#todo-list div.todo-content')
30 .html('<div class="help-box">Nothing to do :)</div>');
31 }
32
33 var nothing_delegated = function() {
34 $('#assigned-todo-list div.todo-content')
35 .html('<div class="help-box">Nothing assigned to other users. \
36 Use "Assign To" in a form to delegate work.</div>');
37 }
38
Rushabh Mehta2886b952012-02-24 11:26:31 +053039 if(r.message) {
40 for(var i in r.message) {
41 new erpnext.todo.ToDoItem(r.message[i]);
42 }
Anand Doshi47afacc2012-06-13 18:02:43 +053043 if (!todo_list.html()) { nothing_to_do(); }
44 if (!assigned_todo_list.html()) { nothing_delegated(); }
Rushabh Mehta2886b952012-02-24 11:26:31 +053045 } else {
Anand Doshi47afacc2012-06-13 18:02:43 +053046 nothing_to_do();
47 nothing_delegated();
Rushabh Mehta2886b952012-02-24 11:26:31 +053048 }
49 }
50 });
Rushabh Mehta2886b952012-02-24 11:26:31 +053051}
52
53erpnext.todo.ToDoItem = Class.extend({
54 init: function(todo) {
55 label_map = {
56 'High': 'label-important',
57 'Medium': 'label-info',
58 'Low':''
59 }
60 todo.labelclass = label_map[todo.priority];
Anand Doshi29ed95b2012-03-21 18:02:17 +053061 todo.userdate = dateutil.str_to_user(todo.date) || '';
Anand Doshi64ddb9f2012-06-12 19:24:12 +053062
63 todo.fullname = '';
Anand Doshi06fcfdf2012-03-22 11:01:38 +053064 if(todo.assigned_by) {
Anand Doshi616c3552012-06-14 16:46:48 +053065 var assigned_by = wn.boot.user_info[todo.assigned_by]
Anand Doshi3c3f9962012-06-13 12:01:41 +053066 todo.fullname = repl("[By %(fullname)s] &nbsp;", {
Anand Doshi616c3552012-06-14 16:46:48 +053067 fullname: (assigned_by ? assigned_by.fullname : todo.assigned_by),
Anand Doshi64ddb9f2012-06-12 19:24:12 +053068 });
69 }
70
71 var parent_list = "#todo-list";
72 if(todo.owner !== user) {
73 parent_list = "#assigned-todo-list";
Anand Doshi616c3552012-06-14 16:46:48 +053074 var owner = wn.boot.user_info[todo.owner];
Anand Doshi3c3f9962012-06-13 12:01:41 +053075 todo.fullname = repl("[To %(fullname)s] &nbsp;", {
Anand Doshi616c3552012-06-14 16:46:48 +053076 fullname: (owner ? owner.fullname : todo.owner),
Anand Doshi64ddb9f2012-06-12 19:24:12 +053077 });
78 }
79 parent_list += " div.todo-content";
80
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053081 if(todo.reference_name && todo.reference_type) {
82 todo.link = repl('<a href="#!Form/%(reference_type)s/%(reference_name)s">\
Anand Doshi29ed95b2012-03-21 18:02:17 +053083 %(reference_type)s: %(reference_name)s</a>', todo);
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053084 } else if(todo.reference_type) {
85 todo.link = repl('<a href="#!List/%(reference_type)s">\
86 %(reference_type)s</a>', todo);
87 } else {
88 todo.link = '';
89 }
Anand Doshi130dfae2012-04-03 12:18:35 +053090 if(!todo.description) todo.description = '';
Anand Doshi64ddb9f2012-06-12 19:24:12 +053091
Rushabh Mehta6252c132012-08-07 12:53:49 +053092 todo.desc = wn.markdown(todo.description);
Anand Doshi64ddb9f2012-06-12 19:24:12 +053093
94 $(parent_list).append(repl('\
95 <div class="todoitem">\
96 <span class="label %(labelclass)s">%(priority)s</span>\
Rushabh Mehta2886b952012-02-24 11:26:31 +053097 <span class="description">\
Anand Doshid104f4b2012-06-13 17:01:52 +053098 <span class="popup-on-click">\
Rushabh Mehta2886b952012-02-24 11:26:31 +053099 <span class="help" style="margin-right: 7px">%(userdate)s</span>\
Anand Doshi64ddb9f2012-06-12 19:24:12 +0530100 %(fullname)s%(desc)s\
Anand Doshid104f4b2012-06-13 17:01:52 +0530101 </span>\
Anand Doshi64ddb9f2012-06-12 19:24:12 +0530102 <span class="ref_link"><br>\
Anand Doshi0b8ac7f2012-02-29 18:33:39 +0530103 %(link)s</span>\
Anand Doshi64ddb9f2012-06-12 19:24:12 +0530104 </span>\
105 <span class="close-span"><a href="#" class="close">&times;</a></span>\
106 </div>\
107 <div class="todo-separator"></div>', todo));
108 $todo = $(parent_list + ' div.todoitem:last');
Rushabh Mehta2886b952012-02-24 11:26:31 +0530109
110 if(todo.checked) {
111 $todo.find('.description').css('text-decoration', 'line-through');
112 }
113
Anand Doshi0b8ac7f2012-02-29 18:33:39 +0530114 if(!todo.reference_type)
Rushabh Mehta2886b952012-02-24 11:26:31 +0530115 $todo.find('.ref_link').toggle(false);
116
Anand Doshid104f4b2012-06-13 17:01:52 +0530117 $todo.find('.popup-on-click')
Rushabh Mehta2886b952012-02-24 11:26:31 +0530118 .data('todo', todo)
119 .click(function() {
120 erpnext.todo.make_dialog($(this).data('todo'));
121 return false;
122 });
123
124 $todo.find('.close')
125 .data('name', todo.name)
126 .click(function() {
127 $(this).parent().css('opacity', 0.5);
128 wn.call({
129 method:'utilities.page.todo.todo.delete',
130 args: {name: $(this).data('name')},
131 callback: function() {
132 erpnext.todo.refresh();
133 }
134 });
135 return false;
136 })
137 }
138});
139
140erpnext.todo.make_dialog = function(det) {
141 if(!erpnext.todo.dialog) {
142 var dialog = new wn.widgets.Dialog();
143 dialog.make({
144 width: 480,
145 title: 'To Do',
146 fields: [
Rushabh Mehta6252c132012-08-07 12:53:49 +0530147 {fieldtype:'Text', fieldname:'description', label:'Description',
148 reqd:1, description:'Use <a href="#markdown-reference">markdown</a> to \
149 format content'},
Rushabh Mehtac2ef0642012-08-07 16:36:10 +0530150 {fieldtype:'Date', fieldname:'date', label:'Event Date', reqd:1},
Rushabh Mehta2886b952012-02-24 11:26:31 +0530151 {fieldtype:'Check', fieldname:'checked', label:'Completed'},
152 {fieldtype:'Select', fieldname:'priority', label:'Priority', reqd:1, 'options':['Medium','High','Low'].join('\n')},
153 {fieldtype:'Button', fieldname:'save', label:'Save'}
154 ]
155 });
156
157 dialog.fields_dict.save.input.onclick = function() {
158 erpnext.todo.save(this);
159 }
160 erpnext.todo.dialog = dialog;
161 }
162
163 if(det) {
164 erpnext.todo.dialog.set_values({
165 date: det.date,
166 priority: det.priority,
167 description: det.description,
168 checked: det.checked
169 });
170 erpnext.todo.dialog.det = det;
171 }
172 erpnext.todo.dialog.show();
173
174}
175
176erpnext.todo.save = function(btn) {
177 var d = erpnext.todo.dialog;
178 var det = d.get_values();
179
180 if(!det) {
181 return;
182 }
183
184 det.name = d.det.name || '';
185 wn.call({
186 method:'utilities.page.todo.todo.edit',
187 args: det,
188 btn: btn,
189 callback: function() {
190 erpnext.todo.dialog.hide();
191 erpnext.todo.refresh();
192 }
193 });
194}
195
Anand Doshic6fce632012-07-09 12:34:02 +0530196wn.pages.todo.onload = function(wrapper) {
197 // create app frame
198 wrapper.appframe = new wn.ui.AppFrame($(wrapper).find('.appframe-area'), 'To Do');
199 wrapper.appframe.add_button('Refresh', erpnext.todo.refresh, 'icon-refresh');
200 wrapper.appframe.add_button('Add', function() {
201 erpnext.todo.make_dialog({
202 date:get_today(), priority:'Medium', checked:0, description:''});
203 }, 'icon-plus');
204
Rushabh Mehta2886b952012-02-24 11:26:31 +0530205 // load todos
206 erpnext.todo.refresh();
Anand Doshi616c3552012-06-14 16:46:48 +0530207}