Rushabh Mehta | 2886b95 | 2012-02-24 11:26:31 +0530 | [diff] [blame] | 1 | wn.provide('erpnext.todo'); |
| 2 | |
| 3 | erpnext.todo.refresh = function() { |
| 4 | wn.call({ |
| 5 | method: 'utilities.page.todo.todo.get', |
| 6 | callback: function(r,rt) { |
| 7 | $('#todo-list').empty(); |
| 8 | if(r.message) { |
| 9 | for(var i in r.message) { |
| 10 | new erpnext.todo.ToDoItem(r.message[i]); |
| 11 | } |
| 12 | } else { |
| 13 | $('#todo-list').html('<div class="help-box">Nothing to do :)</div>'); |
| 14 | } |
| 15 | } |
| 16 | }); |
| 17 | |
| 18 | $('#add-todo').click(function() { |
| 19 | erpnext.todo.make_dialog({ |
| 20 | date:get_today(), priority:'Medium', checked:0, description:''}); |
| 21 | }) |
| 22 | } |
| 23 | |
| 24 | erpnext.todo.ToDoItem = Class.extend({ |
| 25 | init: function(todo) { |
| 26 | label_map = { |
| 27 | 'High': 'label-important', |
| 28 | 'Medium': 'label-info', |
| 29 | 'Low':'' |
| 30 | } |
| 31 | todo.labelclass = label_map[todo.priority]; |
| 32 | todo.userdate = dateutil.str_to_user(todo.date); |
| 33 | $('#todo-list').append(repl('<div class="todoitem">\ |
| 34 | <span class="description">\ |
| 35 | <span class="label %(labelclass)s">%(priority)s</span>\ |
| 36 | <span class="help" style="margin-right: 7px">%(userdate)s</span>\ |
| 37 | %(description)s</span>\ |
| 38 | <span class="ref_link">→\ |
| 39 | <a href="#!Form/%(reference_type)s/%(reference_name)s">\ |
| 40 | [%(reference_name)s]</a></span>\ |
| 41 | <a href="#" class="close">×</a>\ |
| 42 | </div>', todo)); |
| 43 | $todo = $('div.todoitem:last'); |
| 44 | |
| 45 | if(todo.checked) { |
| 46 | $todo.find('.description').css('text-decoration', 'line-through'); |
| 47 | } |
| 48 | |
| 49 | if(!todo.reference_name) |
| 50 | $todo.find('.ref_link').toggle(false); |
| 51 | |
| 52 | $todo.find('.description') |
| 53 | .data('todo', todo) |
| 54 | .click(function() { |
| 55 | erpnext.todo.make_dialog($(this).data('todo')); |
| 56 | return false; |
| 57 | }); |
| 58 | |
| 59 | $todo.find('.close') |
| 60 | .data('name', todo.name) |
| 61 | .click(function() { |
| 62 | $(this).parent().css('opacity', 0.5); |
| 63 | wn.call({ |
| 64 | method:'utilities.page.todo.todo.delete', |
| 65 | args: {name: $(this).data('name')}, |
| 66 | callback: function() { |
| 67 | erpnext.todo.refresh(); |
| 68 | } |
| 69 | }); |
| 70 | return false; |
| 71 | }) |
| 72 | } |
| 73 | }); |
| 74 | |
| 75 | erpnext.todo.make_dialog = function(det) { |
| 76 | if(!erpnext.todo.dialog) { |
| 77 | var dialog = new wn.widgets.Dialog(); |
| 78 | dialog.make({ |
| 79 | width: 480, |
| 80 | title: 'To Do', |
| 81 | fields: [ |
| 82 | {fieldtype:'Date', fieldname:'date', label:'Event Date', reqd:1}, |
| 83 | {fieldtype:'Text', fieldname:'description', label:'Description', reqd:1}, |
| 84 | {fieldtype:'Check', fieldname:'checked', label:'Completed'}, |
| 85 | {fieldtype:'Select', fieldname:'priority', label:'Priority', reqd:1, 'options':['Medium','High','Low'].join('\n')}, |
| 86 | {fieldtype:'Button', fieldname:'save', label:'Save'} |
| 87 | ] |
| 88 | }); |
| 89 | |
| 90 | dialog.fields_dict.save.input.onclick = function() { |
| 91 | erpnext.todo.save(this); |
| 92 | } |
| 93 | erpnext.todo.dialog = dialog; |
| 94 | } |
| 95 | |
| 96 | if(det) { |
| 97 | erpnext.todo.dialog.set_values({ |
| 98 | date: det.date, |
| 99 | priority: det.priority, |
| 100 | description: det.description, |
| 101 | checked: det.checked |
| 102 | }); |
| 103 | erpnext.todo.dialog.det = det; |
| 104 | } |
| 105 | erpnext.todo.dialog.show(); |
| 106 | |
| 107 | } |
| 108 | |
| 109 | erpnext.todo.save = function(btn) { |
| 110 | var d = erpnext.todo.dialog; |
| 111 | var det = d.get_values(); |
| 112 | |
| 113 | if(!det) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | det.name = d.det.name || ''; |
| 118 | wn.call({ |
| 119 | method:'utilities.page.todo.todo.edit', |
| 120 | args: det, |
| 121 | btn: btn, |
| 122 | callback: function() { |
| 123 | erpnext.todo.dialog.hide(); |
| 124 | erpnext.todo.refresh(); |
| 125 | } |
| 126 | }); |
| 127 | } |
| 128 | |
| 129 | pscript.onload_todo = function() { |
| 130 | // load todos |
| 131 | erpnext.todo.refresh(); |
| 132 | } |