2
0
mirror of https://github.com/frappe/books.git synced 2025-01-29 18:18:26 +00:00
books/client/desk/index.js

181 lines
5.9 KiB
JavaScript
Raw Normal View History

2018-01-16 11:39:17 +05:30
const frappe = require('frappejs');
2018-02-08 15:08:47 +05:30
// const Search = require('./search');
2018-01-16 11:39:17 +05:30
const Router = require('frappejs/common/router');
const Page = require('frappejs/client/view/page');
const views = {};
2018-03-05 22:15:21 +05:30
views.Form = require('./formpage');
views.List = require('./listpage');
views.Tree = require('./treepage');
2018-03-05 22:15:21 +05:30
views.Print = require('./printpage');
views.FormModal = require('./formmodal');
views.Table = require('./tablepage');
2018-02-14 22:14:50 +05:30
const DeskMenu = require('./menu');
2018-01-12 17:55:07 +05:30
module.exports = class Desk {
2018-02-14 22:14:50 +05:30
constructor(columns=2) {
2018-01-12 17:55:07 +05:30
frappe.router = new Router();
frappe.router.listen();
2018-01-12 17:55:07 +05:30
2018-01-23 13:30:29 +05:30
let body = document.querySelector('body');
2018-02-15 15:23:28 +05:30
//this.navbar = new Navbar();
frappe.ui.empty(body);
2018-02-15 15:23:28 +05:30
this.container = frappe.ui.add('div', '', body);
this.containerRow = frappe.ui.add('div', 'row no-gutters', this.container)
2018-02-14 22:14:50 +05:30
this.makeColumns(columns);
2018-01-12 17:55:07 +05:30
this.pages = {
formModals: {},
List: {}
};
2018-02-09 18:25:55 +05:30
this.routeItems = {};
this.initRoutes();
2018-01-12 17:55:07 +05:30
// this.search = new Search(this.nav);
}
2018-02-14 22:14:50 +05:30
makeColumns(columns) {
this.menu = null; this.center = null;
this.columnCount = columns;
if (columns === 3) {
this.makeMenu();
this.center = frappe.ui.add('div', 'col-md-4 desk-center', this.containerRow);
this.body = frappe.ui.add('div', 'col-md-6 desk-body', this.containerRow);
} else if (columns === 2) {
this.makeMenu();
this.body = frappe.ui.add('div', 'col-md-10 desk-body', this.containerRow);
} else if (columns === 1) {
this.makeMenuPage();
this.body = frappe.ui.add('div', 'col-md-12 desk-body', this.containerRow);
} else {
throw 'columns can be 1, 2 or 3'
}
}
makeMenu() {
this.menuColumn = frappe.ui.add('div', 'col-md-2 desk-menu', this.containerRow);
this.menu = new DeskMenu(this.menuColumn);
}
makeMenuPage() {
// make menu page for 1 column layout
this.menuPage = null;
}
2018-02-09 18:25:55 +05:30
initRoutes() {
2018-01-23 13:30:29 +05:30
frappe.router.add('not-found', async (params) => {
2018-02-09 18:25:55 +05:30
if (!this.notFoundPage) {
2018-02-14 22:14:50 +05:30
this.notFoundPage = new Page({title: 'Not Found'});
2018-01-23 13:30:29 +05:30
}
2018-02-09 18:25:55 +05:30
await this.notFoundPage.show();
this.notFoundPage.renderError('Not Found', params ? params.route : '');
2018-01-23 13:30:29 +05:30
})
frappe.router.add('list/:doctype', async (params) => {
await this.showViewPage('List', params.doctype);
});
2018-01-12 17:55:07 +05:30
frappe.router.add('tree/:doctype', async (params) => {
await this.showViewPage('Tree', params.doctype);
});
2018-03-05 22:15:21 +05:30
frappe.router.add('table/:doctype', async (params) => {
await this.showViewPage('Table', params.doctype, params);
})
frappe.router.add('edit/:doctype/:name', async (params) => {
await this.showViewPage('Form', params.doctype, params);
})
frappe.router.add('print/:doctype/:name', async (params) => {
await this.showViewPage('Print', params.doctype, params);
2018-01-12 17:55:07 +05:30
})
frappe.router.add('new/:doctype', async (params) => {
2018-02-08 17:58:51 +05:30
let doc = await frappe.getNewDoc(params.doctype);
// unset the name, its local
2018-02-08 15:08:47 +05:30
await frappe.router.setRoute('edit', doc.doctype, doc.name);
2018-02-20 12:09:41 +05:30
// focus on new page
frappe.desk.body.activePage.body.querySelector('input').focus();
});
2018-02-09 18:25:55 +05:30
frappe.router.on('change', () => {
2018-02-14 22:14:50 +05:30
if (this.menu) {
this.menu.setActive();
2018-02-09 18:25:55 +05:30
}
})
}
2018-03-05 22:15:21 +05:30
toggleCenter(show) {
const current = !frappe.desk.center.classList.contains('hide');
if (show===undefined) {
show = current;
} else if (!!show===!!current) {
// no change
return;
}
// add hide
frappe.desk.center.classList.toggle('hide', !show);
if (show) {
// set body to 6
frappe.desk.body.classList.toggle('col-md-6', true);
frappe.desk.body.classList.toggle('col-md-10', false);
} else {
// set body to 10
frappe.desk.body.classList.toggle('col-md-6', false);
frappe.desk.body.classList.toggle('col-md-10', true);
}
}
async showViewPage(view, doctype, params) {
if (!params) params = doctype;
if (!this.pages[view]) this.pages[view] = {};
if (!this.pages[view][doctype]) this.pages[view][doctype] = new views[view](doctype);
2018-03-05 22:15:21 +05:30
const page = this.pages[view][doctype];
2018-03-08 19:01:22 +05:30
await page.show(params);
2018-01-12 17:55:07 +05:30
}
2018-02-19 12:01:07 +05:30
async showFormModal(doctype, name) {
if (!this.pages.formModals[doctype]) {
this.pages.formModals[doctype] = new views.FormModal(doctype);
}
2018-02-19 12:01:07 +05:30
await this.pages.formModals[doctype].showWith(doctype, name);
return this.pages.formModals[doctype];
}
async setActiveDoc(doc) {
this.activeDoc = doc;
if (frappe.desk.center && !frappe.desk.center.activePage) {
await frappe.desk.showViewPage('List', doc.doctype);
}
if (frappe.desk.pages.List[doc.doctype]) {
frappe.desk.pages.List[doc.doctype].list.setActiveListRow(doc.name);
}
}
2018-02-09 18:25:55 +05:30
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) {
2018-03-08 19:01:22 +05:30
let item = frappe.ui.add('a', 'list-group-item list-group-item-action', this.sidebarList, label);
2018-01-12 17:55:07 +05:30
if (typeof action === 'string') {
item.href = action;
2018-02-09 18:25:55 +05:30
this.routeItems[action] = item;
2018-01-12 17:55:07 +05:30
} else {
item.addEventHandler('click', () => {
action();
2018-02-09 18:25:55 +05:30
this.setActive(item);
2018-01-12 17:55:07 +05:30
});
}
}
}