emailengine-app 2.79.0 → 2.79.2
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/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +79 -0
- package/bin/emailengine.js +8 -1
- package/config/default.toml +6 -0
- package/data/google-crawlers.json +1 -1
- package/lib/account.js +137 -19
- package/lib/api-routes/account-routes.js +13 -1
- package/lib/api-routes/index.js +12 -0
- package/lib/api-routes/mailbox-routes.js +6 -0
- package/lib/api-routes/mcp-oauth-routes.js +259 -0
- package/lib/api-routes/mcp-routes.js +172 -0
- package/lib/api-routes/message-routes.js +125 -4
- package/lib/api-routes/outbox-routes.js +7 -0
- package/lib/api-routes/permission-map.js +101 -1
- package/lib/api-routes/route-helpers.js +87 -4
- package/lib/api-routes/route-metadata.js +24 -4
- package/lib/api-routes/settings-routes.js +37 -14
- package/lib/api-routes/submit-routes.js +41 -0
- package/lib/api-routes/template-routes.js +7 -0
- package/lib/api-routes/token-routes.js +292 -122
- package/lib/arf-detect.js +17 -4
- package/lib/bounce-detect.js +28 -17
- package/lib/consts.js +36 -1
- package/lib/db.js +6 -0
- package/lib/email-client/base-client.js +102 -117
- package/lib/email-client/gmail-client.js +91 -29
- package/lib/email-client/imap/mailbox.js +175 -101
- package/lib/email-client/imap-client.js +49 -2
- package/lib/email-client/outlook-client.js +20 -24
- package/lib/enum-descriptions.js +2 -1
- package/lib/field-usage.js +17 -1
- package/lib/lua/h-set-new-mark.lua +36 -0
- package/lib/lua/s-list-accounts.lua +5 -2
- package/lib/mcp/index.js +29 -0
- package/lib/mcp/inject.js +70 -0
- package/lib/mcp/json-schema.js +120 -0
- package/lib/mcp/listen.js +171 -0
- package/lib/mcp/oauth.js +324 -0
- package/lib/mcp/protocol.js +489 -0
- package/lib/mcp/resources.js +139 -0
- package/lib/mcp/tools.js +655 -0
- package/lib/message-port-stream.js +84 -1
- package/lib/oauth2-apps.js +84 -15
- package/lib/openapi/build-document.js +5 -1
- package/lib/openapi/index.js +3 -1
- package/lib/response-stream.js +52 -21
- package/lib/routes-ui.js +4 -0
- package/lib/schemas.js +26 -5
- package/lib/settings.js +23 -9
- package/lib/token-audit-log.js +32 -9
- package/lib/token-permission-view.js +104 -8
- package/lib/tokens.js +194 -68
- package/lib/tools.js +96 -0
- package/lib/ui-routes/account-routes.js +55 -6
- package/lib/ui-routes/admin-config-routes.js +114 -1
- package/lib/ui-routes/admin-entities-routes.js +103 -19
- package/lib/ui-routes/auth-routes.js +13 -0
- package/lib/ui-routes/mcp-consent-routes.js +330 -0
- package/lib/ui-routes/oauth-config-routes.js +7 -7
- package/lib/ui-routes/route-helpers.js +143 -1
- package/lib/ui-routes/suppression-list-routes.js +14 -9
- package/lib/utils/decode-headers.js +42 -0
- package/lib/utils/header-map.js +48 -0
- package/lib/utils/network.js +9 -1
- package/lib/utils/unsafe-key.js +59 -0
- package/lib/web-safe-html.js +61 -0
- package/lib/webhook-routing.js +6 -2
- package/lib/webhooks.js +31 -8
- package/package.json +9 -8
- package/sbom.json +1 -1
- package/scripts/README.md +25 -0
- package/scripts/build-icon.js +77 -0
- package/server.js +39 -17
- package/static/css/flyonui.css +1 -1
- package/static/css/src/app.tailwind.css +98 -0
- package/static/emailengine.ico +0 -0
- package/static/js/ui.js +465 -1
- package/static/licenses.html +42 -22
- package/translations/messages.pot +57 -57
- package/views/accounts/account.hbs +8 -8
- package/views/accounts/browse.hbs +1 -1
- package/views/accounts/edit.hbs +2 -2
- package/views/config/mcp.hbs +361 -0
- package/views/config/oauth/index.hbs +7 -0
- package/views/gateways/edit.hbs +1 -1
- package/views/gateways/gateway.hbs +2 -2
- package/views/gateways/index.hbs +7 -7
- package/views/layout/prompt.hbs +62 -0
- package/views/mcp/authorize.hbs +136 -0
- package/views/partials/accounts_table.hbs +7 -7
- package/views/partials/head_assets.hbs +3 -3
- package/views/partials/mcp_access_levels.hbs +13 -0
- package/views/partials/mcp_account_limit.hbs +14 -0
- package/views/partials/mcp_tool_count.hbs +19 -0
- package/views/partials/side_menu.hbs +1 -0
- package/views/partials/ui/account-picker.hbs +59 -0
- package/views/partials/ui/code-block.hbs +26 -0
- package/views/partials/ui/menu-item.hbs +5 -0
- package/views/partials/ui/radio.hbs +41 -0
- package/views/partials/ui/tab.hbs +15 -10
- package/views/partials/ui/tabs.hbs +8 -5
- package/views/suppression-lists/list.hbs +1 -1
- package/views/templates/index.hbs +2 -2
- package/views/templates/template.hbs +5 -4
- package/views/tokens/index.hbs +30 -4
- package/views/tokens/new.hbs +517 -239
- package/winconf.js +148 -48
- package/workers/api.js +239 -95
- package/workers/imap.js +65 -72
- package/workers/webhooks.js +21 -1
|
@@ -173,6 +173,41 @@
|
|
|
173
173
|
--link-color: var(--color-primary);
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
+
/* Segmented (pill) tab strip, used through ui/tabs variant="segment" for a
|
|
177
|
+
* choice nested inside a page whose top already carries a bordered tab row -
|
|
178
|
+
* a second identical bordered row would read as the same navigation level.
|
|
179
|
+
* FlyonUI ships no segmented tabs variant, only bordered/lifted/sizes. The
|
|
180
|
+
* active state is the same .active class the FlyonUI data-tabs plugin toggles
|
|
181
|
+
* on .tab buttons. Un-layered on purpose: FlyonUI emits .tab and .tab-active
|
|
182
|
+
* in @layer utilities, which beats any components-layer rule regardless of
|
|
183
|
+
* specificity - in a layer, the pill metrics and colors would all be dead
|
|
184
|
+
* declarations. The active color is stated explicitly because these
|
|
185
|
+
* un-layered rules in turn beat .tab-active's own primary. */
|
|
186
|
+
.tabs-segment {
|
|
187
|
+
background-color: var(--color-base-200);
|
|
188
|
+
/* visible on the base-200 page ground too, not only on base-100 cards */
|
|
189
|
+
border: 1px solid color-mix(in oklab, var(--color-base-content) 10%, transparent);
|
|
190
|
+
border-radius: var(--radius-field, 0.375rem);
|
|
191
|
+
padding: 0.25rem;
|
|
192
|
+
gap: 0.25rem;
|
|
193
|
+
width: fit-content;
|
|
194
|
+
}
|
|
195
|
+
.tabs-segment .tab {
|
|
196
|
+
height: 2rem;
|
|
197
|
+
border-radius: calc(var(--radius-field, 0.375rem) - 0.125rem);
|
|
198
|
+
padding-inline: 0.75rem;
|
|
199
|
+
font-size: 0.875rem;
|
|
200
|
+
color: color-mix(in oklab, var(--color-base-content) 70%, transparent);
|
|
201
|
+
}
|
|
202
|
+
.tabs-segment .tab:hover {
|
|
203
|
+
color: var(--color-base-content);
|
|
204
|
+
}
|
|
205
|
+
.tabs-segment .tab.active {
|
|
206
|
+
background-color: var(--color-base-100);
|
|
207
|
+
color: var(--color-primary);
|
|
208
|
+
box-shadow: 0 1px 2px 0 rgb(0 0 0 / 0.08);
|
|
209
|
+
}
|
|
210
|
+
|
|
176
211
|
/* highlight.js colors live in the hand-vendored static/css/highlight.min.css,
|
|
177
212
|
* rewritten to the FlyonUI theme tokens (single source; no load-order or
|
|
178
213
|
* specificity coupling between the two sheets). */
|
|
@@ -610,4 +645,67 @@
|
|
|
610
645
|
.alert-margin .alert {
|
|
611
646
|
margin: 10px 10px 0 0;
|
|
612
647
|
}
|
|
648
|
+
|
|
649
|
+
/* --- Account picker (ui/account-picker) --------------------------------
|
|
650
|
+
* The suggestion list and the card that replaces the search box once an
|
|
651
|
+
* account is picked. Classes rather than utilities because the list is an
|
|
652
|
+
* overlay: it has to be positioned, layered and scroll-bounded as one
|
|
653
|
+
* piece, and that is a component rather than a string of utilities on a
|
|
654
|
+
* node ui.js builds. (Utilities in ui.js ARE scanned - it is an @source
|
|
655
|
+
* above - so the row-level styling stays inline there.) */
|
|
656
|
+
.ee-account-picker-search {
|
|
657
|
+
position: relative;
|
|
658
|
+
}
|
|
659
|
+
.ee-account-picker-results {
|
|
660
|
+
position: absolute;
|
|
661
|
+
z-index: 20;
|
|
662
|
+
inset-inline: 0;
|
|
663
|
+
top: calc(100% + 0.25rem);
|
|
664
|
+
max-height: 18rem;
|
|
665
|
+
overflow-y: auto;
|
|
666
|
+
/* the list is an overlay over page content, so it needs its own ground */
|
|
667
|
+
background-color: var(--color-base-100);
|
|
668
|
+
border: var(--border, 1px) solid color-mix(in oklab, var(--color-base-content) 12%, transparent);
|
|
669
|
+
border-radius: var(--radius-box, 0.5rem);
|
|
670
|
+
box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
|
|
671
|
+
padding: 0.25rem;
|
|
672
|
+
}
|
|
673
|
+
.ee-account-picker-option {
|
|
674
|
+
display: block;
|
|
675
|
+
width: 100%;
|
|
676
|
+
text-align: start;
|
|
677
|
+
cursor: pointer;
|
|
678
|
+
padding: 0.5rem 0.625rem;
|
|
679
|
+
border-radius: var(--radius-field, 0.375rem);
|
|
680
|
+
/* every row is one account: its lines must not wrap into each other */
|
|
681
|
+
min-width: 0;
|
|
682
|
+
}
|
|
683
|
+
/* Hover and keyboard land on the same highlight, so the row under the
|
|
684
|
+
* pointer and the row Enter would pick are never two different rows. */
|
|
685
|
+
.ee-account-picker-option.is-active {
|
|
686
|
+
background-color: color-mix(in oklab, var(--color-base-content) 8%, transparent);
|
|
687
|
+
}
|
|
688
|
+
/* A modal body scrolls its own content (`overflow: hidden auto` in FlyonUI), which would clip
|
|
689
|
+
* the overlay to whatever room is left below the field. In flow the body simply grows and
|
|
690
|
+
* scrolls to the list instead - keyed on the ancestor rather than on a class ui.js would have
|
|
691
|
+
* to add, since the DOM already says where the picker is. */
|
|
692
|
+
.modal-body .ee-account-picker-results {
|
|
693
|
+
position: static;
|
|
694
|
+
margin-top: 0.25rem;
|
|
695
|
+
box-shadow: none;
|
|
696
|
+
}
|
|
697
|
+
.ee-account-picker-note {
|
|
698
|
+
padding: 0.5rem 0.625rem;
|
|
699
|
+
font-size: 0.875rem;
|
|
700
|
+
color: color-mix(in oklab, var(--color-base-content) 60%, transparent);
|
|
701
|
+
}
|
|
702
|
+
.ee-account-picker-card {
|
|
703
|
+
display: flex;
|
|
704
|
+
align-items: center;
|
|
705
|
+
gap: 0.75rem;
|
|
706
|
+
padding: 0.625rem 0.75rem;
|
|
707
|
+
border: var(--border, 1px) solid color-mix(in oklab, var(--color-base-content) 12%, transparent);
|
|
708
|
+
border-radius: var(--radius-field, 0.375rem);
|
|
709
|
+
background-color: var(--color-base-200);
|
|
710
|
+
}
|
|
613
711
|
}
|
package/static/emailengine.ico
CHANGED
|
Binary file
|
package/static/js/ui.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* global document, window, navigator, localStorage, HSStaticMethods, HSOverlay */
|
|
1
|
+
/* global document, window, navigator, localStorage, fetch, Event, AbortController, HSStaticMethods, HSOverlay */
|
|
2
2
|
|
|
3
3
|
'use strict';
|
|
4
4
|
|
|
@@ -392,6 +392,38 @@ document.addEventListener('click', e => {
|
|
|
392
392
|
window.uiCopyText(value, btn);
|
|
393
393
|
});
|
|
394
394
|
|
|
395
|
+
// Cross-tab links: an element with data-goto-tab="<panel id>" activates that
|
|
396
|
+
// panel's tab in a ui/tabs strip. The target may sit inside another strip's
|
|
397
|
+
// panel (a nested method switch), so the whole ancestor chain of tabpanels is
|
|
398
|
+
// activated outermost-first - otherwise the target tab would light up inside
|
|
399
|
+
// a panel that stays hidden. Each panel names its own button through
|
|
400
|
+
// aria-labelledby (the ui/tabs contract, read the same way in reference.js)
|
|
401
|
+
// rather than this handler recomposing ui/tab's "<id>-tab" id convention.
|
|
402
|
+
// Delegated like the copy buttons, so pages need no script of their own for
|
|
403
|
+
// a "see the other tab" pointer.
|
|
404
|
+
document.addEventListener('click', e => {
|
|
405
|
+
let link = e.target.closest('[data-goto-tab]');
|
|
406
|
+
if (!link) {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
let tabs = [];
|
|
411
|
+
for (
|
|
412
|
+
let panel = document.getElementById(link.dataset.gotoTab);
|
|
413
|
+
panel;
|
|
414
|
+
panel = panel.parentElement && panel.parentElement.closest('[role="tabpanel"]')
|
|
415
|
+
) {
|
|
416
|
+
let tab = document.getElementById(panel.getAttribute('aria-labelledby'));
|
|
417
|
+
if (tab) {
|
|
418
|
+
tabs.unshift(tab);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
for (let tab of tabs) {
|
|
423
|
+
tab.click();
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
|
|
395
427
|
// Resource-list row delete: a .list-delete-btn (rendered by ui/row-actions in
|
|
396
428
|
// a kebab menu) opens the page's shared confirm modal, filling in the resource
|
|
397
429
|
// name and either the modal form's hidden id field (payload-based delete
|
|
@@ -613,3 +645,435 @@ window.uiCodeExamples = config => {
|
|
|
613
645
|
return renderExamples;
|
|
614
646
|
};
|
|
615
647
|
|
|
648
|
+
|
|
649
|
+
// The MCP tool count: how many of the endpoint's tools a credential would actually be offered.
|
|
650
|
+
//
|
|
651
|
+
// Shared by the three places an mcp-scoped token is minted - the access-token form, the MCP config
|
|
652
|
+
// page's generator and the OAuth consent prompt - because all three ask the same question, and the
|
|
653
|
+
// count is the one thing a permission record does not tell a reader: the MCP surface reaches six of
|
|
654
|
+
// the thirteen sections, so a record that looks generous can still leave an agent holding one tool.
|
|
655
|
+
//
|
|
656
|
+
// Two filters, the same two lib/mcp/tools.js toolVisibleTo() applies to tools/list: the permission
|
|
657
|
+
// record, and the account binding - a credential bound to one account is never offered the tools
|
|
658
|
+
// that take no account argument, because there is nothing to bind them to. This is the browser's
|
|
659
|
+
// copy of that rule, and test/mcp-tools-test.js asserts the two agree over the whole catalog.
|
|
660
|
+
//
|
|
661
|
+
// `record` is { actions, groups, unrestricted, account }. `unrestricted` is the absence of a
|
|
662
|
+
// permissions record, which is a different answer from an empty one: the scopes are the only bound.
|
|
663
|
+
// A null record clears the element - the question does not apply to this credential at all, which
|
|
664
|
+
// is not the same as it having no tools.
|
|
665
|
+
window.uiMcpToolCount = (elm, record) => {
|
|
666
|
+
let tools = JSON.parse(elm.dataset.mcpTools || '[]');
|
|
667
|
+
|
|
668
|
+
elm.replaceChildren();
|
|
669
|
+
if (!tools.length || !record) {
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
let bound = !!(record.account || '').trim();
|
|
674
|
+
let offered = bound ? tools.filter(tool => tool.accountScoped) : tools;
|
|
675
|
+
let available = record.unrestricted ? offered : offered.filter(tool => record.actions.includes(tool.action) && record.groups.includes(tool.group));
|
|
676
|
+
|
|
677
|
+
let count = document.createElement('div');
|
|
678
|
+
let countLabel = document.createElement('strong');
|
|
679
|
+
countLabel.textContent = available.length + ' of ' + offered.length + ' MCP tools available';
|
|
680
|
+
count.append(countLabel);
|
|
681
|
+
elm.append(count);
|
|
682
|
+
|
|
683
|
+
let names = document.createElement('div');
|
|
684
|
+
names.className = 'text-base-content/60 break-words';
|
|
685
|
+
names.textContent = available.length ? available.map(tool => tool.name).join(', ') : 'A connected agent would see no tools at all.';
|
|
686
|
+
elm.append(names);
|
|
687
|
+
|
|
688
|
+
// Said out loud rather than left as a smaller total: the tools a binding takes away are the
|
|
689
|
+
// ones an agent would otherwise use to discover what it is connected to.
|
|
690
|
+
let instanceWide = tools.filter(tool => !tool.accountScoped);
|
|
691
|
+
if (bound && instanceWide.length) {
|
|
692
|
+
let note = document.createElement('div');
|
|
693
|
+
note.className = 'text-base-content/60';
|
|
694
|
+
note.textContent = 'Bound to one account, so the instance-wide tools are not offered: ' + instanceWide.map(tool => tool.name).join(', ') + '.';
|
|
695
|
+
elm.append(note);
|
|
696
|
+
}
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
// Auto-wiring for the pages whose whole answer is a named access level (the MCP config generator
|
|
700
|
+
// and the consent prompt). The access-token form drives its own count instead, because its fourth
|
|
701
|
+
// option is a hand-built record rather than a level, and it carries no data-mcp-level-name.
|
|
702
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
703
|
+
for (let elm of document.querySelectorAll('[data-mcp-level-name]')) {
|
|
704
|
+
let presets = JSON.parse(elm.dataset.mcpPresets || '{}');
|
|
705
|
+
let accountElm = elm.dataset.mcpAccountId ? document.getElementById(elm.dataset.mcpAccountId) : null;
|
|
706
|
+
let radios = Array.from(document.querySelectorAll('input[name="' + elm.dataset.mcpLevelName + '"]'));
|
|
707
|
+
|
|
708
|
+
let paint = () => {
|
|
709
|
+
let selected = radios.find(radio => radio.checked);
|
|
710
|
+
let level = (selected && selected.value) || 'read';
|
|
711
|
+
let preset = presets[level];
|
|
712
|
+
|
|
713
|
+
window.uiMcpToolCount(elm, {
|
|
714
|
+
actions: preset ? preset.actions : [],
|
|
715
|
+
groups: preset ? preset.groups : [],
|
|
716
|
+
// A level the table stores as null is the unrestricted one. A level missing from
|
|
717
|
+
// the table entirely is not, and counts as nothing rather than as everything -
|
|
718
|
+
// the same direction every other reader of this table fails in.
|
|
719
|
+
unrestricted: !preset && Object.prototype.hasOwnProperty.call(presets, level),
|
|
720
|
+
account: accountElm ? accountElm.value : ''
|
|
721
|
+
});
|
|
722
|
+
};
|
|
723
|
+
|
|
724
|
+
radios.forEach(radio => radio.addEventListener('change', paint));
|
|
725
|
+
if (accountElm) {
|
|
726
|
+
// Per keystroke, so what the binding costs is visible while the field is being filled
|
|
727
|
+
// in rather than only after it loses focus
|
|
728
|
+
accountElm.addEventListener('input', paint);
|
|
729
|
+
}
|
|
730
|
+
paint();
|
|
731
|
+
}
|
|
732
|
+
});
|
|
733
|
+
|
|
734
|
+
// The account picker (views/partials/ui/account-picker.hbs).
|
|
735
|
+
//
|
|
736
|
+
// A free-text account id is a field only the person who already knows the id can fill in, so the
|
|
737
|
+
// control is a search box: type any part of an id, a name or an address, pick from the
|
|
738
|
+
// suggestions, and the box is replaced by a card naming what was picked.
|
|
739
|
+
//
|
|
740
|
+
// The posted value never stops being a plain account id in the hidden input the partial renders,
|
|
741
|
+
// and every change to it fires `input` and `change` there - so a page that watches that field (the
|
|
742
|
+
// MCP tool count follows it on all three pages that mint a token) needs no knowledge of this at
|
|
743
|
+
// all. Everything below is about which id is in that input, and nothing else reads the choice.
|
|
744
|
+
const uiAccountPickerEndpoint = '/admin/accounts/suggestions';
|
|
745
|
+
|
|
746
|
+
// How long a keystroke waits before it becomes a request. Long enough that typing an account id
|
|
747
|
+
// costs one round trip rather than twenty, short enough to feel like the list is following along.
|
|
748
|
+
const uiAccountPickerDebounce = 200;
|
|
749
|
+
|
|
750
|
+
// Every row is built as DOM nodes with textContent - account names and addresses are attacker-set
|
|
751
|
+
// (a display name arrives from a provider), and this list is rendered on an authenticated admin
|
|
752
|
+
// page, which is the worst place to hand one an innerHTML.
|
|
753
|
+
const uiAccountPickerLine = (className, text) => {
|
|
754
|
+
const elm = document.createElement('div');
|
|
755
|
+
elm.className = className;
|
|
756
|
+
elm.textContent = text;
|
|
757
|
+
return elm;
|
|
758
|
+
};
|
|
759
|
+
|
|
760
|
+
// The name a person recognises. An account may carry none, in which case the address is the name,
|
|
761
|
+
// and an account with neither is only ever its id.
|
|
762
|
+
const uiAccountPickerTitle = entry => entry.name || entry.email || entry.account;
|
|
763
|
+
|
|
764
|
+
const uiAccountPickerBadge = entry => {
|
|
765
|
+
if (!entry.state || !entry.state.name) {
|
|
766
|
+
return null;
|
|
767
|
+
}
|
|
768
|
+
const badge = document.createElement('span');
|
|
769
|
+
badge.className = 'badge badge-sm badge-' + (entry.state.type || 'neutral');
|
|
770
|
+
badge.textContent = entry.state.name;
|
|
771
|
+
return badge;
|
|
772
|
+
};
|
|
773
|
+
|
|
774
|
+
// The title line: the name a person recognises, plus the connection state. Shared by the card and
|
|
775
|
+
// by a result row, which differ only in the weight of the name - the two faces of the control are
|
|
776
|
+
// meant to read as the same thing, and building them from one place is what keeps them that way.
|
|
777
|
+
const uiAccountPickerTitleLine = (entry, nameClass) => {
|
|
778
|
+
const title = document.createElement('div');
|
|
779
|
+
title.className = 'flex items-center gap-2 min-w-0';
|
|
780
|
+
|
|
781
|
+
const name = document.createElement('span');
|
|
782
|
+
name.className = nameClass;
|
|
783
|
+
name.textContent = uiAccountPickerTitle(entry);
|
|
784
|
+
title.append(name);
|
|
785
|
+
|
|
786
|
+
const badge = uiAccountPickerBadge(entry);
|
|
787
|
+
if (badge) {
|
|
788
|
+
title.append(badge);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
return title;
|
|
792
|
+
};
|
|
793
|
+
|
|
794
|
+
// The identifying line under the title: the address when it is not already the title, and the id,
|
|
795
|
+
// which is the value actually being chosen and so is always shown. One line rather than two, so a
|
|
796
|
+
// screenful of the dropdown is a useful number of accounts to choose between.
|
|
797
|
+
const uiAccountPickerDetails = entry => {
|
|
798
|
+
const line = uiAccountPickerLine('text-base-content/60 truncate text-xs', '');
|
|
799
|
+
line.title = entry.account;
|
|
800
|
+
|
|
801
|
+
if (entry.email && entry.email !== uiAccountPickerTitle(entry)) {
|
|
802
|
+
line.append(entry.email + ' \u00b7 ');
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
const id = document.createElement('span');
|
|
806
|
+
id.className = 'font-mono';
|
|
807
|
+
id.textContent = entry.account;
|
|
808
|
+
line.append(id);
|
|
809
|
+
|
|
810
|
+
return line;
|
|
811
|
+
};
|
|
812
|
+
|
|
813
|
+
window.uiAccountPicker = root => {
|
|
814
|
+
const input = document.getElementById(root.dataset.input);
|
|
815
|
+
const card = root.querySelector('[data-picker-card]');
|
|
816
|
+
const search = root.querySelector('[data-picker-search]');
|
|
817
|
+
const results = root.querySelector('[data-picker-results]');
|
|
818
|
+
const box = search.querySelector('input');
|
|
819
|
+
|
|
820
|
+
if (!input || !card || !results || !box) {
|
|
821
|
+
return;
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
let entries = [];
|
|
825
|
+
// The rendered option nodes, so moving the highlight touches two of them rather than
|
|
826
|
+
// re-querying the list and rewriting every row
|
|
827
|
+
let options = [];
|
|
828
|
+
let active = -1;
|
|
829
|
+
let timer = null;
|
|
830
|
+
let inflight = null;
|
|
831
|
+
// The last answered query and its result. A refocus on unchanged text repaints from this
|
|
832
|
+
// instead of asking the server for the same page again - every one of those costs an account
|
|
833
|
+
// listing, and tabbing through a form would otherwise pay for one per pass.
|
|
834
|
+
let cached = null;
|
|
835
|
+
|
|
836
|
+
const open = () => {
|
|
837
|
+
results.classList.remove('hidden');
|
|
838
|
+
box.setAttribute('aria-expanded', 'true');
|
|
839
|
+
};
|
|
840
|
+
|
|
841
|
+
const close = () => {
|
|
842
|
+
results.classList.add('hidden');
|
|
843
|
+
results.replaceChildren();
|
|
844
|
+
box.setAttribute('aria-expanded', 'false');
|
|
845
|
+
box.removeAttribute('aria-activedescendant');
|
|
846
|
+
entries = [];
|
|
847
|
+
options = [];
|
|
848
|
+
active = -1;
|
|
849
|
+
};
|
|
850
|
+
|
|
851
|
+
// Paints whichever of the two faces the control currently has. Called for every change of the
|
|
852
|
+
// selection, so the card and the search box can never both be showing.
|
|
853
|
+
const paint = selected => {
|
|
854
|
+
card.replaceChildren();
|
|
855
|
+
|
|
856
|
+
if (!selected) {
|
|
857
|
+
card.classList.add('hidden');
|
|
858
|
+
search.classList.remove('hidden');
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
const text = document.createElement('div');
|
|
863
|
+
text.className = 'min-w-0 grow';
|
|
864
|
+
text.append(uiAccountPickerTitleLine(selected, 'font-medium truncate'), uiAccountPickerDetails(selected));
|
|
865
|
+
|
|
866
|
+
const clear = document.createElement('button');
|
|
867
|
+
clear.type = 'button';
|
|
868
|
+
clear.className = 'btn btn-text btn-sm btn-circle shrink-0';
|
|
869
|
+
clear.setAttribute('aria-label', 'Clear the selected account');
|
|
870
|
+
const clearIcon = document.createElement('span');
|
|
871
|
+
clearIcon.className = 'icon-[tabler--x] size-4';
|
|
872
|
+
clear.append(clearIcon);
|
|
873
|
+
clear.addEventListener('click', () => clearSelection(true));
|
|
874
|
+
|
|
875
|
+
card.append(text, clear);
|
|
876
|
+
card.classList.remove('hidden');
|
|
877
|
+
search.classList.add('hidden');
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
// Back to empty: the hidden input, the card and the search box all have to agree, so this is
|
|
881
|
+
// one path rather than a clear button that happens to do the same three things.
|
|
882
|
+
const clearSelection = focus => {
|
|
883
|
+
select(null);
|
|
884
|
+
box.value = '';
|
|
885
|
+
if (focus) {
|
|
886
|
+
box.focus();
|
|
887
|
+
}
|
|
888
|
+
};
|
|
889
|
+
|
|
890
|
+
// The one writer of the hidden input. The events are what the rest of the page listens to, and
|
|
891
|
+
// a programmatic value assignment fires neither on its own.
|
|
892
|
+
const select = selected => {
|
|
893
|
+
close();
|
|
894
|
+
input.value = selected ? selected.account : '';
|
|
895
|
+
paint(selected);
|
|
896
|
+
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
897
|
+
input.dispatchEvent(new Event('change', { bubbles: true }));
|
|
898
|
+
};
|
|
899
|
+
|
|
900
|
+
// Wraps at both ends, so ArrowUp from nothing selected lands on the last row
|
|
901
|
+
const highlight = index => {
|
|
902
|
+
if (options[active]) {
|
|
903
|
+
options[active].classList.remove('is-active');
|
|
904
|
+
options[active].setAttribute('aria-selected', 'false');
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
active = options.length ? (index + options.length) % options.length : -1;
|
|
908
|
+
|
|
909
|
+
if (active < 0) {
|
|
910
|
+
box.removeAttribute('aria-activedescendant');
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
const option = options[active];
|
|
915
|
+
option.classList.add('is-active');
|
|
916
|
+
option.setAttribute('aria-selected', 'true');
|
|
917
|
+
box.setAttribute('aria-activedescendant', option.id);
|
|
918
|
+
option.scrollIntoView({ block: 'nearest' });
|
|
919
|
+
};
|
|
920
|
+
|
|
921
|
+
const render = data => {
|
|
922
|
+
results.replaceChildren();
|
|
923
|
+
entries = data.accounts || [];
|
|
924
|
+
options = [];
|
|
925
|
+
active = -1;
|
|
926
|
+
|
|
927
|
+
if (!entries.length) {
|
|
928
|
+
results.append(uiAccountPickerLine('ee-account-picker-note', 'No account matches that.'));
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
entries.forEach((entry, index) => {
|
|
932
|
+
const option = document.createElement('button');
|
|
933
|
+
option.type = 'button';
|
|
934
|
+
option.id = box.id + '-option-' + index;
|
|
935
|
+
option.className = 'ee-account-picker-option';
|
|
936
|
+
option.setAttribute('role', 'option');
|
|
937
|
+
option.setAttribute('aria-selected', 'false');
|
|
938
|
+
option.append(uiAccountPickerTitleLine(entry, 'truncate'), uiAccountPickerDetails(entry));
|
|
939
|
+
|
|
940
|
+
// mousedown rather than click: the box loses focus first otherwise, and the blur
|
|
941
|
+
// handler closes the list out from under the click that was choosing from it
|
|
942
|
+
option.addEventListener('mousedown', event => {
|
|
943
|
+
event.preventDefault();
|
|
944
|
+
select(entry);
|
|
945
|
+
});
|
|
946
|
+
option.addEventListener('mouseenter', () => highlight(index));
|
|
947
|
+
|
|
948
|
+
options.push(option);
|
|
949
|
+
results.append(option);
|
|
950
|
+
});
|
|
951
|
+
|
|
952
|
+
// Said out loud rather than left as a list that silently stops: the reader would otherwise
|
|
953
|
+
// read a capped list as the whole instance
|
|
954
|
+
if (data.more > 0) {
|
|
955
|
+
results.append(uiAccountPickerLine('ee-account-picker-note', data.more + ' more match. Type a little more to narrow it down.'));
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
open();
|
|
959
|
+
};
|
|
960
|
+
|
|
961
|
+
const load = () => {
|
|
962
|
+
const query = box.value.trim();
|
|
963
|
+
|
|
964
|
+
if (cached && cached.query === query) {
|
|
965
|
+
render(cached.data);
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
// A request the box has already moved on from is cancelled rather than left to finish:
|
|
970
|
+
// each one is a full account listing, and a typed word would otherwise leave several of
|
|
971
|
+
// them running server-side for a result nobody paints.
|
|
972
|
+
if (inflight) {
|
|
973
|
+
inflight.abort();
|
|
974
|
+
}
|
|
975
|
+
const controller = new AbortController();
|
|
976
|
+
inflight = controller;
|
|
977
|
+
|
|
978
|
+
fetch(uiAccountPickerEndpoint + (query ? '?query=' + encodeURIComponent(query) : ''), {
|
|
979
|
+
headers: { accept: 'application/json' },
|
|
980
|
+
signal: controller.signal
|
|
981
|
+
})
|
|
982
|
+
.then(res => (res.ok ? res.json() : Promise.reject(new Error('HTTP ' + res.status))))
|
|
983
|
+
.then(data => {
|
|
984
|
+
cached = { query, data };
|
|
985
|
+
render(data);
|
|
986
|
+
})
|
|
987
|
+
.catch(err => {
|
|
988
|
+
// An aborted request was superseded; its replacement is already on its way
|
|
989
|
+
if (err.name === 'AbortError') {
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
992
|
+
results.replaceChildren(uiAccountPickerLine('ee-account-picker-note', 'The account list could not be loaded.'));
|
|
993
|
+
options = [];
|
|
994
|
+
active = -1;
|
|
995
|
+
open();
|
|
996
|
+
});
|
|
997
|
+
};
|
|
998
|
+
|
|
999
|
+
const schedule = () => {
|
|
1000
|
+
window.clearTimeout(timer);
|
|
1001
|
+
timer = window.setTimeout(load, uiAccountPickerDebounce);
|
|
1002
|
+
};
|
|
1003
|
+
|
|
1004
|
+
box.addEventListener('input', schedule);
|
|
1005
|
+
// Reopening on an unchanged box repaints from the cache, so this is only a request when the
|
|
1006
|
+
// text has actually moved on since the list was last filled in
|
|
1007
|
+
box.addEventListener('focus', load);
|
|
1008
|
+
|
|
1009
|
+
box.addEventListener('keydown', event => {
|
|
1010
|
+
switch (event.key) {
|
|
1011
|
+
case 'ArrowDown':
|
|
1012
|
+
event.preventDefault();
|
|
1013
|
+
highlight(active + 1);
|
|
1014
|
+
break;
|
|
1015
|
+
case 'ArrowUp':
|
|
1016
|
+
event.preventDefault();
|
|
1017
|
+
highlight(active - 1);
|
|
1018
|
+
break;
|
|
1019
|
+
case 'Enter':
|
|
1020
|
+
if (active >= 0 && entries[active]) {
|
|
1021
|
+
event.preventDefault();
|
|
1022
|
+
select(entries[active]);
|
|
1023
|
+
}
|
|
1024
|
+
break;
|
|
1025
|
+
case 'Escape':
|
|
1026
|
+
close();
|
|
1027
|
+
break;
|
|
1028
|
+
}
|
|
1029
|
+
});
|
|
1030
|
+
|
|
1031
|
+
box.addEventListener('blur', () => {
|
|
1032
|
+
window.clearTimeout(timer);
|
|
1033
|
+
if (inflight) {
|
|
1034
|
+
inflight.abort();
|
|
1035
|
+
inflight = null;
|
|
1036
|
+
}
|
|
1037
|
+
close();
|
|
1038
|
+
});
|
|
1039
|
+
|
|
1040
|
+
// Reached from the card's own clear button and, through window.uiAccountPickerClear() below,
|
|
1041
|
+
// from a page resetting a form it did not build.
|
|
1042
|
+
root.uiPickerClear = clearSelection;
|
|
1043
|
+
|
|
1044
|
+
// A stored value the server could resolve renders as the account it names; one it could not is
|
|
1045
|
+
// still shown, because an id pointing at a deleted account is exactly the thing the person
|
|
1046
|
+
// filling in the form needs to see rather than an empty box.
|
|
1047
|
+
let initial = null;
|
|
1048
|
+
try {
|
|
1049
|
+
initial = JSON.parse(root.dataset.selected || 'null');
|
|
1050
|
+
} catch (err) {
|
|
1051
|
+
initial = null;
|
|
1052
|
+
}
|
|
1053
|
+
if (!initial && input.value) {
|
|
1054
|
+
initial = { account: input.value, name: '', email: '', state: { type: 'error', name: 'No such account' } };
|
|
1055
|
+
}
|
|
1056
|
+
paint(initial);
|
|
1057
|
+
};
|
|
1058
|
+
|
|
1059
|
+
/**
|
|
1060
|
+
* Clears an account picker, given its hidden input.
|
|
1061
|
+
*
|
|
1062
|
+
* For pages that reset a form as a whole - the template page empties its "send test email" modal
|
|
1063
|
+
* every time it opens. Assigning '' to the input is not enough on its own: the card is painted from
|
|
1064
|
+
* the last choice, so the control would keep showing an account the form no longer carries.
|
|
1065
|
+
*
|
|
1066
|
+
* @param {Element} input - the picker's hidden input
|
|
1067
|
+
*/
|
|
1068
|
+
window.uiAccountPickerClear = input => {
|
|
1069
|
+
const root = input && input.closest && input.closest('[data-account-picker]');
|
|
1070
|
+
if (root && root.uiPickerClear) {
|
|
1071
|
+
root.uiPickerClear();
|
|
1072
|
+
}
|
|
1073
|
+
};
|
|
1074
|
+
|
|
1075
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
1076
|
+
for (let root of document.querySelectorAll('[data-account-picker]')) {
|
|
1077
|
+
window.uiAccountPicker(root);
|
|
1078
|
+
}
|
|
1079
|
+
});
|