Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

160 rindas
4.2 KiB

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