Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1. import * as React from "react";
  2. import {connect} from "react-redux";
  3. import * as _ from "lodash";
  4. import {changeTableScrollTopPositionAction, resizeWindow as resizeWindowAction, changeColCheckbox } from "../actions.ts";
  5.  
  6. const push = require("react-router-redux").push;
  7. const classNames = require("classnames/bind");
  8. const styles = require("./styles.scss");
  9. const cx = classNames.bind(styles);
  10. const Select = require("react-select");
  11.  
  12. declare interface IELPSourceTableProps {
  13. clickOnItemSettings?: any;
  14. timeStampOptions?: any;
  15. currentFileCSV?: any;
  16. isFirstRowColumnNames?: any;
  17. currentFileCSVTypes?: any;
  18. }
  19.  
  20. export class ELPSourceTable extends React.Component<any, any> {
  21. refs: any;
  22. constructor(props) {
  23. super(props);
  24. }
  25.  
  26. handleScroll = (event) => {
  27. let scrollTop = event.srcElement.scrollTop;
  28. this.props.changeTableScrollTopPosition(scrollTop);
  29. };
  30.  
  31. changeColCheckboxValue = (mapping, columnNum, newTable, existingTablesRadio) => {
  32. let currentCheckboxState = this.isColumnChecked(mapping, columnNum, newTable, existingTablesRadio);
  33. if (!existingTablesRadio) this.props.changeColCheckbox(columnNum, currentCheckboxState);
  34. };
  35.  
  36. componentWillUnmount () {
  37. if(this.refs.sourceTable) {
  38. this.refs.sourceTable.removeEventListener("scroll", this.handleScroll);
  39. }
  40.  
  41. }
  42.  
  43. componentWillReceiveProps(nextProps) {
  44. if ( nextProps.tableScrollTopPosition !== this.props.tableScrollTopPosition && this.refs.sourceTable) {
  45. this.refs.sourceTable.scrollTop = nextProps.tableScrollTopPosition;
  46. }
  47. //if ( nextProps.w1 !== this.props.w1 || nextProps.w2 !== this.props.w2 || nextProps.w3 !== this.props.w3) {
  48. // console.log("componentWillReceiveProps");
  49. // this.refs.headerRef_1.width = nextProps.w1 + "px";
  50. // this.refs.headerRef_2.width = nextProps.w2 + "px";
  51. // this.refs.headerRef_3.width = nextProps.w3 + "px";
  52. //}
  53. }
  54.  
  55. componentDidMount() {
  56. //this.props.resizeWindow(50, 40, 390);
  57. this.props.clickOnItemSettings(false, 0, 0);
  58. this.refs.sourceTable.addEventListener("scroll", this.handleScroll);
  59. this.refs.sourceTable.scrollTop = this.props.tableScrollTopPosition;
  60. //setTimeout(() => (
  61. //this.updateHeaderWidth(),
  62. //window.addEventListener("resize", this.updateHeaderWidth)), 1000);
  63. // document.getElementById('headerCol3').style.width = document.getElementById('column3').offsetWidth + 'px';
  64. // document.getElementById('headerCol2').style.width = document.getElementById('column2').offsetWidth + 'px';
  65. // document.getElementById('headerCol1').style.width = document.getElementById('column1').offsetWidth + 'px';
  66. }
  67.  
  68. updateHeaderWidth = () => {
  69. let w1 = this.refs.col1.getBoundingClientRect().width;
  70. let w2 = this.refs.col2.getBoundingClientRect().width;
  71. let w3 = this.refs.col3.getBoundingClientRect().width;
  72.  
  73. this.props.resizeWindow(w1, w2, w3);
  74. }
  75.  
  76. modalTimestampWindow = (params) => {
  77. let opions = {
  78. left:params.clientx,
  79. top:params.clienty - 307
  80. };
  81.  
  82. return (
  83. <div
  84. className={cx({timestampBlock:true}, {visible:params.bool})}
  85. style={opions}>
  86. <Select
  87. placeholder={"DD/MM/YYYY"}
  88. searchable={false}/>
  89. <Select
  90. placeholder={"HH:MM:SS:sss"}
  91. searchable={false}/>
  92. <button>OK</button>
  93. <button onClick={(event)=>{this.props.clickOnItemSettings(false, 0, 0)}}>Cancel</button>
  94. <button>Reset</button>
  95. </div>
  96. )
  97. };
  98.  
  99. fieldPreview = (currentFileCSV, columnNum, isFirstRowColumnNames) => {
  100. let fieldPreview = _.map(currentFileCSV, (file: any) => {
  101. return file[columnNum];
  102. });
  103.  
  104. if (isFirstRowColumnNames) {
  105. fieldPreview.shift();
  106. }
  107. return fieldPreview.join(", ");
  108. };
  109.  
  110. isColumnChecked = (mapping, columnNum, newTable, existingTablesRadio) => {
  111. return (existingTablesRadio) ? !_.get(mapping[columnNum], "skipped", true) : !_.get( _.get(newTable, "columns", [])[columnNum], "skipped", false);
  112. };
  113.  
  114. render() {
  115. const {clickOnItemSettings, timeStampOptions, currentFileCSV, isFirstRowColumnNames, currentFileCSVTypes, tableScrollTopPosition, mapping, newTable, existingTablesRadio} = this.props;
  116.  
  117. const maxLine: any = _.maxBy(currentFileCSV, _.property("length")) || [];
  118. const header = ( <div className={styles.tableHeader}>
  119. <table className={styles.sourceTableHeader}>
  120. <th className={styles.tableHeaderRow}>
  121. <td className={styles.col1} ref="headerRef_1" id="headerCol1">Select</td>
  122. <td className={styles.col2} ref="headerRef_2" id="headerCol2">ID</td>
  123. <td className={styles.col3} ref="headerRef_3" id="headerCol3">Field Preview</td>
  124. </th>
  125. </table>
  126. </div>);
  127. return (
  128. <div className={styles.ELPSourceTableContainer}>
  129. <div className={styles.tableContainer}>
  130. <div className={cx({tableBody:true}, {scrollDisabled:timeStampOptions.bool})} ref="sourceTable" >
  131. <div className={styles.tableCover}>
  132. <table className={styles.sourceTable} >
  133. <thead>
  134. <tr className={styles.tableHeaderRow}>
  135. <th className={styles.col1} id="headerCol1">Select
  136. <div className={styles.div} >Select</div>
  137. </th>
  138. <th className={styles.col2} id="headerCol2">ID
  139. <div className={styles.div}>ID</div></th>
  140. <th className={styles.col3} id="headerCol3">Field Preview
  141. <div className={styles.div}>Field Preview</div></th>
  142. </tr>
  143. </thead>
  144. <tbody>
  145. {
  146. maxLine.map((e, columnNum) => {
  147. return (
  148. <tr className={styles.tableRow}>
  149. <td className={styles.col1} ref="col1" id="column1">
  150. <input type="checkbox"
  151. id = {columnNum}
  152. defaultChecked = {true}
  153. checked = {this.isColumnChecked(mapping, columnNum, newTable, existingTablesRadio)}
  154. onClick = {() => this.changeColCheckboxValue(mapping, columnNum, newTable, existingTablesRadio)}/>
  155. <label htmlFor={columnNum}><i className="fa fa-check"/></label>
  156. </td>
  157. <td className={styles.col2}ref="col2" id="column2">{isFirstRowColumnNames ? e : columnNum + 1}</td>
  158. <td className={cx({col3:true})} ref="col3" id="column3">
  159. <div className={styles.text}>
  160. {this.fieldPreview(currentFileCSV, columnNum, isFirstRowColumnNames)}
  161. </div>
  162. {(currentFileCSVTypes[columnNum] === "TIMESTAMP") ? (<div
  163. className={styles.gear}
  164. onClick={(event) => {clickOnItemSettings(true, event.pageX, event.pageY);}}></div>) : ""}
  165. </td>
  166. </tr>
  167. );
  168. })
  169. }
  170. </tbody>
  171. </table>
  172. </div>
  173. </div>
  174. </div>
  175. </div>
  176. );
  177. }
  178. }
  179.  
  180. const mapStateToProps = (state, props) => ({
  181. tableScrollTopPosition: _.get(state, "loadConstructor.tableScrollTopPosition", 0),
  182. mapping: _.get(state, "loadConstructor.mapping"),
  183. newTable: _.get(state, "loadConstructor.newTable"),
  184. existingTablesRadio: _.get(state, "loadConstructor.existingTablesRadio", true),
  185. w1: _.get(state, "loadConstructor.w1"),
  186. w2: _.get(state, "loadConstructor.w2"),
  187. w3: _.get(state, "loadConstructor.w3"),
  188. });
  189.  
  190. const mapDispatchToProps = (dispatch, getState) => ({
  191. changeTableScrollTopPosition: (pos) => {
  192. dispatch(changeTableScrollTopPositionAction(pos));
  193. },
  194. changeColCheckbox: (columnNum, currentCheckboxState) => {
  195. dispatch(changeColCheckbox(columnNum, currentCheckboxState));
  196. },
  197. resizeWindow: (w1, w2, w3) => {
  198. dispatch(resizeWindowAction(w1, w2, w3));
  199. },
  200. });
  201.  
  202. export default connect(
  203. mapStateToProps,
  204. mapDispatchToProps
  205. )(ELPSourceTable);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement