fix(desk): links customization by fetching properties

This commit is contained in:
Prateeksha Singh 2019-03-04 07:48:49 +05:30
parent 75d98df16c
commit d2bde40992
5 changed files with 185 additions and 117 deletions

View file

@ -2,7 +2,7 @@ from __future__ import unicode_literals
from frappe import _
import frappe
import json
from frappe.desk.moduleview import get_data, get_onboard_items, config_exists
from frappe.desk.moduleview import get_data, get_onboard_items, config_exists, get_module_link_items_from_list
from six import iteritems
def get_modules_from_all_apps_for_user(user=None):
@ -22,8 +22,14 @@ def get_modules_from_all_apps_for_user(user=None):
module_name = module["module_name"]
if module_name in empty_tables_by_module:
module["onboard_present"] = 1
if module_name not in home_settings[module["category"]]:
category_settings = home_settings[module["category"]]
if module_name not in category_settings:
module["hidden"] = 1
else:
links = category_settings[module_name]["links"]
if links:
module["links"] = get_module_link_items_from_list(module["app"], module_name, links.split(","))
return allowed_modules_list
@ -71,7 +77,6 @@ def get_modules_from_app(app):
to_add = False
if to_add:
m["shortcuts"] = get_onboard_items(app, frappe.scrub(m["module_name"]))[:5]
m["app"] = app
active_modules_list.append(m)

View file

@ -3,6 +3,7 @@
from __future__ import unicode_literals
import frappe
import json
from frappe import _
from frappe.boot import get_allowed_pages, get_allowed_reports
from frappe.desk.doctype.desktop_icon.desktop_icon import set_hidden, clear_desktop_icons_cache
@ -296,13 +297,38 @@ def get_links(app, module):
link_names = []
for section in sections:
for item in section["items"]:
link_names.append(item.get("label"))
print(link_names)
return link_names
@frappe.whitelist()
def get_module_link_items_from_dict(module_link_list_map):
module_link_list_map = json.loads(module_link_list_map)
module_links = {}
for module, data in module_link_list_map.items():
print(data)
module_links[module] = get_module_link_items_from_list(data["app"], module, data["links"])
return module_links
def get_module_link_items_from_list(app, module, list_of_link_names):
try:
sections = get_config(app, frappe.scrub(module))
except ImportError:
return []
links = []
for section in sections:
for item in section["items"]:
if item.get("label", "") in list_of_link_names:
links.append(item)
return links
def set_last_modified(data):
for section in data:
for item in section["items"]:

View file

@ -3,6 +3,7 @@ from __future__ import unicode_literals
import frappe
import json
from frappe.config import get_modules_from_all_apps_for_user
from frappe.desk.moduleview import get_onboard_items
def execute():
frappe.reload_doc("core", "doctype", "user")
@ -11,8 +12,9 @@ def execute():
settings = {}
for idx, m in enumerate(all_modules):
links = get_onboard_items(m["app"], frappe.scrub(m["module_name"]))[:5]
module_settings = {
"links": ",".join([d["label"] for d in m.get("shortcuts")])
"links": ",".join([d["label"] for d in links])
}
category_dict = settings.get(m.get("category", ""), None)
if category_dict:

View file

