blob: 206becf36de11bb334b6eaa97da0d00ea76121dc [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 });
51
52 $('#add-todo').click(function() {
53 erpnext.todo.make_dialog({
54 date:get_today(), priority:'Medium', checked:0, description:''});
55 })
56}
57
58erpnext.todo.ToDoItem = Class.extend({
59 init: function(todo) {
60 label_map = {
61 'High': 'label-important',
62 'Medium': 'label-info',
63 'Low':''
64 }
65 todo.labelclass = label_map[todo.priority];
Anand Doshi29ed95b2012-03-21 18:02:17 +053066 todo.userdate = dateutil.str_to_user(todo.date) || '';
Anand Doshi64ddb9f2012-06-12 19:24:12 +053067
68 todo.fullname = '';
Anand Doshi06fcfdf2012-03-22 11:01:38 +053069 if(todo.assigned_by) {
Anand Doshi616c3552012-06-14 16:46:48 +053070 var assigned_by = wn.boot.user_info[todo.assigned_by]
Anand Doshi3c3f9962012-06-13 12:01:41 +053071 todo.fullname = repl("[By %(fullname)s] &nbsp;", {
Anand Doshi616c3552012-06-14 16:46:48 +053072 fullname: (assigned_by ? assigned_by.fullname : todo.assigned_by),
Anand Doshi64ddb9f2012-06-12 19:24:12 +053073 });
74 }
75
76 var parent_list = "#todo-list";
77 if(todo.owner !== user) {
78 parent_list = "#assigned-todo-list";
Anand Doshi616c3552012-06-14 16:46:48 +053079 var owner = wn.boot.user_info[todo.owner];
Anand Doshi3c3f9962012-06-13 12:01:41 +053080 todo.fullname = repl("[To %(fullname)s] &nbsp;", {
Anand Doshi616c3552012-06-14 16:46:48 +053081 fullname: (owner ? owner.fullname : todo.owner),
Anand Doshi64ddb9f2012-06-12 19:24:12 +053082 });
83 }
84 parent_list += " div.todo-content";
85
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053086 if(todo.reference_name && todo.reference_type) {
87 todo.link = repl('<a href="#!Form/%(reference_type)s/%(reference_name)s">\
Anand Doshi29ed95b2012-03-21 18:02:17 +053088 %(reference_type)s: %(reference_name)s</a>', todo);
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053089 } else if(todo.reference_type) {
90 todo.link = repl('<a href="#!List/%(reference_type)s">\
91 %(reference_type)s</a>', todo);
92 } else {
93 todo.link = '';
94 }
Anand Doshi130dfae2012-04-03 12:18:35 +053095 if(!todo.description) todo.description = '';
Anand Doshi64ddb9f2012-06-12 19:24:12 +053096
97 todo.desc = todo.description.replace(/\n/g, "<br>");
98
99 $(parent_list).append(repl('\
100 <div class="todoitem">\
101 <span class="label %(labelclass)s">%(priority)s</span>\
Rushabh Mehta2886b952012-02-24 11:26:31 +0530102 <span class="description">\
Anand Doshid104f4b2012-06-13 17:01:52 +0530103 <span class="popup-on-click">\
Rushabh Mehta2886b952012-02-24 11:26:31 +0530104 <span class="help" style="margin-right: 7px">%(userdate)s</span>\
Anand Doshi64ddb9f2012-06-12 19:24:12 +0530105 %(fullname)s%(desc)s\
Anand Doshid104f4b2012-06-13 17:01:52 +0530106 </span>\
Anand Doshi64ddb9f2012-06-12 19:24:12 +0530107 <span class="ref_link"><br>\
Anand Doshi0b8ac7f2012-02-29 18:33:39 +0530108 %(link)s</span>\
Anand Doshi64ddb9f2012-06-12 19:24:12 +0530109 </span>\
110 <span class="close-span"><a href="#" class="close">&times;</a></span>\
111 </div>\
112 <div class="todo-separator"></div>', todo));
113 $todo = $(parent_list + ' div.todoitem:last');
Rushabh Mehta2886b952012-02-24 11:26:31 +0530114
115 if(todo.checked) {
116 $todo.find('.description').css('text-decoration', 'line-through');
117 }
118
Anand Doshi0b8ac7f2012-02-29 18:33:39 +0530119 if(!todo.reference_type)
Rushabh Mehta2886b952012-02-24 11:26:31 +0530120 $todo.find('.ref_link').toggle(false);
121
Anand Doshid104f4b2012-06-13 17:01:52 +0530122 $todo.find('.popup-on-click')
Rushabh Mehta2886b952012-02-24 11:26:31 +0530123 .data('todo', todo)
124 .click(function() {
125 erpnext.todo.make_dialog($(this).data('todo'));
126 return false;
127 });
128
129 $todo.find('.close')
130 .data('name', todo.name)
131 .click(function() {
132 $(this).parent().css('opacity', 0.5);
133 wn.call({
134 method:'utilities.page.todo.todo.delete',
135 args: {name: $(this).data('name')},
136 callback: function() {
137 erpnext.todo.refresh();
138 }
139 });
140 return false;
141 })
142 }
143});
144
145erpnext.todo.make_dialog = function(det) {
146 if(!erpnext.todo.dialog) {
147 var dialog = new wn.widgets.Dialog();
148 dialog.make({
149 width: 480,
150 title: 'To Do',
151 fields: [
152 {fieldtype:'Date', fieldname:'date', label:'Event Date', reqd:1},
153 {fieldtype:'Text', fieldname:'description', label:'Description', reqd:1},
154 {fieldtype:'Check', fieldname:'checked', label:'Completed'},
155 {fieldtype:'Select', fieldname:'priority', label:'Priority', reqd:1, 'options':['Medium','High','Low'].join('\n')},
156 {fieldtype:'Button', fieldname:'save', label:'Save'}
157 ]
158 });
159
160 dialog.fields_dict.save.input.onclick = function() {
161 erpnext.todo.save(this);
162 }
163 erpnext.todo.dialog = dialog;
164 }
165
166 if(det) {
167 erpnext.todo.dialog.set_values({
168 date: det.date,
169 priority: det.priority,
170 description: det.description,
171 checked: det.checked
172 });
173 erpnext.todo.dialog.det = det;
174 }
175 erpnext.todo.dialog.show();
176
177}
178
179erpnext.todo.save = function(btn) {
180 var d = erpnext.todo.dialog;
181 var det = d.get_values();
182
183 if(!det) {
184 return;
185 }
186
187 det.name = d.det.name || '';
188 wn.call({
189 method:'utilities.page.todo.todo.edit',
190 args: det,
191 btn: btn,
192 callback: function() {
193 erpnext.todo.dialog.hide();
194 erpnext.todo.refresh();
195 }
196 });
197}
198
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530199wn.pages.todo.onload = function() {
Rushabh Mehta2886b952012-02-24 11:26:31 +0530200 // load todos
201 erpnext.todo.refresh();
Anand Doshi616c3552012-06-14 16:46:48 +0530202}