選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

704 行
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("lib/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 = 'lib/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. wn.require('lib/js/legacy/widgets/print_query.js');
  133. _p.print_query = new _p.PrintQuery();
  134. _p.print_query.show_dialog(args);
  135. }
  136. // -------------------------------------------------------
  137. Listing.prototype.do_calc = function() {
  138. show_calc(this.result_tab, this.colnames, this.coltypes, 0)
  139. }
  140. // -------------------------------------------------------
  141. Listing.prototype.add_filter = function(label, ftype, options, tname, fname, cond) {
  142. if(!this.filter_area){alert('[Listing] make() must be called before add_filter');}
  143. var me = this;
  144. // create filter area
  145. if(!this.filter_set) {
  146. // actual area
  147. var h = $a(this.filter_area, 'div', '', {fontSize:'14px', fontWeight:'bold', marginBottom:'4px'});
  148. h.innerHTML = 'Filter your search';
  149. this.filter_area.div = $a(this.filter_area, 'div');
  150. this.perm = [[1,1],]
  151. this.filters = {};
  152. }
  153. $ds(this.filter_wrapper);
  154. // create new table (or new line)
  155. if((!this.inp_tab) || (this.cell_idx==this.filters_per_line)) {
  156. this.inp_tab = $a(this.filter_area.div, 'table','',{width:'100%', tableLayout:'fixed'});
  157. this.inp_tab.insertRow(0);
  158. for(var i=0;i<this.filters_per_line;i++) {
  159. this.inp_tab.rows[0].insertCell(i);
  160. }
  161. this.cell_idx = 0;
  162. }
  163. var c= this.inp_tab.rows[0].cells[this.cell_idx];
  164. this.cell_idx++;
  165. $y(c,{width:cint(100/this.filters_per_line) + '%',textAlign:'left',verticalAlign:'top'});
  166. // filter label
  167. var d1= $a(c,'div','',{fontSize:'11px', marginBottom:'2px'}); d1.innerHTML = label;
  168. if(ftype=='Link') d1.innerHTML += ' <img src="lib/images/icons/link.png" style="margin-bottom:-5px" title="Link">';
  169. var d2= $a(c,'div');
  170. // create the filter
  171. if(in_list(['Text', 'Small Text', 'Code', 'Text Editor','Read Only'],ftype))
  172. ftype='Data';
  173. // add empty option in select (if missing)
  174. if(ftype=='Select' && !in_list(options.split('\n'), '')) options = '\n'+options
  175. var inp = make_field({fieldtype:ftype, 'label':label, 'options':options, no_buttons:1}, '', d2, this, 0, 1);
  176. inp.not_in_form = 1;
  177. inp.report = this;
  178. // filter style
  179. inp.df.single_select = 1;
  180. inp.parent_cell = c;
  181. inp.parent_tab = this.input_tab;
  182. $y(inp.wrapper,{width:'95%'});
  183. inp.refresh();
  184. //if(!inp.input.custom_select && !inp.input.txt)
  185. // $y(inp.input,{width:'100%'});
  186. inp.tn = tname; inp.fn = fname; inp.condition = ftype=='Data' ? 'like' : cond;
  187. var me = this;
  188. inp.onchange = function() { me.start = 0; }
  189. this.filters[label] = inp;
  190. this.filter_set = 1;
  191. }
  192. // -------------------------------------------------------
  193. Listing.prototype.remove_filter = function(label) {
  194. var inp = this.filters[label];
  195. inp.parent_tab.rows[0].deleteCell(inp.parent_cell.cellIndex);
  196. delete this.filters[label];
  197. }
  198. // -------------------------------------------------------
  199. Listing.prototype.remove_all_filters = function() {
  200. for(var k in this.filters) this.remove_filter(k);
  201. $dh(this.filter_wrapper);
  202. }
  203. Listing.prototype.add_sort = function(ci, fname) { this.sort_list[ci]=fname; }
  204. Listing.prototype.has_data = function() { return this.n_records; }
  205. // -------------------------------------------------------
  206. Listing.prototype.set_default_sort = function(fname, sort_order) {
  207. this.sort_order = sort_order;
  208. this.sort_order_dict[fname] = sort_order;
  209. this.sort_by = fname;
  210. if(this.sort_heads[fname])
  211. this.sort_heads[fname].set_sorting_as(sort_order);
  212. }
  213. // -------------------------------------------------------
  214. Listing.prototype.set_sort = function(cell, ci, fname) {
  215. var me = this;
  216. $y(cell.sort_cell,{width:'18px'});
  217. cell.sort_img = $a(cell.sort_cell, 'img');
  218. cell.fname = fname;
  219. $dh(cell.sort_img);
  220. cell.set_sort_img = function(order) {
  221. var t = 'images/icons/sort_desc.gif';
  222. if(order=='ASC') {
  223. t = 'images/icons/sort_asc.gif';
  224. }
  225. this.sort_img.src = t;
  226. }
  227. cell.set_sorting_as = function(order) {
  228. // set values for query building
  229. me.sort_order = order;
  230. me.sort_by = this.fname
  231. me.sort_order_dict[this.fname] = order;
  232. // set the image
  233. this.set_sort_img(order)
  234. // deselect active
  235. if(me.cur_sort) {
  236. $y(me.cur_sort, {backgroundColor:"#FFF"});
  237. $dh(me.cur_sort.sort_img);
  238. }
  239. // set at active
  240. me.cur_sort = this;
  241. $y(this, {backgroundColor:"#DDF"});
  242. $di(this.sort_img);
  243. }
  244. $y(cell.label_cell,{color:'#44A',cursor:'pointer'});
  245. // set default image
  246. cell.set_sort_img(me.sort_order_dict[fname] ? me.sort_order_dict[fname] : 'ASC');
  247. cell.onmouseover = function() {
  248. $di(this.sort_img);
  249. }
  250. cell.onmouseout = function() {
  251. if(this != me.cur_sort)
  252. $dh(this.sort_img);
  253. }
  254. cell.onclick = function() {
  255. // switch
  256. this.set_sorting_as((me.sort_order_dict[fname]=='ASC') ? 'DESC' : 'ASC');
  257. // run
  258. me.run();
  259. }
  260. this.sort_heads[fname] = cell;
  261. }
  262. // -------------------------------------------------------
  263. Listing.prototype.do_export = function() {
  264. this.build_query();
  265. var me = this;
  266. me.cn = [];
  267. if(this.no_index)
  268. me.cn = this.colnames; // No index
  269. else {
  270. for(var i=1;i<this.colnames.length;i++)
  271. me.cn.push(this.colnames[i]); // Ignore the SR label
  272. }
  273. var q = export_query(this.query,
  274. function(query) {
  275. export_csv(query, me.head_text, null, 1, null, me.cn);
  276. });
  277. }
  278. // -------------------------------------------------------
  279. Listing.prototype.build_query = function() {
  280. if(this.get_query)this.get_query(this);
  281. if(!this.query) { alert('No Query!'); return; }
  282. if(!this.prefix) this.prefix = 'tab';
  283. // add filters
  284. var cond = [];
  285. for(var i in this.filters) {
  286. var f = this.filters[i];
  287. var val = f.get_value();
  288. var c = f.condition;
  289. if(!c)c='=';
  290. if(val && c.toLowerCase()=='like')val += '%';
  291. if(f.tn && val && !in_list(['All','Select...',''],val))
  292. cond.push(repl(' AND `%(prefix)s%(dt)s`.%(fn)s %(condition)s "%(val)s"', {
  293. prefix: this.prefix, dt:f.tn, fn:f.fn, condition:c, val:val}));
  294. }
  295. if(cond) {
  296. this.query += NEWLINE + cond.join(NEWLINE)
  297. if(this.query_max)
  298. this.query_max += NEWLINE + cond.join(NEWLINE)
  299. }
  300. // add grouping
  301. if(this.group_by)
  302. this.query += ' ' + this.group_by + ' ';
  303. // add sorting
  304. if(this.sort_by && this.sort_order) {
  305. this.query += NEWLINE + ' ORDER BY `' + this.sort_by + '` ' + this.sort_order;
  306. }
  307. if(this.show_query) msgprint(this.query);
  308. }
  309. // -------------------------------------------------------
  310. Listing.prototype.set_rec_label = function(total, cur_page_len) {
  311. if(this.opts.hide_rec_label)
  312. return;
  313. else if(total==-1)
  314. this.rec_label.innerHTML = 'Fetching...'
  315. else if(total > 0)
  316. 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});
  317. else if(total==null)
  318. this.rec_label.innerHTML = ''
  319. else if(total==0)
  320. this.rec_label.innerHTML = this.no_rec_message;
  321. }
  322. // -------------------------------------------------------
  323. Listing.prototype.run = function(run_callback) {
  324. this.build_query();
  325. var q = this.query;
  326. var me = this;
  327. // add limits
  328. if(this.max_len && this.start>=this.max_len) this.start-= this.page_len;
  329. q += ' LIMIT ' + this.start + ',' + this.page_len;
  330. // callback
  331. var call_back = function(r,rt) {
  332. $dh(me.loading_img);
  333. // show results
  334. me.max_len = r.n_values;
  335. // result!
  336. if(r.values && r.values.length) {
  337. me.n_records = r.values.length;
  338. var nc = r.values[0].length;
  339. if(me.colwidths) nc = me.colwidths.length-(me.no_index?0:1); // -1 for sr no
  340. // redraw table
  341. if(me.opts.append_records && me.start!=0) {
  342. // add columns
  343. me.append_rows(r.values.length);
  344. } else {
  345. me.clear_tab();
  346. if(!me.show_empty_tab) {
  347. me.remove_result_tab();
  348. me.make_result_tab(r.values.length);
  349. }
  350. }
  351. me.refresh(r.values.length, nc, r.values, r.n_values);
  352. me.total_records = r.n_values;
  353. me.set_rec_label(r.n_values, r.values.length);
  354. // no result
  355. } else {
  356. me.n_records = 0;
  357. me.set_rec_label(0);
  358. me.clear_tab();
  359. if(!me.opts.append_records) {
  360. if(me.show_empty_tab) {
  361. me.clear_tab();
  362. } else {
  363. me.remove_result_tab();
  364. me.make_result_tab(0);
  365. if(me.opts.show_no_records_label) {
  366. $ds(me.show_no_records);
  367. }
  368. }
  369. }
  370. }
  371. $ds(me.results);
  372. if(run_callback)run_callback();
  373. if(me.onrun) me.onrun();
  374. }
  375. // run
  376. $dh(me.show_no_records);
  377. this.set_rec_label(-1);
  378. $di(this.loading_img);
  379. if(this.server_call) {
  380. this.server_call(this, call_back);
  381. } else {
  382. args={ query_max: (this.query_max ? this.query_max : '') }
  383. if(this.is_std_query) args.query = q;
  384. else args.simple_query = q;
  385. if(this.opts.formatted) args.formatted = 1;
  386. $c('webnotes.widgets.query_builder.runquery', args, call_back, null, this.no_loading);
  387. }
  388. }
  389. // -------------------------------------------------------
  390. Listing.prototype.remove_result_tab = function() {
  391. if(!this.result_tab) return;
  392. this.result_tab.parentNode.removeChild(this.result_tab);
  393. delete this.result_tab;
  394. }
  395. // -------------------------------------------------------
  396. Listing.prototype.reset_tab = function() {
  397. this.remove_result_tab();
  398. this.make_result_tab();
  399. }
  400. // -------------------------------------------------------
  401. Listing.prototype.make_result_tab = function(nr) {
  402. if(this.result_tab)return;
  403. if(!this.colwidths) alert("Listing: Must specify column widths");
  404. var has_headrow = this.colnames ? 1 : 0;
  405. if(nr==null)nr = this.page_len;
  406. nr += has_headrow;
  407. var nc = this.colwidths.length;
  408. var t=make_table(this.results, nr, nc, (this.opts.table_width ? this.opts.table_width : '100%'), this.colwidths,{padding:'0px'});
  409. t.className = 'srs_result_tab'; this.result_tab = t;
  410. $y(t,{borderCollapse:'collapse'});
  411. if(this.opts.table_width) {
  412. $y(this.results, {overflowX:'auto'});
  413. $y(t,{tableLayout:'fixed'});
  414. }
  415. // display headings
  416. if(has_headrow) {
  417. this.make_headings(t,nr,nc);
  418. // hilight sorted cell
  419. if(this.sort_by && this.sort_heads[this.sort_by]) {
  420. this.sort_heads[this.sort_by].set_sorting_as(this.sort_order);
  421. }
  422. }
  423. // style
  424. this.set_table_style();
  425. if(this.opts.no_border == 1) {
  426. $y(t,{border:'0px'});
  427. }
  428. this.result_tab = t;
  429. }
  430. // -------------------------------------------------------
  431. Listing.prototype.set_table_style = function() {
  432. // set style
  433. var t = this.result_tab;
  434. for(var ri=(this.colnames?1:0); ri<t.rows.length; ri++) {
  435. for(var ci=0; ci<t.rows[ri].cells.length; ci++) {
  436. // default style
  437. if(this.opts.cell_style)$y($td(t,ri,ci), this.opts.cell_style);
  438. // color alternate rows (itunes style)
  439. if(this.opts.alt_cell_style && (ri % 2))$y($td(t,ri,ci), this.opts.alt_cell_style);
  440. // show blank empty cells
  441. if(this.opts.show_empty_tab && !$td(t, ri, ci).innerHTML)$td(t, ri, ci).innerHTML = '&nbsp;';
  442. }
  443. }
  444. }
  445. // -------------------------------------------------------
  446. Listing.prototype.append_rows = function(nr) {
  447. for(var i=0; i<nr; i++) { append_row(this.result_tab); }
  448. this.set_table_style();
  449. }
  450. // -------------------------------------------------------
  451. Listing.prototype.clear_tab = function() {
  452. $dh(this.results);
  453. if(this.result_tab) {
  454. var nr = this.result_tab.rows.length;
  455. var nc = this.result_tab.rows[0].cells.length;
  456. for(var ri=(this.colnames?1:0); ri<nr; ri++)
  457. for(var ci=0; ci < nc; ci++)
  458. $td(this.result_tab, ri, ci).innerHTML = (this.opts.show_empty_tab ? '&nbsp;' : '');
  459. }
  460. }
  461. // -------------------------------------------------------
  462. Listing.prototype.clear = function() {
  463. this.rec_label.innerHTML = '';
  464. this.clear_tab();
  465. }
  466. // -------------------------------------------------------
  467. Listing.prototype.refresh_calc = function() {
  468. if(!this.opts.show_calc) return;
  469. if(has_common(this.coltypes, ['Currency','Int','Float'])) {
  470. $di(this.buttons['Calc']);
  471. } else {
  472. $dh(this.buttons['Calc']);
  473. }
  474. }
  475. // -------------------------------------------------------
  476. Listing.prototype.refresh = function(nr, nc, d, n_values) {
  477. this.refresh_more_button(nr, n_values);
  478. this.refresh_calc();
  479. if(this.show_result)
  480. this.show_result();
  481. else {
  482. if(nr) {
  483. // Standard Result Display
  484. var start = this.result_tab.rows.length - nr;
  485. // display results
  486. for(var ri=start ; ri<start+nr ; ri++) {
  487. var c0 = $td(this.result_tab,ri,0);
  488. // show index
  489. if(!this.no_index) {
  490. c0.innerHTML = cint(this.start) + cint(ri-start) + 1;
  491. }
  492. // columns
  493. for(var ci=0 ; ci<nc ; ci++) {
  494. var c = $td(this.result_tab, ri, ci+(this.no_index?0:1));
  495. if(c) {
  496. c.innerHTML = ''; // clear
  497. if(this.show_cell) this.show_cell(c, ri-start, ci, d);
  498. else this.std_cell(c, ri-start, ci, d);
  499. }
  500. }
  501. }
  502. }
  503. }
  504. }
  505. // -------------------------------------------------------
  506. Listing.prototype.refresh_more_button = function(nr, n_values) {
  507. var me = this;
  508. if(this.more_btn) {
  509. $dh(this.more_btn);
  510. }
  511. if((this.start + nr) == this.max_len || (!this.max_len && nr < this.page_len)) {
  512. // all records shown
  513. } else if(nr) {
  514. if(!this.more_btn) {
  515. // make button
  516. $y(this.bottom_div, {margin:'8px 0px 16px 0px', textAlign:'center'});
  517. this.more_btn = $btn(this.bottom_div, 'Show more results...',
  518. function() {
  519. me.start = me.start + me.page_len;
  520. me.more_btn.set_working();
  521. me.run(function() { me.more_btn.done_working(); });
  522. }, {fontSize:'14px'}, 0, 1);
  523. $y(this.more_btn.loading_img, {marginBottom:'0px'});
  524. }
  525. $di(this.more_btn);
  526. }
  527. }
  528. // -------------------------------------------------------
  529. Listing.prototype.make_headings = function(t,nr,nc) {
  530. for(var ci=0 ; ci<nc ; ci++) {
  531. var tmp = make_table($td(t,0,ci),1,2,'100%',['','0px'],this.opts.head_style);
  532. $y(tmp,{tableLayout:'fixed',borderCollapse:'collapse'});
  533. $y($td(t,0,ci),this.opts.head_main_style); // right border on main table
  534. $td(t,0,ci).sort_cell = $td(tmp,0,1);
  535. $td(t,0,ci).label_cell = $td(tmp,0,0);
  536. $td(tmp,0,1).style.padding = '0px';
  537. $td(tmp,0,0).innerHTML = this.colnames[ci]?this.colnames[ci]:'&nbsp;';
  538. if(this.sort_list[ci])this.set_sort($td(t,0,ci), ci, this.sort_list[ci]);
  539. var div = $a($td(t,0,ci), 'div');
  540. $td(t,0,ci).style.borderBottom ='1px solid #CCC';
  541. if(this.coltypes && this.coltypes[ci] && in_list(['Currency','Float','Int'], this.coltypes[ci])) $y($td(t,0,ci).label_cell,{textAlign:'right'})
  542. }
  543. }
  544. // -------------------------------------------------------
  545. Listing.prototype.std_cell = function(cell, ri, ci, d) {
  546. var has_headrow = this.colnames ? 1 : 0;
  547. cell.div = $a(cell, 'div');
  548. $s(
  549. cell.div,
  550. d[ri][ci],
  551. this.coltypes ? this.coltypes[ci+(this.no_index?0:1)] : null,
  552. this.coloptions ? this.coloptions[ci+(this.no_index?0:1)] : null
  553. );
  554. }