// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors // MIT License. See license.txt wn.provide('wn.views'); // opts: // stats = list of fields // doctype // parent // set_filter = function called on click wn.views.SidebarStats = Class.extend({ init: function(opts) { $.extend(this, opts); this.wrapper = $("
").css({"padding-bottom": "15px"}).appendTo(this.parent); this.get_stats(); }, get_stats: function() { var me = this return wn.call({ type: "GET", method: 'webnotes.widgets.reportview.get_stats', args: { stats: me.stats, doctype: me.doctype }, callback: function(r) { // This gives a predictable stats order $.each(me.stats, function(i, v) { me.render_stat(v, (r.message || {})[v]); }); // reload button at the end if(me.stats.length) { $(' '+wn._('Refresh')+'') .css({"margin-top":"15px", "display":"inline-block"}) .click(function() { me.reload_stats(); return false; }).appendTo($('
') .appendTo(me.wrapper)); } me.doclistview.set_sidebar_height(); } }); }, render_stat: function(field, stat) { var me = this; if(!stat || !stat.length) { if(field==='_user_tags') { $('
\
'+wn._('Tags')+'
\
\
'+wn._('No records tagged.')+'
' +'
\
').appendTo(this.wrapper); } return; } var label = wn.meta.docfield_map[this.doctype][field] ? wn.meta.docfield_map[this.doctype][field].label : field; if(label==='_user_tags') label = 'Tags'; // grid var $w = $('
\
'+ wn._(label) +'
\
\
\
'); // sort items stat = stat.sort(function(a, b) { return b[1] - a[1] }); var sum = 0; $.each(stat, function(i,v) { sum = sum + v[1]; }) // render items $.each(stat, function(i, v) { me.render_stat_item(i, v, sum, field).appendTo($w.find('.side-panel-body')); }); $w.appendTo(this.wrapper); }, render_stat_item: function(i, v, max, field) { var me = this; var args = {} args.label = v[0]; args._label = wn._(v[0]); args.width = flt(v[1]) / max * 100; args.count = v[1]; args.field = field; args.bar_style = ""; $item = $(repl('
\
\
\
\ \ %(_label)s (%(count)s)\
', args)); this.setup_stat_item_click($item); return $item; }, reload_stats: function() { this.wrapper.empty(); this.get_stats(); }, setup_stat_item_click: function($item) { var me = this; $item.find('a').click(function() { var fieldname = $(this).attr('data-field'); var label = $(this).attr('data-label'); me.set_filter(fieldname, label); return false; }); }, });