Newer
Older
import m from 'mithril';
import infinite from 'mithril-infinite';
import { List, ListTile, Toolbar, Search, Button } from 'polythene-mithril';
import 'polythene-css';
import { styler } from 'polythene-core-css';
import { debounce } from '../utils';
const tableStyles = [
{
'.tabletool': {
display: 'grid',
height: '100%',
'.toolbar': {
'grid-row': 1,
display: 'flex',
},
'.scrollTable': {
'grid-row': 2,
'background-color': 'white',
},
'.tableTile': {
padding: '10px',
},
},
];
styler.add('tableview', tableStyles);
/* Shows a table of objects for a given API resource.
*
* Required attributes:
* vnode: { attrs: { controller, titles, keys } }
* - controller: a listcontroller for some API resource data
* - titles: the titles of the table
* - keys: Keys of this resource to display as columns, e.g. ['firstname']
* Works with embedded resources, i.e. if you add
* { embedded: { event: 1 } } to a list of eventsignups,
* you can display event.title_de as a table key
*/
this.tableKeys = keys;
}
getItemData(data) {
return this.tableKeys.map((key) => {
// Access a nested key, indicated by dot-notation
let nestedData = data;
key.split('.').forEach((subKey) => { nestedData = nestedData[subKey]; });
return m(
'div',
{ style: { width: `${98 / this.tableKeys.length}%` } },
nestedData,
);
});
}
item() {
return (data, opts) => m(ListTile, {
className: 'themed-list-tile',
hoverable: true,
compactFront: true,
content: m('div', {
onclick() { m.route.set(`/${data._links.self.href}`); },
style: { width: '100%', display: 'flex' },
}, this.getItemData(data)),
});
const updateList = debounce(() => {
controller.refresh();
}, 500);
return m('div.tabletool', [
m(Toolbar, {
className: 'toolbar',
content: [
m(Search, {
textfield: {
label: 'Search',
onChange: ({ value }) => {
controller.setSearch(value);
updateList();
},
},
}),
m(Button, {
className: 'blue-button',
borders: true,
label: 'Add',
events: { onclick: () => { onAdd(); } },
}),
],
}),
m(List, {
className: 'scrollTable',
tiles: [
m(ListTile, {
className: 'tableTile',
content: m(
'div',
{ style: { width: '100%', display: 'flex' } },
titles.map(title => m('div', {
}, title)),
),
}),
m(infinite, controller.infiniteScrollParams(this.item())),
],