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.
 
 
 
 
 
 

284 lines
8.0 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),wn._('Del'),
  42. function() { me.delete_row(); }, 'icon-remove-sign');
  43. this.tbar_btns['Ins'] = this.make_tbar_link($td(this.tbar_tab,0,1),wn._('Ins'),
  44. function() { me.insert_row(); }, 'icon-plus');
  45. this.tbar_btns['Up'] = this.make_tbar_link($td(this.tbar_tab,0,2),wn._('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),wn._('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 p = this.field.perm;
  72. if(p[this.field.df.permlevel] && p[this.field.df.permlevel][READ]) { // if read
  73. var gl = wn.meta.docfield_list[this.field.df.options];
  74. if(!gl) {
  75. alert('Table details not found "'+this.field.df.options+'"');
  76. }
  77. gl.sort(function(a,b) { return a.idx - b.idx});
  78. for(var i=0;i<gl.length;i++) {
  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. // also, hide column if no permissions found
  83. if(gl[i].hidden || !(p[gl[i].permlevel] && p[gl[i].permlevel][READ])) {
  84. this.set_column_disp(gl[i].fieldname, false);
  85. }
  86. }
  87. }
  88. }
  89. _f.FormGrid.prototype.set_column_label = function(fieldname, label) {
  90. for(var i=0;i<this.head_row.cells.length;i++) {
  91. var c = this.head_row.cells[i];
  92. if(c.fieldname == fieldname) {
  93. c.innerHTML = '<div class="grid_head_div">'+label+'</div>';
  94. c.cur_label = label;
  95. break;
  96. }
  97. }
  98. }
  99. _f.FormGrid.prototype.get_children = function() {
  100. return getchildren(this.doctype, this.field.frm.docname, this.field.df.fieldname, this.field.frm.doctype);
  101. }
  102. _f.FormGrid.prototype.refresh = function() {
  103. var docset = this.get_children();
  104. var data = [];
  105. //alert(docset.length);
  106. for(var i=0; i<docset.length; i++) {
  107. locals[this.doctype][docset[i].name].idx = i+1;
  108. data[data.length] = docset[i].name;
  109. }
  110. this.set_data(data);
  111. // if form open, refresh form
  112. if(_f.frm_dialog && _f.frm_dialog.dialog.display && _f.frm_dialog.cur_frm) {
  113. _f.frm_dialog.cur_frm.refresh();
  114. }
  115. }
  116. _f.FormGrid.prototype.set_unsaved = function() {
  117. // set unsaved
  118. cur_frm.set_unsaved();
  119. }
  120. _f.FormGrid.prototype.insert_row = function() {
  121. var d = this.new_row_doc();
  122. var ci = _f.cur_grid_cell.cellIndex;
  123. var row_idx = _f.cur_grid_cell.row.rowIndex;
  124. d.idx = row_idx+1;
  125. for(var ri = row_idx; ri<this.tab.rows.length; ri++) {
  126. var r = this.tab.rows[ri];
  127. if(r.docname)
  128. locals[this.doctype][r.docname].idx++;
  129. }
  130. // refresh
  131. this.refresh();
  132. this.cell_select('', row_idx, ci);
  133. if(this.onrowadd) this.onrowadd(cur_frm.doc, d.doctype, d.name);
  134. }
  135. _f.FormGrid.prototype.new_row_doc = function() {
  136. // create row doc
  137. var n = wn.model.make_new_doc_and_get_name(this.doctype);
  138. var d = locals[this.doctype][n];
  139. d.parent = this.field.frm.docname;
  140. d.parentfield = this.field.df.fieldname;
  141. d.parenttype = this.field.frm.doctype;
  142. this.set_unsaved();
  143. return d;
  144. }
  145. _f.FormGrid.prototype.add_newrow = function() {
  146. var r = this.tab.rows[this.tab.rows.length - 1];
  147. if(!r.is_newrow)
  148. throw 'Adding a row which is not flagged as new';
  149. var d = this.new_row_doc();
  150. d.idx = r.rowIndex + 1;
  151. // set row
  152. r.docname = d.name;
  153. //r.cells[0].div.innerHTML = r.rowIndex + 1;
  154. r.is_newrow = false;
  155. this.set_cell_value(r.cells[0]);
  156. // one more
  157. this.make_newrow();
  158. this.refresh_row(r.rowIndex, d.name); // added 26-Mar-09
  159. if(this.onrowadd) this.onrowadd(cur_frm.doc, d.doctype, d.name);
  160. return d.name;
  161. }
  162. _f.FormGrid.prototype.make_newrow = function(from_add_btn) {
  163. if(!this.can_add_rows) // No Addition
  164. return;
  165. // check if exists
  166. if(this.tab.rows.length) {
  167. var r = this.tab.rows[this.tab.rows.length - 1];
  168. if(r.is_newrow)
  169. return;
  170. }
  171. // make new
  172. var r = this.append_row();
  173. r.cells[0].div.innerHTML = '<b style="font-size: 18px;">*</b>';
  174. r.is_newrow = true;
  175. }
  176. _f.FormGrid.prototype.check_selected = function() {
  177. if(!_f.cur_grid_cell) {
  178. show_alert(wn._('Select a cell first'));
  179. return false;
  180. }
  181. if(_f.cur_grid_cell.grid != this) {
  182. show_alert(wn._('Select a cell first'));
  183. return false;
  184. }
  185. return true;
  186. }
  187. _f.FormGrid.prototype.delete_row = function(dt, dn) {
  188. if(dt && dn) {
  189. wn.model.clear_doc(dt, dn);
  190. this.refresh();
  191. } else {
  192. if(!this.check_selected()) return;
  193. var r = _f.cur_grid_cell.row;
  194. if(r.is_newrow)return;
  195. var ci = _f.cur_grid_cell.cellIndex;
  196. var ri = _f.cur_grid_cell.row.rowIndex;
  197. wn.model.clear_doc(this.doctype, r.docname);
  198. this.refresh();
  199. if(ri < (this.tab.rows.length-1))
  200. this.cell_select(null, ri, ci);
  201. else _f.cur_grid_cell = null;
  202. }
  203. this.set_unsaved();
  204. if(this.on_row_delete) this.on_row_delete(cur_frm.doc, dt, dn);
  205. }
  206. _f.FormGrid.prototype.move_row = function(up) {
  207. if(!this.check_selected()) return;
  208. var r = _f.cur_grid_cell.row;
  209. if(r.is_newrow)return;
  210. if(up && r.rowIndex > 0) {
  211. var swap_row = this.tab.rows[r.rowIndex - 1];
  212. } else if (!up) {
  213. var len = this.tab.rows.length;
  214. if(this.tab.rows[len-1].is_newrow)
  215. len = len - 1;
  216. if(r.rowIndex < (len-1))
  217. var swap_row = this.tab.rows[r.rowIndex + 1];
  218. }
  219. if(swap_row) {
  220. var cidx = _f.cur_grid_cell.cellIndex;
  221. this.cell_deselect();
  222. // swap index
  223. var aidx = locals[this.doctype][r.docname].idx;
  224. locals[this.doctype][r.docname].idx = locals[this.doctype][swap_row.docname].idx;
  225. locals[this.doctype][swap_row.docname].idx = aidx;
  226. // swap rows
  227. var adocname = swap_row.docname;
  228. this.refresh_row(swap_row.rowIndex, r.docname);
  229. this.refresh_row(r.rowIndex, adocname);
  230. this.cell_select(this.tab.rows[swap_row.rowIndex].cells[cidx]);
  231. this.set_unsaved();
  232. }
  233. }