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.

13 年之前
12 年之前
12 年之前
12 年之前
12 年之前
13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. $(cell.div).html(wn.format(v, hc, {for_print:true}));
  200. } else {
  201. // Index column
  202. cell.div.style.padding = '2px';
  203. cell.div.style.textAlign = 'left';
  204. cell.div.innerHTML = '';
  205. var t = make_table(cell.div,1,3,'60px',['20px','20px','20px'],{verticalAlign: 'middle', padding:'2px'});
  206. $y($td(t,0,0),{paddingLeft:'4px'});
  207. $td(t,0,0).innerHTML = cell.row.rowIndex + 1;
  208. $(cell.div).click(function() {
  209. if(me.can_edit) {
  210. me.cell_deselect();
  211. cell.div.style.border = '2px solid #88F';
  212. _f.cur_grid_cell = cell;
  213. }
  214. });
  215. if(this.can_edit) {
  216. $("<a title='Edit Row'><i class='icon-edit'></i></a>")
  217. .click(function() {
  218. _f.cur_grid = me;
  219. _f.cur_grid_ridx = cell.row.rowIndex;
  220. _f.edit_record(me.doctype, cell.row.docname, 1);
  221. })
  222. .appendTo($td(t,0,1))
  223. } else {
  224. cell.div.innerHTML = (cell.row.rowIndex + 1);
  225. cell.div.style.cursor = 'default';
  226. cell.div.onclick = function() { }
  227. }
  228. }
  229. }
  230. // if clicked on whitespace
  231. // and a grid cell is selected
  232. // deselect the cell
  233. $(document).bind('click', function(e) {
  234. var me = this;
  235. var is_target_toolbar = function() {
  236. return $(e.target).parents('.grid_tbarlinks').length;
  237. }
  238. var is_target_input = function() {
  239. // select opened
  240. if(e.target.tagName.toLowerCase()=='option') return true;
  241. // autosuggest openend
  242. //if(wn._autosugg_open) return true;
  243. return $(e.target).parents().get().indexOf(_f.cur_grid_cell)!=-1;
  244. }
  245. if(_f.cur_grid_cell && !is_target_input() && !is_target_toolbar()) {
  246. if(!(text_dialog && text_dialog.display)
  247. && !datepicker_active && !(selector && selector.display)) {
  248. setTimeout('_f.cur_grid_cell.grid.cell_deselect()', 500);
  249. return false;
  250. }
  251. }
  252. });
  253. _f.Grid.prototype.cell_deselect = function() {
  254. if(_f.cur_grid_cell) {
  255. var c = _f.cur_grid_cell;
  256. c.grid.remove_template(c);
  257. c.div.className = 'grid_cell_div';
  258. if(c.is_odd) c.div.style.border = '2px solid ' + c.grid.alt_row_bg;
  259. else c.div.style.border = '2px solid #FFF';
  260. _f.cur_grid_cell = null;
  261. }
  262. }
  263. _f.Grid.prototype.cell_select = function(cell, ri, ci) {
  264. if(cell && _f.cur_grid_cell==cell && cell.hc) return;
  265. if(ri!=null && ci!=null)
  266. cell = this.tab.rows[ri].cells[ci];
  267. var hc = this.head_row.cells[cell.cellIndex];
  268. if(!hc.doctype) return;
  269. if(!hc.template) {
  270. this.make_template(hc);
  271. }
  272. hc.template.perm = this.field ? this.field.perm : hc.perm; // get latest permissions
  273. if(hc.fieldname && hc.template.get_status()=='Write') {
  274. this.cell_deselect();
  275. cell.div.style.border = '2px solid #88F';
  276. _f.cur_grid_cell = cell;
  277. this.add_template(cell);
  278. }
  279. }
  280. _f.Grid.prototype.add_template = function(cell) {
  281. if(!cell.row.docname && this.add_newrow) { // activate new row here
  282. this.add_newrow();
  283. this.cell_select(cell);
  284. } else {
  285. var hc = this.head_row.cells[cell.cellIndex];
  286. cell.div.innerHTML = '';
  287. cell.div.appendChild(hc.template.wrapper);
  288. hc.template.activate(cell.row.docname);
  289. hc.template.activated=1;
  290. cell.hc = hc;
  291. if(hc.template.input && hc.template.input.set_width) {
  292. hc.template.input.set_width($(cell).width());
  293. }
  294. }
  295. }
  296. _f.Grid.prototype.get_field = function(fieldname) { // get template
  297. for(var i=0;i<this.head_row.cells.length;i++) {
  298. var hc = this.head_row.cells[i];
  299. if(hc.fieldname == fieldname) {
  300. if(!hc.template) {
  301. this.make_template(hc);
  302. }
  303. return hc.template;
  304. }
  305. }
  306. return {} // did not find, return empty object not to throw error in get_query
  307. }
  308. _f.grid_date_cell = '';
  309. _f.grid_refresh_date = function() {
  310. _f.grid_date_cell.grid.set_cell_value(_f.grid_date_cell);
  311. }
  312. _f.grid_refresh_field = function(temp, input) {
  313. if($(input).val() != _f.get_value(temp.doctype, temp.docname, temp.df.fieldname))
  314. $(input).trigger('change');
  315. }
  316. _f.Grid.prototype.remove_template = function(cell) {
  317. var hc = this.head_row.cells[cell.cellIndex];
  318. if(!hc.template)return;
  319. if(!hc.template.activated)return;
  320. if(hc.template && hc.template.wrapper.parentNode)
  321. cell.div.removeChild(hc.template.wrapper);
  322. this.set_cell_value(cell);
  323. hc.template.activated=0;
  324. }
  325. _f.Grid.prototype.notify_keypress = function(e, keycode) {
  326. if(keycode>=37 && keycode<=40 && e.shiftKey) {
  327. if(text_dialog && text_dialog.display) {
  328. return;
  329. }
  330. } else
  331. return;
  332. if(!_f.cur_grid_cell) return;
  333. if(_f.cur_grid_cell.grid != this) return;
  334. var ri = _f.cur_grid_cell.row.rowIndex;
  335. var ci = _f.cur_grid_cell.cellIndex;
  336. switch(keycode) {
  337. case 38: // up
  338. if (ri > 0) {
  339. this.cell_select('', ri - 1, ci);
  340. } break;
  341. case 40: // down
  342. if (ri < (this.tab.rows.length - 1)) {
  343. this.cell_select('', ri + 1, ci);
  344. } break;
  345. case 39: // right
  346. if (ci < (this.head_row.cells.length - 1)) {
  347. this.cell_select('', ri, ci + 1);
  348. } break;
  349. case 37: // left
  350. if (ci > 1) {
  351. this.cell_select('', ri, ci - 1);
  352. } break;
  353. }
  354. }
  355. _f.Grid.prototype.make_template = function(hc) {
  356. hc.template = make_field(wn.meta.get_docfield(hc.doctype, hc.fieldname), hc.doctype, '', this.field.frm, true);
  357. hc.template.grid = this;
  358. }
  359. _f.Grid.prototype.append_rows = function(n) {
  360. for(var i=0;i<n;i++) this.append_row();
  361. }
  362. _f.Grid.prototype.truncate_rows = function(n) {
  363. for(var i=0;i<n;i++) this.tab.deleteRow(this.tab.rows.length-1);
  364. }
  365. _f.Grid.prototype.set_data = function(data) {
  366. // deselect if not done yet
  367. this.cell_deselect();
  368. // set table widths
  369. this.reset_table_width();
  370. // append if reqd
  371. if(data.length > this.tab.rows.length)
  372. this.append_rows(data.length - this.tab.rows.length);
  373. // truncate if reqd
  374. if(data.length < this.tab.rows.length)
  375. this.truncate_rows(this.tab.rows.length - data.length);
  376. // set data
  377. for(var ridx=0;ridx<data.length;ridx++) {
  378. this.refresh_row(ridx, data[ridx]);
  379. }
  380. if(this.can_add_rows && this.make_newrow) {
  381. this.make_newrow();
  382. }
  383. if(this.wrapper.onscroll)this.wrapper.onscroll();
  384. }
  385. _f.Grid.prototype.set_ht = function() {
  386. var max_ht = cint(0.37 * screen.width);
  387. var ht = $(this.tab).height() + $(this.head_tab).height() + 30;
  388. if(ht < 100)
  389. ht=100;
  390. if(ht > max_ht) ht = max_ht;
  391. ht += 4;
  392. $y(this.wrapper,{height:ht+'px'});
  393. }
  394. _f.Grid.prototype.refresh_row = function(ridx, docname) {
  395. var row = this.tab.rows[ridx];
  396. row.docname = docname;
  397. row.is_newrow = false;
  398. for(var cidx=0; cidx<row.cells.length; cidx++) {
  399. this.set_cell_value(row.cells[cidx]);
  400. }
  401. }