/* Copyright (C) 2017 Cloudbase Solutions SRL This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ import React, { Component, PropTypes } from 'react'; import Collapse from 'react-collapse'; import withStyles from 'isomorphic-style-loader/lib/withStyles'; import s from './Table.scss'; class Table extends Component { static defaultProps = { headerItems: [], listItems: [], parentId: null, show: true } static propTypes = { headerItems: PropTypes.array, listItems: PropTypes.array, customClassName: PropTypes.string, show: PropTypes.bool, parentId: PropTypes.string } constructor(props) { super(props) this.state = { openState: [], parentId: null } } componentWillReceiveProps(newProps) { let openState = [] for (let i in newProps.listItems) { // Use the previous open state if the table's parent ID is the same // i.e. don't close the collapser if new props arrive let isSameParent = this.state.parentId === null || newProps.parentId === this.state.parentId let prevOpenState = isSameParent && this.state.openState[i] openState.push(newProps.listItems[i].openState || prevOpenState) } this.setState({ openState: openState, parentId: newProps.parentId }) } toggleDrawer(index) { let newOpenState = this.state.openState let toggled = !newOpenState[index] for (let i in newOpenState) { newOpenState[i] = false } newOpenState[index] = toggled this.setState({ openState: newOpenState }) } rowMouseDown(e) { this.dragStartPosition = { x: e.screenX, y: e.screenY }; } rowMouseUp(e, index) { this.dragStartPosition = this.dragStartPosition || { x: e.screenX, y: e.screenY }; // If a drag operation has been initiated (i.e. text selection), don't call toggleDrawer if (Math.abs(this.dragStartPosition.x - e.screenX) < 3 && Math.abs(this.dragStartPosition.y - e.screenY) < 3) { this.toggleDrawer(index); } this.dragStartPosition = null; } render() { let headerItems = this.props.headerItems.map((item, index) =>
{item.label}
) let listItems = (
No results
) if (this.props.listItems) { listItems = this.props.listItems.map((listItem, index) => { let row = this.props.headerItems.map((headerItem) => (
{listItem[headerItem.key]}
) ) let detailView = null if (listItem.detailView) { detailView = (
{listItem.detailView}
) } return (
this.rowMouseDown(e)} onMouseUp={e => this.rowMouseUp(e, index)} > {row} {detailView}
) }, this) } return (
{headerItems}
{listItems}
); } } export default withStyles(Table, s);