blob: dcdf50aa9dccd0c9ec98015e3fc55d6448d9e461 [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) {
23 $('#todo-list').empty();
24 if(r.message) {
25 for(var i in r.message) {
26 new erpnext.todo.ToDoItem(r.message[i]);
27 }
28 } else {
29 $('#todo-list').html('<div class="help-box">Nothing to do :)</div>');
30 }
31 }
32 });
33
34 $('#add-todo').click(function() {
35 erpnext.todo.make_dialog({
36 date:get_today(), priority:'Medium', checked:0, description:''});
37 })
38}
39
40erpnext.todo.ToDoItem = Class.extend({
41 init: function(todo) {
42 label_map = {
43 'High': 'label-important',
44 'Medium': 'label-info',
45 'Low':''
46 }
47 todo.labelclass = label_map[todo.priority];
Anand Doshi29ed95b2012-03-21 18:02:17 +053048 todo.userdate = dateutil.str_to_user(todo.date) || '';
Anand Doshi06fcfdf2012-03-22 11:01:38 +053049 if(todo.assigned_by) {
50 todo.fullname = repl("[By %(fullname)s] ", {
51 fullname: wn.boot.user_info[todo.assigned_by].fullname
52 })
Anand Doshi98f847c2012-03-22 11:18:28 +053053 } else { todo.fullname = ''; }
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053054 if(todo.reference_name && todo.reference_type) {
55 todo.link = repl('<a href="#!Form/%(reference_type)s/%(reference_name)s">\
Anand Doshi29ed95b2012-03-21 18:02:17 +053056 %(reference_type)s: %(reference_name)s</a>', todo);
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053057 } else if(todo.reference_type) {
58 todo.link = repl('<a href="#!List/%(reference_type)s">\
59 %(reference_type)s</a>', todo);
60 } else {
61 todo.link = '';
62 }
Rushabh Mehta2886b952012-02-24 11:26:31 +053063 $('#todo-list').append(repl('<div class="todoitem">\
64 <span class="description">\
65 <span class="label %(labelclass)s">%(priority)s</span>\
66 <span class="help" style="margin-right: 7px">%(userdate)s</span>\
Anand Doshi06fcfdf2012-03-22 11:01:38 +053067 %(fullname)s%(description)s</span>\
Anand Doshi29ed95b2012-03-21 18:02:17 +053068 <span class="ref_link">&rarr; &nbsp;\
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053069 %(link)s</span>\
Rushabh Mehta2886b952012-02-24 11:26:31 +053070 <a href="#" class="close">&times;</a>\
71 </div>', todo));
72 $todo = $('div.todoitem:last');
73
74 if(todo.checked) {
75 $todo.find('.description').css('text-decoration', 'line-through');
76 }
77
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053078 if(!todo.reference_type)
Rushabh Mehta2886b952012-02-24 11:26:31 +053079 $todo.find('.ref_link').toggle(false);
80
81 $todo.find('.description')
82 .data('todo', todo)
83 .click(function() {
84 erpnext.todo.make_dialog($(this).data('todo'));
85 return false;
86 });
87
88 $todo.find('.close')
89 .data('name', todo.name)
90 .click(function() {
91 $(this).parent().css('opacity', 0.5);
92 wn.call({
93 method:'utilities.page.todo.todo.delete',
94 args: {name: $(this).data('name')},
95 callback: function() {
96 erpnext.todo.refresh();
97 }
98 });
99 return false;
100 })
101 }
102});
103
104erpnext.todo.make_dialog = function(det) {
105 if(!erpnext.todo.dialog) {
106 var dialog = new wn.widgets.Dialog();
107 dialog.make({
108 width: 480,
109 title: 'To Do',
110 fields: [
111 {fieldtype:'Date', fieldname:'date', label:'Event Date', reqd:1},
112 {fieldtype:'Text', fieldname:'description', label:'Description', reqd:1},
113 {fieldtype:'Check', fieldname:'checked', label:'Completed'},
114 {fieldtype:'Select', fieldname:'priority', label:'Priority', reqd:1, 'options':['Medium','High','Low'].join('\n')},
115 {fieldtype:'Button', fieldname:'save', label:'Save'}
116 ]
117 });
118
119 dialog.fields_dict.save.input.onclick = function() {
120 erpnext.todo.save(this);
121 }
122 erpnext.todo.dialog = dialog;
123 }
124
125 if(det) {
126 erpnext.todo.dialog.set_values({
127 date: det.date,
128 priority: det.priority,
129 description: det.description,
130 checked: det.checked
131 });
132 erpnext.todo.dialog.det = det;
133 }
134 erpnext.todo.dialog.show();
135
136}
137
138erpnext.todo.save = function(btn) {
139 var d = erpnext.todo.dialog;
140 var det = d.get_values();
141
142 if(!det) {
143 return;
144 }
145
146 det.name = d.det.name || '';
147 wn.call({
148 method:'utilities.page.todo.todo.edit',
149 args: det,
150 btn: btn,
151 callback: function() {
152 erpnext.todo.dialog.hide();
153 erpnext.todo.refresh();
154 }
155 });
156}
157
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530158wn.pages.todo.onload = function() {
Rushabh Mehta2886b952012-02-24 11:26:31 +0530159 // load todos
160 erpnext.todo.refresh();
Anand Doshi0b8ac7f2012-02-29 18:33:39 +0530161}