-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathThemeList.jsx
288 lines (279 loc) · 13.7 KB
/
ThemeList.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
/**
* Copyright 2020-2024 Sourcepole AG
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import React from 'react';
import {connect} from 'react-redux';
import axios from 'axios';
import {remove as removeDiacritics} from 'diacritics';
import isEmpty from 'lodash.isempty';
import PropTypes from 'prop-types';
import {setActiveLayerInfo} from '../actions/layerinfo';
import {LayerRole, addLayer} from '../actions/layers';
import {setUserInfoFields} from '../actions/localConfig';
import {setCurrentTask} from '../actions/task';
import {setCurrentTheme, setThemeLayersList} from '../actions/theme';
import ConfigUtils from '../utils/ConfigUtils';
import LocaleUtils from '../utils/LocaleUtils';
import ThemeUtils from '../utils/ThemeUtils';
import Icon from './Icon';
import './style/ThemeListPortal.css';
import './style/ThemeListSidebar.css';
class ThemeList extends React.Component {
static propTypes = {
activeTheme: PropTypes.object,
addLayer: PropTypes.func,
allowAddingOtherThemeLayers: PropTypes.bool,
allowAddingOtherThemes: PropTypes.bool,
changeTheme: PropTypes.func,
collapsibleGroups: PropTypes.bool,
defaultUrlParams: PropTypes.string,
dontPreserveSettingsOnSwitch: PropTypes.bool,
filter: PropTypes.string,
layers: PropTypes.array,
mapConfig: PropTypes.object,
setActiveLayerInfo: PropTypes.func,
setCurrentTask: PropTypes.func,
setThemeLayersList: PropTypes.func,
setUserInfoFields: PropTypes.func,
showDefaultThemeSelector: PropTypes.bool,
showLayerAfterChangeTheme: PropTypes.bool,
themes: PropTypes.object,
className: PropTypes.string
};
state = {
expandedGroups: [],
visibleThemeInfoMenu: null
};
groupMatchesFilter = (group, filter) => {
if (group && group.items) {
for (let i = 0, n = group.items.length; i < n; ++i) {
if (removeDiacritics(group.items[i].title).match(filter) ||
removeDiacritics(group.items[i].keywords || "").match(filter) ||
removeDiacritics(group.items[i].abstract || "").match(filter)) {
return true;
}
}
}
if (group && group.subdirs) {
for (let i = 0, n = group.subdirs.length; i < n; ++i) {
if (this.groupMatchesFilter(group.subdirs[i], filter)) {
return true;
}
}
}
return false;
};
renderThemeGroup = (group, filter) => {
const assetsPath = ConfigUtils.getAssetsPath();
let subdirs = (group && group.subdirs ? group.subdirs : []);
if (filter) {
subdirs = subdirs.filter(subdir => this.groupMatchesFilter(subdir, filter));
}
const subtree = subdirs.map((subdir) => {
const expanded = !this.props.collapsibleGroups || filter || this.state.expandedGroups.includes(subdir.id) || (this.props.activeTheme && this.groupContainsActiveTheme(subdir));
if (isEmpty(subdir.items) && isEmpty(subdir.subdirs)) {
return null;
}
return (
<li className={"theme-group-header " + (expanded ? "theme-group-header-expanded" : "")} key={subdir.id}>
<span onClick={() => this.setState((state) => ({expandedGroups: expanded ? state.expandedGroups.filter(id => id !== subdir.id) : [...state.expandedGroups, subdir.id]}))}>
{this.props.collapsibleGroups ? (<Icon icon={expanded ? "collapse" : "expand"} />) : null} {subdir.title}
</span>
{expanded ? this.renderThemeGroup(subdir, filter) : null}
</li>
);
});
const activeThemeId = this.props.activeTheme ? this.props.activeTheme.id : null;
const addLayersTitle = LocaleUtils.tr("themeswitcher.addlayerstotheme");
const addTitle = LocaleUtils.tr("themeswitcher.addtotheme");
const changeDefaultUrlTitle = LocaleUtils.tr("themeswitcher.changedefaulttheme");
const openTabTitle = LocaleUtils.tr("themeswitcher.openintab");
const username = ConfigUtils.getConfigProp("username");
return (
<ul className="theme-group-body">
{(!isEmpty(group.items) ? group.items : []).map(item => {
const infoLinks = (item.themeInfoLinks && item.themeInfoLinks.entries || []).map(name => this.props.themes.themeInfoLinks.find(entry => entry.name === name)).filter(entry => entry);
const matches = [];
if (filter) {
let match = null;
if ((match = removeDiacritics(item.title).match(filter))) {
matches.push([LocaleUtils.trmsg("themeswitcher.match.title"), this.extractSubstr(match, item.title), item.title]);
}
if ((match = removeDiacritics(item.keywords || "").match(filter))) {
matches.push([LocaleUtils.trmsg("themeswitcher.match.keywords"), this.extractSubstr(match, item.keywords), item.keywords]);
}
if ((match = removeDiacritics(item.abstract || "").match(filter))) {
matches.push([LocaleUtils.trmsg("themeswitcher.match.abstract"), this.extractSubstr(match, item.abstract), item.abstract]);
}
if (isEmpty(matches)) {
return null;
}
}
let title = item.abstract;
if (title && item.keywords) {
title += "\n\n";
}
if (item.keywords) {
title += LocaleUtils.tr("themeswitcher.match.keywords") + ": " + item.keywords;
}
return (
<li className={activeThemeId === item.id ? "theme-item theme-item-active" : "theme-item"}
key={item.id}
onClick={() => this.setTheme(item)}
title={title}
>
<div className="theme-item-body">
{item.description ? (<div className="theme-item-description" dangerouslySetInnerHTML={{__html: item.description}} />) : null}
<img className="theme-item-thumbnail" src={assetsPath + "/" + item.thumbnail} />
</div>
{!item.restricted ? (
<div className="theme-item-icons">
{this.props.allowAddingOtherThemeLayers ? (<Icon icon="layers" onClick={ev => this.getThemeLayersToList(ev, item)} title={addLayersTitle} />) : null}
{this.props.allowAddingOtherThemes ? (<Icon icon="plus" onClick={ev => this.addThemeLayers(ev, item)} title={addTitle} />) : null}
<Icon icon="open_link" onClick={ev => this.openInTab(ev, item.id)} title={openTabTitle} />
{this.props.showDefaultThemeSelector && username ? (<Icon className={ (this.extractThemeId(this.props.defaultUrlParams) === item.id ? "icon-active" : "")} icon="new" onClick={ev => this.changeDefaultUrlParams(ev, item.id)} title={changeDefaultUrlTitle} />) : null }
</div>
) : (
<div className="theme-item-restricted-overlay">
<Icon icon="lock" />
</div>
)}
<div className="theme-item-title" title={item.title}>
<span>{item.title}</span>
</div>
{!isEmpty(infoLinks) ? (<div className={"theme-item-info-menu " + (this.state.visibleThemeInfoMenu ? "theme-item-info-menu-active" : "")} onClick={ev => this.toggleThemeInfoMenu(ev, item.id)}>
<Icon icon="info" />
{item.themeInfoLinks.title ? (<span>{item.themeInfoLinks.title}</span>) : LocaleUtils.tr(item.themeInfoLinks.titleMsgId)}
<Icon icon="triangle-down" />
{this.state.visibleThemeInfoMenu === item.id ? (
<div className="theme-item-info-links" onClick={ev => ev.stopPropagation()}>
{infoLinks.map(link => (
<a href={link.url} key={link.name} target={link.target}>{link.title}</a>
))}
</div>
) : null}
</div>) : null}
</li>
);
})}
{subtree}
</ul>
);
};
groupContainsActiveTheme = (group) => {
for (const item of (group.items || [])) {
if (item.id === this.props.activeTheme.id) {
return true;
}
}
for (const subdir of (group.subdirs || [])) {
if (this.groupContainsActiveTheme(subdir)) {
return true;
}
}
return false;
};
render() {
const { className } = this.props;
const filter = this.props.filter ? new RegExp(removeDiacritics(this.props.filter).replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&"), "i") : null;
return (
<div className={`ThemeList ${className}`}>
{this.renderThemeGroup(this.props.themes, filter)}
</div>
);
}
extractSubstr = (match, text) => {
const cleanText = removeDiacritics(text);
const cleanFilter = removeDiacritics(this.props.filter);
let padding = Math.round((20 - cleanFilter.length) / 2);
// Add unused right padding to left
padding += -Math.min(cleanText.length - (match.index + cleanFilter.length) - padding, 0);
const leftStart = Math.max(match.index - padding, 0);
const leftLen = Math.min(match.index, padding);
return [
(leftStart > 0 ? "\u2026" : "") + cleanText.substr(leftStart, leftLen),
cleanText.substr(match.index, cleanFilter.length),
cleanText.substr(match.index + cleanFilter.length)
];
};
extractThemeId = (text) => {
return Object.fromEntries(text.split("&").map(x => x.split("="))).t;
};
setTheme = (theme) => {
if (theme.restricted) {
// eslint-disable-next-line
alert(LocaleUtils.tr("themeswitcher.restrictedthemeinfo"));
return;
}
const hasUserLayer = this.props.layers.find(layer => layer.role === LayerRole.USERLAYER);
const preserveNonThemeLayers = ConfigUtils.getConfigProp("preserveNonThemeLayersOnThemeSwitch", this.props.activeTheme);
// eslint-disable-next-line
if (hasUserLayer && !preserveNonThemeLayers && !confirm(LocaleUtils.tr("themeswitcher.confirmswitch"))) {
return;
}
this.props.setActiveLayerInfo(null, null);
if (this.props.showLayerAfterChangeTheme) {
this.props.setCurrentTask('LayerTree');
} else {
this.props.setCurrentTask(null);
}
this.props.changeTheme(theme, this.props.themes, !this.props.dontPreserveSettingsOnSwitch);
};
toggleThemeInfoMenu = (ev, themeId) => {
ev.stopPropagation();
this.setState((state) => ({visibleThemeInfoMenu: state.visibleThemeInfoMenu === themeId ? null : themeId}));
};
addThemeLayers = (ev, theme) => {
ev.stopPropagation();
this.props.addLayer(ThemeUtils.createThemeLayer(theme, this.props.themes, LayerRole.USERLAYER));
// Show layer tree to notify user that something has happened
this.props.setCurrentTask('LayerTree');
};
getThemeLayersToList = (ev, theme) => {
ev.stopPropagation();
this.props.setThemeLayersList(theme);
// Show layer tree to notify user that something has happened
this.props.setCurrentTask('LayerTree');
};
openInTab = (ev, themeid) => {
ev.stopPropagation();
const url = location.href.split("?")[0] + '?t=' + themeid;
window.open(url, '_blank');
};
changeDefaultUrlParams = (ev, themeid) => {
ev.stopPropagation();
const params = {
default_url_params: "t=" + themeid
};
const baseurl = location.href.split("?")[0].replace(/\/$/, '');
axios.get(baseurl + "/setuserinfo", {params}).then(response => {
if (!response.data.success) {
/* eslint-disable-next-line */
alert(LocaleUtils.tr("settings.defaultthemefailed", response.data.error));
} else {
this.props.setUserInfoFields(response.data.fields);
}
}).catch((e) => {
/* eslint-disable-next-line */
alert(LocaleUtils.tr("settings.defaultthemefailed", String(e)));
});
};
}
const selector = (state) => ({
themes: state.theme.themes || {},
layers: state.layers.flat,
mapConfig: state.map,
defaultUrlParams: state.localConfig.user_infos?.default_url_params || ""
});
export default connect(selector, {
changeTheme: setCurrentTheme,
setCurrentTask: setCurrentTask,
addLayer: addLayer,
setActiveLayerInfo: setActiveLayerInfo,
setThemeLayersList: setThemeLayersList,
setUserInfoFields: setUserInfoFields
})(ThemeList);