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.
 
 
 
 
 
 

192 regels
4.9 KiB

  1. // new re-factored Listing object
  2. // uses FieldGroup for rendering filters
  3. // removed rarely used functionality
  4. //
  5. // opts:
  6. // parent
  7. // method (method to call on server)
  8. // args (additional args to method)
  9. // query or get_query (will be deprecated)
  10. // query_max
  11. // no_result_message ("No result")
  12. // page_length (20)
  13. // filters ([{docfield}, ..])
  14. // hide_refresh (False)
  15. // new_doctype
  16. // [function] render_row(parent, data)
  17. // [function] onrun
  18. // no_loading (no ajax indicator)
  19. wn.widgets.Listing = function(opts) {
  20. this.opts = opts;
  21. this.page_length = 20;
  22. this.btns = {};
  23. this.start = 0;
  24. var me = this;
  25. // create place holders for all the elements
  26. this.make = function(opts) {
  27. this.wrapper = $a(this.opts.parent, 'div');
  28. this.filters_area = $a(this.wrapper, 'div', 'listing-filters');
  29. this.toolbar_area = $a(this.wrapper, 'div', 'listing-toolbar');
  30. this.results_area = $a(this.wrapper, 'div', 'listing-results');
  31. this.more_button_area = $a(this.wrapper, 'div', 'listing-more');
  32. this.no_results_area = $a(this.wrapper, 'div', 'help_box', {display: 'none'},
  33. (this.opts.no_result_message ? this.opts.no_result_message : 'No results'));
  34. if(opts) this.opts = opts;
  35. this.page_length = this.opts.page_length ? this.opts.page_length : this.page_length;
  36. this.make_toolbar();
  37. this.make_filters();
  38. this.make_more_button();
  39. }
  40. // make filters using FieldGroup
  41. this.make_filters = function() {
  42. if(this.opts.filters) {
  43. $ds(this.filters_area);
  44. // expand / collapse filters
  45. this.filters = new wn.widgets.FieldGroup(this.filters_area, this.opts.fields);
  46. }
  47. }
  48. // make the toolbar
  49. this.make_toolbar = function() {
  50. if(!(this.opts.hide_refresh || this.opts.no_refresh)) {
  51. this.ref_img = $a(this.toolbar_area, 'span', 'link_type', {color:'#888'}, '[refresh]');
  52. this.ref_img.onclick = function() { me.run(); }
  53. this.loading_img = $a(this.toolbar_area, 'img', 'lib/images/ui/button-load.gif', {display:'none', marginLeft:'3px', marginBottom:'-2px'});
  54. }
  55. if(this.opts.new_doctype) {
  56. this.new_btn = $btn(this.toolbar_area,
  57. 'New ' + get_doctype_label(this.opts.new_doctype),
  58. function() {
  59. newdoc(me.opts.new_doctype, me.opts.new_doc_onload, me.opts.new_doc_indialog, me.opts.new_doc_onsave);
  60. },
  61. {marginLeft:'7px'});
  62. }
  63. }
  64. // make more button
  65. // that shows more results when they are displayed
  66. this.make_more_button = function() {
  67. this.more_btn = $btn(this.more_button_area, 'More...',
  68. function() {
  69. me.more_btn.set_working();
  70. me.run(function() {
  71. me.more_btn.done_working();
  72. }, 1);
  73. }, '', 0, 1
  74. );
  75. $y(this.more_btn.loading_img, {marginBottom:'0px'});
  76. }
  77. // clear the results and re-run the query
  78. this.clear = function() {
  79. this.results_area.innerHTML = '';
  80. this.table = null;
  81. $ds(this.results_area);
  82. $dh(this.no_results_area);
  83. }
  84. // callback on the query
  85. // build the table
  86. // returns r.values as a table of results
  87. this.make_results = function(r, rt) {
  88. if(this.start==0) this.clear();
  89. $dh(this.more_button_area);
  90. if(this.loading_img) $dh(this.loading_img)
  91. if(r.values && r.values.length) {
  92. this.values = r.values;
  93. var m = Math.min(r.values.length, this.page_length);
  94. // render the rows
  95. for(var i=0; i < m; i++) {
  96. var row = this.add_row();
  97. // call the show_cell with row, ri, ci, d
  98. this.opts.render_row(row, r.values[i], this, i);
  99. }
  100. // extend start
  101. this.start += m;
  102. // refreh more button
  103. if(r.values.length > this.page_length) $ds(this.more_button_area);
  104. } else {
  105. if(this.start==0) {
  106. $dh(this.results_area);
  107. $ds(this.no_results_area);
  108. }
  109. }
  110. // callbacks
  111. if(this.onrun) this.onrun();
  112. if(this.opts.onrun) this.opts.onrun();
  113. }
  114. // add a results row
  115. this.add_row = function() {
  116. return $a(this.results_area, 'div', '',
  117. (opts.cell_style ? opts.cell_style : {padding: '3px 0px'}));
  118. }
  119. // run the query, get the query from
  120. // the get_query method of opts
  121. this.run = function(callback, append) {
  122. if(callback)
  123. this.onrun = callback;
  124. if(!append)
  125. this.start = 0;
  126. // load query
  127. if(!this.opts.method) {
  128. this.query = this.opts.get_query ? this.opts.get_query() : this.opts.query;
  129. this.add_limits();
  130. var args={
  131. query_max: this.query_max || this.opts.query_max || '',
  132. as_dict: 1
  133. }
  134. args.simple_query = this.query;
  135. } else {
  136. var args = {
  137. limit_start: this.start,
  138. limit_page_length: this.page_length
  139. }
  140. }
  141. if(this.opts.args)
  142. $.extend(args, this.opts.args)
  143. // show loading
  144. if(this.loading_img) $di(this.loading_img);
  145. wn.call({
  146. method: this.opts.method || 'webnotes.widgets.query_builder.runquery',
  147. args: args,
  148. callback: function(r, rt) { me.make_results(r, rt) },
  149. no_spinner: this.opts.no_loading,
  150. btn: this.opts.run_btn
  151. });
  152. }
  153. this.refresh = this.run;
  154. this.add_limits = function() {
  155. this.query += ' LIMIT ' + this.start + ',' + (this.page_length+1);
  156. }
  157. if(opts) this.make();
  158. }