-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathplaces-demo-preact.htm
More file actions
344 lines (311 loc) · 16.7 KB
/
places-demo-preact.htm
File metadata and controls
344 lines (311 loc) · 16.7 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title data-i18n="Countries Demo | Preact"></title>
<link rel="stylesheet" href="css/countries.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui-flag@2.4.0/flag.min.css">
<link rel="canonical" href="https://www.dataformsjs.com/examples/places-demo-hbs.htm">
</head>
<body>
<div id="root" dir="auto"></div>
<!--
Almost all code is the same on this file is the same as the React
version except that [jsxLoader.usePreact()] is called on the page
to support Preact and this page specifically uses several Preact
functions. All code in separate JSX files is shared between the
React version and this demo and works for both.
-->
<!--
Preact
https://preactjs.com
-->
<script src="https://cdn.jsdelivr.net/npm/preact@10.5.7/dist/preact.min.js"></script>
<!--
For testing with Preact DevTools include the following:
https://preactjs.github.io/preact-devtools/
-->
<script src="https://cdn.jsdelivr.net/npm/preact@10.5.7/debug/dist/debug.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/preact@10.5.7/devtools/dist/devtools.umd.js"></script>
<!-- DataFormsJS JSX Loader -->
<script src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/jsxLoader.min.js"></script>
<script>
// Including [jsxLoader] and calling [usePreact()] prior to loading
// React Router and other React Components allows for compatibility with Preact.
jsxLoader.usePreact();
</script>
<!--
React Router
https://reacttraining.com/react-router/
https://github.com/ReactTraining/react-router
-->
<script src="https://cdn.jsdelivr.net/npm/react-router@5.2.0/umd/react-router.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/react-router-dom@5.2.0/umd/react-router-dom.min.js"></script>
<!--
DataFormsJS React Components
The Core file includes commonly used React Components for data apps:
[ErrorBoundary, InputFilter, JsonData, LazyLoad, SortableTable]
and JavaScript Classes [Cache, Format]. All React Components are also
compatible with Preact when using jsxLoader.
Additional components and classes [LeafletMap, I18n] are loaded individually.
The [es6] scripts will be loaded for modern browsers because [type="module"] is used.
and [es5] scripts will be loaded for legacy browsers because [nomodule] is used.
-->
<script type="module" src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/es6/DataFormsJS.min.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/es6/I18n.min.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/es6/LeafletMap.min.js"></script>
<script nomodule src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/es5/DataFormsJS.min.js"></script>
<script nomodule src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/es5/I18n.min.js"></script>
<script nomodule src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/es5/LeafletMap.min.js"></script>
<!--
DataFormsJS Web Components
Excel and CSV Support are provided by Web Components for Modern Browsers only.
Web Components are a native DOM technology and can be used with all JS Frameworks.
-->
<script type="module" src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/web-components/export-to-csv-service.min.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/web-components/export-to-excel-service.min.js"></script>
<!--
Un-minimized Components would need to use [type="text/babel"] because
import React from 'react';
is used on most source files which is not compatible with web browsers.
-->
<!--
<script type="text/babel" src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/es6/ErrorBoundary.js"></script>
<script type="text/babel" src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/es6/LazyLoad.js"></script>
<script type="text/babel" src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/es6/JsonData.js"></script>
<script type="text/babel" src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/es6/InputFilter.js"></script>
<script type="text/babel" src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/es6/SortableTable.js"></script>
<script type="text/babel" src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/es6/I18n.js"></script>
<script type="text/babel" src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/es6/Format.js"></script>
<script type="text/babel" src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/es6/LeafletMap.js"></script>
<script type="text/babel" src="https://cdn.jsdelivr.net/npm/dataformsjs@5/js/react/es6/Cache.js"></script>
-->
<!--
JSX Templates for this App
Additionally [place-react.jsx] is loaded dynamically from <LazyLoad>
-->
<script type="text/babel" src="./html/regions-react.jsx"></script>
<script type="text/babel" src="./html/cities-react.jsx"></script>
<script type="text/babel" src="./html/places-info-react.jsx"></script>
<script type="text/babel" src="./html/search-places-react.jsx"></script>
<script type="text/babel">
const Router = window.ReactRouterDOM.HashRouter;
const Route = window.ReactRouterDOM.Route;
const Redirect = window.ReactRouterDOM.Redirect;
const Link = window.ReactRouterDOM.Link;
const defaultLocale = 'en';
const supportedLocales = ['en', 'ja', 'es', 'pt-BR', 'ar', 'fr', 'zh-CN'];
const fileName = 'countries';
const i18n = new I18n(defaultLocale, supportedLocales, fileName);
const defaultLang = i18n.getUserDefaultLang;
const format = new Format();
const supportsWebComponents = ('customElements' in window && 'define' in window.customElements);
// Basic function to allow row onClick events to highlight the selected row.
// This allows a user to easily see where they are on wide rows or mobile devices.
// Based on DataFormJS [clickToHighlight] Plugin: [js/plugins/clickToHighlight.js]
function toggleHighlight(e) {
if (e.target.nodeName === 'A') {
return;
}
e.currentTarget.classList.toggle('highlight');
}
function ShowLoading() {
return <h3>{i18n.text('Loading...')}</h3>;
}
function ShowError(props) {
return <p>{props.error}</p>;
}
function ShowCountries(props) {
return (
<>
<h1>{i18n.text('Countries')}</h1>
<nav>
<Link to={'/' + props.params.lang + '/search'}>{i18n.text('Search')}</Link>
<Link to={'/' + props.params.lang + '/info'}>{i18n.text('Info')}</Link>
<DownloadLinks fileName='Countries' />
</nav>
<InputFilter
filter-selector="table"
filter-results-selector="h1"
filter-results-text-all={i18n.text('{totalCount} Countries')}
filter-results-text-filtered={i18n.text('Showing {displayCount} of {totalCount} Countries')}
placeholder={i18n.text("Enter filter, example 'North America'")} />
<SortableTable
data-sort-class-odd="row-odd"
data-sort-class-even="row-even">
<thead>
<tr>
<th>{i18n.text('Code')}</th>
<th>{i18n.text('Name')}</th>
<th>{i18n.text('Size (KM)')}</th>
<th>{i18n.text('Population')}</th>
<th>{i18n.text('Continent')}</th>
</tr>
</thead>
<tbody>
{props.data && props.data.countries && props.data.countries.map(country => {
return (
<tr key={country.iso} onClick={toggleHighlight} className="pointer">
<td>
<i className={country.iso.toLowerCase() + ' flag'}></i>
<Link to={'/' + props.params.lang + '/regions/' + country.iso}>{country.iso}</Link>
</td>
<td>{country.country}</td>
<td className="align-right" data-value={country.area_km}>{format.number(country.area_km)}</td>
<td className="align-right" data-value={country.population}>{format.number(country.population)}</td>
<td>{country.continent}</td>
</tr>
)
})}
</tbody>
</SortableTable>
</>
)
}
function DownloadLinks({ fileName, noMarginLeft }) {
if (!supportsWebComponents) {
return null;
}
const className = (noMarginLeft ? 'download' : 'download ml-20');
return (<>
<div className="download-links">
<span className={className}>{i18n.text('Download')}</span>
<img className="download"
src="img/Excel.svg"
height="16"
width="16"
alt={i18n.text('Download to Excel')}
title={i18n.text('Download to Excel')}
data-export-excel-selector="table"
data-export-file-name={i18n.text(fileName) + '.xlsx'} />
<img className="download"
src="img/CSV.svg"
height="16"
width="16"
alt={i18n.text('Download to CSV')}
title={i18n.text('Download to CSV')}
data-export-csv-selector="table"
data-export-file-name={i18n.text(fileName) + '.csv'} />
</div>
<export-to-excel-service></export-to-excel-service>
<export-to-csv-service></export-to-csv-service>
</>)
}
// Download Data for the Main Page and show once ready
function PageCountries({match}) {
// NOTE - this can be updated to change which web service runs.
// Both options are included as examples.
const useGraphQL = true;
// JSON Web Service
if (useGraphQL === false) {
return (
<JsonData
url="https://www.dataformsjs.com/data/geonames/countries"
lang={match.params.lang}
isLoading={<ShowLoading />}
hasError={<ShowError />}
isLoaded={<ShowCountries />}
loadOnlyOnce={true} />
);
}
// GraphQL Service
// In this example the prop [variables] is used so language
// can be passed as a param to <ShowCountries> however it is
// not needed or used for the GraphQL service.
return (
<JsonData
url="https://www.dataformsjs.com/graphql"
graphQL={true}
query={`
{
countries {
iso
country
area_km
population
continent
}
}
`}
variables={{
lang: match.params.lang,
}}
isLoading={<ShowLoading />}
hasError={<ShowError />}
isLoaded={<ShowCountries />}
loadOnlyOnce={true} />
);
}
// Download leaflet (Maps) and [place-react.jsx] when the city page
// is first accessed. These scripts will not be downloaded unless the
// page is accessed. Once loaded `ShowCity` from [place-react.jsx]
// will be dynamically called.
function LoadMapAndPage(props) {
return (
<LazyLoad
scripts={[
// https://leafletjs.com
'https://cdn.jsdelivr.net/npm/leaflet@1.5.1/dist/leaflet.css',
'https://cdn.jsdelivr.net/npm/leaflet@1.5.1/dist/leaflet.js',
'./html/place-react.jsx',
]}
isLoading={<ShowLoading />}
isLoaded="ShowCity"
data={props.data}
params={props.params} />
);
}
function PageCity({match}) {
return (
<JsonData
url="https://www.dataformsjs.com/data/geonames/place/:id"
lang={match.params.lang}
id={match.params.id}
isLoading={<ShowLoading />}
hasError={<ShowError />}
isLoaded={<LoadMapAndPage />}
loadOnlyOnce={true} />
);
}
class App extends preact.Component {
constructor(props) {
super(props);
this.state = {
langLoaded: null,
};
}
componentDidMount() {
i18n.onLangStart(() => { this.setState({ langLoaded:false }); });
i18n.onLangLoaded(() => { this.setState({ langLoaded:true }); });
}
render() {
if (!this.state.langLoaded) {
return <ShowLoading />;
}
return (
<ErrorBoundary>
<Router>
<React.Fragment>
<Route exact path="/" render={() => (
<Redirect to={'/' + defaultLang + '/'} />
)}/>
<Route exact path="/:lang/" component={PageCountries} />
<Route path="/:lang/regions/:country" component={PageRegions} />
<Route path="/:lang/cities/:country/:region" component={PageCities} />
<Route path="/:lang/city/:id" component={PageCity} />
<Route path="/:lang/search" component={PageSearch} />
<Route path="/:lang/info" component={PageInfo} />
</React.Fragment>
</Router>
</ErrorBoundary>
)
}
}
preact.render(
<App />,
document.getElementById('root')
);
</script>
</body>
</html>