// Listing // this listing object will soon be // deprecated because it has a very non standard way of creation // the new listing object is at wn.widgets.Listing // ----------------------- list_opts = { cell_style : {padding:'3px 2px'}, alt_cell_style : {}, head_style : {height:'20px',overflow:'hidden',verticalAlign:'middle',fontWeight:'bold',padding:'1px',fontSize:'13px'}, head_main_style : {padding:'0px'}, hide_export : 1, hide_print : 1, hide_refresh : 0, hide_rec_label: 0, show_calc: 1, show_empty_tab : 0, no_border: 1, append_records: 1, table_width: null }; // ------------------------------------------------------- function Listing(head_text, no_index, no_loading) { this.start = 0; this.page_len = 20; this.filters_per_line = 7; this.cell_idx = 0; this.head_text = head_text ? head_text : 'Result'; this.keyword = 'records'; this.no_index = no_index; this.underline = 1; this.no_rec_message = 'No Result'; // interfaces // show_cell(cell, cell_id, data) - override cell display // show_result() // server_call(srs, call_back) - override query function this.show_cell = null; this.show_result = null; this.colnames = null; // sr num is required this.colwidths = null; this.coltypes = null; this.coloptions = null; this.filters = {}; this.sort_list = {}; this.sort_order_dict = {}; this.sort_heads = {}; this.is_std_query = false; this.server_call = null; this.no_loading = no_loading; this.opts = copy_dict(list_opts); } // ------------------------------------------------------- Listing.prototype.make = function(parent) { var me = this; this.wrapper = parent; // filter this.filter_wrapper = $a(parent, 'div', 'srs_filter_wrapper'); this.filter_area = $a(this.filter_wrapper, 'div', 'srs_filter_area'); $dh(this.filter_wrapper); this.btn_area = $a(parent, 'div', '', {margin:'8px 0px'}); this.body_area = $a(parent,'div','srs_body_area'); if(!this.opts.hide_rec_label) this.rec_label = $a(this.body_area, 'div', '', {margin:'4px 0px',color:'#888'}); // results this.results = $a($a(this.body_area, 'div','srs_results_area'),'div'); this.fetching_area = $a(this.body_area, 'div','',{height:'120px', background:'url("images/ui/square_loading.gif") center no-repeat', display:'none'}); this.show_no_records = $a(this.body_area,'div','',{margin:'200px 0px', textAlign:'center', fontSize:'14px', color:'#888', display:'none'}); this.show_no_records.innerHTML = 'No Result'; // empty table (old style) if(this.opts.show_empty_tab) this.make_result_tab(); this.bottom_div = $a(this.body_area,'div','',{paddingTop:'8px'}); this.make_toolbar(); } // ------------------------------------------------------- Listing.prototype.make_toolbar = function() { var me = this; this.buttons = {}; // buttons var make_btn = function(label,icon,onclick,bold) { var btn = $btn(me.btn_area,label,onclick,{marginRight:'4px'}); if(bold)$y(btn,{fontWeight: 'bold'}); me.buttons[label] = btn; } // refresh btn if(!this.opts.hide_refresh) { make_btn('Refresh','ui-icon-refresh',function(btn) { me.start = 0; me.run(); },1); } // new if(this.opts.show_new) { make_btn('New ','ui-icon-document',function() { new_doc(me.dt); },1); } // report if(this.opts.show_report) { make_btn('Report Builder','ui-icon-clipboard',function() { loadreport(me.dt, null, null, null, 1); },0); } // export if(!this.opts.hide_export) { make_btn('Export','ui-icon-circle-arrow-e',function() {me.do_export();}); } // print if(!this.opts.hide_print) { make_btn('Print','ui-icon-print',function() {me.do_print();}); } // calc if(this.opts.show_calc) { make_btn('Calc','ui-icon-calculator',function() {me.do_calc();}); $dh(me.buttons['Calc']) } this.loading_img = $a(this.btn_area,'img','',{display:'none',marginBottom:'-2px'}); this.loading_img.src = 'images/ui/button-load.gif'; if(!keys(this.buttons).length) $dh(this.btn_area); } // ------------------------------------------------------- Listing.prototype.do_print = function() { this.build_query(); if(!this.query) { alert('No Query!'); return; } args = { query:this.query, title:this.head_text, colnames:this.colnames, colwidths:this.colwidths, coltypes:this.coltypes, has_index:(this.no_index ? 0 : 1), has_headings: 1, check_limit:1, is_simple:1 } new_widget('_p.PrintQuery', function(w) { // global if(!_p.print_query) _p.print_query = w; _p.print_query.show_dialog(args); }, 1); } // ------------------------------------------------------- Listing.prototype.do_calc = function() { show_calc(this.result_tab, this.colnames, this.coltypes, 0) } // ------------------------------------------------------- Listing.prototype.add_filter = function(label, ftype, options, tname, fname, cond) { if(!this.filter_area){alert('[Listing] make() must be called before add_filter');} var me = this; // create filter area if(!this.filter_set) { // actual area var h = $a(this.filter_area, 'div', '', {fontSize:'14px', fontWeight:'bold', marginBottom:'4px'}); h.innerHTML = 'Filter your search'; this.filter_area.div = $a(this.filter_area, 'div'); this.perm = [[1,1],] this.filters = {}; } $ds(this.filter_wrapper); // create new table (or new line) if((!this.inp_tab) || (this.cell_idx==this.filters_per_line)) { this.inp_tab = $a(this.filter_area.div, 'table','',{width:'100%', tableLayout:'fixed'}); this.inp_tab.insertRow(0); for(var i=0;i 0) 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}); else if(total==null) this.rec_label.innerHTML = '' else if(total==0) this.rec_label.innerHTML = this.no_rec_message; } // ------------------------------------------------------- Listing.prototype.run = function(run_callback) { this.build_query(); var q = this.query; var me = this; // add limits if(this.max_len && this.start>=this.max_len) this.start-= this.page_len; q += ' LIMIT ' + this.start + ',' + this.page_len; // callback var call_back = function(r,rt) { $dh(me.loading_img); // show results me.max_len = r.n_values; // result! if(r.values && r.values.length) { me.n_records = r.values.length; var nc = r.values[0].length; if(me.colwidths) nc = me.colwidths.length-(me.no_index?0:1); // -1 for sr no // redraw table if(me.opts.append_records && me.start!=0) { // add columns me.append_rows(r.values.length); } else { me.clear_tab(); if(!me.show_empty_tab) { me.remove_result_tab(); me.make_result_tab(r.values.length); } } me.refresh(r.values.length, nc, r.values, r.n_values); me.total_records = r.n_values; me.set_rec_label(r.n_values, r.values.length); // no result } else { me.n_records = 0; me.set_rec_label(0); me.clear_tab(); if(!me.opts.append_records) { if(me.show_empty_tab) { me.clear_tab(); } else { me.remove_result_tab(); me.make_result_tab(0); if(me.opts.show_no_records_label) { $ds(me.show_no_records); } } } } $ds(me.results); if(run_callback)run_callback(); if(me.onrun) me.onrun(); } // run $dh(me.show_no_records); this.set_rec_label(-1); $di(this.loading_img); if(this.server_call) { this.server_call(this, call_back); } else { args={ query_max: (this.query_max ? this.query_max : '') } if(this.is_std_query) args.query = q; else args.simple_query = q; if(this.opts.formatted) args.formatted = 1; $c('webnotes.widgets.query_builder.runquery', args, call_back, null, this.no_loading); } } // ------------------------------------------------------- Listing.prototype.remove_result_tab = function() { if(!this.result_tab) return; this.result_tab.parentNode.removeChild(this.result_tab); delete this.result_tab; } // ------------------------------------------------------- Listing.prototype.reset_tab = function() { this.remove_result_tab(); this.make_result_tab(); } // ------------------------------------------------------- Listing.prototype.make_result_tab = function(nr) { if(this.result_tab)return; if(!this.colwidths) alert("Listing: Must specify column widths"); var has_headrow = this.colnames ? 1 : 0; if(nr==null)nr = this.page_len; nr += has_headrow; var nc = this.colwidths.length; var t=make_table(this.results, nr, nc, (this.opts.table_width ? this.opts.table_width : '100%'), this.colwidths,{padding:'0px'}); t.className = 'srs_result_tab'; this.result_tab = t; $y(t,{borderCollapse:'collapse'}); if(this.opts.table_width) { $y(this.results, {overflowX:'auto'}); $y(t,{tableLayout:'fixed'}); } // display headings if(has_headrow) { this.make_headings(t,nr,nc); // hilight sorted cell if(this.sort_by && this.sort_heads[this.sort_by]) { this.sort_heads[this.sort_by].set_sorting_as(this.sort_order); } } // style this.set_table_style(); if(this.opts.no_border == 1) { $y(t,{border:'0px'}); } this.result_tab = t; } // ------------------------------------------------------- Listing.prototype.set_table_style = function() { // set style var t = this.result_tab; for(var ri=(this.colnames?1:0); ri