Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

444 wiersze
11 KiB

  1. // _f.Grid
  2. _f.cur_grid_cell = null;
  3. _f.Grid = function(parent) { }
  4. _f.Grid.prototype.init = function(parent, row_height) {
  5. this.col_idx_by_name = {}
  6. this.alt_row_bg = '#F2F2FF';
  7. this.row_height = row_height;
  8. // make the grid
  9. if(!row_height)this.row_height = '26px';
  10. this.make_ui(parent);
  11. // Sr No
  12. this.insert_column('', '', 'Int', 'Sr', '50px', '', [1,0,0]);
  13. if(this.oninit)this.oninit();
  14. keypress_observers.push(this);
  15. }
  16. _f.Grid.prototype.make_ui = function(parent) {
  17. var ht = make_table($a(parent, 'div'), 1, 2, '100%', ['60%','40%']);
  18. this.main_title = $td(ht,0,0); this.main_title.className = 'columnHeading';
  19. $td(ht,0,1).style.textAlign = 'right';
  20. this.tbar_div = $a($td(ht,0,1), 'div', 'grid_tbarlinks');
  21. if(isIE) $y(this.tbar_div, {width:'200px'});
  22. this.tbar_tab = make_table(this.tbar_div,1,4,'100%',['25%','25%','25%','25%']);
  23. this.wrapper = $a(parent, 'div', 'grid_wrapper');
  24. $h(this.wrapper, cint(screen.width * 0.5) + 'px');
  25. this.head_wrapper = $a(this.wrapper, 'div', 'grid_head_wrapper');
  26. this.head_tab = $a(this.head_wrapper, 'table', 'grid_head_table');
  27. this.head_row = this.head_tab.insertRow(0);
  28. this.tab_wrapper = $a(this.wrapper, 'div', 'grid_tab_wrapper');
  29. this.tab = $a(this.tab_wrapper, 'table', 'grid_table');
  30. var me = this;
  31. this.wrapper.onscroll = function() { me.head_wrapper.style.top = me.wrapper.scrollTop+'px'; }
  32. }
  33. _f.Grid.prototype.show = function() {
  34. if(this.can_add_rows) {
  35. $ds(this.tbar_div);
  36. } else {
  37. $dh(this.tbar_div);
  38. }
  39. $ds(this.wrapper);
  40. }
  41. _f.Grid.prototype.hide = function() {
  42. $dh(this.wrapper); $dh(this.tbar_div);
  43. }
  44. _f.Grid.prototype.insert_column = function(doctype, fieldname, fieldtype, label, width, options, perm, reqd) {
  45. var idx = this.head_row.cells.length;
  46. if(!width)width = '140px';
  47. if((width+'').slice(-2)!='px') {
  48. width= width + 'px';
  49. }
  50. var col = this.head_row.insertCell(idx);
  51. col.doctype = doctype; // for report (fields may be from diff doctypes)
  52. col.fieldname = fieldname;
  53. col.fieldtype = fieldtype;
  54. col.innerHTML = '<div>'+label+'</div>';
  55. col.label = label;
  56. if(reqd)
  57. col.childNodes[0].style.color = "#D22";
  58. col.style.width = width;
  59. col.options = options;
  60. col.perm = perm;
  61. this.col_idx_by_name[fieldname] = idx;
  62. }
  63. _f.Grid.prototype.reset_table_width = function() {
  64. var w = 0;
  65. for(var i=0, len=this.head_row.cells.length; i<len; i++) {
  66. w += cint(this.head_row.cells[i].style.width);
  67. }
  68. this.head_tab.style.width = w + 'px';
  69. this.tab.style.width = w + 'px';
  70. }
  71. _f.Grid.prototype.set_column_disp = function(fieldname, show) {
  72. var cidx = this.col_idx_by_name[fieldname];
  73. if(!cidx) {
  74. msgprint('Trying to hide unknown column: ' + fieldname);
  75. return;
  76. }
  77. var disp = show ? 'table-cell' : 'none';
  78. // head
  79. this.head_row.cells[cidx].style.display = disp;
  80. // body
  81. for(var i=0, len=this.tab.rows.length; i<len; i++) {
  82. var cell = this.tab.rows[i].cells[cidx];
  83. cell.style.display = disp;
  84. }
  85. // reset table width
  86. this.reset_table_width();
  87. }
  88. _f.Grid.prototype.append_row = function(idx, docname) {
  89. if(!idx)idx = this.tab.rows.length;
  90. var row = this.tab.insertRow(idx);
  91. row.docname = docname;
  92. if(idx % 2)var odd=true; else var odd=false;
  93. var me = this;
  94. // make cells
  95. for(var i=0; i<this.head_row.cells.length; i++){
  96. var cell = row.insertCell(i);
  97. var hc = this.head_row.cells[i];
  98. // ape style of head
  99. cell.style.width = hc.style.width;
  100. cell.style.display = hc.style.display;
  101. cell.row = row;
  102. cell.grid = this;
  103. cell.className = 'grid_cell';
  104. cell.div = $a(cell, 'div', 'grid_cell_div');
  105. if(this.row_height) {
  106. cell.div.style.height = this.row_height; }
  107. cell.div.cell = cell;
  108. cell.div.onclick = function(e) { me.cell_click(this.cell, e); }
  109. if(odd) {
  110. $bg(cell, this.alt_row_bg); cell.is_odd = 1;
  111. cell.div.style.border = '2px solid ' + this.alt_row_bg;
  112. } else $bg(cell,'#FFF');
  113. if(!hc.fieldname) cell.div.style.cursor = 'default'; // Index
  114. }
  115. this.set_ht();
  116. return row;
  117. }
  118. _f.Grid.prototype.refresh_cell = function(docname, fieldname) {
  119. for(var r=0;r<this.tab.rows.length;r++) {
  120. if(this.tab.rows[r].docname==docname) {
  121. for(var c=0;c<this.head_row.cells.length;c++) {
  122. var hc = this.head_row.cells[c];
  123. if(hc.fieldname==fieldname) {
  124. this.set_cell_value(this.tab.rows[r].cells[c]);
  125. }
  126. }
  127. }
  128. }
  129. }
  130. // for form edit
  131. _f.cur_grid;
  132. _f.cur_grid_ridx;
  133. _f.Grid.prototype.set_cell_value = function(cell) {
  134. // if newrow
  135. if(cell.row.is_newrow)return;
  136. // show static
  137. var hc = this.head_row.cells[cell.cellIndex];
  138. if(hc.fieldname) {
  139. var v = locals[hc.doctype][cell.row.docname][hc.fieldname];
  140. } else {
  141. var v = (cell.row.rowIndex + 1); // Index
  142. }
  143. if(v==null){ v=''; }
  144. var me = this;
  145. // variations
  146. if(cell.cellIndex) {
  147. var ft = hc.fieldtype;
  148. if(ft=='Link' && cur_frm.doc.docstatus < 1) ft='Data';
  149. $s(cell.div, v, ft, hc.options);
  150. } else {
  151. // Index column
  152. cell.div.style.padding = '2px';
  153. cell.div.style.textAlign = 'left';
  154. cell.innerHTML = '';
  155. var t = make_table(cell,1,3,'60px',['20px','20px','20px'],{verticalAlign: 'middle', padding:'2px'});
  156. $y($td(t,0,0),{paddingLeft:'4px'});
  157. $td(t,0,0).innerHTML = cell.row.rowIndex + 1;
  158. if(cur_frm.editable && this.can_edit) {
  159. var ed = $a($td(t,0,1),'div','wn-icon ic-doc_edit',{cursor:'pointer'}); ed.cell = cell; ed.title = 'Edit Row';
  160. ed.onclick = function() {
  161. _f.cur_grid = me;
  162. _f.cur_grid_ridx = this.cell.row.rowIndex;
  163. _f.edit_record(me.doctype, this.cell.row.docname, 1);
  164. }
  165. } else {
  166. cell.div.innerHTML = (cell.row.rowIndex + 1);
  167. cell.div.style.cursor = 'default';
  168. cell.div.onclick = function() { }
  169. }
  170. }
  171. }
  172. _f.Grid.prototype.cell_click = function(cell, e) {
  173. if(_f.cur_grid_cell==cell)
  174. return; // on existing cell
  175. this.cell_select(cell);
  176. if(cur_frm.editable) {
  177. if(isIE) {
  178. window.event.cancelBubble = true;
  179. window.event.returnValue = false;
  180. } else {
  181. e.preventDefault();
  182. }
  183. }
  184. }
  185. _f.Grid.prototype.notify_click = function(e, target) {
  186. if(_f.cur_grid_cell && !target.isactive) {
  187. if(!(text_dialog && text_dialog.display) && !datepicker_active && !(selector && selector.display) && !(cur_autosug)) {
  188. _f.cur_grid_cell.grid.cell_deselect();
  189. }
  190. }
  191. }
  192. _f.Grid.prototype.cell_deselect = function() {
  193. if(_f.cur_grid_cell) {
  194. var c = _f.cur_grid_cell;
  195. c.grid.remove_template(c);
  196. c.div.className = 'grid_cell_div';
  197. if(c.is_odd) c.div.style.border = '2px solid ' + c.grid.alt_row_bg;
  198. else c.div.style.border = '2px solid #FFF';
  199. _f.cur_grid_cell = null;
  200. _f.cur_grid = null;
  201. this.isactive = false;
  202. // remove from observer
  203. delete click_observers[this.observer_id];
  204. }
  205. }
  206. _f.Grid.prototype.cell_select = function(cell, ri, ci) {
  207. if(ri!=null && ci!=null)
  208. cell = this.tab.rows[ri].cells[ci];
  209. var hc = this.head_row.cells[cell.cellIndex];
  210. if(!hc.template) {
  211. this.make_template(hc);
  212. }
  213. hc.template.perm = this.field ? this.field.perm : hc.perm; // get latest permissions
  214. if(hc.fieldname && hc.template.get_status()=='Write') {
  215. this.cell_deselect();
  216. cell.div.style.border = '2px solid #88F';
  217. _f.cur_grid_cell = cell;
  218. this.add_template(cell);
  219. this.isactive = true;
  220. // start observing clicks
  221. click_observers.push(this);
  222. this.observer_id = click_observers.length - 1;
  223. }
  224. }
  225. _f.Grid.prototype.add_template = function(cell) {
  226. if(!cell.row.docname && this.add_newrow) { // activate new row here
  227. this.add_newrow();
  228. this.cell_select(cell);
  229. } else {
  230. var hc = this.head_row.cells[cell.cellIndex];
  231. cell.div.innerHTML = '';
  232. cell.div.appendChild(hc.template.wrapper);
  233. hc.template.activate(cell.row.docname);
  234. hc.template.activated=1;
  235. if(hc.template.input && hc.template.input.set_width) {
  236. hc.template.input.set_width(isIE ? cell.offsetWidth : cell.clientWidth);
  237. }
  238. }
  239. }
  240. _f.Grid.prototype.get_field = function(fieldname) { // get template
  241. for(var i=0;i<this.head_row.cells.length;i++) {
  242. var hc = this.head_row.cells[i];
  243. if(hc.fieldname == fieldname) {
  244. if(!hc.template) {
  245. this.make_template(hc);
  246. }
  247. return hc.template;
  248. }
  249. }
  250. return {} // did not find, return empty object not to throw error in get_query
  251. }
  252. _f.grid_date_cell = '';
  253. _f.grid_refresh_date = function() {
  254. _f.grid_date_cell.grid.set_cell_value(_f.grid_date_cell);
  255. }
  256. _f.grid_refresh_field = function(temp, input) {
  257. if(input.value != _f.get_value(temp.doctype, temp.docname, temp.df.fieldname))
  258. if(input.onchange)input.onchange();
  259. }
  260. _f.Grid.prototype.remove_template = function(cell) {
  261. var hc = this.head_row.cells[cell.cellIndex];
  262. if(!hc.template)return;
  263. if(!hc.template.activated)return;
  264. if(hc.template.txt) {
  265. if(hc.template.df.fieldtype=='Date') {
  266. // for calendar popup. the value will come after this
  267. _f.grid_date_cell = cell;
  268. setTimeout('_f.grid_refresh_date()', 100);
  269. }
  270. if(hc.template.txt.value)
  271. _f.grid_refresh_field(hc.template, hc.template.txt);
  272. } else if(hc.template.input) {
  273. _f.grid_refresh_field(hc.template, hc.template.input);
  274. }
  275. if(hc.template && hc.template.wrapper.parentNode)
  276. cell.div.removeChild(hc.template.wrapper);
  277. this.set_cell_value(cell);
  278. hc.template.activated=0;
  279. if(isIE6) {
  280. $dh(this.wrapper); $ds(this.wrapper);
  281. }
  282. }
  283. _f.Grid.prototype.notify_keypress = function(e, keycode) {
  284. if(keycode>=37 && keycode<=40 && e.shiftKey) {
  285. if(text_dialog && text_dialog.display) {
  286. return;
  287. }
  288. } else
  289. return;
  290. if(!_f.cur_grid_cell) return;
  291. if(_f.cur_grid_cell.grid != this) return;
  292. var ri = _f.cur_grid_cell.row.rowIndex;
  293. var ci = _f.cur_grid_cell.cellIndex;
  294. switch(keycode) {
  295. case 38: // up
  296. if (ri > 0) {
  297. this.cell_select('', ri - 1, ci);
  298. } break;
  299. case 40: // down
  300. if (ri < (this.tab.rows.length - 1)) {
  301. this.cell_select('', ri + 1, ci);
  302. } break;
  303. case 39: // right
  304. if (ci < (this.head_row.cells.length - 1)) {
  305. this.cell_select('', ri, ci + 1);
  306. } break;
  307. case 37: // left
  308. if (ci > 1) {
  309. this.cell_select('', ri, ci - 1);
  310. } break;
  311. }
  312. }
  313. _f.Grid.prototype.make_template = function(hc) {
  314. hc.template = make_field(get_field(hc.doctype, hc.fieldname), hc.doctype, '', this.field.frm, true);
  315. hc.template.grid = this;
  316. }
  317. _f.Grid.prototype.append_rows = function(n) { for(var i=0;i<n;i++) this.append_row(); }
  318. _f.Grid.prototype.truncate_rows = function(n) { for(var i=0;i<n;i++) this.tab.deleteRow(this.tab.rows.length-1); }
  319. _f.Grid.prototype.set_data = function(data) {
  320. // deselect if not done yet
  321. this.cell_deselect();
  322. // set table widths
  323. this.reset_table_width();
  324. // append if reqd
  325. if(data.length > this.tab.rows.length)
  326. this.append_rows(data.length - this.tab.rows.length);
  327. // truncate if reqd
  328. if(data.length < this.tab.rows.length)
  329. this.truncate_rows(this.tab.rows.length - data.length);
  330. // set data
  331. for(var ridx=0;ridx<data.length;ridx++) {
  332. this.refresh_row(ridx, data[ridx]);
  333. }
  334. if(this.can_add_rows && this.make_newrow) {
  335. this.make_newrow();
  336. }
  337. this.set_ht();
  338. if(this.wrapper.onscroll)this.wrapper.onscroll();
  339. }
  340. _f.Grid.prototype.set_ht = function(ridx, docname) {
  341. var ht = ((cint(this.row_height) + 10) * (((this.tab && this.tab.rows) ? this.tab.rows.length : 0) + 1));
  342. if(ht < 100)ht=100;
  343. if(ht > cint(0.3 * screen.width))ht=cint(0.3 * screen.width);
  344. ht += 4;
  345. $y(this.wrapper,{height:ht+'px'});
  346. }
  347. _f.Grid.prototype.refresh_row = function(ridx, docname) {
  348. var row = this.tab.rows[ridx];
  349. row.docname = docname;
  350. row.is_newrow = false;
  351. for(var cidx=0; cidx<row.cells.length; cidx++) {
  352. this.set_cell_value(row.cells[cidx]);
  353. }
  354. }