forked from mapbox/mapbox-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
240 lines (227 loc) · 8.12 KB
/
Copy pathapp.js
File metadata and controls
240 lines (227 loc) · 8.12 KB
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
import React from 'react';
import Navigation from './navigation';
import Content from './content';
import RoundedToggle from './rounded_toggle';
import PureRenderMixin from 'react-pure-render/mixin';
import GithubSlugger from 'github-slugger';
import debounce from 'lodash.debounce';
import { brandNames, brandClasses } from '../custom';
import qs from 'querystring';
let slugger = new GithubSlugger();
let slug = title => { slugger.reset(); return slugger.slug(title); };
let languageOptions = [
{ title: 'Java',
short: 'Java',
value: 'java' },
{ title: 'Groovy',
short: 'Groovy',
value: 'groovy' },
{ title: 'Gradle',
short: 'Gradle',
value: 'gradle' },
{ title: 'cURL',
short: 'cURL',
value: 'curl' },
{ title: 'CLI',
short: 'cli',
value: 'cli' },
{ title: 'Python',
short: 'Python',
value: 'python' },
{ title: 'JavaScript',
short: 'JS',
value: 'javascript' },
{ title: 'Objective-C',
short: 'ObjC',
value: 'objc' },
{ title: 'Swift',
short: 'Swift',
value: 'swift' },
];
let defaultLanguage = languageOptions[0];
let debouncedReplaceState = debounce(hash => {
window.history.replaceState('', '', hash);
}, 100);
var App = React.createClass({
mixins: [PureRenderMixin],
propTypes: {
content: React.PropTypes.string.isRequired,
ast: React.PropTypes.object.isRequired
},
getInitialState() {
var active = 'Introduction';
if (process.browser) {
let hash = window.location.hash.split('#').pop();
let languageFromURL = qs.parse(window.location.search.substring(1)).language;
let language = languageOptions.find(option => option.title === languageFromURL) ||
defaultLanguage;
let mqls = [
{ name: 'widescreen', query: window.matchMedia('(min-width: 1200px)') },
{ name: 'desktop', query: window.matchMedia('(min-width: 961px)') },
{ name: 'tablet', query: window.matchMedia('(max-width: 960px)') },
{ name: 'mobile', query: window.matchMedia('(max-width: 640px)') }
];
mqls.forEach(q => q.query.addListener(this.mediaQueryChanged));
if (hash) {
let headingForHash = this.props.ast.children
.filter(child => child.type === 'heading')
.find(heading => heading.data.id === hash);
if (headingForHash) {
active = headingForHash.children[0].value;
}
}
return {
// media queryMatches
mqls: mqls,
// object of currently matched queries, like { desktop: true }
queryMatches: {},
language: language,
columnMode: 2,
activeSection: active,
// for the toggle-able navigation on mobile
showNav: false
};
} else {
return {
mqls: { },
queryMatches: {
desktop: true
},
language: defaultLanguage,
activeSection: '',
showNav: false
};
}
},
toggleNav() {
this.setState({ showNav: !this.state.showNav });
},
componentDidMount() {
this.mediaQueryChanged();
this.onScroll = debounce(this.onScrollImmediate, 100);
document.addEventListener('scroll', this.onScroll);
this.onScrollImmediate();
},
onScrollImmediate() {
var sections = document.querySelectorAll('div.section');
if (!sections.length) return;
for (var i = 0; i < sections.length; i++) {
var rect = sections[i].getBoundingClientRect();
if (rect.bottom > 0) {
this.setState({
activeSection: sections[i].getAttribute('data-title')
});
return;
}
}
},
mediaQueryChanged() {
this.setState({
queryMatches: this.state.mqls.reduce((memo, q) => {
memo[q.name] = q.query.matches;
return memo;
}, {})
});
},
componentWillUnmount() {
this.state.mqls.forEach(q => q.removeListener(this.mediaQueryChanged));
document.body.removeEventListener('scroll', this.onScroll);
},
onChangeLanguage(language) {
this.setState({ language }, () => {
if (window.history) {
window.history.pushState(null, null,
`?${qs.stringify({ language: language.title })}${window.location.hash}`);
}
});
},
componentDidUpdate(_, prevState) {
if (prevState.activeSection !== this.state.activeSection) {
// when the section changes, replace the hash
debouncedReplaceState(`#${slug(this.state.activeSection)}`);
} else if (prevState.language.title !== this.state.language.title ||
prevState.columnMode !== this.state.columnMode) {
// when the language changes, use the hash to set scroll
window.location.hash = window.location.hash;
}
},
navigationItemClicked(activeSection) {
setTimeout(() => {
this.setState({ activeSection });
}, 10);
if (!this.state.queryMatches.desktop) {
this.toggleNav();
}
},
toggleColumnMode() {
this.setState({
columnMode: this.state.columnMode === 1 ? 2 : 1
});
},
render() {
let ast = JSON.parse(JSON.stringify(this.props.ast));
let { activeSection, queryMatches, showNav, columnMode } = this.state;
let col1 = columnMode === 1 && queryMatches.desktop;
return (<div className='container unlimiter'>
{/* Content background */ }
{(!col1 && !queryMatches.mobile) && <div className={`fixed-top fixed-right ${queryMatches.desktop && 'space-left16'}`}>
<div className='fill-light col6 pin-right'></div>
</div>}
{/* Desktop nav */ }
{queryMatches.desktop && <div className='space-top5 scroll-styled overflow-auto pad1 width16 sidebar fixed-left fill-dark dark'>
<Navigation
navigationItemClicked={this.navigationItemClicked}
activeSection={activeSection}
ast={ast} />
</div>}
{/* Content */ }
<div className={`${queryMatches.desktop && 'space-left16'}`}>
<div className={col1 ? 'col8 margin1' : ''}>
<Content
leftClassname={col1 ? 'space-bottom4 pad2x prose clip' : 'space-bottom8 col6 pad2x prose clip'}
rightClassname={col1 ? 'space-bottom2 pad2 prose clip fill-light space-top5' : 'space-bottom4 col6 pad2 prose clip fill-light space-top5'}
ast={ast}
language={this.state.language}/>
</div>
</div>
{/* Language toggle */ }
<div className={`fixed-top ${queryMatches.desktop && 'space-left16'}`}>
<div className={`events fill-light bottom-shadow pad1 ${col1 ? '' : 'col6 pin-topright'} ${queryMatches.tablet ? 'dark fill-blue' : ''} ${queryMatches.mobile ? 'space-top5 fixed-topright' : ''}`}>
<div className='fr pad0'>
{queryMatches.desktop ?
<a
title={`Display as ${col1 ? 2 : 1} column`}
onClick={this.toggleColumnMode}
style={{ cursor: 'pointer' }}
className={`icon quiet caret-${col1 ? 'right' : 'left'} pad0 fill-darken0 round`}></a> : null}
</div>
</div>
</div>
{/* Header */ }
<div className={`fill-dark dark bottom-shadow fixed-top ${queryMatches.tablet ? 'pad1y pad2x col6' : 'pad0 width16'}`}>
<a href='/' className={`active space-top1 space-left1 pin-topleft icon round dark pad0 ${brandClasses}`}></a>
<div className={`strong small pad0
${queryMatches.mobile ? 'space-left3' : ''}
${queryMatches.tablet ? 'space-left2' : 'space-left4 line-height15' }`}>
{queryMatches.desktop ? brandNames.desktop :
queryMatches.mobile ? brandNames.mobile : brandNames.tablet}
</div>
{queryMatches.tablet && <div>
<button
onClick={this.toggleNav}
className={`short quiet pin-topright button rcon ${showNav ? 'caret-up' : 'caret-down'} space-right1 space-top1`}>
<span className='micro'>{activeSection}</span>
</button>
{showNav && <div
className='fixed-left keyline-top fill-dark pin-left col6 pad2 scroll-styled space-top5'>
<Navigation
navigationItemClicked={this.navigationItemClicked}
activeSection={activeSection}
ast={ast} />
</div>}
</div>}
</div>
</div>);
}
});
module.exports = App;