2
0
mirror of https://github.com/frappe/books.git synced 2025-01-26 16:48:28 +00:00
books/client/view/page.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-01-16 11:39:17 +05:30
const frappe = require('frappejs');
2018-01-12 17:55:07 +05:30
module.exports = class Page {
2018-01-12 17:55:07 +05:30
constructor(title) {
this.handlers = {};
this.title = title;
this.make();
}
make() {
2018-01-23 13:30:29 +05:30
this.wrapper = frappe.ui.add('div', 'page hide', frappe.desk.body);
this.body = frappe.ui.add('div', 'page-body', this.wrapper);
2018-01-12 17:55:07 +05:30
}
hide() {
this.wrapper.classList.add('hide');
2018-01-12 17:55:07 +05:30
this.trigger('hide');
}
2018-01-16 11:39:17 +05:30
async show(params) {
2018-01-12 17:55:07 +05:30
if (frappe.router.current_page) {
frappe.router.current_page.hide();
}
this.wrapper.classList.remove('hide');
this.body.classList.remove('hide');
if (this.page_error) {
this.page_error.classList.add('hide');
}
2018-01-12 17:55:07 +05:30
frappe.router.current_page = this;
document.title = this.title;
2018-01-16 11:39:17 +05:30
await this.trigger('show', params);
2018-01-12 17:55:07 +05:30
}
2018-01-23 13:30:29 +05:30
render_error(title, message) {
if (!this.page_error) {
this.page_error = frappe.ui.add('div', 'page-error', this.wrapper);
}
this.body.classList.add('hide');
this.page_error.classList.remove('hide');
2018-01-23 13:30:29 +05:30
this.page_error.innerHTML = `<h3 class="text-extra-muted">${title ? title : ""}</h3><p class="text-muted">${message ? message : ""}</p>`;
}
2018-01-12 17:55:07 +05:30
on(event, fn) {
if (!this.handlers[event]) this.handlers[event] = [];
2018-01-12 17:55:07 +05:30
this.handlers[event].push(fn);
}
async trigger(event, params) {
2018-01-12 17:55:07 +05:30
if (this.handlers[event]) {
for (let handler of this.handlers[event]) {
await handler(params);
2018-01-12 17:55:07 +05:30
}
}
}
}