2
0
mirror of https://github.com/frappe/books.git synced 2025-02-04 04:58:30 +00:00
books/client/view/list.js

170 lines
4.7 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
2018-01-24 17:22:05 +05:30
module.exports = class BaseList {
2018-02-08 15:08:47 +05:30
constructor({doctype, parent, fields, page}) {
Object.assign(this, arguments[0]);
2018-01-12 17:55:07 +05:30
this.meta = frappe.getMeta(this.doctype);
2018-01-12 17:55:07 +05:30
this.start = 0;
2018-02-08 15:08:47 +05:30
this.pageLength = 20;
2018-01-12 17:55:07 +05:30
this.body = null;
this.rows = [];
this.data = [];
}
2018-02-08 15:08:47 +05:30
async refresh() {
return await this.run();
}
2018-01-12 17:55:07 +05:30
async run() {
2018-02-08 15:08:47 +05:30
this.makeBody();
2018-01-12 17:55:07 +05:30
2018-02-08 15:08:47 +05:30
let data = await this.getData();
2018-01-12 17:55:07 +05:30
2018-02-08 15:08:47 +05:30
for (let i=0; i< Math.min(this.pageLength, data.length); i++) {
this.renderRow(this.start + i, data[i]);
2018-01-12 17:55:07 +05:30
}
if (this.start > 0) {
this.data = this.data.concat(data);
} else {
this.data = data;
}
2018-02-08 15:08:47 +05:30
this.clearEmptyRows();
this.updateMore(data.length > this.pageLength);
2018-01-12 17:55:07 +05:30
}
2018-02-08 15:08:47 +05:30
async getData() {
return await frappe.db.getAll({
2018-01-24 17:22:05 +05:30
doctype: this.doctype,
2018-02-08 15:08:47 +05:30
fields: this.getFields(),
filters: this.getFilters(),
2018-01-24 17:22:05 +05:30
start: this.start,
2018-02-08 15:08:47 +05:30
limit: this.pageLength + 1
2018-01-24 17:22:05 +05:30
});
}
2018-02-08 15:08:47 +05:30
getFields() {
2018-01-24 17:22:05 +05:30
return ['name'];
}
2018-01-12 17:55:07 +05:30
async append() {
2018-02-08 15:08:47 +05:30
this.start += this.pageLength;
2018-01-12 17:55:07 +05:30
await this.run();
}
2018-02-08 15:08:47 +05:30
getFilters() {
2018-01-24 17:22:05 +05:30
let filters = {};
2018-02-08 15:08:47 +05:30
if (this.searchInput.value) {
filters.keywords = ['like', '%' + this.searchInput.value + '%'];
2018-01-12 17:55:07 +05:30
}
2018-01-24 17:22:05 +05:30
return filters;
2018-01-12 17:55:07 +05:30
}
2018-02-08 15:08:47 +05:30
makeBody() {
2018-01-12 17:55:07 +05:30
if (!this.body) {
2018-02-08 15:08:47 +05:30
this.makeToolbar();
2018-01-12 17:55:07 +05:30
this.body = frappe.ui.add('div', 'list-body', this.parent);
2018-02-08 15:08:47 +05:30
this.makeMoreBtn();
2018-01-12 17:55:07 +05:30
}
}
2018-02-08 15:08:47 +05:30
makeToolbar() {
this.makeSearch();
this.btnNew = this.page.addButton(frappe._('New'), 'btn-outline-primary', async () => {
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 13:30:29 +05:30
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 15:08:47 +05:30
this.searchInput = this.toolbar.querySelector('input');
this.searchInput.addEventListener('keypress', (event) => {
2018-01-12 17:55:07 +05:30
if (event.keyCode===13) {
2018-02-08 15:08:47 +05:30
this.refresh();
2018-01-12 17:55:07 +05:30
}
});
2018-02-08 15:08:47 +05:30
this.btnSearch = this.toolbar.querySelector('.btn-search');
this.btnSearch.addEventListener('click', (event) => {
this.refresh();
2018-01-12 17:55:07 +05:30
});
}
2018-02-08 15:08:47 +05:30
makeMoreBtn() {
this.btnMore = frappe.ui.add('button', 'btn btn-secondary hide', this.parent);
this.btnMore.textContent = 'More';
this.btnMore.addEventListener('click', () => {
2018-01-12 17:55:07 +05:30
this.append();
})
}
2018-02-08 15:08:47 +05:30
renderRow(i, data) {
let row = this.getRow(i);
row.innerHTML = this.getRowBodyHTML(data);
2018-01-12 17:55:07 +05:30
row.style.display = 'block';
}
2018-02-08 15:08:47 +05:30
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 17:22:05 +05:30
}
2018-02-08 15:08:47 +05:30
getRow(i) {
2018-01-12 17:55:07 +05:30
if (!this.rows[i]) {
2018-01-23 13:30:29 +05:30
this.rows[i] = frappe.ui.add('div', 'list-row py-2', this.body);
2018-01-12 17:55:07 +05:30
}
return this.rows[i];
}
2018-02-08 15:08:47 +05:30
getCheckedRowNames() {
return [...this.body.querySelectorAll('.checkbox:checked')].map(check => check.getAttribute('data-name'));
}
clearEmptyRows() {
2018-01-12 17:55:07 +05:30
if (this.rows.length > this.data.length) {
for (let i=this.data.length; i < this.rows.length; i++) {
2018-02-08 15:08:47 +05:30
let row = this.getRow(i);
2018-01-12 17:55:07 +05:30
row.innerHTML = '';
row.style.display = 'none';
}
}
}
2018-02-08 15:08:47 +05:30
updateMore(show) {
2018-01-12 17:55:07 +05:30
if (show) {
2018-02-08 15:08:47 +05:30
this.btnMore.classList.remove('hide');
2018-01-12 17:55:07 +05:30
} else {
2018-02-08 15:08:47 +05:30
this.btnMore.classList.add('hide');
2018-01-12 17:55:07 +05:30
}
}
};