blob: b30626dc22713939756170328c16cf413e07a1df [file] [log] [blame]
Rushabh Mehta3966f1d2012-02-23 12:35:32 +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
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053017pscript['onload_Trash'] = function() {
18
19 // header and toolbar
20 var h = new PageHeader('trash_header','Trash Bin','Restore the documents that you have trashed')
21
22 if(!pscript.trash_bin) pscript.trash_bin = new pscript.Trash();
23}
24
25pscript.Trash = function() {
26 // create UI elements
27 this.wrapper = $i('trash_div');
28
29 this.head = $a(this.wrapper, 'div');
30 this.body = $a(this.wrapper, 'div');
31 $y(this.body, {margin:'8px'})
32
33 this.make_head();
34 this.load_masters();
35}
36
37// Make Button
38// ------------
39pscript.Trash.prototype.make_button = function(label, area){
40 var me = this;
41 var w = $a(area, 'div', '', {margin:'8px'});
42 var t = make_table(w,1,1,'400px',['50%','50%']);
43 var s = $a($td(t,0,0),'button');
44 s.innerHTML = label;
45 s.wrapper = w;
46 return s;
47}
48
49
50// Make Head
51// -------------
52pscript.Trash.prototype.make_head = function() {
53 var me = this;
54
55 var make_select = function(label) {
56 var w = $a(me.head, 'div', '', {margin:'8px'});
57 var t = make_table(w,1,2,'400px',['50%','50%']);
58 $td(t,0,0).innerHTML = label;
59 var s = $a($td(t,0,1),'select','',{width:'140px'});
60 s.wrapper = w;
61 return s;
62 }
63
64 // Select Master Name
65 this.master_select = make_select('Select Master');
66
67 var me = this;
68 // Get Records
69 this.get_records_button = me.make_button('Get Records', me.head);
70 this.get_records_button.onclick = function() {
71 me.get_records();
72 }
73}
74
75
76// Load Masters
77// -------------
78pscript.Trash.prototype.load_masters = function(){
79 var me = this;
80 var callback = function(r, rt){
81 // Masters
82 empty_select(me.master_select);
83 add_sel_options(me.master_select,add_lists(['All'], r.message), 'All');
84 }
85 $c_obj('Trash Control','get_masters','',callback);
86}
87
88
89// Get Records
90// -----------
91pscript.Trash.prototype.get_records = function(){
92 var me = this;
93 me.body.innerHTML = '';
94 var callback = function(r, rt){
95 if(r.message) me.generate_trash_records(r.message);
96 else msgprint("No Records Found");
97 }
98 $c_obj('Trash Control','get_trash_records',sel_val(me.master_select),callback);
99}
100
101
102// Generate Trash Records
103// -----------------------
104pscript.Trash.prototype.generate_trash_records = function(rec_dict){
105 var me = this;
106 pscript.all_checkboxes = [];
107 mnames = keys(rec_dict).sort();
108 for(var i = 0; i < mnames.length; i ++){
109 var head = $a(me.body, 'h3'); head.innerHTML = mnames[i];
110 var rec_table = make_table(me.body,rec_dict[mnames[i]].length,2,'375px',['350px','25px'],{border:'1px solid #AAA',padding:'2px'});
111 for(var j = 0; j < rec_dict[mnames[i]].length; j++){
112 $a_input($td(rec_table,j,0), 'data');
113 $td(rec_table,j,0).innerHTML = rec_dict[mnames[i]][j];
114 var chk = $a_input($td(rec_table,j,1), 'checkbox');
115 chk.master = mnames[i];
116 chk.record = rec_dict[mnames[i]][j];
117 pscript.all_checkboxes.push(chk);
118 }
119 }
120 this.restore_button = me.make_button('Restore Selected', me.body);
121 this.restore_button.onclick = function() {
122 me.restore_records(0);
123 }
124 this.restore_all_button = me.make_button('Restore All', me.body);
125 this.restore_all_button.onclick = function() {
126 me.restore_records(1);
127 }
128}
129
130
131// Restore Records
132// ---------------
133pscript.Trash.prototype.restore_records = function(restore_all){
134 var me = this;
135 var out = {};
136 for(i in pscript.all_checkboxes) {
137 c = pscript.all_checkboxes[i];
138 if (restore_all || (!restore_all && c.checked)) {
139 if(!out[c.master]) out[c.master] = [c.record];
140 else {out[c.master].push(c.record);}
141 }
142 }
143 $c_obj('Trash Control','restore_records',JSON.stringify(out),function(r, rt){me.get_records();})
144}