@openproject/primer-view-components 0.96.0 → 0.96.1-rc.47f1ed3a0

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.
@@ -0,0 +1,221 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _MenuLoader_instances, _MenuLoader_host, _MenuLoader_request, _MenuLoader_ownedLoaders, _MenuLoader_emptyLoaders, _MenuLoader_preload_get, _MenuLoader_invalidate, _MenuLoader_load, _MenuLoader_adopt, _MenuLoader_own, _MenuLoader_onReplace, _MenuLoader_onReplaced, _MenuLoader_keepInputs, _MenuLoader_dropKeptInputs;
13
+ // Loads a deferred menu's items. The one rule (`sync`): a load starts when
14
+ // there is no current request, when the host src differs from the current
15
+ // request's, or when the body holds no element (other code emptied it, or
16
+ // the last response was empty); the current loader still in the body is
17
+ // retried. A closed lazy menu waits for the open moment (`opening`) instead.
18
+ export class MenuLoader {
19
+ constructor(host) {
20
+ _MenuLoader_instances.add(this);
21
+ _MenuLoader_host.set(this, void 0);
22
+ // The current request: the loader it was made with and the src it was
23
+ // made for. Never updated; a new request replaces it. The src is recorded
24
+ // because the loader's attribute is configuration a morph may rewrite while
25
+ // the request is in flight; the record is what was requested, so a
26
+ // rewritten loader cannot pass an old response off as new.
27
+ _MenuLoader_request.set(this, null
28
+ // Every loader this module created or adopted, so completions of
29
+ // superseded ones can be told apart
30
+ );
31
+ // Every loader this module created or adopted, so completions of
32
+ // superseded ones can be told apart
33
+ _MenuLoader_ownedLoaders.set(this, new WeakSet()
34
+ // Loaders whose response had no menu items, with the body they left
35
+ // empty. That body staying empty is not a reason to load again before the
36
+ // next open; a body other code puts in its place is.
37
+ );
38
+ // Loaders whose response had no menu items, with the body they left
39
+ // empty. That body staying empty is not a reason to load again before the
40
+ // next open; a body other code puts in its place is.
41
+ _MenuLoader_emptyLoaders.set(this, new WeakMap());
42
+ __classPrivateFieldSet(this, _MenuLoader_host, host, "f");
43
+ }
44
+ // A primary menu with `src` renders its loader as a template
45
+ get deferred() {
46
+ return Boolean(__classPrivateFieldGet(this, _MenuLoader_host, "f").template());
47
+ }
48
+ // The current loader is still waiting in the body
49
+ get pending() {
50
+ const loader = __classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader;
51
+ return Boolean(loader) && loader.parentElement === (__classPrivateFieldGet(this, _MenuLoader_host, "f").body() ?? null);
52
+ }
53
+ // `data-ready` means no load is pending
54
+ updateReady() {
55
+ if (!this.deferred)
56
+ return;
57
+ if (this.pending) {
58
+ __classPrivateFieldGet(this, _MenuLoader_host, "f").element.removeAttribute('data-ready');
59
+ }
60
+ else {
61
+ __classPrivateFieldGet(this, _MenuLoader_host, "f").element.setAttribute('data-ready', 'true');
62
+ }
63
+ }
64
+ sync(opening = false) {
65
+ const src = (__classPrivateFieldGet(this, _MenuLoader_host, "f").element.getAttribute('src') ?? '').trim();
66
+ if (!this.deferred || !src)
67
+ return;
68
+ if (!(opening || __classPrivateFieldGet(this, _MenuLoader_host, "f").isOpen() || __classPrivateFieldGet(this, _MenuLoader_instances, "a", _MenuLoader_preload_get))) {
69
+ // A pending or failed loader for another src must not render meanwhile;
70
+ // a completed one is kept, so setting its src again does not reload
71
+ if (__classPrivateFieldGet(this, _MenuLoader_request, "f") && src !== __classPrivateFieldGet(this, _MenuLoader_request, "f").src && this.pending)
72
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_invalidate).call(this);
73
+ return;
74
+ }
75
+ const loader = __classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader;
76
+ if (!__classPrivateFieldGet(this, _MenuLoader_request, "f") || src !== __classPrivateFieldGet(this, _MenuLoader_request, "f").src || !__classPrivateFieldGet(this, _MenuLoader_host, "f").body()?.firstElementChild) {
77
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_load).call(this, src, opening);
78
+ }
79
+ else if (loader?.isConnected) {
80
+ // A retry fetches the loader's own src, which a morph may have rewritten
81
+ if ((loader.getAttribute('src') ?? '').trim() === __classPrivateFieldGet(this, _MenuLoader_request, "f").src) {
82
+ loader.classList.remove('is-error');
83
+ loader.refetch();
84
+ }
85
+ else {
86
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_load).call(this, src, opening);
87
+ }
88
+ }
89
+ }
90
+ // Forgets the current request so the menu loads again at the current src:
91
+ // at once when open or preloaded, otherwise when it next opens.
92
+ refresh() {
93
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_invalidate).call(this);
94
+ this.sync();
95
+ }
96
+ // Picks up loaders other code inserts into the body (the last one wins, so
97
+ // a loader appended next to a pending one supersedes it), and loads again
98
+ // when other code empties the body, such as a morph of the server markup,
99
+ // which renders the body empty.
100
+ mutated(records) {
101
+ if (!this.deferred)
102
+ return;
103
+ const body = __classPrivateFieldGet(this, _MenuLoader_host, "f").body();
104
+ if (!body)
105
+ return;
106
+ if (records.some(record => record.addedNodes.length > 0)) {
107
+ const fragments = Array.from(body.querySelectorAll(':scope > include-fragment')).filter(fragment => !__classPrivateFieldGet(this, _MenuLoader_ownedLoaders, "f").has(fragment));
108
+ const fragment = fragments.at(-1);
109
+ if (fragment)
110
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_adopt).call(this, fragment);
111
+ }
112
+ // An empty body was emptied or replaced by other code, unless it is the
113
+ // one the current loader left empty; loading again there would loop on a
114
+ // preloaded menu, so that one waits for the next open
115
+ const loader = __classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader;
116
+ if (!body.firstElementChild && !(loader && __classPrivateFieldGet(this, _MenuLoader_emptyLoaders, "f").get(loader) === body))
117
+ this.sync();
118
+ this.updateReady();
119
+ }
120
+ // Form inputs are kept on the host for a pending or failed load
121
+ get keepingInputs() {
122
+ return Boolean(__classPrivateFieldGet(this, _MenuLoader_host, "f").element.querySelector(':scope > [data-list-inputs]'));
123
+ }
124
+ }
125
+ _MenuLoader_host = new WeakMap(), _MenuLoader_request = new WeakMap(), _MenuLoader_ownedLoaders = new WeakMap(), _MenuLoader_emptyLoaders = new WeakMap(), _MenuLoader_instances = new WeakSet(), _MenuLoader_preload_get = function _MenuLoader_preload_get() {
126
+ return __classPrivateFieldGet(this, _MenuLoader_host, "f").template()?.content.firstElementChild?.getAttribute('loading') === 'eager';
127
+ }, _MenuLoader_invalidate = function _MenuLoader_invalidate() {
128
+ __classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader.remove();
129
+ __classPrivateFieldSet(this, _MenuLoader_request, null, "f");
130
+ this.updateReady();
131
+ }, _MenuLoader_load = function _MenuLoader_load(src, opening) {
132
+ const body = __classPrivateFieldGet(this, _MenuLoader_host, "f").body();
133
+ const template = __classPrivateFieldGet(this, _MenuLoader_host, "f").template()?.content.firstElementChild;
134
+ if (!body || !template)
135
+ return;
136
+ // Imported, not cloned, so the loader is upgraded before it connects
137
+ const loader = __classPrivateFieldGet(this, _MenuLoader_host, "f").element.ownerDocument.importNode(template, true);
138
+ loader.setAttribute('src', src);
139
+ loader.setAttribute('loading', 'eager');
140
+ __classPrivateFieldSet(this, _MenuLoader_request, { loader, src }, "f");
141
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_own).call(this, loader);
142
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_keepInputs).call(this, body);
143
+ body.prepend(loader);
144
+ __classPrivateFieldGet(this, _MenuLoader_host, "f").onLoadStart(loader, opening || __classPrivateFieldGet(this, _MenuLoader_host, "f").isOpen());
145
+ // A focus handler may have started a newer load meanwhile
146
+ if (__classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader !== loader)
147
+ return;
148
+ for (const child of Array.from(body.childNodes)) {
149
+ if (child !== loader)
150
+ child.remove();
151
+ }
152
+ this.updateReady();
153
+ }, _MenuLoader_adopt = function _MenuLoader_adopt(fragment) {
154
+ const body = __classPrivateFieldGet(this, _MenuLoader_host, "f").body();
155
+ if (!body)
156
+ return;
157
+ const src = (fragment.getAttribute('src') ?? '').trim();
158
+ __classPrivateFieldSet(this, _MenuLoader_request, { loader: fragment, src }, "f");
159
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_own).call(this, fragment);
160
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_keepInputs).call(this, body);
161
+ const open = __classPrivateFieldGet(this, _MenuLoader_host, "f").isOpen();
162
+ __classPrivateFieldGet(this, _MenuLoader_host, "f").onLoadStart(fragment, open);
163
+ // A focus handler may have started a newer load meanwhile
164
+ if (__classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader !== fragment)
165
+ return;
166
+ for (const child of Array.from(body.childNodes)) {
167
+ if (child !== fragment)
168
+ child.remove();
169
+ }
170
+ const element = __classPrivateFieldGet(this, _MenuLoader_host, "f").element;
171
+ if (src && src !== element.getAttribute('src')) {
172
+ element.setAttribute('src', src);
173
+ }
174
+ else if (__classPrivateFieldGet(this, _MenuLoader_host, "f").isOpen() || __classPrivateFieldGet(this, _MenuLoader_instances, "a", _MenuLoader_preload_get)) {
175
+ // An unchanged src does not run the rule, and a lazy loader inserted
176
+ // before its attributes never starts on its own
177
+ this.sync();
178
+ }
179
+ }, _MenuLoader_own = function _MenuLoader_own(loader) {
180
+ __classPrivateFieldGet(this, _MenuLoader_ownedLoaders, "f").add(loader);
181
+ loader.addEventListener('include-fragment-replace', event => __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_onReplace).call(this, loader, event));
182
+ loader.addEventListener('include-fragment-replaced', () => __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_onReplaced).call(this, loader));
183
+ }, _MenuLoader_onReplace = function _MenuLoader_onReplace(loader, event) {
184
+ if (loader !== __classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader)
185
+ return;
186
+ // Other code removed the current loader before it completed, eg. by
187
+ // emptying the body, or rewrote its src, eg. by a morph: its response is
188
+ // not the one requested and must not render or mark the menu ready
189
+ const body = __classPrivateFieldGet(this, _MenuLoader_host, "f").body();
190
+ if (!body || loader.parentElement !== body || (loader.getAttribute('src') ?? '').trim() !== __classPrivateFieldGet(this, _MenuLoader_request, "f").src) {
191
+ event.preventDefault();
192
+ return;
193
+ }
194
+ // A response without menu items is discarded, so the body stays empty
195
+ // and the menu loads again when it next opens
196
+ const fragment = event instanceof CustomEvent ? event.detail?.fragment : null;
197
+ if (!(fragment instanceof DocumentFragment))
198
+ return;
199
+ if (!fragment.querySelector(__classPrivateFieldGet(this, _MenuLoader_host, "f").itemSelector)) {
200
+ fragment.replaceChildren();
201
+ __classPrivateFieldGet(this, _MenuLoader_emptyLoaders, "f").set(loader, body);
202
+ }
203
+ }, _MenuLoader_onReplaced = function _MenuLoader_onReplaced(loader) {
204
+ // A superseded loader that completed late
205
+ if (loader !== __classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader)
206
+ return;
207
+ // Before the menu syncs its inputs, so they come from the response; a
208
+ // load the menu's completion work starts keeps the response's inputs
209
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_dropKeptInputs).call(this);
210
+ __classPrivateFieldGet(this, _MenuLoader_host, "f").onLoaded();
211
+ this.updateReady();
212
+ }, _MenuLoader_keepInputs = function _MenuLoader_keepInputs(body) {
213
+ const inputs = body.querySelector('[data-list-inputs]');
214
+ if (!inputs)
215
+ return;
216
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_dropKeptInputs).call(this);
217
+ __classPrivateFieldGet(this, _MenuLoader_host, "f").element.append(inputs);
218
+ }, _MenuLoader_dropKeptInputs = function _MenuLoader_dropKeptInputs() {
219
+ for (const inputs of __classPrivateFieldGet(this, _MenuLoader_host, "f").element.querySelectorAll(':scope > [data-list-inputs]'))
220
+ inputs.remove();
221
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openproject/primer-view-components",
3
- "version": "0.96.0",
3
+ "version": "0.96.1-rc.47f1ed3a0",
4
4
  "description": "ViewComponents of the Primer Design System for OpenProject",
5
5
  "main": "app/assets/javascripts/primer_view_components.js",
6
6
  "module": "app/components/primer/primer.js",