Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

708 строки
18 KiB

  1. // Listing
  2. // this listing object will soon be
  3. // deprecated because it has a very non standard way of creation
  4. // the new listing object is at wn.widgets.Listing
  5. // -----------------------
  6. list_opts = {
  7. cell_style : {padding:'3px 2px'},
  8. alt_cell_style : {},
  9. head_style : {height:'20px',overflow:'hidden',verticalAlign:'middle',fontWeight:'bold',padding:'1px',fontSize:'13px'},
  10. head_main_style : {padding:'0px'},
  11. hide_export : 1,
  12. hide_print : 1,
  13. hide_refresh : 0,
  14. hide_rec_label: 0,
  15. show_calc: 1,
  16. show_empty_tab : 0,
  17. no_border: 1,
  18. append_records: 1,
  19. table_width: null
  20. };
  21. // -------------------------------------------------------
  22. function Listing(head_text, no_index, no_loading) {
  23. this.start = 0;
  24. this.page_len = 20;
  25. this.filters_per_line = 7;
  26. this.cell_idx = 0;
  27. this.head_text = head_text ? head_text : 'Result';
  28. this.keyword = 'records';
  29. this.no_index = no_index;
  30. this.underline = 1;
  31. this.no_rec_message = 'No Result';
  32. // interfaces
  33. // show_cell(cell, cell_id, data) - override cell display
  34. // show_result()
  35. // server_call(srs, call_back) - override query function
  36. this.show_cell = null;
  37. this.show_result = null;
  38. this.colnames = null; // sr num is required
  39. this.colwidths = null;
  40. this.coltypes = null;
  41. this.coloptions = null;
  42. this.filters = {};
  43. this.sort_list = {};
  44. this.sort_order_dict = {};
  45. this.sort_heads = {};
  46. this.is_std_query = false;
  47. this.server_call = null;
  48. this.no_loading = no_loading;
  49. this.opts = copy_dict(list_opts);
  50. }
  51. // -------------------------------------------------------
  52. Listing.prototype.make = function(parent) {
  53. var me = this;
  54. this.wrapper = parent;
  55. // filter
  56. this.filter_wrapper = $a(parent, 'div', 'srs_filter_wrapper');
  57. this.filter_area = $a(this.filter_wrapper, 'div', 'srs_filter_area');
  58. $dh(this.filter_wrapper);
  59. this.btn_area = $a(parent, 'div', '', {margin:'8px 0px'});
  60. this.body_area = $a(parent,'div','srs_body_area');
  61. if(!this.opts.hide_rec_label)
  62. this.rec_label = $a(this.body_area, 'div', '', {margin:'4px 0px',color:'#888'});
  63. // results
  64. this.results = $a($a(this.body_area, 'div','srs_results_area'),'div');
  65. this.fetching_area = $a(this.body_area, 'div','',{height:'120px', background:'url("images/ui/square_loading.gif") center no-repeat', display:'none'});
  66. this.show_no_records = $a(this.body_area,'div','',{margin:'200px 0px', textAlign:'center', fontSize:'14px', color:'#888', display:'none'});
  67. this.show_no_records.innerHTML = 'No Result';
  68. // empty table (old style)
  69. if(this.opts.show_empty_tab)
  70. this.make_result_tab();
  71. this.bottom_div = $a(this.body_area,'div','',{paddingTop:'8px'});
  72. this.make_toolbar();
  73. }
  74. // -------------------------------------------------------
  75. Listing.prototype.make_toolbar = function() {
  76. var me = this;
  77. this.buttons = {};
  78. // buttons
  79. var make_btn = function(label,icon,onclick,bold) {
  80. var btn = $btn(me.btn_area,label,onclick,{marginRight:'4px'});
  81. if(bold)$y(btn,{fontWeight: 'bold'});
  82. me.buttons[label] = btn;
  83. }
  84. // refresh btn
  85. if(!this.opts.hide_refresh) {
  86. make_btn('Refresh','ui-icon-refresh',function(btn) {
  87. me.start = 0;
  88. me.run();
  89. },1);
  90. }
  91. // new
  92. if(this.opts.show_new) {
  93. make_btn('New ','ui-icon-document',function() { new_doc(me.dt); },1);
  94. }
  95. // report
  96. if(this.opts.show_report) {
  97. make_btn('Report Builder','ui-icon-clipboard',function() { loadreport(me.dt, null, null, null, 1); },0);
  98. }
  99. // export
  100. if(!this.opts.hide_export) {
  101. make_btn('Export','ui-icon-circle-arrow-e',function() {me.do_export();});
  102. }
  103. // print
  104. if(!this.opts.hide_print) {
  105. make_btn('Print','ui-icon-print',function() {me.do_print();});
  106. }
  107. // calc
  108. if(this.opts.show_calc) {
  109. make_btn('Calc','ui-icon-calculator',function() {me.do_calc();});
  110. $dh(me.buttons['Calc'])
  111. }
  112. this.loading_img = $a(this.btn_area,'img','',{display:'none',marginBottom:'-2px'});
  113. this.loading_img.src = 'images/ui/button-load.gif';
  114. if(!keys(this.buttons).length)
  115. $dh(this.btn_area);
  116. }
  117. // -------------------------------------------------------
  118. Listing.prototype.do_print = function() {
  119. this.build_query();
  120. if(!this.query) { alert('No Query!'); return; }
  121. args = {
  122. query:this.query,
  123. title:this.head_text,
  124. colnames:this.colnames,
  125. colwidths:this.colwidths,
  126. coltypes:this.coltypes,
  127. has_index:(this.no_index ? 0 : 1),
  128. has_headings: 1,
  129. check_limit:1,
  130. is_simple:1
  131. }
  132. new_widget('_p.PrintQuery', function(w) {
  133. // global
  134. if(!_p.print_query)
  135. _p.print_query = w;
  136. _p.print_query.show_dialog(args);
  137. }, 1);
  138. }
  139. // -------------------------------------------------------
  140. Listing.prototype.do_calc = function() {
  141. show_calc(this.result_tab, this.colnames, this.coltypes, 0)
  142. }
  143. // -------------------------------------------------------
  144. Listing.prototype.add_filter = function(label, ftype, options, tname, fname, cond) {
  145. if(!this.filter_area){alert('[Listing] make() must be called before add_filter');}
  146. var me = this;
  147. // create filter area
  148. if(!this.filter_set) {
  149. // actual area
  150. var h = $a(this.filter_area, 'div', '', {fontSize:'14px', fontWeight:'bold', marginBottom:'4px'});
  151. h.innerHTML = 'Filter your search';
  152. this.filter_area.div = $a(this.filter_area, 'div');
  153. this.perm = [[1,1],]
  154. this.filters = {};
  155. }
  156. $ds(this.filter_wrapper);
  157. // create new table (or new line)
  158. if((!this.inp_tab) || (this.cell_idx==this.filters_per_line)) {
  159. this.inp_tab = $a(this.filter_area.div, 'table','',{width:'100%', tableLayout:'fixed'});
  160. this.inp_tab.insertRow(0);
  161. for(var i=0;i<this.filters_per_line;i++) {
  162. this.inp_tab.rows[0].insertCell(i);
  163. }
  164. this.cell_idx = 0;
  165. }
  166. var c= this.inp_tab.rows[0].cells[this.cell_idx];
  167. this.cell_idx++;
  168. $y(c,{width:cint(100/this.filters_per_line) + '%',textAlign:'left',verticalAlign:'top'});
  169. // filter label
  170. var d1= $a(c,'div','',{fontSize:'11px', marginBottom:'2px'}); d1.innerHTML = label;
  171. if(ftype=='Link') d1.innerHTML += ' <img src="images/icons/link.png" style="margin-bottom:-5px" title="Link">';
  172. var d2= $a(c,'div');
  173. // create the filter
  174. if(in_list(['Text', 'Small Text', 'Code', 'Text Editor','Read Only'],ftype))
  175. ftype='Data';
  176. // add empty option in select (if missing)
  177. if(ftype=='Select' && !in_list(options.split('\n'), '')) options = '\n'+options
  178. var inp = make_field({fieldtype:ftype, 'label':label, 'options':options, no_buttons:1}, '', d2, this, 0, 1);
  179. inp.not_in_form = 1;
  180. inp.report = this;
  181. // filter style
  182. inp.df.single_select = 1;
  183. inp.parent_cell = c;
  184. inp.parent_tab = this.input_tab;
  185. $y(inp.wrapper,{width:'95%'});
  186. inp.refresh();
  187. //if(!inp.input.custom_select && !inp.input.txt)
  188. // $y(inp.input,{width:'100%'});
  189. inp.tn = tname; inp.fn = fname; inp.condition = cond;
  190. var me = this;
  191. inp.onchange = function() { me.start = 0; }
  192. this.filters[label] = inp;
  193. this.filter_set = 1;
  194. }
  195. // -------------------------------------------------------
  196. Listing.prototype.remove_filter = function(label) {
  197. var inp = this.filters[label];
  198. inp.parent_tab.rows[0].deleteCell(inp.parent_cell.cellIndex);
  199. delete this.filters[label];
  200. }
  201. // -------------------------------------------------------
  202. Listing.prototype.remove_all_filters = function() {
  203. for(var k in this.filters) this.remove_filter(k);
  204. $dh(this.filter_wrapper);
  205. }
  206. Listing.prototype.add_sort = function(ci, fname) { this.sort_list[ci]=fname; }
  207. Listing.prototype.has_data = function() { return this.n_records; }
  208. // -------------------------------------------------------
  209. Listing.prototype.set_default_sort = function(fname, sort_order) {
  210. this.sort_order = sort_order;
  211. this.sort_order_dict[fname] = sort_order;
  212. this.sort_by = fname;
  213. if(this.sort_heads[fname])
  214. this.sort_heads[fname].set_sorting_as(sort_order);
  215. }
  216. // -------------------------------------------------------
  217. Listing.prototype.set_sort = function(cell, ci, fname) {
  218. var me = this;
  219. $y(cell.sort_cell,{width:'18px'});
  220. cell.sort_img = $a(cell.sort_cell, 'img');
  221. cell.fname = fname;
  222. $dh(cell.sort_img);
  223. cell.set_sort_img = function(order) {
  224. var t = 'images/icons/sort_desc.gif';
  225. if(order=='ASC') {
  226. t = 'images/icons/sort_asc.gif';
  227. }
  228. this.sort_img.src = t;
  229. }
  230. cell.set_sorting_as = function(order) {
  231. // set values for query building
  232. me.sort_order = order;
  233. me.sort_by = this.fname
  234. me.sort_order_dict[this.fname] = order;
  235. // set the image
  236. this.set_sort_img(order)
  237. // deselect active
  238. if(me.cur_sort) {
  239. $y(me.cur_sort, {backgroundColor:"#FFF"});
  240. $dh(me.cur_sort.sort_img);
  241. }
  242. // set at active
  243. me.cur_sort = this;
  244. $y(this, {backgroundColor:"#DDF"});
  245. $di(this.sort_img);
  246. }
  247. $y(cell.label_cell,{color:'#44A',cursor:'pointer'});
  248. // set default image
  249. cell.set_sort_img(me.sort_order_dict[fname] ? me.sort_order_dict[fname] : 'ASC');
  250. cell.onmouseover = function() {
  251. $di(this.sort_img);
  252. }
  253. cell.onmouseout = function() {
  254. if(this != me.cur_sort)
  255. $dh(this.sort_img);
  256. }
  257. cell.onclick = function() {
  258. // switch
  259. this.set_sorting_as((me.sort_order_dict[fname]=='ASC') ? 'DESC' : 'ASC');
  260. // run
  261. me.run();
  262. }
  263. this.sort_heads[fname] = cell;
  264. }
  265. // -------------------------------------------------------
  266. Listing.prototype.do_export = function() {
  267. this.build_query();
  268. var me = this;
  269. me.cn = [];
  270. if(this.no_index)
  271. me.cn = this.colnames; // No index
  272. else {
  273. for(var i=1;i<this.colnames.length;i++)
  274. me.cn.push(this.colnames[i]); // Ignore the SR label
  275. }
  276. var q = export_query(this.query,
  277. function(query) {
  278. export_csv(query, me.head_text, null, 1, null, me.cn);
  279. });
  280. }
  281. // -------------------------------------------------------
  282. Listing.prototype.build_query = function() {
  283. if(this.get_query)this.get_query(this);
  284. if(!this.query) { alert('No Query!'); return; }
  285. if(!this.prefix) this.prefix = 'tab';
  286. // add filters
  287. var cond = [];
  288. for(var i in this.filters) {
  289. var f = this.filters[i];
  290. var val = f.get_value();
  291. var c = f.condition;
  292. if(!c)c='=';
  293. if(val && c.toLowerCase()=='like')val += '%';
  294. if(f.tn && val && !in_list(['All','Select...',''],val))
  295. cond.push(repl(' AND `%(prefix)s%(dt)s`.%(fn)s %(condition)s "%(val)s"', {
  296. prefix: this.prefix, dt:f.tn, fn:f.fn, condition:c, val:val}));
  297. }
  298. if(cond) {
  299. this.query += NEWLINE + cond.join(NEWLINE)
  300. if(this.query_max)
  301. this.query_max += NEWLINE + cond.join(NEWLINE)
  302. }
  303. // add grouping
  304. if(this.group_by)
  305. this.query += ' ' + this.group_by + ' ';
  306. // add sorting
  307. if(this.sort_by && this.sort_order) {
  308. this.query += NEWLINE + ' ORDER BY `' + this.sort_by + '` ' + this.sort_order;
  309. }
  310. if(this.show_query) msgprint(this.query);
  311. }
  312. // -------------------------------------------------------
  313. Listing.prototype.set_rec_label = function(total, cur_page_len) {
  314. if(this.opts.hide_rec_label)
  315. return;
  316. else if(total==-1)
  317. this.rec_label.innerHTML = 'Fetching...'
  318. else if(total > 0)
  319. this.rec_label.innerHTML = repl('Total %(total)s %(keyword)s. Showing %(start)s to %(end)s', {total:total,start:cint(this.start)+1,end:cint(this.start)+cint(cur_page_len), keyword:this.keyword});
  320. else if(total==null)
  321. this.rec_label.innerHTML = ''
  322. else if(total==0)
  323. this.rec_label.innerHTML = this.no_rec_message;
  324. }
  325. // -------------------------------------------------------
  326. Listing.prototype.run = function(run_callback) {
  327. this.build_query();
  328. var q = this.query;
  329. var me = this;
  330. // add limits
  331. if(this.max_len && this.start>=this.max_len) this.start-= this.page_len;
  332. q += ' LIMIT ' + this.start + ',' + this.page_len;
  333. // callback
  334. var call_back = function(r,rt) {
  335. $dh(me.loading_img);
  336. // show results
  337. me.max_len = r.n_values;
  338. // result!
  339. if(r.values && r.values.length) {
  340. me.n_records = r.values.length;
  341. var nc = r.values[0].length;
  342. if(me.colwidths) nc = me.colwidths.length-(me.no_index?0:1); // -1 for sr no
  343. // redraw table
  344. if(me.opts.append_records && me.start!=0) {
  345. // add columns
  346. me.append_rows(r.values.length);
  347. } else {
  348. me.clear_tab();
  349. if(!me.show_empty_tab) {
  350. me.remove_result_tab();
  351. me.make_result_tab(r.values.length);
  352. }
  353. }
  354. me.refresh(r.values.length, nc, r.values, r.n_values);
  355. me.total_records = r.n_values;
  356. me.set_rec_label(r.n_values, r.values.length);
  357. // no result
  358. } else {
  359. me.n_records = 0;
  360. me.set_rec_label(0);
  361. me.clear_tab();
  362. if(!me.opts.append_records) {
  363. if(me.show_empty_tab) {
  364. me.clear_tab();
  365. } else {
  366. me.remove_result_tab();
  367. me.make_result_tab(0);
  368. if(me.opts.show_no_records_label) {
  369. $ds(me.show_no_records);
  370. }
  371. }
  372. }
  373. }
  374. $ds(me.results);
  375. if(run_callback)run_callback();
  376. if(me.onrun) me.onrun();
  377. }
  378. // run
  379. $dh(me.show_no_records);
  380. this.set_rec_label(-1);
  381. $di(this.loading_img);
  382. if(this.server_call) {
  383. this.server_call(this, call_back);
  384. } else {
  385. args={ query_max: (this.query_max ? this.query_max : '') }
  386. if(this.is_std_query) args.query = q;
  387. else args.simple_query = q;
  388. if(this.opts.formatted) args.formatted = 1;
  389. $c('webnotes.widgets.query_builder.runquery', args, call_back, null, this.no_loading);
  390. }
  391. }
  392. // -------------------------------------------------------
  393. Listing.prototype.remove_result_tab = function() {
  394. if(!this.result_tab) return;
  395. this.result_tab.parentNode.removeChild(this.result_tab);
  396. delete this.result_tab;
  397. }
  398. // -------------------------------------------------------
  399. Listing.prototype.reset_tab = function() {
  400. this.remove_result_tab();
  401. this.make_result_tab();
  402. }
  403. // -------------------------------------------------------
  404. Listing.prototype.make_result_tab = function(nr) {
  405. if(this.result_tab)return;
  406. if(!this.colwidths) alert("Listing: Must specify column widths");
  407. var has_headrow = this.colnames ? 1 : 0;
  408. if(nr==null)nr = this.page_len;
  409. nr += has_headrow;
  410. var nc = this.colwidths.length;
  411. var t=make_table(this.results, nr, nc, (this.opts.table_width ? this.opts.table_width : '100%'), this.colwidths,{padding:'0px'});
  412. t.className = 'srs_result_tab'; this.result_tab = t;
  413. $y(t,{borderCollapse:'collapse'});
  414. if(this.opts.table_width) {
  415. $y(this.results, {overflowX:'auto'});
  416. $y(t,{tableLayout:'fixed'});
  417. }
  418. // display headings
  419. if(has_headrow) {
  420. this.make_headings(t,nr,nc);
  421. // hilight sorted cell
  422. if(this.sort_by && this.sort_heads[this.sort_by]) {
  423. this.sort_heads[this.sort_by].set_sorting_as(this.sort_order);
  424. }
  425. }
  426. // style
  427. this.set_table_style();
  428. if(this.opts.no_border == 1) {
  429. $y(t,{border:'0px'});
  430. }
  431. this.result_tab = t;
  432. }
  433. // -------------------------------------------------------
  434. Listing.prototype.set_table_style = function() {
  435. // set style
  436. var t = this.result_tab;
  437. for(var ri=(this.colnames?1:0); ri<t.rows.length; ri++) {
  438. for(var ci=0; ci<t.rows[ri].cells.length; ci++) {
  439. // default style
  440. if(this.opts.cell_style)$y($td(t,ri,ci), this.opts.cell_style);
  441. // color alternate rows (itunes style)
  442. if(this.opts.alt_cell_style && (ri % 2))$y($td(t,ri,ci), this.opts.alt_cell_style);
  443. // show blank empty cells
  444. if(this.opts.show_empty_tab && !$td(t, ri, ci).innerHTML)$td(t, ri, ci).innerHTML = '&nbsp;';
  445. }
  446. }
  447. }
  448. // -------------------------------------------------------
  449. Listing.prototype.append_rows = function(nr) {
  450. for(var i=0; i<nr; i++) { append_row(this.result_tab); }
  451. this.set_table_style();
  452. }
  453. // -------------------------------------------------------
  454. Listing.prototype.clear_tab = function() {
  455. $dh(this.results);
  456. if(this.result_tab) {
  457. var nr = this.result_tab.rows.length;
  458. var nc = this.result_tab.rows[0].cells.length;
  459. for(var ri=(this.colnames?1:0); ri<nr; ri++)
  460. for(var ci=0; ci < nc; ci++)
  461. $td(this.result_tab, ri, ci).innerHTML = (this.opts.show_empty_tab ? '&nbsp;' : '');
  462. }
  463. }
  464. // -------------------------------------------------------
  465. Listing.prototype.clear = function() {
  466. this.rec_label.innerHTML = '';
  467. this.clear_tab();
  468. }
  469. // -------------------------------------------------------
  470. Listing.prototype.refresh_calc = function() {
  471. if(!this.opts.show_calc) return;
  472. if(has_common(this.coltypes, ['Currency','Int','Float'])) {
  473. $di(this.buttons['Calc']);
  474. } else {
  475. $dh(this.buttons['Calc']);
  476. }
  477. }
  478. // -------------------------------------------------------
  479. Listing.prototype.refresh = function(nr, nc, d, n_values) {
  480. this.refresh_more_button(nr, n_values);
  481. this.refresh_calc();
  482. if(this.show_result)
  483. this.show_result();
  484. else {
  485. if(nr) {
  486. // Standard Result Display
  487. var start = this.result_tab.rows.length - nr;
  488. // display results
  489. for(var ri=start ; ri<start+nr ; ri++) {
  490. var c0 = $td(this.result_tab,ri,0);
  491. // show index
  492. if(!this.no_index) {
  493. c0.innerHTML = cint(this.start) + cint(ri-start) + 1;
  494. }
  495. // columns
  496. for(var ci=0 ; ci<nc ; ci++) {
  497. var c = $td(this.result_tab, ri, ci+(this.no_index?0:1));
  498. if(c) {
  499. c.innerHTML = ''; // clear
  500. if(this.show_cell) this.show_cell(c, ri-start, ci, d);
  501. else this.std_cell(c, ri-start, ci, d);
  502. }
  503. }
  504. }
  505. }
  506. }
  507. }
  508. // -------------------------------------------------------
  509. Listing.prototype.refresh_more_button = function(nr, n_values) {
  510. var me = this;
  511. if(this.more_btn) {
  512. $dh(this.more_btn);
  513. }
  514. if((this.start + nr) == this.max_len || (!this.max_len && nr < this.page_len)) {
  515. // all records shown
  516. } else if(nr) {
  517. if(!this.more_btn) {
  518. // make button
  519. $y(this.bottom_div, {margin:'8px 0px 16px 0px', textAlign:'center'});
  520. this.more_btn = $btn(this.bottom_div, 'Show more results...',
  521. function() {
  522. me.start = me.start + me.page_len;
  523. me.more_btn.set_working();
  524. me.run(function() { me.more_btn.done_working(); });
  525. }, {fontSize:'14px'}, 0, 1);
  526. $y(this.more_btn.loading_img, {marginBottom:'0px'});
  527. }
  528. $di(this.more_btn);
  529. }
  530. }
  531. // -------------------------------------------------------
  532. Listing.prototype.make_headings = function(t,nr,nc) {
  533. for(var ci=0 ; ci<nc ; ci++) {
  534. var tmp = make_table($td(t,0,ci),1,2,'100%',['','0px'],this.opts.head_style);
  535. $y(tmp,{tableLayout:'fixed',borderCollapse:'collapse'});
  536. $y($td(t,0,ci),this.opts.head_main_style); // right border on main table
  537. $td(t,0,ci).sort_cell = $td(tmp,0,1);
  538. $td(t,0,ci).label_cell = $td(tmp,0,0);
  539. $td(tmp,0,1).style.padding = '0px';
  540. $td(tmp,0,0).innerHTML = this.colnames[ci]?this.colnames[ci]:'&nbsp;';
  541. if(this.sort_list[ci])this.set_sort($td(t,0,ci), ci, this.sort_list[ci]);
  542. var div = $a($td(t,0,ci), 'div');
  543. $td(t,0,ci).style.borderBottom ='1px solid #CCC';
  544. if(this.coltypes && this.coltypes[ci] && in_list(['Currency','Float','Int'], this.coltypes[ci])) $y($td(t,0,ci).label_cell,{textAlign:'right'})
  545. }
  546. }
  547. // -------------------------------------------------------
  548. Listing.prototype.std_cell = function(cell, ri, ci, d) {
  549. var has_headrow = this.colnames ? 1 : 0;
  550. cell.div = $a(cell, 'div');
  551. $s(
  552. cell.div,
  553. d[ri][ci],
  554. this.coltypes ? this.coltypes[ci+(this.no_index?0:1)] : null,
  555. this.coloptions ? this.coloptions[ci+(this.no_index?0:1)] : null
  556. );
  557. }