2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 19:29:02 +00:00
books/client/view/list.js

189 lines
5.3 KiB
JavaScript
Raw Normal View History

2018-01-16 06:09:17 +00:00
const frappe = require('frappejs');
2018-01-12 12:25:07 +00:00
2018-01-24 11:52:05 +00:00
module.exports = class BaseList {
2018-02-08 09:38:47 +00:00
constructor({doctype, parent, fields, page}) {
Object.assign(this, arguments[0]);
2018-01-12 12:25:07 +00:00
this.meta = frappe.getMeta(this.doctype);
2018-01-12 12:25:07 +00:00
this.start = 0;
2018-02-08 09:38:47 +00:00
this.pageLength = 20;
2018-01-12 12:25:07 +00:00
this.body = null;
this.rows = [];
this.data = [];
2018-02-19 16:41:10 +00:00
frappe.db.on(`change:${this.doctype}`, (params) => {
this.dirty = true;
});
setInterval(() => {
if (this.dirty) this.refresh();
}, 500);
2018-01-12 12:25:07 +00:00
}
2018-02-08 09:38:47 +00:00
async refresh() {
return await this.run();
}
2018-01-12 12:25:07 +00:00
async run() {
2018-02-08 09:38:47 +00:00
this.makeBody();
2018-02-19 16:41:10 +00:00
this.dirty = false;
2018-01-12 12:25:07 +00:00
2018-02-08 09:38:47 +00:00
let data = await this.getData();
2018-01-12 12:25:07 +00:00
2018-02-08 09:38:47 +00:00
for (let i=0; i< Math.min(this.pageLength, data.length); i++) {
this.renderRow(this.start + i, data[i]);
2018-01-12 12:25:07 +00:00
}
if (this.start > 0) {
this.data = this.data.concat(data);
} else {
this.data = data;
}
2018-02-08 09:38:47 +00:00
this.clearEmptyRows();
this.updateMore(data.length > this.pageLength);
2018-01-12 12:25:07 +00:00
}
2018-02-08 09:38:47 +00:00
async getData() {
return await frappe.db.getAll({
2018-01-24 11:52:05 +00:00
doctype: this.doctype,
2018-02-08 09:38:47 +00:00
fields: this.getFields(),
filters: this.getFilters(),
2018-01-24 11:52:05 +00:00
start: this.start,
2018-02-08 09:38:47 +00:00
limit: this.pageLength + 1
2018-01-24 11:52:05 +00:00
});
}
2018-02-08 09:38:47 +00:00
getFields() {
2018-01-24 11:52:05 +00:00
return ['name'];
}
2018-01-12 12:25:07 +00:00
async append() {
2018-02-08 09:38:47 +00:00
this.start += this.pageLength;
2018-01-12 12:25:07 +00:00
await this.run();
}
2018-02-08 09:38:47 +00:00
getFilters() {
2018-01-24 11:52:05 +00:00
let filters = {};
2018-02-08 09:38:47 +00:00
if (this.searchInput.value) {
filters.keywords = ['like', '%' + this.searchInput.value + '%'];
2018-01-12 12:25:07 +00:00
}
2018-01-24 11:52:05 +00:00
return filters;
2018-01-12 12:25:07 +00:00
}
2018-02-08 09:38:47 +00:00
makeBody() {
2018-01-12 12:25:07 +00:00
if (!this.body) {
2018-02-08 09:38:47 +00:00
this.makeToolbar();
2018-02-15 09:53:28 +00:00
this.parent.classList.add('list-page');
2018-01-12 12:25:07 +00:00
this.body = frappe.ui.add('div', 'list-body', this.parent);
2018-02-15 09:53:28 +00:00
this.body.setAttribute('data-doctype', this.doctype);
2018-02-08 09:38:47 +00:00
this.makeMoreBtn();
2018-01-12 12:25:07 +00:00
}
}
2018-02-08 09:38:47 +00:00
makeToolbar() {
this.makeSearch();
2018-02-15 09:53:28 +00:00
this.btnNew = this.page.addButton(frappe._('New'), 'btn-primary', async () => {
await frappe.router.setRoute('new', this.doctype);
2018-02-08 09:38:47 +00:00
})
2018-02-15 09:53:28 +00:00
this.btnDelete = this.page.addButton(frappe._('Delete'), 'btn-secondary hide', async () => {
2018-02-08 09:38:47 +00:00
await frappe.db.deleteMany(this.doctype, this.getCheckedRowNames());
await this.refresh();
});
this.page.body.addEventListener('click', (event) => {
if(event.target.classList.contains('checkbox')) {
this.btnDelete.classList.toggle('hide', this.getCheckedRowNames().length===0);
}
})
}
makeSearch() {
2018-01-23 08:00:29 +00:00
this.toolbar = frappe.ui.add('div', 'list-toolbar', this.parent);
this.toolbar.innerHTML = `
2018-02-15 09:53:28 +00:00
<div class="input-group list-search">
<input class="form-control" type="text" placeholder="Search...">
<div class="input-group-append">
<button class="btn btn-outline-secondary btn-search">Search</button>
2018-01-23 08:00:29 +00:00
</div>
</div>
`;
2018-02-08 09:38:47 +00:00
this.searchInput = this.toolbar.querySelector('input');
this.searchInput.addEventListener('keypress', (event) => {
2018-01-12 12:25:07 +00:00
if (event.keyCode===13) {
2018-02-08 09:38:47 +00:00
this.refresh();
2018-01-12 12:25:07 +00:00
}
});
2018-02-08 09:38:47 +00:00
this.btnSearch = this.toolbar.querySelector('.btn-search');
this.btnSearch.addEventListener('click', (event) => {
this.refresh();
2018-01-12 12:25:07 +00:00
});
}
2018-02-08 09:38:47 +00:00
makeMoreBtn() {
this.btnMore = frappe.ui.add('button', 'btn btn-secondary hide', this.parent);
this.btnMore.textContent = 'More';
this.btnMore.addEventListener('click', () => {
2018-01-12 12:25:07 +00:00
this.append();
})
}
2018-02-08 09:38:47 +00:00
renderRow(i, data) {
let row = this.getRow(i);
row.innerHTML = this.getRowBodyHTML(data);
2018-02-15 09:53:28 +00:00
row.setAttribute('data-name', data.name);
row.style.display = 'flex';
2018-01-12 12:25:07 +00:00
}
2018-02-08 09:38:47 +00:00
getRowBodyHTML(data) {
2018-02-15 09:53:28 +00:00
return `<div class="col-1">
<input class="checkbox" type="checkbox" data-name="${data.name}">
</div>` + this.getRowHTML(data);
2018-02-08 09:38:47 +00:00
}
getRowHTML(data) {
2018-02-15 09:53:28 +00:00
return `<div class="col-11">${data.name}</div>`;
2018-01-24 11:52:05 +00:00
}
2018-02-08 09:38:47 +00:00
getRow(i) {
2018-01-12 12:25:07 +00:00
if (!this.rows[i]) {
2018-02-15 09:53:28 +00:00
this.rows[i] = frappe.ui.add('div', 'list-row row no-gutters', this.body);
// open on click
let doctype = this.doctype;
this.rows[i].addEventListener('click', function(e) {
if (!e.target.tagName !== 'input') {
let name = this.getAttribute('data-name');
frappe.router.setRoute('edit', doctype, name);
}
});
2018-01-12 12:25:07 +00:00
}
return this.rows[i];
}
2018-02-08 09:38:47 +00:00
getCheckedRowNames() {
return [...this.body.querySelectorAll('.checkbox:checked')].map(check => check.getAttribute('data-name'));
}
clearEmptyRows() {
2018-01-12 12:25:07 +00:00
if (this.rows.length > this.data.length) {
for (let i=this.data.length; i < this.rows.length; i++) {
2018-02-08 09:38:47 +00:00
let row = this.getRow(i);
2018-01-12 12:25:07 +00:00
row.innerHTML = '';
row.style.display = 'none';
}
}
}
2018-02-08 09:38:47 +00:00
updateMore(show) {
2018-01-12 12:25:07 +00:00
if (show) {
2018-02-08 09:38:47 +00:00
this.btnMore.classList.remove('hide');
2018-01-12 12:25:07 +00:00
} else {
2018-02-08 09:38:47 +00:00
this.btnMore.classList.add('hide');
2018-01-12 12:25:07 +00:00
}
}
};