2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00
books/client/desk/index.js

105 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-01-16 06:09:17 +00:00
const frappe = require('frappejs');
2018-02-08 09:38:47 +00:00
// const Search = require('./search');
2018-01-16 06:09:17 +00:00
const Router = require('frappejs/common/router');
const Page = require('frappejs/client/view/page');
2018-01-30 12:03:04 +00:00
const FormPage = require('frappejs/client/desk/formpage');
const ListPage = require('frappejs/client/desk/listpage');
2018-01-23 08:00:29 +00:00
const Navbar = require('./navbar');
2018-01-12 12:25:07 +00:00
module.exports = class Desk {
constructor() {
frappe.router = new Router();
frappe.router.listen();
2018-01-12 12:25:07 +00:00
2018-01-23 08:00:29 +00:00
let body = document.querySelector('body');
this.navbar = new Navbar();
this.container = frappe.ui.add('div', 'container-fluid', body);
2018-01-12 12:25:07 +00:00
2018-02-09 12:55:55 +00:00
this.containerRow = frappe.ui.add('div', 'row', this.container)
this.sidebar = frappe.ui.add('div', 'col-md-2 p-3 sidebar d-none d-md-block', this.containerRow);
this.sidebarList = frappe.ui.add('div', 'list-group list-group-flush', this.sidebar);
this.body = frappe.ui.add('div', 'col-md-10 p-4 main', this.containerRow);
2018-01-12 12:25:07 +00:00
this.pages = {
lists: {},
forms: {}
};
2018-02-09 12:55:55 +00:00
this.routeItems = {};
this.initRoutes();
2018-01-12 12:25:07 +00:00
// this.search = new Search(this.nav);
}
2018-02-09 12:55:55 +00:00
initRoutes() {
2018-01-23 08:00:29 +00:00
frappe.router.add('not-found', async (params) => {
2018-02-09 12:55:55 +00:00
if (!this.notFoundPage) {
this.notFoundPage = new Page('Not Found');
2018-01-23 08:00:29 +00:00
}
2018-02-09 12:55:55 +00:00
await this.notFoundPage.show();
this.notFoundPage.renderError('Not Found', params ? params.route : '');
2018-01-23 08:00:29 +00:00
})
frappe.router.add('list/:doctype', async (params) => {
2018-02-09 12:55:55 +00:00
let page = this.getListPage(params.doctype);
await page.show(params);
});
2018-01-12 12:25:07 +00:00
frappe.router.add('edit/:doctype/:name', async (params) => {
2018-02-09 12:55:55 +00:00
let page = this.getFormPage(params.doctype);
await page.show(params);
2018-01-12 12:25:07 +00:00
})
frappe.router.add('new/:doctype', async (params) => {
2018-02-08 12:28:51 +00:00
let doc = await frappe.getNewDoc(params.doctype);
// unset the name, its local
2018-02-08 09:38:47 +00:00
await frappe.router.setRoute('edit', doc.doctype, doc.name);
await doc.set('name', '');
});
2018-02-09 12:55:55 +00:00
frappe.router.on('change', () => {
if (this.routeItems[window.location.hash]) {
this.setActive(this.routeItems[window.location.hash]);
}
})
}
2018-02-09 12:55:55 +00:00
getListPage(doctype) {
if (!this.pages.lists[doctype]) {
2018-01-30 12:03:04 +00:00
this.pages.lists[doctype] = new ListPage(doctype);
}
return this.pages.lists[doctype];
}
2018-01-12 12:25:07 +00:00
2018-02-09 12:55:55 +00:00
getFormPage(doctype) {
if (!this.pages.forms[doctype]) {
2018-01-30 12:03:04 +00:00
this.pages.forms[doctype] = new FormPage(doctype);
}
return this.pages.forms[doctype];
2018-01-12 12:25:07 +00:00
}
2018-02-09 12:55:55 +00:00
setActive(item) {
let className = 'list-group-item-secondary';
let activeItem = this.sidebarList.querySelector('.' + className);
if (activeItem) {
activeItem.classList.remove(className);
}
item.classList.add(className);
}
addSidebarItem(label, action) {
let item = frappe.ui.add('a', 'list-group-item list-group-item-action', this.sidebarList);
2018-01-12 12:25:07 +00:00
item.textContent = label;
if (typeof action === 'string') {
item.href = action;
2018-02-09 12:55:55 +00:00
this.routeItems[action] = item;
2018-01-12 12:25:07 +00:00
} else {
item.addEventHandler('click', () => {
action();
2018-02-09 12:55:55 +00:00
this.setActive(item);
2018-01-12 12:25:07 +00:00
});
}
}
}