2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00
books/client/ui/utils.js

62 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-03-26 12:18:07 +00:00
const frappe = require('frappejs');
module.exports = {
convertFieldsToDatatableColumns(fields, layout = 'fixed') {
return fields.map(field => {
if (!field.width) {
if (layout==='ratio') {
field.width = 1;
} else if (layout==='fixed') {
field.width = 120;
}
}
return {
id: field.fieldname || frappe.slug(field.label),
field: field,
content: field.label,
editable: true,
sortable: false,
resizable: true,
dropdown: false,
width: field.width,
align: ['Int', 'Float', 'Currency'].includes(field.fieldtype) ? 'right' : 'left',
format: (value) => frappe.format(value, field)
}
});
},
2018-03-30 16:58:00 +00:00
addButton(label, parent, action, unhide = true) {
const link = frappe.ui.add('button', 'btn btn-sm btn-outline-secondary', parent, label);
2018-03-30 16:58:00 +00:00
link.type = 'button';
link.addEventListener('click', action);
if (unhide) {
parent.classList.remove('hide');
}
return link;
2018-03-28 11:29:28 +00:00
},
// https://css-tricks.com/snippets/javascript/loop-queryselectorall-matches/
forEachNode(nodeList, callback, scope) {
if(!nodeList) return;
for (var i = 0; i < nodeList.length; i++) {
callback.call(scope, nodeList[i], i);
}
},
2018-03-30 16:58:00 +00:00
activate($parent, $child, commonSelector, activeClass='active', index = -1) {
let $children = $parent.querySelectorAll(`${commonSelector}.${activeClass}`);
if (typeof $child === 'string') {
$child = $parent.querySelector($child);
}
2018-03-28 11:29:28 +00:00
this.forEachNode($children, (node, i) => {
if(index >= 0 && i <= index) return;
node.classList.remove(activeClass);
})
$child.classList.add(activeClass);
}
}