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.
 
 
 
 
 
 

279 lines
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.parent,{marginTop:'8px'});
  31. this.init(field.parent, 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_tbar_link = function(parent, label, fn, icon) {
  39. var div = $a(parent,'div','',{cursor:'pointer'});
  40. var t = make_table(div, 1, 2, '90%', ['20px',null]);
  41. var img = $a($td(t,0,0), 'i' , icon);
  42. $y($td(t,0,0),{textAlign:'right'});
  43. var l = $a($td(t,0,1),'span','link_type',{color:'#333'});
  44. l.style.fontSize = '11px';
  45. l.innerHTML = label;
  46. div.onclick = fn;
  47. div.show = function() { $ds(this); }
  48. div.hide = function() { $dh(this); }
  49. $td(t,0,0).isactive = 1;
  50. $td(t,0,1).isactive = 1;
  51. l.isactive = 1;
  52. div.isactive = 1;
  53. img.isactive = 1;
  54. return div;
  55. }
  56. _f.FormGrid.prototype.make_buttons = function() {
  57. var me = this;
  58. this.tbar_btns = {};
  59. this.tbar_btns['Del'] = this.make_tbar_link($td(this.tbar_tab,0,0),'Del',
  60. function() { me.delete_row(); }, 'icon-remove-sign');
  61. this.tbar_btns['Ins'] = this.make_tbar_link($td(this.tbar_tab,0,1),'Ins',
  62. function() { me.insert_row(); }, 'icon-plus');
  63. this.tbar_btns['Up'] = this.make_tbar_link($td(this.tbar_tab,0,2),'Up',
  64. function() { me.move_row(true); }, 'icon-arrow-up');
  65. this.tbar_btns['Dn'] = this.make_tbar_link($td(this.tbar_tab,0,3),'Dn',
  66. function() { me.move_row(false); }, 'icon-arrow-down');
  67. for(var i in this.btns)
  68. this.btns[i].isactive = true;
  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. locals[cur_frm.doctype][cur_frm.docname].__unsaved=1;
  118. cur_frm.frm_head && cur_frm.frm_head.refresh_labels();
  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. this.set_unsaved();
  134. }
  135. _f.FormGrid.prototype.new_row_doc = function() {
  136. // create row doc
  137. var n = LocalDB.create(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. return d;
  143. }
  144. _f.FormGrid.prototype.add_newrow = function() {
  145. var r = this.tab.rows[this.tab.rows.length - 1];
  146. if(!r.is_newrow)
  147. show_alert('fn: add_newrow: Adding a row which is not flagged as new');
  148. var d = this.new_row_doc();
  149. d.idx = r.rowIndex + 1;
  150. // set row
  151. r.docname = d.name;
  152. //r.cells[0].div.innerHTML = r.rowIndex + 1;
  153. r.is_newrow = false;
  154. this.set_cell_value(r.cells[0]);
  155. // one more
  156. this.make_newrow();
  157. this.refresh_row(r.rowIndex, d.name); // added 26-Mar-09
  158. if(this.onrowadd) this.onrowadd(cur_frm.doc, d.doctype, d.name);
  159. return d.name;
  160. }
  161. _f.FormGrid.prototype.make_newrow = function(from_add_btn) {
  162. if(!this.can_add_rows) // No Addition
  163. return;
  164. // check if exists
  165. if(this.tab.rows.length) {
  166. var r = this.tab.rows[this.tab.rows.length - 1];
  167. if(r.is_newrow)
  168. return;
  169. }
  170. // make new
  171. var r = this.append_row();
  172. r.cells[0].div.innerHTML = '<b style="font-size: 18px;">*</b>';
  173. r.is_newrow = true;
  174. }
  175. _f.FormGrid.prototype.check_selected = function() {
  176. if(!_f.cur_grid_cell) {
  177. show_alert('Select a cell first');
  178. return false;
  179. }
  180. if(_f.cur_grid_cell.grid != this) {
  181. show_alert('Select a cell first');
  182. return false;
  183. }
  184. return true;
  185. }
  186. _f.FormGrid.prototype.delete_row = function(dt, dn) {
  187. if(dt && dn) {
  188. LocalDB.delete_record(dt, dn);
  189. this.refresh();
  190. } else {
  191. if(!this.check_selected()) return;
  192. var r = _f.cur_grid_cell.row;
  193. if(r.is_newrow)return;
  194. var ci = _f.cur_grid_cell.cellIndex;
  195. var ri = _f.cur_grid_cell.row.rowIndex;
  196. LocalDB.delete_record(this.doctype, r.docname);
  197. this.refresh();
  198. if(ri < (this.tab.rows.length-2))
  199. this.cell_select(null, ri, ci);
  200. else _f.cur_grid_cell = null;
  201. }
  202. this.set_unsaved();
  203. }
  204. _f.FormGrid.prototype.move_row = function(up) {
  205. if(!this.check_selected()) return;
  206. var r = _f.cur_grid_cell.row;
  207. if(r.is_newrow)return;
  208. if(up && r.rowIndex > 0) {
  209. var swap_row = this.tab.rows[r.rowIndex - 1];
  210. } else if (!up) {
  211. var len = this.tab.rows.length;
  212. if(this.tab.rows[len-1].is_newrow)
  213. len = len - 1;
  214. if(r.rowIndex < (len-1))
  215. var swap_row = this.tab.rows[r.rowIndex + 1];
  216. }
  217. if(swap_row) {
  218. var cidx = _f.cur_grid_cell.cellIndex;
  219. this.cell_deselect();
  220. // swap index
  221. var aidx = locals[this.doctype][r.docname].idx;
  222. locals[this.doctype][r.docname].idx = locals[this.doctype][swap_row.docname].idx;
  223. locals[this.doctype][swap_row.docname].idx = aidx;
  224. // swap rows
  225. var adocname = swap_row.docname;
  226. this.refresh_row(swap_row.rowIndex, r.docname);
  227. this.refresh_row(r.rowIndex, adocname);
  228. this.cell_select(this.tab.rows[swap_row.rowIndex].cells[cidx]);
  229. this.set_unsaved();
  230. }
  231. }