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.
 
 
 
 
 
 

268 lines
7.8 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. // MIT License. See license.txt
  3. wn.provide("wn.views.moduleview");
  4. wn.provide("wn.module_page");
  5. wn.views.moduleview.make = function(wrapper, module) {
  6. wrapper.module_view = new wn.views.moduleview.ModuleView(wrapper, module);
  7. wrapper.refresh = function() {
  8. // remake on refresh
  9. if((new Date() - wrapper.module_view.created_on) > (180 * 1000)) {
  10. wrapper.module_view = new wn.views.moduleview.ModuleView(wrapper, module);
  11. }
  12. }
  13. }
  14. wn.views.show_open_count_list = function(element) {
  15. var doctype = $(element).attr("data-doctype");
  16. var condition = wn.boot.notification_info.conditions[doctype];
  17. if(condition) {
  18. wn.route_options = condition;
  19. var route = wn.get_route()
  20. if(route[0]==="List" && route[1]===doctype) {
  21. wn.pages["List/" + doctype].doclistview.refresh();
  22. } else {
  23. wn.set_route("List", doctype);
  24. }
  25. }
  26. }
  27. wn.views.moduleview.ModuleView = Class.extend({
  28. init: function(wrapper, module) {
  29. this.doctypes = [];
  30. this.top_item_total = {};
  31. this.top_item_open = {};
  32. $(wrapper).empty();
  33. wn.ui.make_app_page({
  34. parent: wrapper,
  35. single_column: true,
  36. title: wn._(wn.modules[module] && wn.modules[module].label || module)
  37. });
  38. wrapper.appframe.add_module_icon(module);
  39. wrapper.appframe.set_title_left('<i class="icon-angle-left"></i> Home', function() { wn.set_route(""); });
  40. this.wrapper = wrapper;
  41. this.module = module;
  42. this.make_body();
  43. this.render_static();
  44. this.render_dynamic();
  45. this.created_on = new Date();
  46. var me = this;
  47. $(document).on("notification-update", function() {
  48. me.update_open_count();
  49. });
  50. },
  51. make_body: function() {
  52. var wrapper = this.wrapper;
  53. // make columns
  54. $(wrapper).find(".layout-main").html("<div class='module-top'></div>\
  55. <div class='row'>\
  56. <div class='col-md-6 main-section'></div>\
  57. <div class='col-md-6 side-section'></div>\
  58. </div>")
  59. $(wrapper).on("click", ".badge-important", function() {
  60. wn.views.show_open_count_list(this);
  61. });
  62. $(wrapper).on("click", ".badge-count", function() {
  63. var doctype = $(this).attr("data-doctype-count");
  64. wn.set_route("List", doctype);
  65. });
  66. },
  67. add_section: function(section) {
  68. section._title = wn._(section.title);
  69. if(section.top) {
  70. var module_top = $(this.wrapper).find(".module-top");
  71. var list_group = $('<div class="row">')
  72. .appendTo(module_top);
  73. $('<hr class="row">')
  74. .insertAfter(module_top);
  75. } else {
  76. var list_group = $('<ul class="list-group">\
  77. <li class="list-group-item">\
  78. <h4 class="list-group-item-heading" style="margin-bottom: 0px;">\
  79. <i class="text-muted '+ section.icon+'"></i> '
  80. + wn._(section.title) +'</h4>\
  81. </li>\
  82. </ul>"').appendTo(section.right
  83. ? $(this.wrapper).find(".side-section")
  84. : $(this.wrapper).find(".main-section"));
  85. }
  86. section.list_group = list_group;
  87. },
  88. add_item: function(item, section) {
  89. if(!item.description) item.description = "";
  90. if(item.count==null) item.count = "";
  91. if(!item.icon) item.icon = "";
  92. if(section.top) {
  93. var $parent = $(repl('<div class="col-sm-4">\
  94. <div class="alert alert-info alert-badge"></div></div>'))
  95. .appendTo(section.list_group)
  96. .find(".alert-badge");
  97. this.top_item_total[item.doctype] = 0;
  98. } else {
  99. var $parent = $('<li class="list-group-item">').appendTo(section.list_group);
  100. }
  101. $(repl('%(icon)s<span' +
  102. ((item.doctype && item.description)
  103. ? " data-doctype='"+item.doctype+"'"
  104. : "") + ">%(link)s</span>"
  105. + ((item.description && !section.top)
  106. ? " <span class='text-muted small'>%(description)s</span>"
  107. : "")
  108. + ((section.right || !item.doctype)
  109. ? ''
  110. : '<span data-doctype-count="%(doctype)s" style="margin-left: 2px;"></span>'), item))
  111. .appendTo($parent);
  112. if(!section.top) {
  113. $('<span class="clearfix"></span>').appendTo($parent);
  114. }
  115. },
  116. set_top_item_count: function(doctype, count, open_count) {
  117. return;
  118. var me = this;
  119. if(this.top_item_total[doctype]!=null) {
  120. if(count!=null)
  121. this.top_item_total[doctype] = count;
  122. if(open_count!=null)
  123. this.top_item_open[doctype] = open_count;
  124. var maxtop = Math.max.apply(this, values(this.top_item_total));
  125. $.each(this.top_item_total, function(doctype, item_count) {
  126. $(me.wrapper).find(".module-item-progress[data-doctype='"+ doctype +"']")
  127. .find(".module-item-progress-total")
  128. .css("width", cint(flt(item_count)/maxtop*100) + "%")
  129. })
  130. $.each(this.top_item_open, function(doctype, item_count) {
  131. $(me.wrapper).find(".module-item-progress[data-doctype='"+ doctype +"']")
  132. .find(".module-item-progress-open")
  133. .css("width", cint(flt(item_count)/me.top_item_total[doctype]*100) + "%")
  134. })
  135. }
  136. },
  137. render_static: function() {
  138. // render sections
  139. var me = this;
  140. $.each(wn.module_page[this.module], function(i, section) {
  141. me.add_section(section);
  142. $.each(section.items, function(i, item) {
  143. if(item.doctype) {
  144. me.doctypes.push(item.doctype);
  145. item.icon = '<i class="icon-fixed-width '+ wn.boot.doctype_icons[item.doctype] + '"></i> ';
  146. }
  147. if(item.doctype && !item.route) {
  148. item.route = "List/" + encodeURIComponent(item.doctype);
  149. }
  150. if(item.page && !item.route) {
  151. item.route = item.page;
  152. }
  153. if(item.page) {
  154. item.icon = '<i class="icon-fixed-width '+ wn.boot.doctype_icons[item.page] + '"></i> ';
  155. }
  156. // link
  157. item.link = repl("<a href='#%(route)s'>%(label)s</a>", item);
  158. // doctype permissions
  159. if(item.doctype && !wn.model.can_read(item.doctype)) {
  160. //item.link = item.label;
  161. return;
  162. }
  163. // page permissions
  164. if(item.page && !in_list(wn.boot.allowed_pages, item.page)) {
  165. //item.link = item.label;
  166. return;
  167. }
  168. if((item.country && wn.boot.control_panel.country==item.country)
  169. || !item.country)
  170. me.add_item(item, section)
  171. });
  172. if(section.list_group.find("li").length==1) {
  173. section.list_group.toggle(false);
  174. }
  175. });
  176. },
  177. render_dynamic: function() {
  178. // render reports
  179. var me = this;
  180. return wn.call({
  181. method: "webnotes.widgets.moduleview.get_data",
  182. args: {
  183. module: me.module,
  184. doctypes: me.doctypes
  185. },
  186. callback: function(r) {
  187. if(r.message) {
  188. // reports
  189. if(r.message.reports.length) {
  190. var section = {
  191. title: wn._("Custom Reports"),
  192. right: true,
  193. icon: "icon-list",
  194. }
  195. me.add_section(section);
  196. $.each(r.message.reports, function(i, item) {
  197. if(wn.model.can_read(item.doctype)) {
  198. item.icon = '<i class="icon-fixed-width '
  199. + wn.boot.doctype_icons[item.doctype] + '"></i> ';
  200. if(item.is_query_report) {
  201. item.link = repl("<a href=\"#query-report/%(name)s\">%(name)s</a>",
  202. item);
  203. } else {
  204. item.link = repl("<a href=\"#Report/%(doctype)s/%(name)s\">\
  205. %(name)s</a>", item);
  206. }
  207. me.add_item(item, section);
  208. }
  209. })
  210. }
  211. // counts
  212. if(r.message.item_count) {
  213. $.each(r.message.item_count, function(doctype, count) {
  214. $(me.wrapper).find("[data-doctype-count='"+doctype+"']")
  215. .html(count)
  216. .addClass("badge badge-count pull-right")
  217. .css({cursor:"pointer"});
  218. me.set_top_item_count(doctype, count)
  219. })
  220. }
  221. // open-counts
  222. me.update_open_count();
  223. }
  224. }
  225. });
  226. },
  227. update_open_count: function() {
  228. var me = this;
  229. $(me.wrapper).find(".badge-important").remove();
  230. if(wn.boot.notification_info.open_count_doctype) {
  231. $.each(wn.boot.notification_info.open_count_doctype, function(doctype, count) {
  232. if(count && in_list(me.doctypes, doctype)) {
  233. me.set_top_item_count(doctype, null, count);
  234. $('<span>')
  235. .css({
  236. "cursor": "pointer",
  237. "margin-right": "0px"
  238. })
  239. .addClass("badge badge-important pull-right")
  240. .html(count)
  241. .attr("data-doctype", doctype)
  242. .insertAfter($(me.wrapper)
  243. .find("[data-doctype-count='"+doctype+"']"));
  244. }
  245. })
  246. }
  247. }
  248. });