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.
 
 
 
 
 
 

120 line
3.2 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. wn.provide('wn.views');
  4. // opts:
  5. // stats = list of fields
  6. // doctype
  7. // parent
  8. // set_filter = function called on click
  9. wn.views.SidebarStats = Class.extend({
  10. init: function(opts) {
  11. $.extend(this, opts);
  12. this.wrapper = $("<div>").css({"padding-bottom": "15px"}).appendTo(this.parent);
  13. this.get_stats();
  14. },
  15. get_stats: function() {
  16. var me = this
  17. return wn.call({
  18. type: "GET",
  19. method: 'webnotes.widgets.reportview.get_stats',
  20. args: {
  21. stats: me.stats,
  22. doctype: me.doctype
  23. },
  24. callback: function(r) {
  25. // This gives a predictable stats order
  26. $.each(me.stats, function(i, v) {
  27. me.render_stat(v, (r.message || {})[v]);
  28. });
  29. // reload button at the end
  30. if(me.stats.length) {
  31. $('<a class="small"><i class="refresh"></i> '+wn._('Refresh')+'</a>')
  32. .css({"margin-top":"15px", "display":"inline-block"})
  33. .click(function() {
  34. me.reload_stats();
  35. return false;
  36. }).appendTo($('<div class="stat-wrapper">')
  37. .appendTo(me.wrapper));
  38. }
  39. me.doclistview.set_sidebar_height();
  40. }
  41. });
  42. },
  43. render_stat: function(field, stat) {
  44. var me = this;
  45. if(!stat || !stat.length) {
  46. if(field==='_user_tags') {
  47. $('<div class="side-panel">\
  48. <h5 class="text-muted"><i class="icon-tag"></i> '+wn._('Tags')+'</h5>\
  49. <div class="side-panel-body">\
  50. <div class="text-muted small"><i>'+wn._('No records tagged.')+'</i><br>'
  51. +'</div>\
  52. </div></div>').appendTo(this.wrapper);
  53. }
  54. return;
  55. }
  56. var label = wn.meta.docfield_map[this.doctype][field] ?
  57. wn.meta.docfield_map[this.doctype][field].label : field;
  58. if(label==='_user_tags') label = 'Tags';
  59. // grid
  60. var $w = $('<div class="side-panel">\
  61. <h5 class="text-muted"><i class="icon-tag"></i> '+ wn._(label) +'</h5>\
  62. <div class="side-panel-body">\
  63. </div>\
  64. </div>');
  65. // sort items
  66. stat = stat.sort(function(a, b) { return b[1] - a[1] });
  67. var sum = 0;
  68. $.each(stat, function(i,v) { sum = sum + v[1]; })
  69. // render items
  70. $.each(stat, function(i, v) {
  71. me.render_stat_item(i, v, sum, field).appendTo($w.find('.side-panel-body'));
  72. });
  73. $w.appendTo(this.wrapper);
  74. },
  75. render_stat_item: function(i, v, max, field) {
  76. var me = this;
  77. var args = {}
  78. args.label = v[0];
  79. args._label = wn._(v[0]);
  80. args.width = flt(v[1]) / max * 100;
  81. args.count = v[1];
  82. args.field = field;
  83. args.bar_style = "";
  84. $item = $(repl('<div class="progress">\
  85. <div class="progress-bar %(bar_style)s" style="width: %(width)s%"></div>\
  86. </div>\
  87. <div class="stat-label" style="margin-top: -19px; text-align: center; \
  88. margin-bottom: 5px; font-size: 80%;">\
  89. <a href="#" data-label="%(label)s" data-field="%(field)s">\
  90. %(_label)s</a> (%(count)s)\
  91. </div>', args));
  92. this.setup_stat_item_click($item);
  93. return $item;
  94. },
  95. reload_stats: function() {
  96. this.wrapper.empty();
  97. this.get_stats();
  98. },
  99. setup_stat_item_click: function($item) {
  100. var me = this;
  101. $item.find('a').click(function() {
  102. var fieldname = $(this).attr('data-field');
  103. var label = $(this).attr('data-label');
  104. me.set_filter(fieldname, label);
  105. return false;
  106. });
  107. },
  108. });