blob: 0a725a742795f637f8259fdb2fd672c6a48a08c8 [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) {
Rushabh Mehta899fe5d2012-08-08 15:30:19 +053082 todo.link = repl('<br><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) {
Rushabh Mehta899fe5d2012-08-08 15:30:19 +053085 todo.link = repl('<br><a href="#!List/%(reference_type)s">\
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053086 %(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">\
Rushabh Mehta2886b952012-02-24 11:26:31 +053098 <span class="help" style="margin-right: 7px">%(userdate)s</span>\
Anand Doshi64ddb9f2012-06-12 19:24:12 +053099 %(fullname)s%(desc)s\
Rushabh Mehta899fe5d2012-08-08 15:30:19 +0530100 <span class="popup-on-click"><a href="#"> [edit]</a></span>\
101 <span class="ref_link">%(link)s</span>\
Anand Doshi64ddb9f2012-06-12 19:24:12 +0530102 </span>\
103 <span class="close-span"><a href="#" class="close">&times;</a></span>\
104 </div>\
105 <div class="todo-separator"></div>', todo));
106 $todo = $(parent_list + ' div.todoitem:last');
Rushabh Mehta2886b952012-02-24 11:26:31 +0530107
108 if(todo.checked) {
109 $todo.find('.description').css('text-decoration', 'line-through');
110 }
111
Anand Doshi0b8ac7f2012-02-29 18:33:39 +0530112 if(!todo.reference_type)
Rushabh Mehta2886b952012-02-24 11:26:31 +0530113 $todo.find('.ref_link').toggle(false);
114
Anand Doshid104f4b2012-06-13 17:01:52 +0530115 $todo.find('.popup-on-click')
Rushabh Mehta2886b952012-02-24 11:26:31 +0530116 .data('todo', todo)
117 .click(function() {
118 erpnext.todo.make_dialog($(this).data('todo'));
119 return false;
120 });
121
122 $todo.find('.close')
123 .data('name', todo.name)
124 .click(function() {
125 $(this).parent().css('opacity', 0.5);
126 wn.call({
127 method:'utilities.page.todo.todo.delete',
128 args: {name: $(this).data('name')},
129 callback: function() {
130 erpnext.todo.refresh();
131 }
132 });
133 return false;
134 })
135 }
136});
137
138erpnext.todo.make_dialog = function(det) {
139 if(!erpnext.todo.dialog) {
Rushabh Mehta2d700dd2012-10-01 14:46:37 +0530140 var dialog = new wn.ui.Dialog({
Rushabh Mehta2886b952012-02-24 11:26:31 +0530141 width: 480,
142 title: 'To Do',
143 fields: [
Rushabh Mehta6252c132012-08-07 12:53:49 +0530144 {fieldtype:'Text', fieldname:'description', label:'Description',
145 reqd:1, description:'Use <a href="#markdown-reference">markdown</a> to \
146 format content'},
Rushabh Mehtac2ef0642012-08-07 16:36:10 +0530147 {fieldtype:'Date', fieldname:'date', label:'Event Date', reqd:1},
Rushabh Mehta2886b952012-02-24 11:26:31 +0530148 {fieldtype:'Check', fieldname:'checked', label:'Completed'},
149 {fieldtype:'Select', fieldname:'priority', label:'Priority', reqd:1, 'options':['Medium','High','Low'].join('\n')},
150 {fieldtype:'Button', fieldname:'save', label:'Save'}
151 ]
152 });
153
154 dialog.fields_dict.save.input.onclick = function() {
155 erpnext.todo.save(this);
156 }
157 erpnext.todo.dialog = dialog;
158 }
159
160 if(det) {
161 erpnext.todo.dialog.set_values({
162 date: det.date,
163 priority: det.priority,
164 description: det.description,
165 checked: det.checked
166 });
167 erpnext.todo.dialog.det = det;
168 }
169 erpnext.todo.dialog.show();
170
171}
172
173erpnext.todo.save = function(btn) {
174 var d = erpnext.todo.dialog;
175 var det = d.get_values();
176
177 if(!det) {
178 return;
179 }
180
181 det.name = d.det.name || '';
182 wn.call({
183 method:'utilities.page.todo.todo.edit',
184 args: det,
185 btn: btn,
186 callback: function() {
187 erpnext.todo.dialog.hide();
188 erpnext.todo.refresh();
189 }
190 });
191}
192
Anand Doshic6fce632012-07-09 12:34:02 +0530193wn.pages.todo.onload = function(wrapper) {
194 // create app frame
195 wrapper.appframe = new wn.ui.AppFrame($(wrapper).find('.appframe-area'), 'To Do');
196 wrapper.appframe.add_button('Refresh', erpnext.todo.refresh, 'icon-refresh');
197 wrapper.appframe.add_button('Add', function() {
198 erpnext.todo.make_dialog({
199 date:get_today(), priority:'Medium', checked:0, description:''});
200 }, 'icon-plus');
201
Rushabh Mehta2886b952012-02-24 11:26:31 +0530202 // load todos
203 erpnext.todo.refresh();
Anand Doshi616c3552012-06-14 16:46:48 +0530204}