|
| 1 | +/* |
| 2 | + * OS.js - JavaScript Cloud/Web Desktop Platform |
| 3 | + * |
| 4 | + * Copyright (c) 2011-2018, Anders Evenrud <[email protected]> |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 11 | + * list of conditions and the following disclaimer |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 20 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + * |
| 27 | + * @author Anders Evenrud <[email protected]> |
| 28 | + * @licence Simplified BSD License |
| 29 | + */ |
| 30 | + |
| 31 | +import {h} from 'hyperapp'; |
| 32 | +import {Element} from './Element'; |
| 33 | + |
| 34 | +const data = c => { |
| 35 | + if (typeof c === 'undefined' || c === null) { |
| 36 | + return ''; |
| 37 | + } else if (typeof c === 'function') { |
| 38 | + return c(); |
| 39 | + } else if (typeof c === 'object') { |
| 40 | + return c.label; |
| 41 | + } |
| 42 | + |
| 43 | + return String(c); |
| 44 | +}; |
| 45 | + |
| 46 | +const col = (tagName, onclick) => (c, props = {}, children = []) => h(tagName, props, h('div', { |
| 47 | + onclick |
| 48 | +}, [ |
| 49 | + h('span', {}, data(c)), |
| 50 | + ...children |
| 51 | +])); |
| 52 | + |
| 53 | +const createHeader = ({sortBy, sortDir, columns, onsort}) => h('thead', {}, h('tr', {}, columns.map((c, i) => { |
| 54 | + const active = sortBy === i; |
| 55 | + |
| 56 | + return col('th', ev => { |
| 57 | + if (typeof onsort === 'function') { |
| 58 | + const dir = !sortDir |
| 59 | + ? 'asc' : (sortDir === 'asc' ? 'desc' : null); |
| 60 | + |
| 61 | + onsort(ev, i, dir, c); |
| 62 | + } |
| 63 | + })(c, { |
| 64 | + 'data-sort': active, |
| 65 | + 'data-direction': active ? sortDir : false |
| 66 | + }, [h('span')]); |
| 67 | +}))); |
| 68 | + |
| 69 | +const createBody = rows => h('tbody', {}, rows.map(columns => h('tr', {}, columns.map(col('td'))))); |
| 70 | + |
| 71 | +const createFooter = columns => h('tfoot', {}, h('tr', {}, columns.map(col('td')))); |
| 72 | + |
| 73 | +/** |
| 74 | + * A toolbar |
| 75 | + * @desc Contains entries with spacing |
| 76 | + * @param {BoxProperties} props Properties |
| 77 | + * @param {h[]} children Children |
| 78 | + */ |
| 79 | +export const Table = (props = {}, children = []) => { |
| 80 | + const newProps = Object.assign({ |
| 81 | + columns: [], |
| 82 | + rows: [], |
| 83 | + footer: [], |
| 84 | + sortBy: null, |
| 85 | + sortDir: null, |
| 86 | + fixed: true, |
| 87 | + collapse: true, |
| 88 | + zebra: false |
| 89 | + }, props); |
| 90 | + |
| 91 | + const boxProps = Object.assign({}, props.box || {}, { |
| 92 | + class: ['osjs-gui-table', props.class] |
| 93 | + }); |
| 94 | + |
| 95 | + |
| 96 | + return h(Element, boxProps, h('table', { |
| 97 | + 'data-collapse': newProps.collapse === true, |
| 98 | + 'data-fixed': newProps.fixed === true, |
| 99 | + 'data-zebra': newProps.zebra === true |
| 100 | + }, [ |
| 101 | + createHeader(newProps), |
| 102 | + createBody([ |
| 103 | + ...newProps.rows, |
| 104 | + ...children |
| 105 | + ]), |
| 106 | + createFooter(newProps.footer) |
| 107 | + ])); |
| 108 | +}; |
0 commit comments