Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

278 righe
7.8 KiB

  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. _f.FormGrid = function(field) {
  23. this.field = field;
  24. this.doctype = field.df.options;
  25. if(!this.doctype) {
  26. show_alert('No Options for table ' + field.df.label);
  27. }
  28. this.col_break_width = cint(this.field.col_break_width);
  29. if(!this.col_break_width) this.col_break_width = 100;
  30. $y(field.wrapper,{marginTop:'8px'});
  31. this.init(field.wrapper, field.df.width);
  32. this.setup();
  33. }
  34. _f.FormGrid.prototype = new _f.Grid();
  35. _f.FormGrid.prototype.setup = function() {
  36. this.make_columns();
  37. }
  38. _f.FormGrid.prototype.make_buttons = function() {
  39. var me = this;
  40. this.tbar_btns = {};
  41. this.tbar_btns['Del'] = this.make_tbar_link($td(this.tbar_tab,0,0),'Del',
  42. function() { me.delete_row(); }, 'icon-remove-sign');
  43. this.tbar_btns['Ins'] = this.make_tbar_link($td(this.tbar_tab,0,1),'Ins',
  44. function() { me.insert_row(); }, 'icon-plus');
  45. this.tbar_btns['Up'] = this.make_tbar_link($td(this.tbar_tab,0,2),'Up',
  46. function() { me.move_row(true); }, 'icon-arrow-up');
  47. this.tbar_btns['Dn'] = this.make_tbar_link($td(this.tbar_tab,0,3),'Dn',
  48. function() { me.move_row(false); }, 'icon-arrow-down');
  49. for(var i in this.btns)
  50. this.btns[i].isactive = true;
  51. }
  52. _f.FormGrid.prototype.make_tbar_link = function(parent, label, fn, icon) {
  53. var div = $a(parent,'div','',{cursor:'pointer'});
  54. var t = make_table(div, 1, 2, '90%', ['20px',null]);
  55. var img = $a($td(t,0,0), 'i' , icon);
  56. $y($td(t,0,0),{textAlign:'right'});
  57. var l = $a($td(t,0,1),'span','link_type',{color:'#333'});
  58. l.style.fontSize = '11px';
  59. l.innerHTML = label;
  60. div.onclick = fn;
  61. div.show = function() { $ds(this); }
  62. div.hide = function() { $dh(this); }
  63. $td(t,0,0).isactive = 1;
  64. $td(t,0,1).isactive = 1;
  65. l.isactive = 1;
  66. div.isactive = 1;
  67. img.isactive = 1;
  68. return div;
  69. }
  70. _f.FormGrid.prototype.make_columns = function() {
  71. var gl = wn.meta.docfield_list[this.field.df.options];
  72. if(!gl) {
  73. alert('Table details not found "'+this.field.df.options+'"');
  74. }
  75. gl.sort(function(a,b) { return a.idx - b.idx});
  76. var p = this.field.perm;
  77. for(var i=0;i<gl.length;i++) {
  78. if(p[this.field.df.permlevel] && p[this.field.df.permlevel][READ]) { // if read
  79. 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);
  80. // hide it even if it is hidden at start..
  81. // so that it can be brought back once
  82. if(gl[i].hidden) {
  83. this.set_column_disp(gl[i].fieldname, false);
  84. }
  85. }
  86. }
  87. }
  88. _f.FormGrid.prototype.set_column_label = function(fieldname, label) {
  89. for(var i=0;i<this.head_row.cells.length;i++) {
  90. var c = this.head_row.cells[i];
  91. if(c.fieldname == fieldname) {
  92. c.innerHTML = '<div class="grid_head_div">'+label+'</div>';
  93. c.cur_label = label;
  94. break;
  95. }
  96. }
  97. }
  98. _f.FormGrid.prototype.get_children = function() {
  99. return getchildren(this.doctype, this.field.frm.docname, this.field.df.fieldname, this.field.frm.doctype);
  100. }
  101. _f.FormGrid.prototype.refresh = function() {
  102. var docset = this.get_children();
  103. var data = [];
  104. //alert(docset.length);
  105. for(var i=0; i<docset.length; i++) {
  106. locals[this.doctype][docset[i].name].idx = i+1;
  107. data[data.length] = docset[i].name;
  108. }
  109. this.set_data(data);
  110. // if form open, refresh form
  111. if(_f.frm_dialog && _f.frm_dialog.dialog.display && _f.frm_dialog.cur_frm) {
  112. _f.frm_dialog.cur_frm.refresh();
  113. }
  114. }
  115. _f.FormGrid.prototype.set_unsaved = function() {
  116. // set unsaved
  117. cur_frm.set_unsaved();
  118. }
  119. _f.FormGrid.prototype.insert_row = function() {
  120. var d = this.new_row_doc();
  121. var ci = _f.cur_grid_cell.cellIndex;
  122. var row_idx = _f.cur_grid_cell.row.rowIndex;
  123. d.idx = row_idx+1;
  124. for(var ri = row_idx; ri<this.tab.rows.length; ri++) {
  125. var r = this.tab.rows[ri];
  126. if(r.docname)
  127. locals[this.doctype][r.docname].idx++;
  128. }
  129. // refresh
  130. this.refresh();
  131. this.cell_select('', row_idx, ci);
  132. }
  133. _f.FormGrid.prototype.new_row_doc = function() {
  134. // create row doc
  135. var n = LocalDB.create(this.doctype);
  136. var d = locals[this.doctype][n];
  137. d.parent = this.field.frm.docname;
  138. d.parentfield = this.field.df.fieldname;
  139. d.parenttype = this.field.frm.doctype;
  140. this.set_unsaved();
  141. return d;
  142. }
  143. _f.FormGrid.prototype.add_newrow = function() {
  144. var r = this.tab.rows[this.tab.rows.length - 1];
  145. if(!r.is_newrow)
  146. show_alert('fn: add_newrow: Adding a row which is not flagged as new');
  147. var d = this.new_row_doc();
  148. d.idx = r.rowIndex + 1;
  149. // set row
  150. r.docname = d.name;
  151. //r.cells[0].div.innerHTML = r.rowIndex + 1;
  152. r.is_newrow = false;
  153. this.set_cell_value(r.cells[0]);
  154. // one more
  155. this.make_newrow();
  156. this.refresh_row(r.rowIndex, d.name); // added 26-Mar-09
  157. if(this.onrowadd) this.onrowadd(cur_frm.doc, d.doctype, d.name);
  158. return d.name;
  159. }
  160. _f.FormGrid.prototype.make_newrow = function(from_add_btn) {
  161. if(!this.can_add_rows) // No Addition
  162. return;
  163. // check if exists
  164. if(this.tab.rows.length) {
  165. var r = this.tab.rows[this.tab.rows.length - 1];
  166. if(r.is_newrow)
  167. return;
  168. }
  169. // make new
  170. var r = this.append_row();
  171. r.cells[0].div.innerHTML = '<b style="font-size: 18px;">*</b>';
  172. r.is_newrow = true;
  173. }
  174. _f.FormGrid.prototype.check_selected = function() {
  175. if(!_f.cur_grid_cell) {
  176. show_alert('Select a cell first');
  177. return false;
  178. }
  179. if(_f.cur_grid_cell.grid != this) {
  180. show_alert('Select a cell first');
  181. return false;
  182. }
  183. return true;
  184. }
  185. _f.FormGrid.prototype.delete_row = function(dt, dn) {
  186. if(dt && dn) {
  187. LocalDB.delete_record(dt, dn);
  188. this.refresh();
  189. } else {
  190. if(!this.check_selected()) return;
  191. var r = _f.cur_grid_cell.row;
  192. if(r.is_newrow)return;
  193. var ci = _f.cur_grid_cell.cellIndex;
  194. var ri = _f.cur_grid_cell.row.rowIndex;
  195. LocalDB.delete_record(this.doctype, r.docname);
  196. this.refresh();
  197. if(ri < (this.tab.rows.length-1))
  198. this.cell_select(null, ri, ci);
  199. else _f.cur_grid_cell = null;
  200. }
  201. this.set_unsaved();
  202. }
  203. _f.FormGrid.prototype.move_row = function(up) {
  204. if(!this.check_selected()) return;
  205. var r = _f.cur_grid_cell.row;
  206. if(r.is_newrow)return;
  207. if(up && r.rowIndex > 0) {
  208. var swap_row = this.tab.rows[r.rowIndex - 1];
  209. } else if (!up) {
  210. var len = this.tab.rows.length;
  211. if(this.tab.rows[len-1].is_newrow)
  212. len = len - 1;
  213. if(r.rowIndex < (len-1))
  214. var swap_row = this.tab.rows[r.rowIndex + 1];
  215. }
  216. if(swap_row) {
  217. var cidx = _f.cur_grid_cell.cellIndex;
  218. this.cell_deselect();
  219. // swap index
  220. var aidx = locals[this.doctype][r.docname].idx;
  221. locals[this.doctype][r.docname].idx = locals[this.doctype][swap_row.docname].idx;
  222. locals[this.doctype][swap_row.docname].idx = aidx;
  223. // swap rows
  224. var adocname = swap_row.docname;
  225. this.refresh_row(swap_row.rowIndex, r.docname);
  226. this.refresh_row(r.rowIndex, adocname);
  227. this.cell_select(this.tab.rows[swap_row.rowIndex].cells[cidx]);
  228. this.set_unsaved();
  229. }
  230. }