Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

496 linhas
13 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.Grid
  23. _f.cur_grid_cell = null;
  24. _f.Grid = function(parent) { }
  25. _f.Grid.prototype.init = function(parent, row_height) {
  26. var me = this;
  27. this.col_idx_by_name = {}
  28. this.alt_row_bg = '#F2F2FF';
  29. this.row_height = row_height;
  30. // make the grid
  31. if(!row_height)this.row_height = '26px';
  32. this.make_ui(parent);
  33. // Sr No
  34. this.insert_column('', '', 'Int', 'Sr', '50px', '', [1,0,0]);
  35. if(this.oninit)this.oninit();
  36. // bind clicks
  37. $(this.wrapper).bind('keydown', function(e) {
  38. me.notify_keypress(e, e.which);
  39. })
  40. // reset grid heights after complete is triggerd on the form
  41. $(cur_frm.wrapper).bind('render_complete', function() {
  42. me.set_ht();
  43. });
  44. }
  45. _f.Grid.prototype.make_ui = function(parent) {
  46. var ht = make_table($a(parent, 'div'), 1, 2, '100%', ['55%','45%']);
  47. this.main_title = $td(ht,0,0); this.main_title.className = 'columnHeading';
  48. $td(ht,0,1).style.textAlign = 'right';
  49. this.tbar_div = $a($td(ht,0,1), 'div', 'grid_tbarlinks');
  50. this.tbar_tab = make_table(this.tbar_div,1,4,'100%',['25%','25%','25%','25%']);
  51. this.wrapper = $a(parent, 'div', 'grid_wrapper round');
  52. this.head_wrapper = $a(this.wrapper, 'div', 'grid_head_wrapper');
  53. this.head_tab = $a(this.head_wrapper, 'table', 'grid_head_table');
  54. this.head_row = this.head_tab.insertRow(0);
  55. this.tab_wrapper = $a(this.wrapper, 'div', 'grid_tab_wrapper');
  56. this.tab = $a(this.tab_wrapper, 'table', 'grid_table');
  57. var me = this;
  58. this.wrapper.onscroll = function() { me.head_wrapper.style.top = me.wrapper.scrollTop+'px'; }
  59. }
  60. _f.Grid.prototype.show = function() {
  61. if(this.can_edit && this.field.df['default'].toLowerCase()!='no toolbar') {
  62. $ds(this.tbar_div);
  63. if(this.can_add_rows) {
  64. $td(this.tbar_tab, 0, 0).style.display = 'table-cell';
  65. $td(this.tbar_tab, 0, 1).style.display = 'table-cell';
  66. } else {
  67. $td(this.tbar_tab, 0, 0).style.display = 'none';
  68. $td(this.tbar_tab, 0, 1).style.display = 'none';
  69. }
  70. } else {
  71. $dh(this.tbar_div);
  72. }
  73. $ds(this.wrapper);
  74. }
  75. _f.Grid.prototype.hide = function() {
  76. $dh(this.wrapper); $dh(this.tbar_div);
  77. }
  78. _f.Grid.prototype.insert_column = function(doctype, fieldname, fieldtype, label, width, options, perm, reqd) {
  79. var idx = this.head_row.cells.length;
  80. if(!width)width = '100px';
  81. if(fieldtype=="Currency" && cint(width) < 100) width = "100px";
  82. width= cint(width) + 'px';
  83. var col = this.head_row.insertCell(idx);
  84. col.doctype = doctype; // for report (fields may be from diff doctypes)
  85. col.fieldname = fieldname;
  86. col.fieldtype = fieldtype;
  87. $(col).attr("data-grid-fieldname", doctype + "-" + fieldname);
  88. col.innerHTML = wn._(label);
  89. col.title = label;
  90. col.label = label;
  91. if(reqd)
  92. col.style.color = "#D22";
  93. col.style.width = width;
  94. col.options = options;
  95. col.perm = perm;
  96. this.col_idx_by_name[fieldname] = idx;
  97. }
  98. _f.Grid.prototype.reset_table_width = function() {
  99. var w = 0;
  100. $.each(this.head_row.cells, function(i, cell) {
  101. if((cell.style.display || '').toLowerCase()!='none')
  102. w += cint(cell.style.width);
  103. })
  104. this.head_tab.style.width = w + 'px';
  105. this.tab.style.width = w + 'px';
  106. }
  107. _f.Grid.prototype.set_column_disp = function(fieldname, show) {
  108. var cidx = this.col_idx_by_name[fieldname];
  109. if(!cidx) {
  110. msgprint('Trying to hide unknown column: ' + fieldname);
  111. return;
  112. }
  113. var disp = show ? 'table-cell' : 'none';
  114. // head
  115. this.head_row.cells[cidx].style.display = disp;
  116. // body
  117. for(var i=0, len=this.tab.rows.length; i<len; i++) {
  118. var cell = this.tab.rows[i].cells[cidx];
  119. cell.style.display = disp;
  120. }
  121. // reset table width
  122. this.reset_table_width();
  123. }
  124. _f.Grid.prototype.toggle_reqd = function(fieldname, reqd) {
  125. // IMPORTANT: this should be called in refresh event
  126. var grid_field = this.get_field(fieldname);
  127. grid_field.df.reqd = reqd ? true : false;
  128. grid_field.refresh();
  129. $(grid_field.grid.head_row).find('[data-grid-fieldname="' + grid_field.grid.doctype
  130. + '-' + fieldname + '"]').css({ color: reqd ? "#D22" : "black" });
  131. }
  132. _f.Grid.prototype.append_row = function(idx, docname) {
  133. if(!idx)idx = this.tab.rows.length;
  134. var row = this.tab.insertRow(idx);
  135. row.docname = docname;
  136. if(idx % 2)var odd=true; else var odd=false;
  137. var me = this;
  138. // make cells
  139. for(var i=0; i<this.head_row.cells.length; i++){
  140. var cell = row.insertCell(i);
  141. var hc = this.head_row.cells[i];
  142. // ape style of head
  143. cell.style.width = hc.style.width;
  144. cell.style.display = hc.style.display;
  145. cell.row = row;
  146. cell.grid = this;
  147. cell.className = 'grid_cell';
  148. cell.div = $a(cell, 'div', 'grid_cell_div');
  149. if(this.row_height) {
  150. cell.div.style.height = this.row_height; }
  151. cell.div.cell = cell;
  152. $(cell.div).click(function(e) {
  153. me.cell_select(this.cell);
  154. // cell selected, don't do anything else
  155. // like deselect it
  156. e.stopPropagation();
  157. });
  158. if(odd) {
  159. $bg(cell, this.alt_row_bg); cell.is_odd = 1;
  160. cell.div.style.border = '2px solid ' + this.alt_row_bg;
  161. } else $bg(cell,'#FFF');
  162. if(!hc.fieldname) cell.div.style.cursor = 'default'; // Index
  163. }
  164. this.set_ht();
  165. return row;
  166. }
  167. _f.Grid.prototype.refresh_cell = function(docname, fieldname) {
  168. for(var r=0;r<this.tab.rows.length;r++) {
  169. if(this.tab.rows[r].docname==docname) {
  170. for(var c=0;c<this.head_row.cells.length;c++) {
  171. var hc = this.head_row.cells[c];
  172. if(hc.fieldname==fieldname) {
  173. this.set_cell_value(this.tab.rows[r].cells[c]);
  174. }
  175. }
  176. }
  177. }
  178. }
  179. // for form edit
  180. _f.cur_grid;
  181. _f.cur_grid_ridx;
  182. _f.Grid.prototype.set_cell_value = function(cell) {
  183. // if newrow
  184. if(cell.row.is_newrow)return;
  185. // show static
  186. var hc = this.head_row.cells[cell.cellIndex];
  187. if(hc.fieldname)
  188. var doc = locals[hc.doctype][cell.row.docname];
  189. if(hc.fieldname && doc) {
  190. var v = locals[hc.doctype][cell.row.docname][hc.fieldname];
  191. } else {
  192. var v = (cell.row.rowIndex + 1); // Index
  193. }
  194. if(v==null){ v=''; }
  195. var me = this;
  196. // variations
  197. if(cell.cellIndex) {
  198. var df = copy_dict(hc);
  199. if(df.fieldtype=="Link")
  200. df.fieldtype=="Data";
  201. $(cell.div).html(wn.format(v, hc, doc));
  202. } else {
  203. // Index column
  204. cell.div.style.padding = '2px';
  205. cell.div.style.textAlign = 'left';
  206. cell.div.innerHTML = '';
  207. var t = make_table(cell.div,1,3,'60px',['20px','20px','20px'],{verticalAlign: 'middle', padding:'2px'});
  208. $y($td(t,0,0),{paddingLeft:'4px'});
  209. $td(t,0,0).innerHTML = cell.row.rowIndex + 1;
  210. $(cell.div).click(function() {
  211. if(me.can_edit) {
  212. me.cell_deselect();
  213. cell.div.style.border = '2px solid #88F';
  214. _f.cur_grid_cell = cell;
  215. }
  216. });
  217. if(this.can_edit) {
  218. $("<a title='Edit Row'><i class='icon-edit'></i></a>")
  219. .click(function() {
  220. _f.cur_grid = me;
  221. _f.cur_grid_ridx = cell.row.rowIndex;
  222. _f.edit_record(me.doctype, cell.row.docname, 1);
  223. })
  224. .appendTo($td(t,0,1))
  225. } else {
  226. cell.div.innerHTML = (cell.row.rowIndex + 1);
  227. cell.div.style.cursor = 'default';
  228. cell.div.onclick = function() { }
  229. }
  230. }
  231. }
  232. // if clicked on whitespace
  233. // and a grid cell is selected
  234. // deselect the cell
  235. $(document).bind('click', function(e) {
  236. var me = this;
  237. var is_target_toolbar = function() {
  238. return $(e.target).parents('.grid_tbarlinks').length;
  239. }
  240. var is_target_input = function() {
  241. // select opened
  242. if(e.target.tagName.toLowerCase()=='option') return true;
  243. // autosuggest openend
  244. //if(wn._autosugg_open) return true;
  245. return $(e.target).parents().get().indexOf(_f.cur_grid_cell)!=-1;
  246. }
  247. if(_f.cur_grid_cell && !is_target_input() && !is_target_toolbar()) {
  248. if(!(text_dialog && text_dialog.display)
  249. && !datepicker_active && !(selector && selector.display)) {
  250. setTimeout('_f.cur_grid_cell.grid.cell_deselect()', 500);
  251. return false;
  252. }
  253. }
  254. });
  255. _f.Grid.prototype.cell_deselect = function() {
  256. if(_f.cur_grid_cell) {
  257. var c = _f.cur_grid_cell;
  258. c.grid.remove_template(c);
  259. c.div.className = 'grid_cell_div';
  260. if(c.is_odd) c.div.style.border = '2px solid ' + c.grid.alt_row_bg;
  261. else c.div.style.border = '2px solid #FFF';
  262. _f.cur_grid_cell = null;
  263. }
  264. }
  265. _f.Grid.prototype.cell_select = function(cell, ri, ci) {
  266. if(cell && _f.cur_grid_cell==cell && cell.hc) return;
  267. if(ri!=null && ci!=null)
  268. cell = this.tab.rows[ri].cells[ci];
  269. var hc = this.head_row.cells[cell.cellIndex];
  270. if(!hc.doctype) return;
  271. if(!hc.template) {
  272. this.make_template(hc);
  273. }
  274. hc.template.perm = this.field ? this.field.perm : hc.perm; // get latest permissions
  275. if(hc.fieldname && hc.template.get_status()=='Write') {
  276. this.cell_deselect();
  277. cell.div.style.border = '2px solid #88F';
  278. _f.cur_grid_cell = cell;
  279. this.add_template(cell);
  280. }
  281. }
  282. _f.Grid.prototype.add_template = function(cell) {
  283. if(!cell.row.docname && this.add_newrow) { // activate new row here
  284. this.add_newrow();
  285. this.cell_select(cell);
  286. } else {
  287. var hc = this.head_row.cells[cell.cellIndex];
  288. cell.div.innerHTML = '';
  289. cell.div.appendChild(hc.template.wrapper);
  290. hc.template.activate(cell.row.docname);
  291. hc.template.activated=1;
  292. cell.hc = hc;
  293. if(hc.template.input && hc.template.input.set_width) {
  294. hc.template.input.set_width($(cell).width());
  295. }
  296. }
  297. }
  298. _f.Grid.prototype.get_field = function(fieldname) { // get template
  299. for(var i=0;i<this.head_row.cells.length;i++) {
  300. var hc = this.head_row.cells[i];
  301. if(hc.fieldname == fieldname) {
  302. if(!hc.template) {
  303. this.make_template(hc);
  304. }
  305. return hc.template;
  306. }
  307. }
  308. return {} // did not find, return empty object not to throw error in get_query
  309. }
  310. _f.grid_date_cell = '';
  311. _f.grid_refresh_date = function() {
  312. _f.grid_date_cell.grid.set_cell_value(_f.grid_date_cell);
  313. }
  314. _f.grid_refresh_field = function(temp, input) {
  315. if($(input).val() != _f.get_value(temp.doctype, temp.docname, temp.df.fieldname))
  316. $(input).trigger('change');
  317. }
  318. _f.Grid.prototype.remove_template = function(cell) {
  319. var hc = this.head_row.cells[cell.cellIndex];
  320. if(!hc.template)return;
  321. if(!hc.template.activated)return;
  322. if(hc.template && hc.template.wrapper.parentNode)
  323. cell.div.removeChild(hc.template.wrapper);
  324. this.set_cell_value(cell);
  325. hc.template.activated=0;
  326. }
  327. _f.Grid.prototype.notify_keypress = function(e, keycode) {
  328. if(keycode>=37 && keycode<=40 && e.shiftKey) {
  329. if(text_dialog && text_dialog.display) {
  330. return;
  331. }
  332. } else
  333. return;
  334. if(!_f.cur_grid_cell) return;
  335. if(_f.cur_grid_cell.grid != this) return;
  336. var ri = _f.cur_grid_cell.row.rowIndex;
  337. var ci = _f.cur_grid_cell.cellIndex;
  338. switch(keycode) {
  339. case 38: // up
  340. if (ri > 0) {
  341. this.cell_select('', ri - 1, ci);
  342. } break;
  343. case 40: // down
  344. if (ri < (this.tab.rows.length - 1)) {
  345. this.cell_select('', ri + 1, ci);
  346. } break;
  347. case 39: // right
  348. if (ci < (this.head_row.cells.length - 1)) {
  349. this.cell_select('', ri, ci + 1);
  350. } break;
  351. case 37: // left
  352. if (ci > 1) {
  353. this.cell_select('', ri, ci - 1);
  354. } break;
  355. }
  356. }
  357. _f.Grid.prototype.make_template = function(hc) {
  358. hc.template = make_field(wn.meta.get_docfield(hc.doctype, hc.fieldname), hc.doctype, '', this.field.frm, true);
  359. hc.template.grid = this;
  360. }
  361. _f.Grid.prototype.append_rows = function(n) {
  362. for(var i=0;i<n;i++) this.append_row();
  363. }
  364. _f.Grid.prototype.truncate_rows = function(n) {
  365. for(var i=0;i<n;i++) this.tab.deleteRow(this.tab.rows.length-1);
  366. }
  367. _f.Grid.prototype.set_data = function(data) {
  368. // deselect if not done yet
  369. this.cell_deselect();
  370. // set table widths
  371. this.reset_table_width();
  372. // append if reqd
  373. if(data.length > this.tab.rows.length)
  374. this.append_rows(data.length - this.tab.rows.length);
  375. // truncate if reqd
  376. if(data.length < this.tab.rows.length)
  377. this.truncate_rows(this.tab.rows.length - data.length);
  378. // set data
  379. for(var ridx=0;ridx<data.length;ridx++) {
  380. this.refresh_row(ridx, data[ridx]);
  381. }
  382. if(this.can_add_rows && this.make_newrow) {
  383. this.make_newrow();
  384. }
  385. if(this.wrapper.onscroll)this.wrapper.onscroll();
  386. }
  387. _f.Grid.prototype.set_ht = function() {
  388. var max_ht = cint(0.37 * screen.width);
  389. var ht = $(this.tab).height() + $(this.head_tab).height() + 30;
  390. if(ht < 100)
  391. ht=100;
  392. if(ht > max_ht) ht = max_ht;
  393. ht += 4;
  394. $y(this.wrapper,{height:ht+'px'});
  395. }
  396. _f.Grid.prototype.refresh_row = function(ridx, docname) {
  397. var row = this.tab.rows[ridx];
  398. row.docname = docname;
  399. row.is_newrow = false;
  400. for(var cidx=0; cidx<row.cells.length; cidx++) {
  401. this.set_cell_value(row.cells[cidx]);
  402. }
  403. }