2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +00:00
books/client/view/list.js

170 lines
4.7 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-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-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-01-12 12:25:07 +00:00
this.body = frappe.ui.add('div', 'list-body', this.parent);
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-14 12:50:56 +00:00
this.btnNew = this.page.addButton(frappe._('New'), 'btn-primary', async () => {
2018-02-08 09:38:47 +00:00
await frappe.router.setRoute('new', frappe.slug(this.doctype));
})
this.btnDelete = this.page.addButton(frappe._('Delete'), 'btn-outline-secondary hide', async () => {
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 = `
<div class="row">
<div class="col-md-6 col-9">
<div class="input-group list-search mb-2">
<input class="form-control" type="text" placeholder="Search...">
<div class="input-group-append">
<button class="btn btn-outline-secondary btn-search">Search</button>
</div>
</div>
</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-01-12 12:25:07 +00:00
row.style.display = 'block';
}
2018-02-08 09:38:47 +00:00
getRowBodyHTML(data) {
return `<input class="checkbox" type="checkbox" data-name="${data.name}"> ` + this.getRowHTML(data);
}
getRowHTML(data) {
return `<a href="#edit/${this.doctype}/${data.name}">${data.name}</a>`;
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-14 12:50:56 +00:00
this.rows[i] = frappe.ui.add('div', 'list-row', this.body);
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
}
}
};