2
0
mirror of https://github.com/frappe/books.git synced 2024-12-24 11:55:46 +00:00
books/client/view/tree.js

181 lines
5.1 KiB
JavaScript
Raw Normal View History

const frappe = require('frappejs');
const BaseList = require('./list');
const Tree = require('frappejs/client/components/tree');
// const keyboard = require('frappejs/client/ui/keyboard');
module.exports = class BaseTree extends BaseList {
init() {
this.meta = frappe.getMeta(this.doctype);
this.body = null;
this.data = [];
this.setupTreeSettings();
frappe.db.on(`change:${this.doctype}`, (params) => {
this.refresh();
});
}
setupTreeSettings() {
// tree settings that can be overridden by meta
this.treeSettings = {
parentField: `parent${this.doctype}`
}
if (this.meta.treeSettings) {
Object.assign(this.treeSettings, this.meta.treeSettings);
}
}
async refresh() {
return await this.run();
}
async run() {
this.makeBody();
this.body.innerHTML = '';
this.dirty = false;
const rootLabel = this.treeSettings.getRootLabel ?
await this.treeSettings.getRootLabel() :
this.doctype;
this.renderTree(rootLabel);
this.trigger('state-change');
}
makeBody() {
if (!this.body) {
this.makeToolbar();
this.parent.classList.add('tree-page');
this.body = frappe.ui.add('div', 'tree-body', this.parent);
this.body.setAttribute('data-doctype', this.doctype);
this.bindKeys();
}
}
renderTree(rootLabel) {
2018-04-09 12:28:51 +00:00
this.rootNode = {
label: rootLabel,
2018-04-09 12:28:51 +00:00
value: rootLabel,
isRoot: true,
isGroup: true,
children: null
2018-04-09 12:28:51 +00:00
}
this.treeWrapper = frappe.ui.create(`
<f-tree>
${this.getTreeNodeHTML(this.rootNode)}
2018-04-09 12:28:51 +00:00
</f-tree>
`);
const rootNode = this.treeWrapper.querySelector('f-tree-node[is-root]');
rootNode.props = this.rootNode;
2018-04-09 12:28:51 +00:00
this.body.appendChild(this.treeWrapper);
frappe.ui.on(this.treeWrapper, 'tree-node-expand', 'f-tree-node', async (e, treeNode) => {
if (!treeNode.expanded) return;
if (!treeNode.props.children) {
const data = await this.getData(treeNode.props);
const children = data.map(d => ({
label: d.name,
value: d.name,
isGroup: d.isGroup,
doc: d
}));
treeNode.props.children = children;
for (let child of children) {
const childNode = frappe.ui.create(this.getTreeNodeHTML(child));
childNode.props = child;
treeNode.appendChild(childNode);
}
}
});
2018-04-09 12:28:51 +00:00
frappe.ui.on(this.treeWrapper, 'tree-node-action', 'f-tree-node', (e, treeNode) => {
if (treeNode.isRoot) return;
2018-04-09 12:28:51 +00:00
const button = e.detail.actionEl;
const action = button.getAttribute('data-action');
2018-04-09 12:28:51 +00:00
if (action === 'edit') {
this.edit(treeNode.props.doc.name);
2018-04-19 10:14:07 +00:00
} else if (action === 'addChild') {
this.addChildNode(treeNode.props.doc.name);
}
});
2018-04-09 12:28:51 +00:00
rootNode.click(); // open the root node
}
2018-04-09 12:28:51 +00:00
edit(name) {
frappe.desk.showFormModal(this.doctype, name);
}
2018-04-19 10:14:07 +00:00
async addChildNode(name) {
const newDoc = await frappe.getNewDoc(this.doctype);
const formModal = await frappe.desk.showFormModal(this.doctype, newDoc.name);
const parentField = this.treeSettings.parentField;
if (formModal.form.doc.meta.hasField(parentField)) {
formModal.form.doc.set(parentField, name);
}
}
async getData(node) {
let fields = this.getFields();
let filters = {};
if (node.isRoot) {
filters[this.treeSettings.parentField] = '';
} else {
filters[this.treeSettings.parentField] = node.value;
}
return await frappe.db.getAll({
doctype: this.doctype,
fields,
filters,
order_by: 'name',
order: 'asc'
});
}
getTreeNodeHTML(node) {
return (
`<f-tree-node
label="${node.label}"
value="${node.value}"
${node.expanded ? 'expanded' : ''}
${node.isRoot ? 'is-root' : ''}
${node.isGroup ? '' : 'is-leaf'}
>
${this.getActionButtonsHTML()}
</f-tree-node>`
);
}
getActionButtonsHTML() {
return [
2018-04-19 10:14:07 +00:00
{ id: 'edit', label: frappe._('Edit') },
{ id: 'addChild', label: frappe._('Add Child') },
// { id: 'delete', label: frappe._('Delete') },
].map(button => {
return `<button class="btn btn-link btn-sm m-0" slot="actions" data-action="${button.id}">
${button.label}
</button>`;
})
.join('');
}
getFields() {
let fields = [this.treeSettings.parentField, 'isGroup']
this.updateStandardFields(fields);
return fields;
}
};