blob: bbac46db7c645aef4b43015f2548743e88f9c878 [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 Doshi64ddb9f2012-06-12 19:24:12 +053023 $('#todo-list div.todo-content').empty();
24 $('#assigned-todo-list div.todo-content').empty();
Rushabh Mehta2886b952012-02-24 11:26:31 +053025 if(r.message) {
26 for(var i in r.message) {
27 new erpnext.todo.ToDoItem(r.message[i]);
28 }
29 } else {
30 $('#todo-list').html('<div class="help-box">Nothing to do :)</div>');
31 }
32 }
33 });
34
35 $('#add-todo').click(function() {
36 erpnext.todo.make_dialog({
37 date:get_today(), priority:'Medium', checked:0, description:''});
38 })
39}
40
41erpnext.todo.ToDoItem = Class.extend({
42 init: function(todo) {
43 label_map = {
44 'High': 'label-important',
45 'Medium': 'label-info',
46 'Low':''
47 }
48 todo.labelclass = label_map[todo.priority];
Anand Doshi29ed95b2012-03-21 18:02:17 +053049 todo.userdate = dateutil.str_to_user(todo.date) || '';
Anand Doshi64ddb9f2012-06-12 19:24:12 +053050
51 todo.fullname = '';
Anand Doshi06fcfdf2012-03-22 11:01:38 +053052 if(todo.assigned_by) {
53 todo.fullname = repl("[By %(fullname)s] ", {
54 fullname: wn.boot.user_info[todo.assigned_by].fullname
Anand Doshi64ddb9f2012-06-12 19:24:12 +053055 });
56 }
57
58 var parent_list = "#todo-list";
59 if(todo.owner !== user) {
60 parent_list = "#assigned-todo-list";
61 todo.fullname = repl("[To %(fullname)s] ", {
62 fullname: wn.boot.user_info[todo.owner].fullname
63 });
64 }
65 parent_list += " div.todo-content";
66
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053067 if(todo.reference_name && todo.reference_type) {
68 todo.link = repl('<a href="#!Form/%(reference_type)s/%(reference_name)s">\
Anand Doshi29ed95b2012-03-21 18:02:17 +053069 %(reference_type)s: %(reference_name)s</a>', todo);
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053070 } else if(todo.reference_type) {
71 todo.link = repl('<a href="#!List/%(reference_type)s">\
72 %(reference_type)s</a>', todo);
73 } else {
74 todo.link = '';
75 }
Anand Doshi130dfae2012-04-03 12:18:35 +053076 if(!todo.description) todo.description = '';
Anand Doshi64ddb9f2012-06-12 19:24:12 +053077
78 todo.desc = todo.description.replace(/\n/g, "<br>");
79
80 $(parent_list).append(repl('\
81 <div class="todoitem">\
82 <span class="label %(labelclass)s">%(priority)s</span>\
Rushabh Mehta2886b952012-02-24 11:26:31 +053083 <span class="description">\
Rushabh Mehta2886b952012-02-24 11:26:31 +053084 <span class="help" style="margin-right: 7px">%(userdate)s</span>\
Anand Doshi64ddb9f2012-06-12 19:24:12 +053085 %(fullname)s%(desc)s\
86 <span class="ref_link"><br>\
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053087 %(link)s</span>\
Anand Doshi64ddb9f2012-06-12 19:24:12 +053088 </span>\
89 <span class="close-span"><a href="#" class="close">&times;</a></span>\
90 </div>\
91 <div class="todo-separator"></div>', todo));
92 $todo = $(parent_list + ' div.todoitem:last');
Rushabh Mehta2886b952012-02-24 11:26:31 +053093
94 if(todo.checked) {
95 $todo.find('.description').css('text-decoration', 'line-through');
96 }
97
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053098 if(!todo.reference_type)
Rushabh Mehta2886b952012-02-24 11:26:31 +053099 $todo.find('.ref_link').toggle(false);
100
101 $todo.find('.description')
102 .data('todo', todo)
103 .click(function() {
104 erpnext.todo.make_dialog($(this).data('todo'));
105 return false;
106 });
107
108 $todo.find('.close')
109 .data('name', todo.name)
110 .click(function() {
111 $(this).parent().css('opacity', 0.5);
112 wn.call({
113 method:'utilities.page.todo.todo.delete',
114 args: {name: $(this).data('name')},
115 callback: function() {
116 erpnext.todo.refresh();
117 }
118 });
119 return false;
120 })
121 }
122});
123
124erpnext.todo.make_dialog = function(det) {
125 if(!erpnext.todo.dialog) {
126 var dialog = new wn.widgets.Dialog();
127 dialog.make({
128 width: 480,
129 title: 'To Do',
130 fields: [
131 {fieldtype:'Date', fieldname:'date', label:'Event Date', reqd:1},
132 {fieldtype:'Text', fieldname:'description', label:'Description', reqd:1},
133 {fieldtype:'Check', fieldname:'checked', label:'Completed'},
134 {fieldtype:'Select', fieldname:'priority', label:'Priority', reqd:1, 'options':['Medium','High','Low'].join('\n')},
135 {fieldtype:'Button', fieldname:'save', label:'Save'}
136 ]
137 });
138
139 dialog.fields_dict.save.input.onclick = function() {
140 erpnext.todo.save(this);
141 }
142 erpnext.todo.dialog = dialog;
143 }
144
145 if(det) {
146 erpnext.todo.dialog.set_values({
147 date: det.date,
148 priority: det.priority,
149 description: det.description,
150 checked: det.checked
151 });
152 erpnext.todo.dialog.det = det;
153 }
154 erpnext.todo.dialog.show();
155
156}
157
158erpnext.todo.save = function(btn) {
159 var d = erpnext.todo.dialog;
160 var det = d.get_values();
161
162 if(!det) {
163 return;
164 }
165
166 det.name = d.det.name || '';
167 wn.call({
168 method:'utilities.page.todo.todo.edit',
169 args: det,
170 btn: btn,
171 callback: function() {
172 erpnext.todo.dialog.hide();
173 erpnext.todo.refresh();
174 }
175 });
176}
177
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530178wn.pages.todo.onload = function() {
Rushabh Mehta2886b952012-02-24 11:26:31 +0530179 // load todos
180 erpnext.todo.refresh();
Anand Doshi0b8ac7f2012-02-29 18:33:39 +0530181}