|
| 1 | +import ReactDOM from "react-dom"; |
| 2 | +import React from "react"; |
| 3 | +import { createStore } from "redux"; |
| 4 | +import { Provider } from "react-redux"; |
| 5 | + |
| 6 | +import rootReducer, { getInitialState } from "./reducers"; |
| 7 | +import { |
| 8 | + initializeStore, |
| 9 | + updateDimensions, |
| 10 | + applyForceAreas, |
| 11 | + applyForcePapers, |
| 12 | +} from "./actions"; |
| 13 | + |
| 14 | +import applyHeadstartMiddleware from "./middleware"; |
| 15 | + |
| 16 | +import { applyForce } from "./utils/force"; |
| 17 | + |
| 18 | +import { getChartSize, getListSize } from "./utils/dimensions"; |
| 19 | +import Headstart from "./components/Headstart"; |
| 20 | +import { removeQueryParams } from "./utils/url"; |
| 21 | +import debounce from "./utils/debounce"; |
| 22 | +import DataManager from "./dataprocessing/managers/DataManager"; |
| 23 | +import FetcherFactory from "./dataprocessing/fetchers/FetcherFactory"; |
| 24 | +import onBackButtonClick from "./utils/backButton"; |
| 25 | + |
| 26 | +import Bowser from "bowser"; |
| 27 | + |
| 28 | +class HeadstartRunner { |
| 29 | + constructor(config) { |
| 30 | + this.config = config; |
| 31 | + this.dataManager = new DataManager(config); |
| 32 | + |
| 33 | + this.originalTitle = document.title; |
| 34 | + this.actionQueue = []; |
| 35 | + |
| 36 | + const initialState = getInitialState(this.config); |
| 37 | + const middleware = applyHeadstartMiddleware(this); |
| 38 | + this.store = createStore(rootReducer, initialState, middleware); |
| 39 | + |
| 40 | + this.hsContainer = document.getElementById(this.config.tag); |
| 41 | + this.hsContainer.classList.add("headstart"); |
| 42 | + |
| 43 | + this.backendData = []; |
| 44 | + } |
| 45 | + |
| 46 | + async run() { |
| 47 | + this.checkBrowserVersion(); |
| 48 | + this.renderReact(); |
| 49 | + this.addBackButtonListener(); |
| 50 | + this.backendData = await this.fetchData(); |
| 51 | + this.dispatchDataEvent(this.backendData); |
| 52 | + this.initStore(this.backendData); |
| 53 | + this.applyQueryParams(); |
| 54 | + this.addWindowResizeListener(); |
| 55 | + } |
| 56 | + |
| 57 | + checkBrowserVersion() { |
| 58 | + const browser = Bowser.getParser(window.navigator.userAgent); |
| 59 | + // TODO use proper browser filtering https://www.npmjs.com/package/bowser#filtering-browsers |
| 60 | + if ( |
| 61 | + !["chrome", "firefox", "safari"].includes(browser.getBrowserName(true)) |
| 62 | + ) { |
| 63 | + alert( |
| 64 | + "You are using an unsupported browser. " + |
| 65 | + "This visualization was successfully tested " + |
| 66 | + "with the latest versions of Chrome, Firefox and Safari." |
| 67 | + ); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + renderReact() { |
| 72 | + ReactDOM.render( |
| 73 | + <Provider store={this.store}> |
| 74 | + <Headstart /> |
| 75 | + </Provider>, |
| 76 | + this.hsContainer |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + addBackButtonListener() { |
| 81 | + const handleBackButtonClick = () => { |
| 82 | + onBackButtonClick(this.dataManager, this.store, this.config); |
| 83 | + }; |
| 84 | + |
| 85 | + window.addEventListener("popstate", debounce(handleBackButtonClick, 300)); |
| 86 | + } |
| 87 | + |
| 88 | + async fetchData() { |
| 89 | + const config = this.config; |
| 90 | + |
| 91 | + const dataFetcher = FetcherFactory.getInstance(config.mode, { |
| 92 | + serverUrl: config.server_url, |
| 93 | + files: config.files, |
| 94 | + isStreamgraph: config.is_streamgraph, |
| 95 | + }); |
| 96 | + |
| 97 | + return await dataFetcher.getData(); |
| 98 | + } |
| 99 | + |
| 100 | + initStore(backendData) { |
| 101 | + const config = this.config; |
| 102 | + |
| 103 | + const { size, width, height } = getChartSize(config); |
| 104 | + |
| 105 | + this.dataManager.parseData(backendData, size); |
| 106 | + |
| 107 | + const context = this.dataManager.context; |
| 108 | + |
| 109 | + const list = getListSize(config, context, size); |
| 110 | + |
| 111 | + this.store.dispatch( |
| 112 | + initializeStore( |
| 113 | + config, |
| 114 | + context, |
| 115 | + this.dataManager.papers, |
| 116 | + this.dataManager.areas, |
| 117 | + this.dataManager.streams, |
| 118 | + size, |
| 119 | + width, |
| 120 | + height, |
| 121 | + list.height, |
| 122 | + this.dataManager.scalingFactors |
| 123 | + ) |
| 124 | + ); |
| 125 | + |
| 126 | + if (!this.config.is_streamgraph) { |
| 127 | + this.applyForceLayout(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + applyQueryParams() { |
| 132 | + const queryParams = new URLSearchParams(window.location.search); |
| 133 | + // enable this for ability to share link to a zoomed bubble / paper |
| 134 | + // if (queryParams.has("paper")) { |
| 135 | + // selectUrlPaper(this.dataManager, this.store, this.config); |
| 136 | + // } else { |
| 137 | + // zoomUrlArea(this.dataManager, this.store, this.config); |
| 138 | + // } |
| 139 | + // remove the following lines if the previous line is uncommented |
| 140 | + const paramsToRemove = []; |
| 141 | + if (queryParams.has("area")) { |
| 142 | + paramsToRemove.push("area"); |
| 143 | + } |
| 144 | + if (queryParams.has("paper")) { |
| 145 | + paramsToRemove.push("paper"); |
| 146 | + } |
| 147 | + if (paramsToRemove.length > 0) { |
| 148 | + removeQueryParams(...paramsToRemove); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + addWindowResizeListener() { |
| 153 | + window.addEventListener("resize", () => { |
| 154 | + const chart = getChartSize(this.config, this.dataManager.context); |
| 155 | + const list = getListSize( |
| 156 | + this.config, |
| 157 | + this.dataManager.context, |
| 158 | + chart.size |
| 159 | + ); |
| 160 | + this.store.dispatch(updateDimensions(chart, list)); |
| 161 | + }); |
| 162 | + } |
| 163 | + |
| 164 | + // used after each INITIALIZE |
| 165 | + applyForceLayout() { |
| 166 | + const state = this.store.getState(); |
| 167 | + applyForce( |
| 168 | + state.areas.list, |
| 169 | + state.data.list, |
| 170 | + state.chart.width, |
| 171 | + (newAreas) => |
| 172 | + this.store.dispatch(applyForceAreas(newAreas, state.chart.height)), |
| 173 | + (newPapers) => |
| 174 | + this.store.dispatch(applyForcePapers(newPapers, state.chart.height)), |
| 175 | + this.config |
| 176 | + ); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Dispatches a custom event that the data has been loaded for reuse outside of Headstart. |
| 181 | + * @param {Object} data the raw data |
| 182 | + */ |
| 183 | + dispatchDataEvent(data) { |
| 184 | + const elem = document.getElementById(this.config.tag); |
| 185 | + const event = new CustomEvent("headstart.data.loaded", { |
| 186 | + detail: { data }, |
| 187 | + }); |
| 188 | + elem.dispatchEvent(event); |
| 189 | + } |
| 190 | + |
| 191 | + rescaleMap(scaleBy, baseUnit, isContentBased, initialSort) { |
| 192 | + this.config.scale_by = scaleBy; |
| 193 | + this.config.base_unit = baseUnit; |
| 194 | + this.config.content_based = isContentBased; |
| 195 | + this.config.initial_sort = initialSort; |
| 196 | + // TODO this might cause a bug (or there might be more params that require setting to false) |
| 197 | + // bug description: map different after rescaling to the same metric |
| 198 | + this.config.dynamic_sizing = false; |
| 199 | + |
| 200 | + this.initStore(this.backendData); |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +export default HeadstartRunner; |
0 commit comments