@ -22,10 +22,10 @@
<!-- <span class="drag-handle octicon octicon-three-bars text-extra-muted"></span> -->
</div>
<p
v-if="shortcuts.length"
v-if="links && links.length"
class="small text-muted">
<a
v-for="shortcut in shortcuts"
v-for="shortcut in links"
:key="shortcut.name"
:href="shortcut.link"
class="btn btn-default btn-xs shortcut-tag" title="toggle Tag"> {{ shortcut.label }}
@ -38,7 +38,7 @@
<script>
export default {
props: ['index', 'name', 'label', 'type', 'module_name', 'link', 'count', 'onboard_present', 'shortcuts', 'description', 'hidden'],
props: ['index', 'name', 'label', 'type', 'module_name', 'link', 'count', 'onboard_present', 'links', 'description', 'hidden'],
data() {
return {
hovered: 0

View file

@ -35,24 +35,166 @@ export default {
DeskModuleBox
},
data() {
let template_modules = this.all_modules;
// TODO: make routes on template modules
return {
template_modules: this.all_modules,
modules: this.all_modules.slice(),
template_modules: template_modules,
modules: template_modules.slice(),
settings: {},
all_settings: {},
dragged_index: -1,
hovered_index: -1,
}
},
methods: {
show_customize_dialog() {
if(!this.dialog) {
this.get_settings()
.then(() => {
const fields = this.make_fields();
this.make_and_show_dialog(fields);
});
} else {
this.dialog.show();
}
},
get_settings() {
return frappe.db.get_value('User', user, 'home_settings')
.then(resp => {
this.all_settings = JSON.parse(resp.message['home_settings']);
this.settings = this.all_settings[this.category];
});
},
make_fields() {
let fields = [];
let template_modules = this.template_modules;
let selected_modules = Object.keys(this.settings);
template_modules.forEach(module => {
fields.push(this.get_module_select_field(module, selected_modules));
if(module.links) {
fields.push(this.get_links_multiselect_field(module));
}
});
return fields;
},
make_and_show_dialog(fields) {
this.dialog = new frappe.ui.Dialog({
title: __("Customize " + this.category),
fields: fields,
primary_action_label: __("Update"),
primary_action: (values) => {
let module_link_list_map = {};
Object.keys(values).forEach(module_name => {
if(!module_name.includes('links') && values[module_name]) {
const links_str = values[module_name + '_links'] || '';
this.settings[module_name]["links"] = links_str;
if(values[module_name]) {
module_link_list_map[module_name] = {
links: links_str.split(","),
app: this.template_modules.filter(m => m.module_name === module_name)[0].app
};
}
}
});
frappe.db.set_value('User', user, 'home_settings', this.all_settings)
.then((resp) => {
this.update_modules(module_link_list_map);
this.dialog.hide();
})
.fail((err) => {
frappe.msgprint(err);
});
}
});
this.dialog.modal_body.find('.clearfix').css({'display': 'none'});
this.dialog.modal_body.find('.frappe-control*[data-fieldtype="MultiSelect"]').css({'margin-bottom': '30px'});
this.dialog.show();
},
update_modules(module_link_list_map) {
frappe.call({
type: "GET",
method:'frappe.desk.moduleview.get_module_link_items_from_dict',
freeze: true,
args: {
module_link_list_map: module_link_list_map
},
callback: (r) => {
const module_links_dict = r.message;
this.template_modules.map((m, i) => {
// TODO: make routes on template modules
let raw_links = module_links_dict[m.module_name];
if(Object.keys(module_link_list_map).includes(m.module_name)) {
m.hidden = 0;
m.links = raw_links;
} else {
m.hidden = 1;
}
});
this.modules = this.template_modules.filter(m => !m.hidden);
}
});
},
get_module_select_field(module, selected_modules) {
return {
label: __(module.module_name),
fieldname: module.module_name,
fieldtype: "Check",
default: selected_modules.includes(module.module_name) ? 1 : 0
}
},
get_links_multiselect_field(module) {
return {
label: __(""),
fieldname: module.module_name + "_links",
fieldtype: "MultiSelect",
get_data: function() {
let data = [];
frappe.call({
type: "GET",
method:'frappe.desk.moduleview.get_links',
async: false,
no_spinner: true,
args: {
app: module.app,
module: module.module_name,
},
callback: function(r) {
data = r.message;
}
});
return data;
},
default: module.links.map(m => (m.name || m.label)),
depends_on: module.module_name
};
},
box_dragstart(index) {
this.dragged_index = index;
},
box_dragend(index) {
this.dragged_index = -1;
this.hovered_index = -1;
},
box_enter(index) {
this.hovered_index = index;
},
box_drop(index) {
let d = this.dragged_index;
let h = this.hovered_index;
@ -61,113 +203,6 @@ export default {
this.modules.splice(d, 1);
}
},
show_customize_dialog() {
if(!this.dialog) {
let all_modules = this.modules;
let fields = [];
const fieldname = 'home_settings';
frappe.db.get_value('User', user, fieldname)
.then((resp) => {
let home_settings = JSON.parse(resp.message[fieldname]);
let settings = home_settings[this.category];
let selected_modules = Object.keys(settings);
all_modules.forEach(module => {
fields.push({
label: __(module.module_name),
fieldname: module.module_name,
fieldtype: "Check",
default: selected_modules.includes(module.module_name) ? 1 : 0
});
if(module.shortcuts) {
fields.push({
label: __(""),
fieldname: module.module_name + "_shortcuts",
fieldtype: "MultiSelect",
get_data: function() {
let data = [];
frappe.call({
type: "GET",
method:'frappe.desk.moduleview.get_links',
async: false,
no_spinner: true,
args: {
app: module.app,
module: module.module_name,
},
callback: function(r) {
data = r.message;
}
});
return data;
},
default: module.shortcuts.map(m => (m.name || m.label)),
depends_on: module.module_name
});
}
});
this.dialog = new frappe.ui.Dialog({
title: __("Customize " + this.category),
fields: fields,
primary_action_label: __("Update"),
primary_action: (values) => {
let settings = {};
this.template_modules.map((m, i) => {
m.hidden = 0;
})
Object.keys(values).forEach(key => {
if(!key.includes('shortcuts') && values[key]) {
settings[key] = {
links: values[key + '_shortcuts'] || []
}
}
if(!values[key]) {
this.template_modules.map((m, i) => {
if(m.module_name === key) {
m.hidden = 1;
}
})
}
});
this.modules = this.template_modules.filter(m => !m.hidden);
const user = frappe.session.user;
const fieldname = 'home_settings';
frappe.db.get_value('User', user, fieldname)
.then((resp) => {
let home_settings = JSON.parse(resp.message[fieldname]);
home_settings[this.category] = settings;
frappe.db.set_value('User', user, fieldname, home_settings)
.then((resp) => {
this.dialog.hide();
})
.fail((err) => {
frappe.msgprint(err);
});
})
}
});
this.dialog.modal_body.find('.clearfix').css({'display': 'none'});
this.dialog.modal_body.find('.frappe-control*[data-fieldtype="MultiSelect"]').css({'margin-bottom': '30px'});
this.dialog.show();
});
} else {
this.dialog.show();
}
}
}
}
</script>