|
| 1 | +import React, { useState, useEffect } from "react"; |
| 2 | +import { |
| 3 | + HashRouter as Router, |
| 4 | + Switch, |
| 5 | + Route, |
| 6 | + NavLink, |
| 7 | + Redirect, |
| 8 | + useLocation, |
| 9 | + withRouter, |
| 10 | +} from "react-router-dom"; |
| 11 | + |
| 12 | +import classes from "classnames"; |
| 13 | +import GanttMin from "./GanttMin"; |
| 14 | +import GanttBackend from "./GanttBackend"; |
| 15 | +import GanttScales from "./GanttScales"; |
| 16 | +import GanttGrid from "./GanttGrid"; |
| 17 | +import GanttForm from "./GanttForm"; |
| 18 | +import GanttNoGrid from "./GanttNoGrid"; |
| 19 | +import GanttReadOnly from "./GanttReadOnly"; |
| 20 | +import GanttSizes from "./GanttSizes"; |
| 21 | +import GanttMultiple from "./GanttMultiple"; |
| 22 | +import GanttPerformance from "./GanttPerformance"; |
| 23 | +import GanttMarkers from "./GanttMarkers"; |
| 24 | +import GanttTooltips from "./GanttTooltips"; |
| 25 | +import GanttText from "./GanttText"; |
| 26 | + |
| 27 | +import { MaterialTheme, DefaultTheme } from "@dhtmlx/trial-react-gantt"; |
| 28 | + |
| 29 | +import css from "./Demos.module.css"; |
| 30 | + |
| 31 | +const skins = [ |
| 32 | + { |
| 33 | + id: "default", |
| 34 | + name: "Classic", |
| 35 | + settings: { borders: "full", cellHeight: 38 }, |
| 36 | + }, |
| 37 | + { |
| 38 | + id: "material", |
| 39 | + name: "Material", |
| 40 | + settings: { borders: "", cellHeight: 32 }, |
| 41 | + }, |
| 42 | +]; |
| 43 | +const skinSettings = {}; |
| 44 | +skins.forEach((a) => (skinSettings[a.id] = a.settings)); |
| 45 | + |
| 46 | +const demos = [ |
| 47 | + ["/base", "Gantt basic", GanttMin], |
| 48 | + ["/backend", "Gantt with backend", GanttBackend], |
| 49 | + ["/scales", "Custom scales", GanttScales], |
| 50 | + ["/grid", "Custom grid", GanttGrid], |
| 51 | + ["/form", "Custom edit form", GanttForm], |
| 52 | + ["/chart", "Without the grid", GanttNoGrid], |
| 53 | + ["/readonly", "Readonly mode", GanttReadOnly], |
| 54 | + ["/sizes", "Scale / cell sizes", GanttSizes], |
| 55 | + ["/many", "Many gantts per page", GanttMultiple], |
| 56 | + ["/performance", "Performance", GanttPerformance], |
| 57 | + ["/markers", "Markers", GanttMarkers], |
| 58 | + ["/tooltips", "Tooltips", GanttTooltips], |
| 59 | + ["/templates", "Custom text", GanttText], |
| 60 | +]; |
| 61 | + |
| 62 | +function showTitle() { |
| 63 | + return <div className={css.title}>Gantt Demos</div>; |
| 64 | +} |
| 65 | + |
| 66 | +function showHint(title) { |
| 67 | + return <div className={css.hint}>{title}</div>; |
| 68 | +} |
| 69 | + |
| 70 | +function showDemoList(skin) { |
| 71 | + return ( |
| 72 | + <div className={css.demos}> |
| 73 | + {demos.map((data) => ( |
| 74 | + <NavLink |
| 75 | + activeClassName={css.active} |
| 76 | + className={css.demo} |
| 77 | + to={`${data[0]}/${skin}`} |
| 78 | + key={data} |
| 79 | + > |
| 80 | + {data[1]} |
| 81 | + </NavLink> |
| 82 | + ))} |
| 83 | + </div> |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +function Routes({ history }) { |
| 88 | + const [skin, setSkin] = useState({}); |
| 89 | + const [page, setPage] = useState({}); |
| 90 | + const [show, setShow] = useState(false); |
| 91 | + const [title, setTitle] = useState(""); |
| 92 | + |
| 93 | + const onClick = () => { |
| 94 | + if (!show) setShow(true); |
| 95 | + }; |
| 96 | + |
| 97 | + const onHide = () => { |
| 98 | + if (show) setShow(false); |
| 99 | + }; |
| 100 | + |
| 101 | + const toggleSkin = (e) => { |
| 102 | + e.stopPropagation(); |
| 103 | + const data = e.target.dataset; |
| 104 | + if (data.role === "skin") { |
| 105 | + setSkin(skins.find((a) => a.id === data.id)); |
| 106 | + history.push(`/${page}/${data.id}`); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + let location = useLocation(); |
| 111 | + useEffect(() => { |
| 112 | + const parts = location.pathname.split("/"); |
| 113 | + if (parts.length === 3) { |
| 114 | + setPage(parts[1]); |
| 115 | + setSkin(parts[2]); |
| 116 | + setTitle(demos.find((a) => a[0] === "/" + parts[1])[1]); |
| 117 | + } |
| 118 | + }, [location]); |
| 119 | + |
| 120 | + return ( |
| 121 | + <Router> |
| 122 | + <div className={css.layout}> |
| 123 | + <MaterialTheme></MaterialTheme> |
| 124 | + <DefaultTheme></DefaultTheme> |
| 125 | + <div |
| 126 | + className={classes(css.sidebar, { [css.move]: show })} |
| 127 | + onClick={onClick} |
| 128 | + > |
| 129 | + {show && showTitle()} |
| 130 | + <div |
| 131 | + className={classes(css.skins, { [css.move]: !show })} |
| 132 | + onClick={toggleSkin} |
| 133 | + > |
| 134 | + {skins.map((data, i) => ( |
| 135 | + <div |
| 136 | + key={i} |
| 137 | + className={classes(css.skin, { |
| 138 | + [css.selected]: data.id === skin, |
| 139 | + })} |
| 140 | + data-role="skin" |
| 141 | + data-id={data.id} |
| 142 | + > |
| 143 | + {data.name} |
| 144 | + </div> |
| 145 | + ))} |
| 146 | + </div> |
| 147 | + {show ? showDemoList(skin) : showHint(title)} |
| 148 | + </div> |
| 149 | + |
| 150 | + <div |
| 151 | + className={classes(css.content, { [css.move]: show }, "wx-" + skin)} |
| 152 | + onClick={onHide} |
| 153 | + > |
| 154 | + <Switch> |
| 155 | + <Route |
| 156 | + exact |
| 157 | + path="/" |
| 158 | + render={() => <Redirect to="/base/default"></Redirect>} |
| 159 | + /> |
| 160 | + {demos.map((data) => ( |
| 161 | + <Route |
| 162 | + key={data[0]} |
| 163 | + path={`${data[0]}/:skin`} |
| 164 | + render={({ match }) => { |
| 165 | + const Demo = data[2]; |
| 166 | + return <Demo {...skinSettings[match.params.skin]} />; |
| 167 | + }} |
| 168 | + /> |
| 169 | + ))} |
| 170 | + </Switch> |
| 171 | + {/* <demo.comp /> */} |
| 172 | + </div> |
| 173 | + </div> |
| 174 | + </Router> |
| 175 | + ); |
| 176 | +} |
| 177 | + |
| 178 | +const RoutesWithHistory = withRouter(Routes); |
| 179 | + |
| 180 | +function Demos() { |
| 181 | + return ( |
| 182 | + <Router> |
| 183 | + <RoutesWithHistory /> |
| 184 | + </Router> |
| 185 | + ); |
| 186 | +} |
| 187 | + |
| 188 | +export default Demos; |
0 commit comments