This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
develop.js
165 lines (144 loc) · 4.28 KB
/
develop.js
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
'use strict'
const { render } = require('react-dom')
const { Provider, connect } = require('react-redux')
const { createStore } = require('redux')
const h = require('react-hyperscript')
const { ThemeProvider } = require('@zendeskgarden/react-theming')
const indexFeatures = (features, gazetteer) => {
gazetteer.features.forEach(feature => {
features[feature.id] = feature
})
return features
}
const gazetteers = Object.values(require('./places').graphs)
const features = gazetteers.reduce(indexFeatures, {})
const featureFocused = key => ({type: 'FEATURE_FOCUSED', key})
const featuresSelected = keys => ({type: 'FEATURES_SELECTED', keys})
const suggestionsRequested = query => ({type: 'SUGGESTIONS_REQUESTED', query})
const capitalize = s => s.replace(/(?:^|\s)\S/g, a => a.toUpperCase())
const renderSuggestion = (suggestion, match) => {
const begin = match.search(suggestion)
if (begin < 0) {
return h('span', suggestion)
} else {
return h('span', [
suggestion.slice(0, begin),
h('strong', capitalize(match.query)),
suggestion.slice(begin + match.query.length)
])
}
}
const escapeRegexCharacters = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
const matcher = query => {
const trimmedQuery = query.trim()
const escapedQuery = escapeRegexCharacters(trimmedQuery)
if (escapedQuery === '') {
return {
// match everything on empty query
test: () => true,
search: () => -1
}
}
const regex = new RegExp('\\b' + escapedQuery, 'i')
return {
test: s => regex.test(s),
search: s => s.search(regex),
query: trimmedQuery
}
}
const matches = query => {
const match = matcher(query)
return (matched, feature) => {
const add = (key, label) => {
matched.push([key, renderSuggestion(label, match), label])
return matched
}
if (! (feature && feature.properties)) {
return matched
}
if (match.test(feature.properties.title)) {
return add(feature.id, feature.properties.title)
}
if (! feature.names) {
return matched
}
for (let name of feature.names) {
if (match.test(name.toponym)) {
return add(feature.id, name.toponym)
}
}
return matched
}
}
const getSuggestions = (query = '') => gazetteers
.map(gazetteer => ({
title: gazetteer.title,
suggestions: gazetteer.features.reduce(matches(query), [])
}))
.filter(section => section.suggestions.length > 0)
const keysToFeatures = keys => {
const o = {}
keys.forEach(key => { o[key] = features[key] })
return o
}
const initialState = {
suggestions: getSuggestions(),
selectedFeatures: {},
focusedFeature: undefined
}
const app = (state = initialState, action) => {
switch (action.type) {
case 'FEATURE_FOCUSED':
return {...state, ...{focusedFeature: features[action.key]}}
case 'FEATURES_SELECTED':
return {...state, ...{selectedFeatures: keysToFeatures(action.keys)}}
case 'SUGGESTIONS_REQUESTED':
return {...state, ...{suggestions: getSuggestions(action.query)}}
default:
return state
}
}
const store = createStore(
app,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
const MultiSelect = connect(
({suggestions, selectedFeatures}) => (
{
placeholder: 'Search for a place name',
suggestions,
selectedKeys: new Set(Object.keys(selectedFeatures)),
renderSelectedKey: key => selectedFeatures[key].properties.title,
renderSectionTitle: section => section.title,
getSectionSuggestions: section => section.suggestions,
}
),
dispatch => (
{
onFocusChange: key => dispatch(featureFocused(key)),
onSelectionChange: keys => dispatch(featuresSelected(keys)),
onSuggestionsRequested: query => dispatch(suggestionsRequested(query))
}
)
)(require('./MultiSelect'))
const Map = connect(
({selectedFeatures, focusedFeature}) => ({
features: Object.values(selectedFeatures),
focusedFeature
}),
)(require('./Map'))
const FeatureLabel = connect(
({focusedFeature}) => ({feature: focusedFeature})
)(require('./FeatureLabel'))
render(
h(ThemeProvider, [
h(Provider, {store},
[
h(Map, {key: 1}),
h(FeatureLabel, {key: 2}),
h(MultiSelect, {key: 3})
]
)
]),
document.body.appendChild(document.createElement('div'))
)