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