blob: 0617b4e2923a40060d5d579b7d43fa54fd56c127 [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')
Rushabh Mehta3009c462012-10-03 11:56:38 +053030 .html('<div class="alert">Nothing to do :)</div>');
Anand Doshi47afacc2012-06-13 18:02:43 +053031 }
32
Anand Doshi47afacc2012-06-13 18:02:43 +053033
Rushabh Mehta2886b952012-02-24 11:26:31 +053034 if(r.message) {
35 for(var i in r.message) {
36 new erpnext.todo.ToDoItem(r.message[i]);
37 }
Anand Doshi47afacc2012-06-13 18:02:43 +053038 if (!todo_list.html()) { nothing_to_do(); }
Rushabh Mehta2886b952012-02-24 11:26:31 +053039 } else {
Anand Doshi47afacc2012-06-13 18:02:43 +053040 nothing_to_do();
Rushabh Mehta2886b952012-02-24 11:26:31 +053041 }
42 }
43 });
Rushabh Mehta2886b952012-02-24 11:26:31 +053044}
45
46erpnext.todo.ToDoItem = Class.extend({
47 init: function(todo) {
48 label_map = {
49 'High': 'label-important',
50 'Medium': 'label-info',
51 'Low':''
52 }
53 todo.labelclass = label_map[todo.priority];
Anand Doshi29ed95b2012-03-21 18:02:17 +053054 todo.userdate = dateutil.str_to_user(todo.date) || '';
Anand Doshi64ddb9f2012-06-12 19:24:12 +053055
56 todo.fullname = '';
Anand Doshi06fcfdf2012-03-22 11:01:38 +053057 if(todo.assigned_by) {
Anand Doshi616c3552012-06-14 16:46:48 +053058 var assigned_by = wn.boot.user_info[todo.assigned_by]
Rushabh Mehtaac7baf02012-10-03 12:22:13 +053059 todo.fullname = repl("[By %(fullname)s] ".bold(), {
Anand Doshi616c3552012-06-14 16:46:48 +053060 fullname: (assigned_by ? assigned_by.fullname : todo.assigned_by),
Anand Doshi64ddb9f2012-06-12 19:24:12 +053061 });
62 }
63
64 var parent_list = "#todo-list";
65 if(todo.owner !== user) {
Anand Doshi616c3552012-06-14 16:46:48 +053066 var owner = wn.boot.user_info[todo.owner];
Rushabh Mehtaac7baf02012-10-03 12:22:13 +053067 todo.fullname = repl("[To %(fullname)s] ".bold(), {
Anand Doshi616c3552012-06-14 16:46:48 +053068 fullname: (owner ? owner.fullname : todo.owner),
Anand Doshi64ddb9f2012-06-12 19:24:12 +053069 });
70 }
71 parent_list += " div.todo-content";
72
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053073 if(todo.reference_name && todo.reference_type) {
Rushabh Mehta3009c462012-10-03 11:56:38 +053074 todo.link = repl('<a href="#!Form/%(reference_type)s/%(reference_name)s">\
Anand Doshi29ed95b2012-03-21 18:02:17 +053075 %(reference_type)s: %(reference_name)s</a>', todo);
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053076 } else if(todo.reference_type) {
Rushabh Mehta899fe5d2012-08-08 15:30:19 +053077 todo.link = repl('<br><a href="#!List/%(reference_type)s">\
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053078 %(reference_type)s</a>', todo);
79 } else {
80 todo.link = '';
81 }
Anand Doshi130dfae2012-04-03 12:18:35 +053082 if(!todo.description) todo.description = '';
Anand Doshi037c2572012-10-09 11:05:25 +053083 todo.description_display = todo.description.replace(/\n\n/g, "<br>").trim();
Rushabh Mehta3009c462012-10-03 11:56:38 +053084
Anand Doshi64ddb9f2012-06-12 19:24:12 +053085 $(parent_list).append(repl('\
86 <div class="todoitem">\
Anand Doshi037c2572012-10-09 11:05:25 +053087 <div class="label %(labelclass)s">%(priority)s</div>\
88 <div class="popup-on-click"><a href="#">[edit]</a></div>\
89 <div class="todo-date-fullname">\
90 <div class="todo-date">%(userdate)s</div>\
91 %(fullname)s:\
92 </div>\
93 <div class="description">%(description_display)s\
Rushabh Mehta899fe5d2012-08-08 15:30:19 +053094 <span class="ref_link">%(link)s</span>\
Anand Doshi037c2572012-10-09 11:05:25 +053095 </div>\
96 <div class="close-span"><a href="#" class="close">&times;</a></div>\
Anand Doshi64ddb9f2012-06-12 19:24:12 +053097 </div>\
98 <div class="todo-separator"></div>', todo));
99 $todo = $(parent_list + ' div.todoitem:last');
Rushabh Mehta2886b952012-02-24 11:26:31 +0530100
101 if(todo.checked) {
102 $todo.find('.description').css('text-decoration', 'line-through');
103 }
104
Anand Doshi0b8ac7f2012-02-29 18:33:39 +0530105 if(!todo.reference_type)
Rushabh Mehta2886b952012-02-24 11:26:31 +0530106 $todo.find('.ref_link').toggle(false);
107
Anand Doshid104f4b2012-06-13 17:01:52 +0530108 $todo.find('.popup-on-click')
Rushabh Mehta2886b952012-02-24 11:26:31 +0530109 .data('todo', todo)
110 .click(function() {
111 erpnext.todo.make_dialog($(this).data('todo'));
112 return false;
113 });
114
115 $todo.find('.close')
116 .data('name', todo.name)
117 .click(function() {
118 $(this).parent().css('opacity', 0.5);
119 wn.call({
120 method:'utilities.page.todo.todo.delete',
121 args: {name: $(this).data('name')},
122 callback: function() {
123 erpnext.todo.refresh();
124 }
125 });
126 return false;
127 })
128 }
129});
130
131erpnext.todo.make_dialog = function(det) {
132 if(!erpnext.todo.dialog) {
Rushabh Mehta2d700dd2012-10-01 14:46:37 +0530133 var dialog = new wn.ui.Dialog({
Rushabh Mehta2886b952012-02-24 11:26:31 +0530134 width: 480,
135 title: 'To Do',
136 fields: [
Rushabh Mehta6252c132012-08-07 12:53:49 +0530137 {fieldtype:'Text', fieldname:'description', label:'Description',
Rushabh Mehta3009c462012-10-03 11:56:38 +0530138 reqd:1},
Rushabh Mehtac2ef0642012-08-07 16:36:10 +0530139 {fieldtype:'Date', fieldname:'date', label:'Event Date', reqd:1},
Rushabh Mehta2886b952012-02-24 11:26:31 +0530140 {fieldtype:'Check', fieldname:'checked', label:'Completed'},
141 {fieldtype:'Select', fieldname:'priority', label:'Priority', reqd:1, 'options':['Medium','High','Low'].join('\n')},
Rushabh Mehtaac7baf02012-10-03 12:22:13 +0530142 {fieldtype:'Button', fieldname:'save', label:'Save (Ctrl+S)'}
Rushabh Mehta2886b952012-02-24 11:26:31 +0530143 ]
144 });
145
146 dialog.fields_dict.save.input.onclick = function() {
147 erpnext.todo.save(this);
148 }
149 erpnext.todo.dialog = dialog;
150 }
151
152 if(det) {
153 erpnext.todo.dialog.set_values({
154 date: det.date,
155 priority: det.priority,
156 description: det.description,
157 checked: det.checked
158 });
159 erpnext.todo.dialog.det = det;
160 }
161 erpnext.todo.dialog.show();
162
163}
164
165erpnext.todo.save = function(btn) {
166 var d = erpnext.todo.dialog;
167 var det = d.get_values();
168
169 if(!det) {
170 return;
171 }
172
173 det.name = d.det.name || '';
174 wn.call({
175 method:'utilities.page.todo.todo.edit',
176 args: det,
177 btn: btn,
178 callback: function() {
179 erpnext.todo.dialog.hide();
180 erpnext.todo.refresh();
181 }
182 });
183}
184
Anand Doshic6fce632012-07-09 12:34:02 +0530185wn.pages.todo.onload = function(wrapper) {
186 // create app frame
187 wrapper.appframe = new wn.ui.AppFrame($(wrapper).find('.appframe-area'), 'To Do');
188 wrapper.appframe.add_button('Refresh', erpnext.todo.refresh, 'icon-refresh');
189 wrapper.appframe.add_button('Add', function() {
190 erpnext.todo.make_dialog({
191 date:get_today(), priority:'Medium', checked:0, description:''});
192 }, 'icon-plus');
Rushabh Mehtaac7baf02012-10-03 12:22:13 +0530193 wrapper.appframe.add_ripped_paper_effect(wrapper);
Anand Doshic6fce632012-07-09 12:34:02 +0530194
Rushabh Mehta2886b952012-02-24 11:26:31 +0530195 // load todos
196 erpnext.todo.refresh();
Rushabh Mehtaac7baf02012-10-03 12:22:13 +0530197
198 // save on click
199 wrapper.save_action = function() {
200 if(erpnext.todo.dialog && erpnext.todo.dialog.display) {
201 erpnext.todo.dialog.fields_dict.save.input.click();
202 }
203 };
Anand Doshi616c3552012-06-14 16:46:48 +0530204}