From 80db1a715a8de19fdcd65a0b5b33aa178d79253f Mon Sep 17 00:00:00 2001 From: shariquerik Date: Wed, 12 May 2021 13:23:47 +0530 Subject: [PATCH] fix: Add New Card --- frappe/desk/desktop.py | 4 +- frappe/desk/doctype/workspace/workspace.py | 29 ++++++ .../public/js/frappe/widgets/widget_dialog.js | 93 +++++++++++++++++++ frappe/public/js/frappe/wiki_blocks/card.js | 65 +++++++------ 4 files changed, 163 insertions(+), 28 deletions(-) diff --git a/frappe/desk/desktop.py b/frappe/desk/desktop.py index 7f054d5c1f..c096f50b78 100644 --- a/frappe/desk/desktop.py +++ b/frappe/desk/desktop.py @@ -543,11 +543,11 @@ def save_new_widget(page, new_widgets): widgets = _dict(loads(new_widgets)) if widgets.chart: - original_page.charts = new_widget(widgets.chart, "Workspace Chart", "charts") + original_page.charts.extend(new_widget(widgets.chart, "Workspace Chart", "charts")) if widgets.shortcut: original_page.shortcuts.extend(new_widget(widgets.shortcut, "Workspace Shortcut", "shortcuts")) if widgets.card: - original_page.build_links_table_from_cards(widgets.card) + original_page.build_links_table_from_card(widgets.card) try: original_page.save(ignore_permissions=True) diff --git a/frappe/desk/doctype/workspace/workspace.py b/frappe/desk/doctype/workspace/workspace.py index 0934138821..20c9240188 100644 --- a/frappe/desk/doctype/workspace/workspace.py +++ b/frappe/desk/doctype/workspace/workspace.py @@ -100,6 +100,35 @@ class Workspace(Document): "is_query_report": link.get('is_query_report') }) + def build_links_table_from_card(self, config): + # Empty links table + # self.links = [] + # order = config.get('order') + # widgets = config.get('widgets') + + for idx, card in enumerate(config): + # card = widgets[name].copy() + links = loads(card.get('links')) + + self.append('links', { + "label": card.get('label'), + "type": "Card Break", + "icon": card.get('icon'), + "hidden": card.get('hidden') or False + }) + + for link in links: + self.append('links', { + "label": link.get('label'), + "type": "Link", + "link_type": link.get('link_type'), + "link_to": link.get('link_to'), + "onboard": link.get('onboard'), + "only_for": link.get('only_for'), + "dependencies": link.get('dependencies'), + "is_query_report": link.get('is_query_report') + }) + def disable_saving_as_standard(): return frappe.flags.in_install or \ diff --git a/frappe/public/js/frappe/widgets/widget_dialog.js b/frappe/public/js/frappe/widgets/widget_dialog.js index eefb78c29a..5bcaaf9e3e 100644 --- a/frappe/public/js/frappe/widgets/widget_dialog.js +++ b/frappe/public/js/frappe/widgets/widget_dialog.js @@ -124,6 +124,98 @@ class ChartDialog extends WidgetDialog { } } +class CardDialog extends WidgetDialog { + constructor(opts) { + super(opts); + } + + get_fields() { + let me = this; + return [ + { + fieldtype: "Data", + fieldname: "label", + label: "Label", + }, + { + fieldname: 'links', + fieldtype: 'Table', + label: __('Card Links'), + editable_grid: 1, + data: this.data || [], + get_data: () => { + return this.data || []; + }, + fields: [ + { + fieldname: "label", + fieldtype: "Data", + in_list_view: 1, + label: "Label" + }, + { + fieldname: "icon", + fieldtype: "Data", + label: "Icon" + }, + { + fieldname: "link_type", + fieldtype: "Select", + in_list_view: 1, + label: "Link Type", + options: ["DocType", "Page", "Report"], + onchange: (e) => { + me.link_to = e.currentTarget.value; + } + }, + { + fieldname: "link_to", + fieldtype: "Dynamic Link", + in_list_view: 1, + label: "Link To", + options: "link_type", + get_options: () => { + return me.link_to; + } + }, + { + fieldname: "column_break_7", + fieldtype: "Column Break" + }, + { + fieldname: "dependencies", + fieldtype: "Data", + label: "Dependencies" + }, + { + fieldname: "only_for", + fieldtype: "Link", + label: "Only for ", + options: "Country" + }, + { + default: "0", + fieldname: "onboard", + fieldtype: "Check", + label: "Onboard" + }, + { + default: "0", + fieldname: "is_query_report", + fieldtype: "Check", + label: "Is Query Report" + } + ], + }, + ]; + } + + process_data(data) { + data.label = data.label ? data.label : data.chart_name; + return data; + } +} + class ShortcutDialog extends WidgetDialog { constructor(opts) { super(opts); @@ -437,6 +529,7 @@ export default function get_dialog_constructor(type) { chart: ChartDialog, shortcut: ShortcutDialog, number_card: NumberCardDialog, + card: CardDialog, }; return widget_map[type] || WidgetDialog; diff --git a/frappe/public/js/frappe/wiki_blocks/card.js b/frappe/public/js/frappe/wiki_blocks/card.js index a859b04439..cc79a57c3e 100644 --- a/frappe/public/js/frappe/wiki_blocks/card.js +++ b/frappe/public/js/frappe/wiki_blocks/card.js @@ -1,3 +1,4 @@ +import get_dialog_constructor from "../widgets/widget_dialog.js"; export default class Card { static get toolbox() { return { @@ -22,22 +23,19 @@ export default class Card { this.pb = this.data.pb ? this.data.pb : "0"; this.pl = this.data.pl ? this.data.pl : "0"; this.allow_customization = !this.readOnly; + this.options = { + allow_sorting: this.allow_customization, + allow_create: this.allow_customization, + allow_delete: this.allow_customization, + allow_hiding: false, + allow_edit: false, + } } render() { - let me = this; this.wrapper = document.createElement('div'); - this._make_fieldgroup(this.wrapper, [{ - fieldtype: "Select", - label: "Card Name", - fieldname: "card_name", - options: this.config.page_data.cards.items.map(({ label }) => label), - change: function() { - if (this.value) { - me._make_cards(this.value) - } - } - }]); + this._new_card(); + if (this.data && this.data.card_name) { this._make_cards(this.data.card_name) } @@ -51,7 +49,8 @@ export default class Card { pt: this._getPadding("t"), pr: this._getPadding("r"), pb: this._getPadding("b"), - pl: this._getPadding("l") + pl: this._getPadding("l"), + new: this.new_card_widget } } @@ -64,6 +63,30 @@ export default class Card { e.classList.add("pl-" + this.pl) } + _new_card() { + const dialog_class = get_dialog_constructor('card'); + this.dialog = new dialog_class({ + label: this.label, + type: 'card', + primary_action: (widget) => { + widget.in_customize_mode = 1; + let wid = frappe.widget.make_widget({ + ...widget, + widget_type: 'links', + container: this.wrapper, + options: this.options, + }); + wid.customize(this.options); + this.wrapper.setAttribute("card_name", wid.label); + this.new_card_widget = wid.get_config(); + }, + }); + + if (!this.readOnly && this.data && !this.data.card_name) { + this.dialog.make(); + } + } + _getCol() { var e = 12, t = "col-12", @@ -135,26 +158,16 @@ export default class Card { return obj.label == card_name }); this.wrapper.innerHTML = ''; - this.sections = {}; card.in_customize_mode = !this.readOnly; - let cards = new frappe.widget.SingleWidgetGroup({ + let card_widget = new frappe.widget.SingleWidgetGroup({ container: this.wrapper, type: "links", - columns: 3, - options: { - allow_sorting: this.allow_customization, - allow_create: this.allow_customization, - allow_delete: this.allow_customization, - allow_hiding: false, - allow_edit: false, - }, + options: this.options, widgets: card }); - - this.sections["cards"] = cards; this.wrapper.setAttribute("card_name", card_name); if (!this.readOnly) { - this.sections["cards"].customize(); + card_widget.customize(); } } } \ No newline at end of file