blob: 2032b3133565c02b9612a572b0511459ba757951 [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 = '';
Rushabh Mehta3009c462012-10-03 11:56:38 +053083
Anand Doshi64ddb9f2012-06-12 19:24:12 +053084 $(parent_list).append(repl('\
85 <div class="todoitem">\
86 <span class="label %(labelclass)s">%(priority)s</span>\
Rushabh Mehta3009c462012-10-03 11:56:38 +053087 <span class="popup-on-click"><a href="#">[edit]</a></span>\
Rushabh Mehta2886b952012-02-24 11:26:31 +053088 <span class="description">\
Rushabh Mehta2886b952012-02-24 11:26:31 +053089 <span class="help" style="margin-right: 7px">%(userdate)s</span>\
Rushabh Mehta3009c462012-10-03 11:56:38 +053090 %(fullname)s: %(description)s\
Rushabh Mehta899fe5d2012-08-08 15:30:19 +053091 <span class="ref_link">%(link)s</span>\
Anand Doshi64ddb9f2012-06-12 19:24:12 +053092 </span>\
93 <span class="close-span"><a href="#" class="close">&times;</a></span>\
94 </div>\
95 <div class="todo-separator"></div>', todo));
96 $todo = $(parent_list + ' div.todoitem:last');
Rushabh Mehta2886b952012-02-24 11:26:31 +053097
98 if(todo.checked) {
99 $todo.find('.description').css('text-decoration', 'line-through');
100 }
101
Anand Doshi0b8ac7f2012-02-29 18:33:39 +0530102 if(!todo.reference_type)
Rushabh Mehta2886b952012-02-24 11:26:31 +0530103 $todo.find('.ref_link').toggle(false);
104
Anand Doshid104f4b2012-06-13 17:01:52 +0530105 $todo.find('.popup-on-click')
Rushabh Mehta2886b952012-02-24 11:26:31 +0530106 .data('todo', todo)
107 .click(function() {
108 erpnext.todo.make_dialog($(this).data('todo'));
109 return false;
110 });
111
112 $todo.find('.close')
113 .data('name', todo.name)
114 .click(function() {
115 $(this).parent().css('opacity', 0.5);
116 wn.call({
117 method:'utilities.page.todo.todo.delete',
118 args: {name: $(this).data('name')},
119 callback: function() {
120 erpnext.todo.refresh();
121 }
122 });
123 return false;
124 })
125 }
126});
127
128erpnext.todo.make_dialog = function(det) {
129 if(!erpnext.todo.dialog) {
Rushabh Mehta2d700dd2012-10-01 14:46:37 +0530130 var dialog = new wn.ui.Dialog({
Rushabh Mehta2886b952012-02-24 11:26:31 +0530131 width: 480,
132 title: 'To Do',
133 fields: [
Rushabh Mehta6252c132012-08-07 12:53:49 +0530134 {fieldtype:'Text', fieldname:'description', label:'Description',
Rushabh Mehta3009c462012-10-03 11:56:38 +0530135 reqd:1},
Rushabh Mehtac2ef0642012-08-07 16:36:10 +0530136 {fieldtype:'Date', fieldname:'date', label:'Event Date', reqd:1},
Rushabh Mehta2886b952012-02-24 11:26:31 +0530137 {fieldtype:'Check', fieldname:'checked', label:'Completed'},
138 {fieldtype:'Select', fieldname:'priority', label:'Priority', reqd:1, 'options':['Medium','High','Low'].join('\n')},
Rushabh Mehtaac7baf02012-10-03 12:22:13 +0530139 {fieldtype:'Button', fieldname:'save', label:'Save (Ctrl+S)'}
Rushabh Mehta2886b952012-02-24 11:26:31 +0530140 ]
141 });
142
143 dialog.fields_dict.save.input.onclick = function() {
144 erpnext.todo.save(this);
145 }
146 erpnext.todo.dialog = dialog;
147 }
148
149 if(det) {
150 erpnext.todo.dialog.set_values({
151 date: det.date,
152 priority: det.priority,
153 description: det.description,
154 checked: det.checked
155 });
156 erpnext.todo.dialog.det = det;
157 }
158 erpnext.todo.dialog.show();
159
160}
161
162erpnext.todo.save = function(btn) {
163 var d = erpnext.todo.dialog;
164 var det = d.get_values();
165
166 if(!det) {
167 return;
168 }
169
170 det.name = d.det.name || '';
171 wn.call({
172 method:'utilities.page.todo.todo.edit',
173 args: det,
174 btn: btn,
175 callback: function() {
176 erpnext.todo.dialog.hide();
177 erpnext.todo.refresh();
178 }
179 });
180}
181
Anand Doshic6fce632012-07-09 12:34:02 +0530182wn.pages.todo.onload = function(wrapper) {
183 // create app frame
184 wrapper.appframe = new wn.ui.AppFrame($(wrapper).find('.appframe-area'), 'To Do');
185 wrapper.appframe.add_button('Refresh', erpnext.todo.refresh, 'icon-refresh');
186 wrapper.appframe.add_button('Add', function() {
187 erpnext.todo.make_dialog({
188 date:get_today(), priority:'Medium', checked:0, description:''});
189 }, 'icon-plus');
Rushabh Mehtaac7baf02012-10-03 12:22:13 +0530190 wrapper.appframe.add_ripped_paper_effect(wrapper);
Anand Doshic6fce632012-07-09 12:34:02 +0530191
Rushabh Mehta2886b952012-02-24 11:26:31 +0530192 // load todos
193 erpnext.todo.refresh();
Rushabh Mehtaac7baf02012-10-03 12:22:13 +0530194
195 // save on click
196 wrapper.save_action = function() {
197 if(erpnext.todo.dialog && erpnext.todo.dialog.display) {
198 erpnext.todo.dialog.fields_dict.save.input.click();
199 }
200 };
Anand Doshi616c3552012-06-14 16:46:48 +0530201}