From 2e73b1a188ea8d9130a23c615da2a3e4d7b45547 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Wed, 5 Mar 2014 16:39:33 +0530 Subject: [PATCH] Load count for each section in ModuleView --- frappe/public/js/frappe/views/moduleview.js | 36 +++++++++-- frappe/widgets/moduleview.py | 71 ++++++++++++++++----- 2 files changed, 84 insertions(+), 23 deletions(-) diff --git a/frappe/public/js/frappe/views/moduleview.js b/frappe/public/js/frappe/views/moduleview.js index 1bb3a3ec08..3f826a35ab 100644 --- a/frappe/public/js/frappe/views/moduleview.js +++ b/frappe/public/js/frappe/views/moduleview.js @@ -36,6 +36,7 @@ frappe.views.show_open_count_list = function(element) { frappe.views.moduleview.ModuleView = Class.extend({ init: function(wrapper, module) { + this.module = module; this.prepare(wrapper, module); this.make(wrapper, module); }, @@ -92,8 +93,9 @@ frappe.views.moduleview.ModuleView = Class.extend({ me.add_items(d, $layout); }); + me.item_count = message.item_count; me.add_reports(message.reports, $layout); - me.show_counts(message.item_count, $layout); + me.show_counts($layout); me.setup_navigation($layout); // for refresh @@ -122,6 +124,7 @@ frappe.views.moduleview.ModuleView = Class.extend({ ') .attr("data-label", d._label) + .attr("data-section-label", d.label) .appendTo($sections); // create content pane for this nav @@ -218,13 +221,17 @@ frappe.views.moduleview.ModuleView = Class.extend({ this.add_items(reports_section, $layout); }, - show_counts: function(item_count, $layout) { + show_counts: function($layout) { + var me = this; // total count - $.each(item_count, function(doctype, count) { - $layout.find("[data-doctype-count='"+doctype+"']") - .html(count) - .addClass("badge badge-count pull-right") - .css({cursor:"pointer"}); + $.each(this.item_count, function(label, counts) { + if(!counts) return false; + $.each(counts, function(doctype, count) { + $layout.find("[data-doctype-count='"+doctype+"']") + .html(count) + .addClass("badge badge-count pull-right") + .css({cursor:"pointer"}); + }); }); // open count @@ -244,6 +251,21 @@ frappe.views.moduleview.ModuleView = Class.extend({ $(this).parent().addClass("active"); $layout.find(".panel").toggle(false); $layout.find('[data-content-label="'+ $(this).parent().attr("data-label") +'"]').toggle(true); + + var section_label = $(this).parent().attr("data-section-label"); + if(!me.item_count || me.item_count[section_label]==null) { + frappe.call({ + "method": "frappe.widgets.moduleview.get_section_count", + "args": { + "module": me.module, + "section_label": section_label, + }, + "callback": function(r) { + me.item_count[section_label] = r.message || {}; + me.show_counts($layout); + } + }); + } }); $sections.find('a:first').trigger("click"); diff --git a/frappe/widgets/moduleview.py b/frappe/widgets/moduleview.py index 27b119de28..fb70c81617 100644 --- a/frappe/widgets/moduleview.py +++ b/frappe/widgets/moduleview.py @@ -6,22 +6,33 @@ import frappe, json from frappe.widgets import reportview from frappe.utils import cint from frappe import _ +from copy import deepcopy @frappe.whitelist() def get(module): data = get_data(module) + + out = { + "data": data, + "item_count": { + data[0]["label"]: get_section_count(section=data[0]) + }, + "reports": get_report_list(module) + } + + return out + +def get_data(module): + data = build_config_from_file(module) + if not data: data = build_standard_config(module) - doctypes = get_doctypes(data) + data = combine_common_sections(data) + + return data - return { - "data": data, - "item_count": get_count(doctypes), - "reports": get_report_list(module) - } - -def get_data(module): +def build_config_from_file(module): data = [] module = frappe.scrub(module) @@ -70,9 +81,21 @@ def build_standard_config(module): return data +def combine_common_sections(data): + sections = [] + sections_dict = {} + for each in data: + if each["label"] not in sections_dict: + sections_dict[each["label"]] = each + sections.append(each) + + sections_dict[each["label"]]["items"] += each["items"] + + return sections + def get_config(app, module): config = frappe.get_module("{app}.config.{module}".format(app=app, module=module)) - return config.get_data() if hasattr(config, "get_data") else config.data + return deepcopy(config.get_data() if hasattr(config, "get_data") else config.data) def add_setup_section(config, app, module, label, icon): try: @@ -91,16 +114,32 @@ def get_setup_section(app, module, label, icon): "icon": icon, "items": section["items"] } + +@frappe.whitelist() +def get_section_count(section=None, module=None, section_label=None): + doctypes = [] + if module and section_label: + data = get_data(module) + for each in data: + if each["label"] == section_label: + section = each + break -def get_doctypes(data): + if section: + doctypes = get_doctypes(section) + + count = get_count(doctypes) + + return count + +def get_doctypes(section): doctypes = [] - for section in data: - for item in section.get("items", []): - if item.get("type")=="doctype": - doctypes.append(item["name"]) - elif item.get("doctype"): - doctypes.append(item["doctype"]) + for item in section.get("items", []): + if item.get("type")=="doctype": + doctypes.append(item["name"]) + elif item.get("doctype"): + doctypes.append(item["doctype"]) return list(set(doctypes))