-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathddb-plugins.js
More file actions
197 lines (171 loc) · 8.32 KB
/
ddb-plugins.js
File metadata and controls
197 lines (171 loc) · 8.32 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
/* Combined DDB plugins: Logo + API selector for Swagger UI */
(function () {
// --- Api Selector ---
function getQueryParam(name) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(name);
}
const mapping = {
'1': 'https://api.deutsche-digitale-bibliothek.de/OpenAPI',
'2': 'https://api.deutsche-digitale-bibliothek.de/2/q/openapi'
};
const defaultUrl = mapping['1'];
function normalize(u) {
return (u || '').replace(/\/+$/, '');
}
function findKeyForUrl(url) {
const norm = normalize(url);
for (const k in mapping) {
if (normalize(mapping[k]) === norm) return k;
}
return null;
}
function computeInitialApiUrl() {
let urlParam = getQueryParam('url');
if (!urlParam) {
const docParam = getQueryParam('doc');
if (docParam && mapping[docParam]) urlParam = mapping[docParam];
}
return urlParam || defaultUrl;
}
const ApiSelectorPlugin = function (system) {
return {
wrapComponents: {
Topbar: (Original, system) => (props) => {
const React = system.React;
const specActions = system.specActions || (window.ui && window.ui.specActions);
const currentUrl = computeInitialApiUrl();
function handleChange(e) {
const newKey = e.target.value;
// only allow mapped values;
const resolved = mapping[newKey] || defaultUrl;
const sp = new URLSearchParams(window.location.search);
sp.set('url', resolved);
history.replaceState(null, '', window.location.pathname + '?' + sp.toString());
try {
if (specActions && typeof specActions.updateUrl === 'function') {
specActions.updateUrl(resolved);
}
if (specActions && typeof specActions.download === 'function') {
specActions.download(resolved);
}
} catch (err) {
// ensure the search string includes the leading '?'
window.location.search = '?' + sp.toString();
}
}
function openRaw() {
const val = document.getElementById('docSelectPlugin')?.value || '1';
const u = mapping[val] || defaultUrl;
// use noopener for safety
window.open(u, '_blank', 'noopener');
}
// We replace the built-in download input with a DOM-based select/button
// to preserve exact layout. Do not render a second dropdown here.
// After mounting, replace the existing .download-url-wrapper with our selector to keep layout identical
setTimeout(() => {
try {
const downloadWrapper = document.querySelector('.download-url-wrapper');
if (downloadWrapper) {
// inject responsive styles once
if (!document.getElementById('ddb-plugin-responsive-styles')) {
const style = document.createElement('style');
style.id = 'ddb-plugin-responsive-styles';
style.textContent = `
/* Dropdown/button layout */
.download-url-wrapper { display:flex; flex-direction:row; align-items:center; gap:8px; }
.download-url-input { width: auto; min-width:350px; max-width:480px; }
/* Keep the topbar logo a consistent size across viewport widths. */
.topbar .link img,
.swagger-ui .topbar .link img,
img#ddb-logo-img { height: clamp(28px, 4vw, 48px) !important; width: auto !important; object-fit: contain !important; }
@media (max-width: 720px) {
.download-url-wrapper { flex-direction:column; align-items:flex-start; gap:6px; padding:6px 0; }
.download-url-wrapper .download-url-input { width:100%; max-width: calc(100vw - 32px); box-sizing:border-box; }
.download-url-wrapper .download-url-button { width:100%; box-sizing:border-box; }
}
`;
document.head.appendChild(style);
}
// create replacement node that matches structure and classes
const repl = document.createElement('form');
repl.className = 'download-url-wrapper';
repl.style.display = 'flex';
repl.style.alignItems = 'center';
const sel = document.createElement('select');
// stable id used by helper functions
sel.id = 'docSelectPlugin';
sel.className = 'download-url-input';
sel.style.height = '30px';
sel.style.marginRight = '8px';
const opt1 = document.createElement('option'); opt1.value = '1'; opt1.text = 'Deutschen Digitalen Bibliothek, Version 1';
const opt2 = document.createElement('option'); opt2.value = '2'; opt2.text = 'Deutschen Digitalen Bibliothek, Version 2';
sel.add(opt1); sel.add(opt2);
const initialKey = findKeyForUrl(currentUrl) || '1';
sel.value = initialKey;
const btn = document.createElement('button');
btn.className = 'download-url-button button';
btn.type = 'button';
btn.textContent = 'Explore';
btn.addEventListener('click', (ev) => {
ev.preventDefault();
const val = sel.value || '1';
// only allow mapped values here (no arbitrary URLs)
const resolved = mapping[val] || defaultUrl;
const sp = new URLSearchParams(window.location.search);
sp.set('url', resolved);
history.replaceState(null, '', window.location.pathname + '?' + sp.toString());
try {
if (specActions && typeof specActions.updateUrl === 'function') specActions.updateUrl(resolved);
if (specActions && typeof specActions.download === 'function') specActions.download(resolved);
} catch (e) {
// ensure the search string includes the leading '?'
window.location.search = '?' + sp.toString();
}
});
sel.addEventListener('change', () => {
// keep URL in sync when changed; only use mapping keys
const val = sel.value || '1';
const resolved = mapping[val] || defaultUrl;
const sp = new URLSearchParams(window.location.search);
sp.set('url', resolved);
history.replaceState(null, '', window.location.pathname + '?' + sp.toString());
});
repl.appendChild(sel);
repl.appendChild(btn);
downloadWrapper.parentNode.replaceChild(repl, downloadWrapper);
}
// Logo handling: always use the white RGB logo file
const link = document.querySelector('.topbar .link');
if (link) {
// remove children safely
while (link.firstChild) link.removeChild(link.firstChild);
const img = document.createElement('img');
img.id = 'ddb-logo-img';
img.alt = 'Logo of German Digital Library';
img.style.minWidth = '80px';
img.style.display = 'inline-block';
img.src = 'images/logo-ddbpro-RGB-white.svg';
link.appendChild(img);
// Only set anchor attributes if element is an anchor
if (link.tagName && link.tagName.toLowerCase() === 'a') {
link.href = 'images/logo-ddbpro-RGB-white.svg';
link.setAttribute('rel', 'noopener noreferrer');
link.setAttribute('target', '_blank');
}
}
// (logo already handled above)
} catch (e) {
// ignore DOM errors
console.warn('ddb plugin DOM replace failed', e);
}
}, 0);
return React.createElement(Original, props);
}
}
};
};
// Expose
window.ApiSelectorPlugin = ApiSelectorPlugin;
window.computeInitialApiUrl = computeInitialApiUrl;
})();