blob: 8cd8fc1e5f5c50fa31848eba658c6b8dbcace92a [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 Doshi3c3f9962012-06-13 12:01:41 +053070 todo.fullname = repl("[By %(fullname)s] &nbsp;", {
Anand Doshi06fcfdf2012-03-22 11:01:38 +053071 fullname: wn.boot.user_info[todo.assigned_by].fullname
Anand Doshi64ddb9f2012-06-12 19:24:12 +053072 });
73 }
74
75 var parent_list = "#todo-list";
76 if(todo.owner !== user) {
77 parent_list = "#assigned-todo-list";
Anand Doshi3c3f9962012-06-13 12:01:41 +053078 todo.fullname = repl("[To %(fullname)s] &nbsp;", {
Anand Doshi64ddb9f2012-06-12 19:24:12 +053079 fullname: wn.boot.user_info[todo.owner].fullname
80 });
81 }
82 parent_list += " div.todo-content";
83
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053084 if(todo.reference_name && todo.reference_type) {
85 todo.link = repl('<a href="#!Form/%(reference_type)s/%(reference_name)s">\
Anand Doshi29ed95b2012-03-21 18:02:17 +053086 %(reference_type)s: %(reference_name)s</a>', todo);
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053087 } else if(todo.reference_type) {
88 todo.link = repl('<a href="#!List/%(reference_type)s">\
89 %(reference_type)s</a>', todo);
90 } else {
91 todo.link = '';
92 }
Anand Doshi130dfae2012-04-03 12:18:35 +053093 if(!todo.description) todo.description = '';
Anand Doshi64ddb9f2012-06-12 19:24:12 +053094
95 todo.desc = todo.description.replace(/\n/g, "<br>");
96
97 $(parent_list).append(repl('\
98 <div class="todoitem">\
99 <span class="label %(labelclass)s">%(priority)s</span>\
Rushabh Mehta2886b952012-02-24 11:26:31 +0530100 <span class="description">\
Anand Doshid104f4b2012-06-13 17:01:52 +0530101 <span class="popup-on-click">\
Rushabh Mehta2886b952012-02-24 11:26:31 +0530102 <span class="help" style="margin-right: 7px">%(userdate)s</span>\
Anand Doshi64ddb9f2012-06-12 19:24:12 +0530103 %(fullname)s%(desc)s\
Anand Doshid104f4b2012-06-13 17:01:52 +0530104 </span>\
Anand Doshi64ddb9f2012-06-12 19:24:12 +0530105 <span class="ref_link"><br>\
Anand Doshi0b8ac7f2012-02-29 18:33:39 +0530106 %(link)s</span>\
Anand Doshi64ddb9f2012-06-12 19:24:12 +0530107 </span>\
108 <span class="close-span"><a href="#" class="close">&times;</a></span>\
109 </div>\
110 <div class="todo-separator"></div>', todo));
111 $todo = $(parent_list + ' div.todoitem:last');
Rushabh Mehta2886b952012-02-24 11:26:31 +0530112
113 if(todo.checked) {
114 $todo.find('.description').css('text-decoration', 'line-through');
115 }
116
Anand Doshi0b8ac7f2012-02-29 18:33:39 +0530117 if(!todo.reference_type)
Rushabh Mehta2886b952012-02-24 11:26:31 +0530118 $todo.find('.ref_link').toggle(false);
119
Anand Doshid104f4b2012-06-13 17:01:52 +0530120 $todo.find('.popup-on-click')
Rushabh Mehta2886b952012-02-24 11:26:31 +0530121 .data('todo', todo)
122 .click(function() {
123 erpnext.todo.make_dialog($(this).data('todo'));
124 return false;
125 });
126
127 $todo.find('.close')
128 .data('name', todo.name)
129 .click(function() {
130 $(this).parent().css('opacity', 0.5);
131 wn.call({
132 method:'utilities.page.todo.todo.delete',
133 args: {name: $(this).data('name')},
134 callback: function() {
135 erpnext.todo.refresh();
136 }
137 });
138 return false;
139 })
140 }
141});
142
143erpnext.todo.make_dialog = function(det) {
144 if(!erpnext.todo.dialog) {
145 var dialog = new wn.widgets.Dialog();
146 dialog.make({
147 width: 480,
148 title: 'To Do',
149 fields: [
150 {fieldtype:'Date', fieldname:'date', label:'Event Date', reqd:1},
151 {fieldtype:'Text', fieldname:'description', label:'Description', reqd:1},
152 {fieldtype:'Check', fieldname:'checked', label:'Completed'},
153 {fieldtype:'Select', fieldname:'priority', label:'Priority', reqd:1, 'options':['Medium','High','Low'].join('\n')},
154 {fieldtype:'Button', fieldname:'save', label:'Save'}
155 ]
156 });
157
158 dialog.fields_dict.save.input.onclick = function() {
159 erpnext.todo.save(this);
160 }
161 erpnext.todo.dialog = dialog;
162 }
163
164 if(det) {
165 erpnext.todo.dialog.set_values({
166 date: det.date,
167 priority: det.priority,
168 description: det.description,
169 checked: det.checked
170 });
171 erpnext.todo.dialog.det = det;
172 }
173 erpnext.todo.dialog.show();
174
175}
176
177erpnext.todo.save = function(btn) {
178 var d = erpnext.todo.dialog;
179 var det = d.get_values();
180
181 if(!det) {
182 return;
183 }
184
185 det.name = d.det.name || '';
186 wn.call({
187 method:'utilities.page.todo.todo.edit',
188 args: det,
189 btn: btn,
190 callback: function() {
191 erpnext.todo.dialog.hide();
192 erpnext.todo.refresh();
193 }
194 });
195}
196
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530197wn.pages.todo.onload = function() {
Rushabh Mehta2886b952012-02-24 11:26:31 +0530198 // load todos
199 erpnext.todo.refresh();
Anand Doshi0b8ac7f2012-02-29 18:33:39 +0530200}