You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

244 rivejä
6.4 KiB

  1. _f.FormGrid = function(field) {
  2. this.field = field;
  3. this.doctype = field.df.options;
  4. if(!this.doctype) {
  5. show_alert('No Options for table ' + field.df.label);
  6. }
  7. this.col_break_width = cint(this.field.col_break_width);
  8. if(!this.col_break_width) this.col_break_width = 100;
  9. $y(field.parent,{marginTop:'8px'});
  10. this.init(field.parent, field.df.width);
  11. this.setup();
  12. }
  13. _f.FormGrid.prototype = new _f.Grid();
  14. _f.FormGrid.prototype.setup = function() {
  15. this.make_columns();
  16. }
  17. _f.FormGrid.prototype.make_tbar_link = function(parent, label, fn, icon) {
  18. var div = $a(parent,'div','',{cursor:'pointer'});
  19. var t = make_table(div, 1, 2, '90%', ['20px',null]);
  20. var img = $a($td(t,0,0),'div','wn-icon ' + icon);
  21. $y($td(t,0,0),{textAlign:'right'});
  22. var l = $a($td(t,0,1),'span','link_type',{color:'#333'});
  23. l.style.fontSize = '11px';
  24. l.innerHTML = label;
  25. div.onclick = fn;
  26. div.show = function() { $ds(this); }
  27. div.hide = function() { $dh(this); }
  28. $td(t,0,0).isactive = 1;
  29. $td(t,0,1).isactive = 1;
  30. l.isactive = 1;
  31. div.isactive = 1;
  32. img.isactive = 1;
  33. return div;
  34. }
  35. _f.FormGrid.prototype.make_buttons = function() {
  36. var me = this;
  37. this.tbar_btns = {};
  38. this.tbar_btns['Del'] = this.make_tbar_link($td(this.tbar_tab,0,0),'Del', function() { me.delete_row(); }, 'ic-round_minus');
  39. this.tbar_btns['Ins'] = this.make_tbar_link($td(this.tbar_tab,0,1),'Ins', function() { me.insert_row(); }, 'ic-round_plus');
  40. this.tbar_btns['Up'] = this.make_tbar_link($td(this.tbar_tab,0,2),'Up', function() { me.move_row(true); }, 'ic-arrow_top');
  41. this.tbar_btns['Dn'] = this.make_tbar_link($td(this.tbar_tab,0,3),'Dn', function() { me.move_row(false); }, 'ic-arrow_bottom');
  42. for(var i in this.btns)
  43. this.btns[i].isactive = true;
  44. }
  45. _f.FormGrid.prototype.make_columns = function() {
  46. var gl = fields_list[this.field.df.options];
  47. if(!gl) {
  48. alert('Table details not found "'+this.field.df.options+'"');
  49. }
  50. gl.sort(function(a,b) { return a.idx - b.idx});
  51. var p = this.field.perm;
  52. for(var i=0;i<gl.length;i++) {
  53. if(p[this.field.df.permlevel] && p[this.field.df.permlevel][READ]) { // if read
  54. this.insert_column(this.field.df.options, gl[i].fieldname, gl[i].fieldtype, gl[i].label, gl[i].width, gl[i].options, this.field.perm, gl[i].reqd);
  55. // hide it even if it is hidden at start..
  56. // so that it can be brought back once
  57. if(gl[i].hidden) {
  58. this.set_column_disp(gl[i].fieldname, false);
  59. }
  60. }
  61. }
  62. }
  63. _f.FormGrid.prototype.set_column_label = function(fieldname, label) {
  64. for(var i=0;i<this.head_row.cells.length;i++) {
  65. var c = this.head_row.cells[i];
  66. if(c.fieldname == fieldname) {
  67. c.innerHTML = '<div class="grid_head_div">'+label+'</div>';
  68. c.cur_label = label;
  69. break;
  70. }
  71. }
  72. }
  73. _f.FormGrid.prototype.refresh = function() {
  74. var docset = getchildren(this.doctype, this.field.frm.docname, this.field.df.fieldname, this.field.frm.doctype);
  75. var data = [];
  76. //alert(docset.length);
  77. for(var i=0; i<docset.length; i++) {
  78. locals[this.doctype][docset[i].name].idx = i+1;
  79. data[data.length] = docset[i].name;
  80. }
  81. this.set_data(data);
  82. }
  83. _f.FormGrid.prototype.set_unsaved = function() {
  84. // set unsaved
  85. locals[cur_frm.doctype][cur_frm.docname].__unsaved=1;
  86. cur_frm.set_heading();
  87. }
  88. _f.FormGrid.prototype.insert_row = function() {
  89. var d = this.new_row_doc();
  90. var ci = _f.cur_grid_cell.cellIndex;
  91. var row_idx = _f.cur_grid_cell.row.rowIndex;
  92. d.idx = row_idx+1;
  93. for(var ri = row_idx; ri<this.tab.rows.length; ri++) {
  94. var r = this.tab.rows[ri];
  95. if(r.docname)
  96. locals[this.doctype][r.docname].idx++;
  97. }
  98. // refresh
  99. this.refresh();
  100. this.cell_select('', row_idx, ci);
  101. this.set_unsaved();
  102. }
  103. _f.FormGrid.prototype.new_row_doc = function() {
  104. // create row doc
  105. var n = LocalDB.create(this.doctype);
  106. var d = locals[this.doctype][n];
  107. d.parent = this.field.frm.docname;
  108. d.parentfield = this.field.df.fieldname;
  109. d.parenttype = this.field.frm.doctype;
  110. return d;
  111. }
  112. _f.FormGrid.prototype.add_newrow = function() {
  113. var r = this.tab.rows[this.tab.rows.length - 1];
  114. if(!r.is_newrow)
  115. show_alert('fn: add_newrow: Adding a row which is not flagged as new');
  116. var d = this.new_row_doc();
  117. d.idx = r.rowIndex + 1;
  118. // set row
  119. r.docname = d.name;
  120. //r.cells[0].div.innerHTML = r.rowIndex + 1;
  121. r.is_newrow = false;
  122. this.set_cell_value(r.cells[0]);
  123. // one more
  124. this.make_newrow();
  125. this.refresh_row(r.rowIndex, d.name); // added 26-Mar-09
  126. if(this.onrowadd) this.onrowadd(cur_frm.doc, d.doctype, d.name);
  127. return d.name;
  128. }
  129. _f.FormGrid.prototype.make_newrow = function(from_add_btn) {
  130. if(!this.can_add_rows) // No Addition
  131. return;
  132. // check if exists
  133. if(this.tab.rows.length) {
  134. var r = this.tab.rows[this.tab.rows.length - 1];
  135. if(r.is_newrow)
  136. return;
  137. }
  138. // make new
  139. var r = this.append_row();
  140. r.cells[0].div.innerHTML = '<b style="font-size: 18px;">*</b>';
  141. r.is_newrow = true;
  142. }
  143. _f.FormGrid.prototype.check_selected = function() {
  144. if(!_f.cur_grid_cell) {
  145. show_alert('Select a cell first');
  146. return false;
  147. }
  148. if(_f.cur_grid_cell.grid != this) {
  149. show_alert('Select a cell first');
  150. return false;
  151. }
  152. return true;
  153. }
  154. _f.FormGrid.prototype.delete_row = function(dt, dn) {
  155. if(dt && dn) {
  156. LocalDB.delete_record(dt, dn);
  157. this.refresh();
  158. } else {
  159. if(!this.check_selected()) return;
  160. var r = _f.cur_grid_cell.row;
  161. if(r.is_newrow)return;
  162. var ci = _f.cur_grid_cell.cellIndex;
  163. var ri = _f.cur_grid_cell.row.rowIndex;
  164. LocalDB.delete_record(this.doctype, r.docname);
  165. this.refresh();
  166. if(ri < (this.tab.rows.length-2))
  167. this.cell_select(null, ri, ci);
  168. else _f.cur_grid_cell = null;
  169. }
  170. this.set_unsaved();
  171. }
  172. _f.FormGrid.prototype.move_row = function(up) {
  173. if(!this.check_selected()) return;
  174. var r = _f.cur_grid_cell.row;
  175. if(r.is_newrow)return;
  176. if(up && r.rowIndex > 0) {
  177. var swap_row = this.tab.rows[r.rowIndex - 1];
  178. } else if (!up) {
  179. var len = this.tab.rows.length;
  180. if(this.tab.rows[len-1].is_newrow)
  181. len = len - 1;
  182. if(r.rowIndex < (len-1))
  183. var swap_row = this.tab.rows[r.rowIndex + 1];
  184. }
  185. if(swap_row) {
  186. var cidx = _f.cur_grid_cell.cellIndex;
  187. this.cell_deselect();
  188. // swap index
  189. var aidx = locals[this.doctype][r.docname].idx;
  190. locals[this.doctype][r.docname].idx = locals[this.doctype][swap_row.docname].idx;
  191. locals[this.doctype][swap_row.docname].idx = aidx;
  192. // swap rows
  193. var adocname = swap_row.docname;
  194. this.refresh_row(swap_row.rowIndex, r.docname);
  195. this.refresh_row(r.rowIndex, adocname);
  196. this.cell_select(this.tab.rows[swap_row.rowIndex].cells[cidx]);
  197. this.set_unsaved();
  198. }
  199. }