blob: dfaa7cdecf8d7f90b95ece89a072f4118b5efcf1 [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];
48 todo.userdate = dateutil.str_to_user(todo.date);
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053049 if(todo.reference_name && todo.reference_type) {
50 todo.link = repl('<a href="#!Form/%(reference_type)s/%(reference_name)s">\
51 %(reference_name)s</a>', todo);
52 } else if(todo.reference_type) {
53 todo.link = repl('<a href="#!List/%(reference_type)s">\
54 %(reference_type)s</a>', todo);
55 } else {
56 todo.link = '';
57 }
Rushabh Mehta2886b952012-02-24 11:26:31 +053058 $('#todo-list').append(repl('<div class="todoitem">\
59 <span class="description">\
60 <span class="label %(labelclass)s">%(priority)s</span>\
61 <span class="help" style="margin-right: 7px">%(userdate)s</span>\
62 %(description)s</span>\
63 <span class="ref_link">&rarr;\
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053064 %(link)s</span>\
Rushabh Mehta2886b952012-02-24 11:26:31 +053065 <a href="#" class="close">&times;</a>\
66 </div>', todo));
67 $todo = $('div.todoitem:last');
68
69 if(todo.checked) {
70 $todo.find('.description').css('text-decoration', 'line-through');
71 }
72
Anand Doshi0b8ac7f2012-02-29 18:33:39 +053073 if(!todo.reference_type)
Rushabh Mehta2886b952012-02-24 11:26:31 +053074 $todo.find('.ref_link').toggle(false);
75
76 $todo.find('.description')
77 .data('todo', todo)
78 .click(function() {
79 erpnext.todo.make_dialog($(this).data('todo'));
80 return false;
81 });
82
83 $todo.find('.close')
84 .data('name', todo.name)
85 .click(function() {
86 $(this).parent().css('opacity', 0.5);
87 wn.call({
88 method:'utilities.page.todo.todo.delete',
89 args: {name: $(this).data('name')},
90 callback: function() {
91 erpnext.todo.refresh();
92 }
93 });
94 return false;
95 })
96 }
97});
98
99erpnext.todo.make_dialog = function(det) {
100 if(!erpnext.todo.dialog) {
101 var dialog = new wn.widgets.Dialog();
102 dialog.make({
103 width: 480,
104 title: 'To Do',
105 fields: [
106 {fieldtype:'Date', fieldname:'date', label:'Event Date', reqd:1},
107 {fieldtype:'Text', fieldname:'description', label:'Description', reqd:1},
108 {fieldtype:'Check', fieldname:'checked', label:'Completed'},
109 {fieldtype:'Select', fieldname:'priority', label:'Priority', reqd:1, 'options':['Medium','High','Low'].join('\n')},
110 {fieldtype:'Button', fieldname:'save', label:'Save'}
111 ]
112 });
113
114 dialog.fields_dict.save.input.onclick = function() {
115 erpnext.todo.save(this);
116 }
117 erpnext.todo.dialog = dialog;
118 }
119
120 if(det) {
121 erpnext.todo.dialog.set_values({
122 date: det.date,
123 priority: det.priority,
124 description: det.description,
125 checked: det.checked
126 });
127 erpnext.todo.dialog.det = det;
128 }
129 erpnext.todo.dialog.show();
130
131}
132
133erpnext.todo.save = function(btn) {
134 var d = erpnext.todo.dialog;
135 var det = d.get_values();
136
137 if(!det) {
138 return;
139 }
140
141 det.name = d.det.name || '';
142 wn.call({
143 method:'utilities.page.todo.todo.edit',
144 args: det,
145 btn: btn,
146 callback: function() {
147 erpnext.todo.dialog.hide();
148 erpnext.todo.refresh();
149 }
150 });
151}
152
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530153wn.pages.todo.onload = function() {
Rushabh Mehta2886b952012-02-24 11:26:31 +0530154 // load todos
155 erpnext.todo.refresh();
Anand Doshi0b8ac7f2012-02-29 18:33:39 +0530156}