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