github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/fish_client/src/components/tables.js (about) 1 /** 2 * Copyright 2017 Intel Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * ---------------------------------------------------------------------------- 16 */ 17 'use strict' 18 19 const m = require('mithril') 20 21 const Table = { 22 oninit (vnode) { 23 if (!vnode.attrs.noRowsText) { 24 vnode.attrs.noRowsText = 'No rows available' 25 } 26 }, 27 28 view (vnode) { 29 return [ 30 m('table.table', 31 m('thead', 32 m('tr', 33 vnode.attrs.headers.map((header) => m('th', header)))), 34 m('tbody', 35 vnode.attrs.rows.length > 0 36 ? vnode.attrs.rows 37 .map((row) => 38 m('tr', 39 row.map((col) => m('td', col)))) 40 : m('tr', 41 m('td.text-center', {colSpan: 5}, 42 vnode.attrs.noRowsText)) 43 ) 44 ) 45 ] 46 } 47 } 48 49 const FilterGroup = { 50 oninit (vnode) { 51 vnode.state.currentFilter = vnode.attrs.initialFilter 52 }, 53 54 view (vnode) { 55 return [ 56 m('.btn-group', { 57 role: 'group', 58 'aria-label': vnode.attrs.ariaLabel 59 }, 60 Object.entries(vnode.attrs.filters).map(([label, action]) => 61 m('button.btn', { 62 className: vnode.state.currentFilter === label ? 'btn-primary' : 'btn-light', 63 onclick: (e) => { 64 e.preventDefault() 65 vnode.state.currentFilter = label 66 action() 67 } 68 }, label))) 69 ] 70 } 71 } 72 73 const PagingButtons = { 74 view (vnode) { 75 return [ 76 m('.d-flex.justify-content-end', 77 m('.btn-group', { 78 role: 'group', 79 'aria-label': 'Paging controls' 80 }, 81 m('button.btn.btn-light', { 82 onclick: (e) => { 83 e.preventDefault() 84 vnode.attrs.setPage(Math.max(0, vnode.attrs.currentPage - 1)) 85 }, 86 disabled: vnode.attrs.currentPage === 0 87 }, '\u25c0'), // right arrow 88 m('button.btn.btn-light', { 89 onclick: (e) => { 90 e.preventDefault() 91 vnode.attrs.setPage( 92 Math.min(vnode.attrs.maxPage, vnode.attrs.currentPage + 1)) 93 }, 94 disabled: (vnode.attrs.currentPage === vnode.attrs.maxPage) 95 }, '\u25b6'))) // left arrow 96 ] 97 } 98 } 99 100 module.exports = { 101 Table, 102 FilterGroup, 103 PagingButtons 104 }