@teipublisher/pb-components 2.26.1-next.2 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/main.yml +3 -3
- package/.github/workflows/node.js.yml +3 -3
- package/.github/workflows/release.js.yml +4 -4
- package/.releaserc.json +2 -2
- package/CHANGELOG.md +267 -9
- package/Dockerfile +78 -70
- package/css/components.css +5 -5
- package/css/leaflet/images/layers.png +0 -0
- package/dist/demo/components.css +46 -1
- package/dist/demo/pb-browse-docs2.html +1 -1
- package/dist/demo/pb-dialog.html +3 -5
- package/dist/demo/pb-drawer2.html +1 -1
- package/dist/demo/pb-facsimile.html +2 -2
- package/dist/demo/pb-grid.html +19 -6
- package/dist/demo/pb-leaflet-map.html +1 -1
- package/dist/demo/pb-login.html +0 -2
- package/dist/demo/pb-message.html +1 -2
- package/dist/demo/pb-progress.html +2 -2
- package/dist/demo/pb-repeat.html +1 -3
- package/dist/demo/pb-search.html +7 -4
- package/dist/demo/pb-search3.html +1 -1
- package/dist/demo/pb-search4.html +2 -2
- package/dist/demo/pb-view3.html +1 -1
- package/dist/{iron-form-3b8dcaa7.js → iron-form-dfb3e3b1.js} +95 -95
- package/dist/paper-checkbox-645e1077.js +200 -0
- package/dist/{paper-icon-button-b1d31571.js → paper-icon-button-984162bd.js} +1 -1
- package/dist/{paper-checkbox-515a5284.js → paper-inky-focus-behavior-fa16796b.js} +58 -247
- package/dist/{paper-listbox-a3b7175c.js → paper-listbox-5f5d1cec.js} +152 -162
- package/dist/pb-code-editor.js +25 -20
- package/dist/pb-component-docs.js +68 -64
- package/dist/pb-components-bundle.js +1983 -2293
- package/dist/pb-edit-app.js +167 -107
- package/dist/pb-elements.json +176 -120
- package/dist/{pb-i18n-0611135a.js → pb-i18n-4cc00bfe.js} +1 -1
- package/dist/pb-leaflet-map.js +23 -23
- package/dist/pb-mei.js +56 -41
- package/dist/{pb-mixin-b1caa22e.js → pb-mixin-886ece32.js} +1 -1
- package/dist/pb-odd-editor.js +1023 -782
- package/dist/pb-tify.js +2 -2
- package/dist/vaadin-element-mixin-beb74ffd.js +545 -0
- package/gh-pages.js +5 -3
- package/i18n/common/en.json +6 -0
- package/i18n/common/pl.json +2 -2
- package/lib/openseadragon.min.js +6 -6
- package/package.json +3 -3
- package/pb-elements.json +176 -120
- package/src/assets/components.css +5 -5
- package/src/authority/airtable.js +20 -21
- package/src/authority/anton.js +129 -129
- package/src/authority/custom.js +23 -21
- package/src/authority/geonames.js +38 -32
- package/src/authority/gnd.js +50 -42
- package/src/authority/kbga.js +137 -134
- package/src/authority/metagrid.js +44 -46
- package/src/authority/reconciliation.js +66 -67
- package/src/authority/registry.js +4 -4
- package/src/docs/pb-component-docs.js +2 -2
- package/src/docs/pb-component-view.js +5 -5
- package/src/docs/pb-components-list.js +2 -2
- package/src/docs/pb-demo-snippet.js +2 -2
- package/src/dts-client.js +299 -297
- package/src/dts-select-endpoint.js +90 -82
- package/src/parse-date-service.js +184 -135
- package/src/pb-ajax.js +158 -171
- package/src/pb-authority-lookup.js +191 -156
- package/src/pb-autocomplete.js +292 -280
- package/src/pb-blacklab-highlight.js +264 -259
- package/src/pb-blacklab-results.js +236 -221
- package/src/pb-browse-docs.js +540 -475
- package/src/pb-browse.js +68 -65
- package/src/pb-clipboard.js +79 -76
- package/src/pb-code-editor.js +110 -102
- package/src/pb-code-highlight.js +209 -204
- package/src/pb-codepen.js +79 -72
- package/src/pb-collapse.js +211 -151
- package/src/pb-combo-box.js +190 -190
- package/src/pb-components-bundle.js +1 -1
- package/src/pb-components.js +1 -0
- package/src/pb-custom-form.js +173 -153
- package/src/pb-dialog.js +98 -62
- package/src/pb-document.js +89 -90
- package/src/pb-download.js +212 -196
- package/src/pb-drawer.js +145 -148
- package/src/pb-edit-app.js +301 -229
- package/src/pb-edit-xml.js +100 -97
- package/src/pb-events.js +114 -107
- package/src/pb-facs-link.js +104 -102
- package/src/pb-facsimile.js +474 -410
- package/src/pb-formula.js +151 -153
- package/src/pb-geolocation.js +129 -131
- package/src/pb-grid-action.js +53 -56
- package/src/pb-grid.js +231 -228
- package/src/pb-highlight.js +140 -140
- package/src/pb-hotkeys.js +40 -42
- package/src/pb-i18n.js +101 -104
- package/src/pb-image-strip.js +84 -78
- package/src/pb-lang.js +142 -57
- package/src/pb-leaflet-map.js +488 -485
- package/src/pb-link.js +126 -124
- package/src/pb-load.js +431 -429
- package/src/pb-login.js +299 -244
- package/src/pb-manage-odds.js +352 -336
- package/src/pb-map-icon.js +89 -89
- package/src/pb-map-layer.js +85 -85
- package/src/pb-markdown.js +90 -99
- package/src/pb-media-query.js +74 -72
- package/src/pb-mei.js +306 -295
- package/src/pb-message.js +139 -97
- package/src/pb-mixin.js +269 -264
- package/src/pb-navigation.js +80 -95
- package/src/pb-observable.js +38 -38
- package/src/pb-odd-editor.js +1054 -958
- package/src/pb-odd-elementspec-editor.js +349 -298
- package/src/pb-odd-model-editor.js +1075 -909
- package/src/pb-odd-parameter-editor.js +200 -178
- package/src/pb-odd-rendition-editor.js +136 -124
- package/src/pb-page.js +431 -422
- package/src/pb-paginate.js +228 -179
- package/src/pb-panel.js +198 -182
- package/src/pb-popover-themes.js +15 -8
- package/src/pb-popover.js +296 -287
- package/src/pb-print-preview.js +127 -127
- package/src/pb-progress.js +51 -51
- package/src/pb-repeat.js +105 -104
- package/src/pb-restricted.js +84 -77
- package/src/pb-search.js +256 -228
- package/src/pb-select-feature.js +127 -120
- package/src/pb-select-odd.js +132 -124
- package/src/pb-select-template.js +89 -78
- package/src/pb-select.js +251 -227
- package/src/pb-split-list.js +179 -174
- package/src/pb-svg.js +80 -79
- package/src/pb-table-column.js +54 -54
- package/src/pb-table-grid.js +221 -203
- package/src/pb-tabs.js +61 -63
- package/src/pb-tify.js +154 -154
- package/src/pb-timeline.js +382 -249
- package/src/pb-toggle-feature.js +195 -187
- package/src/pb-upload.js +184 -174
- package/src/pb-version.js +30 -30
- package/src/pb-view-annotate.js +135 -98
- package/src/pb-view.js +1282 -1270
- package/src/pb-zoom.js +127 -45
- package/src/polymer-hack.js +1 -1
- package/src/search-result-service.js +256 -223
- package/src/seed-element.js +13 -20
- package/src/settings.js +4 -4
- package/src/theming.js +98 -91
- package/src/urls.js +289 -289
- package/src/utils.js +53 -51
- package/css/pb-styles.css +0 -51
- package/dist/vaadin-element-mixin-6e4cee3a.js +0 -527
- package/src/assets/pb-styles.css +0 -51
- package/src/pb-light-dom.js +0 -40
package/src/urls.js
CHANGED
|
@@ -1,337 +1,337 @@
|
|
|
1
|
-
import { match, compile, pathToRegexp } from
|
|
2
|
-
import { PbEvents } from
|
|
3
|
-
import { getSubscribedChannels } from
|
|
1
|
+
import { match, compile, pathToRegexp } from 'path-to-regexp';
|
|
2
|
+
import { PbEvents } from './pb-events.js';
|
|
3
|
+
import { getSubscribedChannels } from './pb-mixin.js';
|
|
4
4
|
|
|
5
5
|
function log(...args) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
args[0] = `%c<registry>%c ${args[0]}`;
|
|
7
|
+
args.splice(1, 0, 'font-weight: bold; color: #99FF33;', 'color: inherit; font-weight: normal');
|
|
8
|
+
console.log.apply(null, args);
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Central class for tracking state. We distinguish between
|
|
13
|
-
*
|
|
13
|
+
*
|
|
14
14
|
* 1. the initial state of the application as determined by the URL opened in the browser
|
|
15
15
|
* 2. state changes in components occurring while the user interacts with the page
|
|
16
|
-
*
|
|
17
|
-
* 1) is relevant if a user accesses a particular URL by following a link, entering an address into the location bar or
|
|
16
|
+
*
|
|
17
|
+
* 1) is relevant if a user accesses a particular URL by following a link, entering an address into the location bar or
|
|
18
18
|
* opening a bookmark. In this case the components on the page will be in a fresh, empty state. However, they may react
|
|
19
|
-
* to URL parameters and initiate specific properties of their state accordingly. Users expect that navigating to a given URL will
|
|
19
|
+
* to URL parameters and initiate specific properties of their state accordingly. Users expect that navigating to a given URL will
|
|
20
20
|
* consistently result in the same display (i.e. restore a certain state). If users bookmark a specific page of a document shown in
|
|
21
|
-
* TEI Publisher's pb-view, they expect that the same page is displayed when they open the bookmark again. Thus the document path and
|
|
21
|
+
* TEI Publisher's pb-view, they expect that the same page is displayed when they open the bookmark again. Thus the document path and
|
|
22
22
|
* the page need to be tracked in the URL.
|
|
23
|
-
*
|
|
23
|
+
*
|
|
24
24
|
* 2) applies while the user interacts with the application, i.e. triggers actions, which cause components to change state. Some
|
|
25
25
|
* actions may lead to a new page load, but many – like navigating pages in a `pb-view` – will only change the state of one or more components.
|
|
26
|
-
* From a user's point of view this should be irrelevant: moving back or forward in browser history should consistently restore the
|
|
26
|
+
* From a user's point of view this should be irrelevant: moving back or forward in browser history should consistently restore the
|
|
27
27
|
* previous or following state.
|
|
28
|
-
*
|
|
28
|
+
*
|
|
29
29
|
* Components should thus comply with the following guidelines:
|
|
30
|
-
*
|
|
30
|
+
*
|
|
31
31
|
* - if the component initializes itself, it should retrieve needed parameters from `registry.state`
|
|
32
32
|
* - it may call `registry.replace` to make sure that all parameters required to later restore its initial state are present in the current URL
|
|
33
33
|
* - it should register a listener with `registry.subscribe` to be informed when the user moves back in history (without reloading the page)
|
|
34
34
|
* - it must call `registry.commit` after changing its current state
|
|
35
35
|
*/
|
|
36
36
|
class Registry {
|
|
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
|
-
|
|
37
|
+
constructor() {
|
|
38
|
+
this.rootPath = '';
|
|
39
|
+
/**
|
|
40
|
+
* Is the resource path part of the URL path or is it
|
|
41
|
+
* passed in a query parameter?
|
|
42
|
+
*/
|
|
43
|
+
this.usePath = false;
|
|
44
|
+
/**
|
|
45
|
+
* Records current state as determined from parsing the URL.
|
|
46
|
+
* This should be used to initialize components.
|
|
47
|
+
*/
|
|
48
|
+
this.state = {};
|
|
49
|
+
/**
|
|
50
|
+
* Used to record state for a given channel. Will be updated
|
|
51
|
+
* if a component calls commit or replace.
|
|
52
|
+
*/
|
|
53
|
+
this.channelStates = {};
|
|
54
|
+
/**
|
|
55
|
+
* Records the hash part of the URL, if any
|
|
56
|
+
*/
|
|
57
|
+
this.hash = null;
|
|
58
|
+
/**
|
|
59
|
+
* Should a hash in the URL be interpreted as an xml:id for loading the content?
|
|
60
|
+
*/
|
|
61
|
+
this.idHash = true;
|
|
62
|
+
this._listeners = [];
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* URL pattern to use for mapping parameters into the URL path
|
|
66
|
+
*/
|
|
67
|
+
this.urlPattern = null;
|
|
68
|
+
|
|
69
|
+
this.urlIgnore = new Set();
|
|
70
|
+
this.pathParams = new Set();
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Information about the user currently logged in or null. If a user is logged in
|
|
74
|
+
* this will be an object with two properties: `user` and `groups`.
|
|
75
|
+
*/
|
|
76
|
+
this.currentUser = null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
configure(usePath = true, idHash = false, rootPath = '', urlPattern, ignoredParams) {
|
|
80
|
+
this.rootPath = rootPath;
|
|
81
|
+
this.usePath = usePath;
|
|
82
|
+
this.idHash = idHash;
|
|
83
|
+
this.urlPattern = urlPattern;
|
|
84
|
+
if (ignoredParams) {
|
|
85
|
+
ignoredParams.split(/\s*,\s*/).forEach(param => this.urlIgnore.add(param));
|
|
78
86
|
}
|
|
79
87
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (this.urlPattern) {
|
|
90
|
-
// save a list of parameter names which go into the path
|
|
91
|
-
const pathParams = [];
|
|
92
|
-
pathToRegexp(this.urlPattern, pathParams);
|
|
93
|
-
pathParams.forEach((param) => this.pathParams.add(param.name));
|
|
94
|
-
// compile URL pattern into a decode and encode function
|
|
95
|
-
this._decodePath = match(this.urlPattern);
|
|
96
|
-
this._encodePath = compile(this.urlPattern);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// determine initial state of the registry by parsing current URL
|
|
100
|
-
const initialState = this._stateFromURL();
|
|
101
|
-
if (!initialState) {
|
|
102
|
-
console.error('<registry> failed to parse URL: %s using template %s', window.location, this.urlTemplate);
|
|
103
|
-
} else {
|
|
104
|
-
this.state = initialState;
|
|
105
|
-
}
|
|
106
|
-
window.history.replaceState(null, '');
|
|
107
|
-
|
|
108
|
-
window.addEventListener('popstate', (ev) => {
|
|
109
|
-
if (!ev.state) {
|
|
110
|
-
// only react to history entries triggered by components
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
try {
|
|
115
|
-
this.channelStates = JSON.parse(ev.state);
|
|
116
|
-
} catch (e) {
|
|
117
|
-
console.error('<registry> error restoring state: %s', e.toString());
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
this.state = this._stateFromURL();
|
|
122
|
-
log('popstate: %o', this.channelStates);
|
|
123
|
-
|
|
124
|
-
this._listeners.forEach((entry) => {
|
|
125
|
-
entry.callback(this.getState(entry.component));
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
PbEvents.emit('pb-popstate', null, this.channelStates);
|
|
129
|
-
});
|
|
88
|
+
if (this.urlPattern) {
|
|
89
|
+
// save a list of parameter names which go into the path
|
|
90
|
+
const pathParams = [];
|
|
91
|
+
pathToRegexp(this.urlPattern, pathParams);
|
|
92
|
+
pathParams.forEach(param => this.pathParams.add(param.name));
|
|
93
|
+
// compile URL pattern into a decode and encode function
|
|
94
|
+
this._decodePath = match(this.urlPattern);
|
|
95
|
+
this._encodePath = compile(this.urlPattern);
|
|
130
96
|
}
|
|
131
97
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
98
|
+
// determine initial state of the registry by parsing current URL
|
|
99
|
+
const initialState = this._stateFromURL();
|
|
100
|
+
if (!initialState) {
|
|
101
|
+
console.error(
|
|
102
|
+
'<registry> failed to parse URL: %s using template %s',
|
|
103
|
+
window.location,
|
|
104
|
+
this.urlTemplate,
|
|
105
|
+
);
|
|
106
|
+
} else {
|
|
107
|
+
this.state = initialState;
|
|
137
108
|
}
|
|
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
|
-
|
|
109
|
+
window.history.replaceState(null, '');
|
|
110
|
+
|
|
111
|
+
window.addEventListener('popstate', ev => {
|
|
112
|
+
if (!ev.state) {
|
|
113
|
+
// only react to history entries triggered by components
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
try {
|
|
118
|
+
this.channelStates = JSON.parse(ev.state);
|
|
119
|
+
} catch (e) {
|
|
120
|
+
console.error('<registry> error restoring state: %s', e.toString());
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
this.state = this._stateFromURL();
|
|
125
|
+
log('popstate: %o', this.channelStates);
|
|
126
|
+
|
|
127
|
+
this._listeners.forEach(entry => {
|
|
128
|
+
entry.callback(this.getState(entry.component));
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
PbEvents.emit('pb-popstate', null, this.channelStates);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
subscribe(component, callback) {
|
|
136
|
+
this._listeners.push({
|
|
137
|
+
component,
|
|
138
|
+
callback,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
_stateFromURL() {
|
|
143
|
+
const params = {};
|
|
144
|
+
this.hash = window.location.hash;
|
|
145
|
+
// use the hash as an xml:id?
|
|
146
|
+
// hashs of the form #1.2.3.4 are internal eXist ids though and thus excluded
|
|
147
|
+
if (this.idHash && this.hash.length > 0 && !/^#\d+\./.test(this.hash)) {
|
|
148
|
+
params.id = this.hash.substring(1);
|
|
178
149
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
this.channelStates[channel] = {};
|
|
187
|
-
return this.channelStates[channel];
|
|
150
|
+
const relpath = window.location.pathname.replace(new RegExp(`^${this.rootPath}/?`), '');
|
|
151
|
+
if (this.urlPattern) {
|
|
152
|
+
const result = this._decodePath(relpath);
|
|
153
|
+
Object.assign(params, result.params);
|
|
154
|
+
log('decoded path %s using template %s: %o', relpath, this.urlPattern, params);
|
|
155
|
+
} else if (this.usePath) {
|
|
156
|
+
params.path = relpath;
|
|
188
157
|
}
|
|
189
158
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
159
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
160
|
+
urlParams.forEach((value, key) => {
|
|
161
|
+
if ((this.urlPattern && this.pathParams.has(key)) || (this.usePath && key === 'path')) {
|
|
162
|
+
console.warn(
|
|
163
|
+
'Found path parameter in query, but usePath is set to true. The path parameter will be ignored.',
|
|
164
|
+
);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
// parameter already set
|
|
168
|
+
if (key in params) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
// check for multiple entries
|
|
172
|
+
const allValues = urlParams.getAll(key);
|
|
173
|
+
if (allValues.length === 1) {
|
|
174
|
+
params[key] = value; // single entry
|
|
175
|
+
} else {
|
|
176
|
+
params[key] = allValues; // array
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
return params;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
getState(component) {
|
|
183
|
+
const channel = getSubscribedChannels(component)[0];
|
|
184
|
+
const state = this.channelStates[channel];
|
|
185
|
+
if (state) {
|
|
186
|
+
return state;
|
|
193
187
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
188
|
+
this.channelStates[channel] = {};
|
|
189
|
+
return this.channelStates[channel];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
setState(component, newState) {
|
|
193
|
+
const channel = getSubscribedChannels(component)[0];
|
|
194
|
+
this.channelStates[channel] = Object.assign(this.channelStates[channel], newState);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
clearParametersMatching(component, regex) {
|
|
198
|
+
const { state } = this;
|
|
199
|
+
|
|
200
|
+
for (const key of Object.keys(state)) {
|
|
201
|
+
if (regex.test(key)) {
|
|
202
|
+
state[key] = null;
|
|
203
|
+
}
|
|
203
204
|
}
|
|
205
|
+
}
|
|
204
206
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
const value = path.split('.').reduce((state, component) => {
|
|
210
|
-
if (!state[component]) {
|
|
211
|
-
return undefined;
|
|
212
|
-
}
|
|
213
|
-
return state[component];
|
|
214
|
-
}, this.state);
|
|
215
|
-
return value || defaultValue;
|
|
207
|
+
get(path, defaultValue) {
|
|
208
|
+
if (!this.state) {
|
|
209
|
+
return undefined;
|
|
216
210
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
231
|
-
return result[nextComponent];
|
|
232
|
-
},
|
|
233
|
-
this.state
|
|
234
|
-
);
|
|
235
|
-
lastIntermediate[lastPart] = value;
|
|
211
|
+
const value = path.split('.').reduce((state, component) => {
|
|
212
|
+
if (!state[component]) {
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
return state[component];
|
|
216
|
+
}, this.state);
|
|
217
|
+
return value || defaultValue;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
set(path, value) {
|
|
221
|
+
if (!path.contains('.')) {
|
|
222
|
+
this.state[path] = value;
|
|
223
|
+
return;
|
|
236
224
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
225
|
+
const components = path.split('.');
|
|
226
|
+
const lastPart = components.pop();
|
|
227
|
+
// make sure all intermediate steps are available
|
|
228
|
+
const lastIntermediate = components.reduce((result, nextComponent) => {
|
|
229
|
+
if (!result[nextComponent]) {
|
|
230
|
+
// eslint-disable-next-line no-param-reassign
|
|
231
|
+
result[nextComponent] = {};
|
|
232
|
+
}
|
|
233
|
+
return result[nextComponent];
|
|
234
|
+
}, this.state);
|
|
235
|
+
lastIntermediate[lastPart] = value;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
commit(elem, newState, overwrite = false) {
|
|
239
|
+
this._commit(elem, newState, overwrite, false);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
replace(elem, newState, overwrite = false) {
|
|
243
|
+
this._commit(elem, newState, overwrite, true);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
_commit(elem, newState, overwrite, replace) {
|
|
247
|
+
this.state = overwrite ? newState : { ...this.state, ...newState };
|
|
248
|
+
const resolved = this.urlFromState();
|
|
249
|
+
|
|
250
|
+
const chs = getSubscribedChannels(elem);
|
|
251
|
+
chs.forEach(channel => {
|
|
252
|
+
if (overwrite || !this.channelStates[channel]) {
|
|
253
|
+
this.channelStates[channel] = newState;
|
|
254
|
+
} else {
|
|
255
|
+
Object.assign(this.channelStates[channel], newState);
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
const json = this.toJSON();
|
|
260
|
+
if (replace) {
|
|
261
|
+
window.history.replaceState(json, '', resolved);
|
|
262
|
+
log('replace %s: %o %d', resolved.toString(), this.channelStates, window.history.length);
|
|
263
|
+
} else {
|
|
264
|
+
window.history.pushState(json, '', resolved);
|
|
265
|
+
log('commit %s: %o %d', resolved.toString(), this.channelStates, window.history.length);
|
|
240
266
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
urlFromState() {
|
|
270
|
+
const newUrl = new URL(window.location.href);
|
|
271
|
+
|
|
272
|
+
function setParam(value, param) {
|
|
273
|
+
if (value === null) {
|
|
274
|
+
newUrl.searchParams.delete(param);
|
|
275
|
+
} else if (Array.isArray(value)) {
|
|
276
|
+
// copy array before mutating it
|
|
277
|
+
const _v = Array.from(value);
|
|
278
|
+
// overwrite any previous value by setting the first member
|
|
279
|
+
newUrl.searchParams.set(param, _v.pop());
|
|
280
|
+
// add additional values
|
|
281
|
+
_v.forEach(v => newUrl.searchParams.append(param, v));
|
|
282
|
+
} else {
|
|
283
|
+
newUrl.searchParams.set(param, value);
|
|
284
|
+
}
|
|
244
285
|
}
|
|
245
286
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
this.channelStates[channel] = newState;
|
|
254
|
-
} else {
|
|
255
|
-
Object.assign(this.channelStates[channel], newState);
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
const json = this.toJSON();
|
|
260
|
-
if (replace) {
|
|
261
|
-
window.history.replaceState(json, '', resolved);
|
|
262
|
-
log('replace %s: %o %d', resolved.toString(), this.channelStates, window.history.length);
|
|
263
|
-
} else {
|
|
264
|
-
window.history.pushState(json, '', resolved);
|
|
265
|
-
log('commit %s: %o %d', resolved.toString(), this.channelStates, window.history.length);
|
|
287
|
+
for (const [param, value] of Object.entries(this.state)) {
|
|
288
|
+
if (this.urlPattern) {
|
|
289
|
+
// check if param should be ignored or is required by the URL template
|
|
290
|
+
// fill up missing parameters by stripping potential "user." prefix
|
|
291
|
+
const normParam = param.replace(/^(?:user\.)?(.*)$/, '$1');
|
|
292
|
+
if (!(this.pathParams.has(normParam) || this.urlIgnore.has(normParam))) {
|
|
293
|
+
setParam(value, normParam);
|
|
266
294
|
}
|
|
295
|
+
} else if (
|
|
296
|
+
(param !== 'path' || !this.usePath) &&
|
|
297
|
+
(param !== 'id' || !this.idHash) &&
|
|
298
|
+
!this.urlIgnore.has(param)
|
|
299
|
+
) {
|
|
300
|
+
setParam(value, param);
|
|
301
|
+
}
|
|
267
302
|
}
|
|
268
303
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
newUrl.searchParams.set(param, _v.pop());
|
|
280
|
-
// add additional values
|
|
281
|
-
_v.forEach(v => newUrl.searchParams.append(param, v));
|
|
282
|
-
} else {
|
|
283
|
-
newUrl.searchParams.set(param, value);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
for (const [param, value] of Object.entries(this.state)) {
|
|
288
|
-
if (this.urlPattern) {
|
|
289
|
-
// check if param should be ignored or is required by the URL template
|
|
290
|
-
// fill up missing parameters by stripping potential "user." prefix
|
|
291
|
-
const normParam = param.replace(/^(?:user\.)?(.*)$/, '$1');
|
|
292
|
-
if (!(this.pathParams.has(normParam) || this.urlIgnore.has(normParam))) {
|
|
293
|
-
setParam(value, normParam);
|
|
294
|
-
}
|
|
295
|
-
} else if (
|
|
296
|
-
(param !== 'path' || !this.usePath) &&
|
|
297
|
-
(param !== 'id' || !this.idHash) &&
|
|
298
|
-
(!this.urlIgnore.has(param))
|
|
299
|
-
) {
|
|
300
|
-
setParam(value, param);
|
|
301
|
-
}
|
|
304
|
+
if (this.state.path && this.state.path.length > 0) {
|
|
305
|
+
if (this.urlPattern) {
|
|
306
|
+
// path parameters should not be the empty string
|
|
307
|
+
const normState = {};
|
|
308
|
+
for (const [key, value] of Object.entries(this.state)) {
|
|
309
|
+
if (this.pathParams.has(key) && value === '') {
|
|
310
|
+
normState[key] = null;
|
|
311
|
+
} else {
|
|
312
|
+
normState[key] = value;
|
|
313
|
+
}
|
|
302
314
|
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
for (const [key, value] of Object.entries(this.state)) {
|
|
309
|
-
if (this.pathParams.has(key) && value === '') {
|
|
310
|
-
normState[key] = null;
|
|
311
|
-
} else {
|
|
312
|
-
normState[key] = value;
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
const encoded = this._encodePath(normState);
|
|
316
|
-
newUrl.pathname = `${this.rootPath}/${encoded}`;
|
|
317
|
-
} else if (this.usePath) {
|
|
318
|
-
newUrl.pathname = `${this.rootPath}/${this.state.path}`;
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
if (this.state.id && !this.urlPattern) {
|
|
323
|
-
newUrl.hash = `#${this.state.id}`;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
return newUrl;
|
|
315
|
+
const encoded = this._encodePath(normState);
|
|
316
|
+
newUrl.pathname = `${this.rootPath}/${encoded}`;
|
|
317
|
+
} else if (this.usePath) {
|
|
318
|
+
newUrl.pathname = `${this.rootPath}/${this.state.path}`;
|
|
319
|
+
}
|
|
327
320
|
}
|
|
328
321
|
|
|
329
|
-
|
|
330
|
-
|
|
322
|
+
if (this.state.id && !this.urlPattern) {
|
|
323
|
+
newUrl.hash = `#${this.state.id}`;
|
|
331
324
|
}
|
|
325
|
+
|
|
326
|
+
return newUrl;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
toJSON() {
|
|
330
|
+
return JSON.stringify(this.channelStates);
|
|
331
|
+
}
|
|
332
332
|
}
|
|
333
333
|
|
|
334
334
|
export const registry = new Registry();
|
|
335
335
|
if (!window.pbRegistry) {
|
|
336
|
-
|
|
337
|
-
}
|
|
336
|
+
window.pbRegistry = registry;
|
|
337
|
+
}
|