All files / javascripts/SqlLab/components TableElement.jsx

0% Statements 0/62
0% Branches 0/30
0% Functions 0/6
0% Lines 0/60
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import React from 'react';
import PropTypes from 'prop-types';
 
import { ButtonGroup, Collapse, Well } from 'react-bootstrap';
import shortid from 'shortid';
 
import CopyToClipboard from '../../components/CopyToClipboard';
import Link from './Link';
import ColumnElement from './ColumnElement';
import ModalTrigger from '../../components/ModalTrigger';
import Loading from '../../components/Loading';
 
const propTypes = {
  table: PropTypes.object,
  actions: PropTypes.object,
  timeout: PropTypes.number,  // used for tests
};
 
const defaultProps = {
  actions: {},
  table: null,
  timeout: 500,
};
 
class TableElement extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      sortColumns: false,
      expanded: true,
    };
  }
 
  popSelectStar() {
    const qe = {
      id: shortid.generate(),
      title: this.props.table.name,
      dbId: this.props.table.dbId,
      autorun: true,
      sql: this.props.table.selectStar,
    };
    this.props.actions.addQueryEditor(qe);
  }
 
  toggleTable(e) {
    e.preventDefault();
    if (this.props.table.expanded) {
      this.props.actions.collapseTable(this.props.table);
    } else {
      this.props.actions.expandTable(this.props.table);
    }
  }
 
  removeTable() {
    this.setState({ expanded: false });
    this.props.actions.removeDataPreview(this.props.table);
  }
  toggleSortColumns() {
    this.setState({ sortColumns: !this.state.sortColumns });
  }
 
  removeFromStore() {
    this.props.actions.removeTable(this.props.table);
  }
 
  renderWell() {
    const table = this.props.table;
    let header;
    if (table.partitions) {
      let partitionQuery;
      let partitionClipBoard;
      if (table.partitions.partitionQuery) {
        partitionQuery = table.partitions.partitionQuery;
        const tt = 'Copy partition query to clipboard';
        partitionClipBoard = (
          <CopyToClipboard
            text={partitionQuery}
            shouldShowText={false}
            tooltipText={tt}
            copyNode={<i className="fa fa-clipboard" />}
          />
        );
      }
      let latest = [];
      for (const k in table.partitions.latest) {
        latest.push(`${k}=${table.partitions.latest[k]}`);
      }
      latest = latest.join('/');
      header = (
        <Well bsSize="small">
          <div>
            <small>
              latest partition: {latest}
            </small> {partitionClipBoard}
          </div>
        </Well>
      );
    }
    return header;
  }
  renderControls() {
    let keyLink;
    const table = this.props.table;
    if (table.indexes && table.indexes.length > 0) {
      keyLink = (
        <ModalTrigger
          modalTitle={
            <div>
              Keys for table <strong>{table.name}</strong>
            </div>
          }
          modalBody={table.indexes.map((ix, i) => (
            <pre key={i}>{JSON.stringify(ix, null, '  ')}</pre>
          ))}
          triggerNode={
            <Link
              className="fa fa-key pull-left m-l-2"
              tooltip={`View keys & indexes (${table.indexes.length})`}
            />
          }
        />
      );
    }
    return (
      <ButtonGroup className="ws-el-controls pull-right">
        {keyLink}
        <Link
          className={
            `fa fa-sort-${!this.state.sortColumns ? 'alpha' : 'numeric'}-asc ` +
            'pull-left sort-cols m-l-2'}
          onClick={this.toggleSortColumns.bind(this)}
          tooltip={
            !this.state.sortColumns ?
            'Sort columns alphabetically' :
            'Original table column order'}
          href="#"
        />
        {table.selectStar &&
          <CopyToClipboard
            copyNode={
              <a className="fa fa-clipboard pull-left m-l-2" />
            }
            text={table.selectStar}
            shouldShowText={false}
            tooltipText="Copy SELECT statement to clipboard"
          />
        }
        <Link
          className="fa fa-times table-remove pull-left m-l-2"
          onClick={this.removeTable.bind(this)}
          tooltip="Remove table preview"
          href="#"
        />
      </ButtonGroup>
    );
  }
  renderHeader() {
    const table = this.props.table;
    return (
      <div className="clearfix">
        <div className="pull-left">
          <a
            href="#"
            className="table-name"
            onClick={(e) => { this.toggleTable(e); }}
          >
            <strong>{table.name}</strong>
            <small className="m-l-5">
              <i className={`fa fa-${table.expanded ? 'minus' : 'plus'}-square-o`} />
            </small>
          </a>
        </div>
        <div className="pull-right">
          {table.isMetadataLoading || table.isExtraMetadataLoading ?
            <Loading size={20} />
            :
            this.renderControls()
          }
        </div>
      </div>
    );
  }
  renderBody() {
    const table = this.props.table;
    let cols;
    if (table.columns) {
      cols = table.columns.slice();
      if (this.state.sortColumns) {
        cols.sort((a, b) => a.name.toUpperCase() > b.name.toUpperCase());
      }
    }
    const metadata = (
      <Collapse
        in={table.expanded}
        timeout={this.props.timeout}
      >
        <div>
          {this.renderWell()}
          <div className="table-columns">
            {cols && cols.map(col => (
              <ColumnElement column={col} key={col.name} />
            ))}
            <hr />
          </div>
        </div>
      </Collapse>
    );
    return metadata;
  }
 
  render() {
    return (
      <Collapse
        in={this.state.expanded}
        timeout={this.props.timeout}
        transitionAppear
        onExited={this.removeFromStore.bind(this)}
      >
        <div className="TableElement">
          {this.renderHeader()}
          <div>
            {this.renderBody()}
          </div>
        </div>
      </Collapse>
    );
  }
}
TableElement.propTypes = propTypes;
TableElement.defaultProps = defaultProps;
 
export default TableElement;