-
Notifications
You must be signed in to change notification settings - Fork 7
/
Section.jsx
328 lines (284 loc) · 10.7 KB
/
Section.jsx
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import React, {Component, Fragment} from "react";
import PropTypes from "prop-types";
import {connect} from "react-redux";
import {withNamespaces} from "react-i18next";
import {nest} from "d3-collection";
import {AnchorLink} from "@datawheel/canon-core";
import styles from "style.yml";
import isIE from "../../utils/isIE.js";
import throttle from "../../utils/throttle";
import pxToInt from "../../utils/formatters/pxToInt";
import toKebabCase from "../../utils/formatters/toKebabCase";
import Button from "../fields/Button";
import SourceGroup from "../Viz/SourceGroup";
import StatGroup from "../Viz/StatGroup";
import Parse from "./components/Parse";
import Selector from "./components/Selector";
import Default from "./Default";
import Grouping from "./Grouping";
import SubGrouping from "./SubGrouping";
import MultiColumn from "./MultiColumn";
import SingleColumn from "./SingleColumn";
import Tabs from "./Tabs";
// User must define custom sections in app/cms/sections, and export them from an index.js in that folder.
import * as CustomSections from "CustomSections";
// used to construct component
// NOTE: should be every Component in `components/sections/` except for Section (i.e., this component) and Hero (always rendered separately)
const sectionTypes = {Default, Grouping, SubGrouping, MultiColumn, SingleColumn, Tabs, ...CustomSections};
/** wrapper for all sections */
class Section extends Component {
constructor(props) {
super(props);
this.state = {
contents: props.contents,
loading: false,
isStickyIE: false,
selectors: {},
sources: [],
// Snapshots of the variables that have been changed by onSetVariables
// So we can reset these and only these to their original values.
changedVariables: {},
showReset: false
};
// used for IE sticky fallback
this.section = React.createRef();
this.scrollBind = this.handleScroll.bind(this);
}
componentDidMount() {
const stickySection = this.state.contents.position === "sticky";
const currentSection = this.section.current;
// make sure the section is sticky
if (stickySection === true && typeof window !== "undefined") {
window.addEventListener("scroll", this.scrollBind);
this.setState({
// combine the position
top: currentSection.getBoundingClientRect().top + document.documentElement.scrollTop,
height: currentSection.getBoundingClientRect().height
});
}
}
componentWillUnmount() {
window.removeEventListener("scroll", this.scrollBind);
}
componentDidUpdate(prevProps) {
if (JSON.stringify(prevProps.contents) !== JSON.stringify(this.props.contents)) {
this.setState({contents: this.props.contents});
this.updateSource.bind(this)(false);
}
}
updateSource(newSources) {
if (!newSources) this.setState({sources: []});
else {
const {sources} = this.state;
newSources
.map(s => s.annotations)
.forEach(source => {
if (source && source.source_name && !sources.find(s => s.source_name === source.source_name)) sources.push(source);
});
this.setState({sources});
}
}
/**
* Sections may be embedded as part of a Front-end Profile OR as a previewed section in the CMS. As a front-end
* Profile, formatters and variables come in through context, and as a CMS Preview these come through as props.
* (This is similar to Viz.jsx which can also be expressed as a front-end component or a CMS preview)
*/
getChildContext() {
const {comparison} = this.props.contents;
return {
formatters: this.props.formatters || this.context.formatters,
variables: this.props.variables || this.context[comparison ? "compVariables" : "variables"],
onSetVariables: this.onSetVariables.bind(this),
updateSource: this.updateSource.bind(this)
};
}
/**
* Sections has received an onSetVariables function from props. However, this Section needs to
* keep track locally of what it has changed, so that when a "reset" button is clicked, it can set
* the variables back to their original state. This local intermediary function, passed down via context,
* is responsible for keeping track of that, then in turn calling the props version of the function.
*/
onSetVariables(newVariables, forceMats, isComparison) {
const initialVariables = this.props.initialVariables || this.context.initialVariables || {};
const changedVariables = {};
Object.keys(newVariables).forEach(key => {
changedVariables[key] = initialVariables[key];
});
this.setState({
changedVariables,
showReset: Object.keys(changedVariables).length > 0
});
if (this.props.onSetVariables) this.props.onSetVariables(newVariables, forceMats, isComparison);
}
/**
* When the user clicks reset, take the snapshot of the variables they changed and use them to
* revert only those variables via the props function.
*/
resetVariables() {
const {changedVariables} = this.state;
const {comparison} = this.props.contents;
if (this.props.onSetVariables) this.props.onSetVariables(changedVariables, false, comparison);
this.setState({
changedVariables: {},
showReset: false
});
}
handleScroll() {
const stickySection = this.state.contents.position === "sticky";
// make sure the current section is sticky & the document window exists
if (stickySection === true && isIE) {
const isStickyIE = this.state.isStickyIE;
const containerTop = this.state.top;
const screenTop = document.documentElement.scrollTop + pxToInt(styles["sticky-section-offset"] || "50px");
throttle(() => {
if (screenTop !== containerTop) {
if (containerTop < screenTop && !isStickyIE) {
this.setState({isStickyIE: true});
}
else if (containerTop > screenTop && isStickyIE) {
this.setState({isStickyIE: false});
}
}
});
}
}
render() {
const {contents, sources, isStickyIE, height, showReset} = this.state;
const {headingLevel, hideAnchor, hideOptions, isModal, loading, t} = this.props;
const {configOverride} = contents;
// remap old section names
const layout = contents.type;
const layoutClass = `cp-${toKebabCase(layout)}-section`;
const Layout = contents.position === "sticky" ? Default : sectionTypes[layout] || Default; // assign the section layout component
const {descriptions, slug, stats, subtitles, title, visualizations} = contents;
const selectors = contents.selectors || [];
let showAnchor = true;
if (isModal || hideAnchor) showAnchor = false;
// heading & subhead(s)
const mainTitle = <Fragment>
{title &&
<div className={`cp-section-heading-wrapper ${layoutClass}-heading-wrapper`}>
<Parse El={headingLevel} id={slug} className={`cp-section-heading ${layoutClass}-heading${layout !== "Hero" && showAnchor ? " cp-section-anchored-heading" : ""}`} tabIndex="0">
{title}
</Parse>
{showAnchor &&
<AnchorLink to={slug} className={`cp-section-heading-anchor ${layoutClass}-heading-anchor`}>
#<span className="u-visually-hidden">permalink to section</span>
</AnchorLink>
}
</div>
}
</Fragment>;
const subTitle = <Fragment>
{contents.position !== "sticky" && subtitles.map((content, i) =>
<Parse className={`cp-section-subhead display ${layoutClass}-subhead`} key={`${content.subtitle}-subhead-${i}`}>
{content.subtitle}
</Parse>
)}
</Fragment>;
const heading = <Fragment>
{mainTitle}
{subTitle}
</Fragment>;
// filters
const filters = selectors.map(selector =>
<Selector
key={selector.id}
{...selector}
loading={loading}
fontSize="xxs"
/>
);
// stats
let statContent;
if (contents.position !== "sticky") {
const statGroups = nest().key(d => d.title).entries(stats);
if (stats.length > 0) {
statContent = <div className={`cp-stat-group-wrapper${stats.length === 1 ? " single-stat" : ""}`}>
{statGroups.map(({key, values}) => <StatGroup key={key} title={key} stats={values} />)}
</div>;
}
}
// paragraphs
let paragraphs;
if (descriptions.length && contents.position !== "sticky") {
paragraphs = descriptions.map((content, i) =>
<Parse className={`cp-section-paragraph ${layoutClass}-paragraph`} key={`${content.description}-paragraph-${i}`}>
{content.description}
</Parse>
);
}
// sources
const sourceContent = <SourceGroup sources={sources} />;
// reset button
const resetButton = showReset ? <Button
onClick={this.resetVariables.bind(this)}
className={`cp-var-reset-button ${layoutClass}-var-reset-button`}
fontSize="xs"
icon="undo"
iconPosition="left"
key="var-reset-button"
>
{t("CMS.Section.Reset visualizations")}
</Button> : null;
const componentProps = {
slug,
title,
heading,
mainTitle,
subTitle,
filters,
stats: statContent,
sources: sourceContent,
paragraphs: layout === "Tabs" ? contents.descriptions : paragraphs,
resetButton,
visualizations: contents.position !== "sticky" ? visualizations : [],
vizHeadingLevel: `h${parseInt(headingLevel.replace("h", ""), 10) + 1}`,
hideOptions,
loading,
contents,
configOverride
};
return (
<Fragment>
<section
className={`cp-section cp-${toKebabCase(contents.type)}-section${
contents.position === "sticky" ? " is-sticky" : ""
}${
isStickyIE ? " ie-is-stuck" : ""
}${
isModal ? " cp-modal-section" : ""
}`}
ref={this.section}
id={`cp-section-${contents.id}`}
key={`cp-section-${contents.id}`}
>
<Layout {...componentProps} />
</section>
{/* in IE, create empty div set to the height of the stuck element */}
{isStickyIE ? <Fragment>
<div className="ie-sticky-spacer" style={{height}} />
<div className="ie-sticky-section-color-fixer" />
</Fragment> : ""}
</Fragment>
);
}
}
Section.defaultProps = {
headingLevel: "h2"
};
Section.contextTypes = {
formatters: PropTypes.object,
variables: PropTypes.object,
initialVariables: PropTypes.object,
compVariables: PropTypes.object
};
Section.childContextTypes = {
formatters: PropTypes.object,
variables: PropTypes.object,
onSetVariables: PropTypes.func,
updateSource: PropTypes.func
};
export default withNamespaces()(connect(state => ({
locale: state.i18n.locale
}))(Section));