Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

477 рядки
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%', ['60%','40%']);
  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');
  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((width+'').slice(-2)!='px') {
  82. width= width + 'px';
  83. }
  84. var col = this.head_row.insertCell(idx);
  85. col.doctype = doctype; // for report (fields may be from diff doctypes)
  86. col.fieldname = fieldname;
  87. col.fieldtype = fieldtype;
  88. col.innerHTML = '<div data-grid-fieldname = "'+doctype+'-'+fieldname+'">'+label+'</div>';
  89. col.label = label;
  90. if(reqd)
  91. col.childNodes[0].style.color = "#D22";
  92. col.style.width = width;
  93. col.options = options;
  94. col.perm = perm;
  95. this.col_idx_by_name[fieldname] = idx;
  96. }
  97. _f.Grid.prototype.reset_table_width = function() {
  98. var w = 0;
  99. $.each(this.head_row.cells, function(i, cell) {
  100. if((cell.style.display || '').toLowerCase()!='none')
  101. w += cint(cell.style.width);
  102. })
  103. this.head_tab.style.width = w + 'px';
  104. this.tab.style.width = w + 'px';
  105. }
  106. _f.Grid.prototype.set_column_disp = function(fieldname, show) {
  107. var cidx = this.col_idx_by_name[fieldname];
  108. if(!cidx) {
  109. msgprint('Trying to hide unknown column: ' + fieldname);
  110. return;
  111. }
  112. var disp = show ? 'table-cell' : 'none';
  113. // head
  114. this.head_row.cells[cidx].style.display = disp;
  115. // body
  116. for(var i=0, len=this.tab.rows.length; i<len; i++) {
  117. var cell = this.tab.rows[i].cells[cidx];
  118. cell.style.display = disp;
  119. }
  120. // reset table width
  121. this.reset_table_width();
  122. }
  123. _f.Grid.prototype.append_row = function(idx, docname) {
  124. if(!idx)idx = this.tab.rows.length;
  125. var row = this.tab.insertRow(idx);
  126. row.docname = docname;
  127. if(idx % 2)var odd=true; else var odd=false;
  128. var me = this;
  129. // make cells
  130. for(var i=0; i<this.head_row.cells.length; i++){
  131. var cell = row.insertCell(i);
  132. var hc = this.head_row.cells[i];
  133. // ape style of head
  134. cell.style.width = hc.style.width;
  135. cell.style.display = hc.style.display;
  136. cell.row = row;
  137. cell.grid = this;
  138. cell.className = 'grid_cell';
  139. cell.div = $a(cell, 'div', 'grid_cell_div');
  140. if(this.row_height) {
  141. cell.div.style.height = this.row_height; }
  142. cell.div.cell = cell;
  143. cell.div.onclick = function(e) { me.cell_select(this.cell); }
  144. if(odd) {
  145. $bg(cell, this.alt_row_bg); cell.is_odd = 1;
  146. cell.div.style.border = '2px solid ' + this.alt_row_bg;
  147. } else $bg(cell,'#FFF');
  148. if(!hc.fieldname) cell.div.style.cursor = 'default'; // Index
  149. }
  150. this.set_ht();
  151. return row;
  152. }
  153. _f.Grid.prototype.refresh_cell = function(docname, fieldname) {
  154. for(var r=0;r<this.tab.rows.length;r++) {
  155. if(this.tab.rows[r].docname==docname) {
  156. for(var c=0;c<this.head_row.cells.length;c++) {
  157. var hc = this.head_row.cells[c];
  158. if(hc.fieldname==fieldname) {
  159. this.set_cell_value(this.tab.rows[r].cells[c]);
  160. }
  161. }
  162. }
  163. }
  164. }
  165. // for form edit
  166. _f.cur_grid;
  167. _f.cur_grid_ridx;
  168. _f.Grid.prototype.set_cell_value = function(cell) {
  169. // if newrow
  170. if(cell.row.is_newrow)return;
  171. // show static
  172. var hc = this.head_row.cells[cell.cellIndex];
  173. if(hc.fieldname && locals[hc.doctype][cell.row.docname]) {
  174. var v = locals[hc.doctype][cell.row.docname][hc.fieldname];
  175. } else {
  176. var v = (cell.row.rowIndex + 1); // Index
  177. }
  178. if(v==null){ v=''; }
  179. var me = this;
  180. // variations
  181. if(cell.cellIndex) {
  182. var ft = hc.fieldtype;
  183. if(ft=='Link' && cur_frm.doc.docstatus < 1) ft='Data';
  184. $s(cell.div, v, ft, hc.options);
  185. } else {
  186. // Index column
  187. cell.div.style.padding = '2px';
  188. cell.div.style.textAlign = 'left';
  189. cell.innerHTML = '';
  190. var t = make_table(cell,1,3,'60px',['20px','20px','20px'],{verticalAlign: 'middle', padding:'2px'});
  191. $y($td(t,0,0),{paddingLeft:'4px'});
  192. $td(t,0,0).innerHTML = cell.row.rowIndex + 1;
  193. if(cur_frm.editable && this.can_edit) {
  194. var ed = $a($td(t,0,1),'i','icon-edit',{cursor:'pointer'}); ed.cell = cell; ed.title = 'Edit Row';
  195. ed.onclick = function() {
  196. _f.cur_grid = me;
  197. _f.cur_grid_ridx = this.cell.row.rowIndex;
  198. _f.edit_record(me.doctype, this.cell.row.docname, 1);
  199. }
  200. } else {
  201. cell.div.innerHTML = (cell.row.rowIndex + 1);
  202. cell.div.style.cursor = 'default';
  203. cell.div.onclick = function() { }
  204. }
  205. }
  206. }
  207. // if clicked on whitespace
  208. // and a grid cell is selected
  209. // deselect the cell
  210. $(document).bind('click', function(e) {
  211. var me = this;
  212. var is_target_toolbar = function() {
  213. return $(e.target).parents('.grid_tbarlinks').length;
  214. }
  215. var is_target_input = function() {
  216. // select opened
  217. if(e.target.tagName.toLowerCase()=='option') return true;
  218. // autosuggest openend
  219. //if(wn._autosugg_open) return true;
  220. return $(e.target).parents().get().indexOf(_f.cur_grid_cell)!=-1;
  221. }
  222. if(_f.cur_grid_cell && !is_target_input() && !is_target_toolbar()) {
  223. if(!(text_dialog && text_dialog.display)
  224. && !datepicker_active && !(selector && selector.display)) {
  225. setTimeout('_f.cur_grid_cell.grid.cell_deselect()', 500);
  226. return false;
  227. }
  228. }
  229. });
  230. _f.Grid.prototype.cell_deselect = function() {
  231. if(_f.cur_grid_cell) {
  232. var c = _f.cur_grid_cell;
  233. c.grid.remove_template(c);
  234. c.div.className = 'grid_cell_div';
  235. if(c.is_odd) c.div.style.border = '2px solid ' + c.grid.alt_row_bg;
  236. else c.div.style.border = '2px solid #FFF';
  237. _f.cur_grid_cell = null;
  238. }
  239. }
  240. _f.Grid.prototype.cell_select = function(cell, ri, ci) {
  241. if(_f.cur_grid_cell==cell && cell.hc) return;
  242. if(ri!=null && ci!=null)
  243. cell = this.tab.rows[ri].cells[ci];
  244. var hc = this.head_row.cells[cell.cellIndex];
  245. if(!hc.template) {
  246. this.make_template(hc);
  247. }
  248. hc.template.perm = this.field ? this.field.perm : hc.perm; // get latest permissions
  249. if(hc.fieldname && hc.template.get_status()=='Write') {
  250. this.cell_deselect();
  251. cell.div.style.border = '2px solid #88F';
  252. _f.cur_grid_cell = cell;
  253. this.add_template(cell);
  254. }
  255. }
  256. _f.Grid.prototype.add_template = function(cell) {
  257. if(!cell.row.docname && this.add_newrow) { // activate new row here
  258. this.add_newrow();
  259. this.cell_select(cell);
  260. } else {
  261. var hc = this.head_row.cells[cell.cellIndex];
  262. cell.div.innerHTML = '';
  263. cell.div.appendChild(hc.template.wrapper);
  264. hc.template.activate(cell.row.docname);
  265. hc.template.activated=1;
  266. cell.hc = hc;
  267. if(hc.template.input && hc.template.input.set_width) {
  268. hc.template.input.set_width($(cell).width());
  269. }
  270. }
  271. }
  272. _f.Grid.prototype.get_field = function(fieldname) { // get template
  273. for(var i=0;i<this.head_row.cells.length;i++) {
  274. var hc = this.head_row.cells[i];
  275. if(hc.fieldname == fieldname) {
  276. if(!hc.template) {
  277. this.make_template(hc);
  278. }
  279. return hc.template;
  280. }
  281. }
  282. return {} // did not find, return empty object not to throw error in get_query
  283. }
  284. _f.grid_date_cell = '';
  285. _f.grid_refresh_date = function() {
  286. _f.grid_date_cell.grid.set_cell_value(_f.grid_date_cell);
  287. }
  288. _f.grid_refresh_field = function(temp, input) {
  289. if($(input).val() != _f.get_value(temp.doctype, temp.docname, temp.df.fieldname))
  290. $(input).trigger('change');
  291. }
  292. _f.Grid.prototype.remove_template = function(cell) {
  293. var hc = this.head_row.cells[cell.cellIndex];
  294. if(!hc.template)return;
  295. if(!hc.template.activated)return;
  296. /*if(hc.template.df.fieldtype=='Date') {
  297. // for calendar popup. the value will come after this
  298. _f.grid_date_cell = cell;
  299. setTimeout('_f.grid_refresh_date()', 100);
  300. } else {
  301. var input = hc.template.txt || hc.template.input;
  302. _f.grid_refresh_field(hc.template, input)
  303. }*/
  304. if(hc.template && hc.template.wrapper.parentNode)
  305. cell.div.removeChild(hc.template.wrapper);
  306. this.set_cell_value(cell);
  307. hc.template.activated=0;
  308. }
  309. _f.Grid.prototype.notify_keypress = function(e, keycode) {
  310. if(keycode>=37 && keycode<=40 && e.shiftKey) {
  311. if(text_dialog && text_dialog.display) {
  312. return;
  313. }
  314. } else
  315. return;
  316. if(!_f.cur_grid_cell) return;
  317. if(_f.cur_grid_cell.grid != this) return;
  318. var ri = _f.cur_grid_cell.row.rowIndex;
  319. var ci = _f.cur_grid_cell.cellIndex;
  320. switch(keycode) {
  321. case 38: // up
  322. if (ri > 0) {
  323. this.cell_select('', ri - 1, ci);
  324. } break;
  325. case 40: // down
  326. if (ri < (this.tab.rows.length - 1)) {
  327. this.cell_select('', ri + 1, ci);
  328. } break;
  329. case 39: // right
  330. if (ci < (this.head_row.cells.length - 1)) {
  331. this.cell_select('', ri, ci + 1);
  332. } break;
  333. case 37: // left
  334. if (ci > 1) {
  335. this.cell_select('', ri, ci - 1);
  336. } break;
  337. }
  338. }
  339. _f.Grid.prototype.make_template = function(hc) {
  340. hc.template = make_field(get_field(hc.doctype, hc.fieldname), hc.doctype, '', this.field.frm, true);
  341. hc.template.grid = this;
  342. }
  343. _f.Grid.prototype.append_rows = function(n) {
  344. for(var i=0;i<n;i++) this.append_row();
  345. }
  346. _f.Grid.prototype.truncate_rows = function(n) {
  347. for(var i=0;i<n;i++) this.tab.deleteRow(this.tab.rows.length-1);
  348. }
  349. _f.Grid.prototype.set_data = function(data) {
  350. // deselect if not done yet
  351. this.cell_deselect();
  352. // set table widths
  353. this.reset_table_width();
  354. // append if reqd
  355. if(data.length > this.tab.rows.length)
  356. this.append_rows(data.length - this.tab.rows.length);
  357. // truncate if reqd
  358. if(data.length < this.tab.rows.length)
  359. this.truncate_rows(this.tab.rows.length - data.length);
  360. // set data
  361. for(var ridx=0;ridx<data.length;ridx++) {
  362. this.refresh_row(ridx, data[ridx]);
  363. }
  364. if(this.can_add_rows && this.make_newrow) {
  365. this.make_newrow();
  366. }
  367. if(this.wrapper.onscroll)this.wrapper.onscroll();
  368. }
  369. _f.Grid.prototype.set_ht = function() {
  370. var max_ht = cint(0.37 * screen.width);
  371. var ht = $(this.tab).height() + $(this.head_tab).height() + 30;
  372. if(ht < 100)
  373. ht=100;
  374. if(ht > max_ht) ht = max_ht;
  375. ht += 4;
  376. $y(this.wrapper,{height:ht+'px'});
  377. }
  378. _f.Grid.prototype.refresh_row = function(ridx, docname) {
  379. var row = this.tab.rows[ridx];
  380. row.docname = docname;
  381. row.is_newrow = false;
  382. for(var cidx=0; cidx<row.cells.length; cidx++) {
  383. this.set_cell_value(row.cells[cidx]);
  384. }
  385. }