@crombolo/backoffice-host 0.64.0 → 0.66.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.
@@ -0,0 +1,92 @@
1
+ /**
2
+ * The banner across the top of the back office while an administrator views it
3
+ * as user groups (story 314): which groups, until when, and Switch back. When
4
+ * the hour is up the back office reloads as them again.
5
+ */
6
+ import { css, html, LitElement, nothing } from '@umbraco-cms/backoffice/external/lit'
7
+ import { reloadBackOffice, stopViewAs } from './view-as.js'
8
+ import { msUntil, viewingLabel } from './view-as-words.js'
9
+
10
+ export default class CromboloViewAsBannerElement extends LitElement {
11
+ static properties = {
12
+ viewing: { attribute: false },
13
+ _busy: { state: true },
14
+ _message: { state: true },
15
+ }
16
+
17
+ constructor() {
18
+ super()
19
+ this.viewing = undefined
20
+ this._busy = false
21
+ this._message = ''
22
+ this._timer = undefined
23
+ }
24
+
25
+ connectedCallback() {
26
+ super.connectedCallback()
27
+ // Past the hour, the server has already ended it; a reload shows that.
28
+ if (this.viewing) this._timer = setTimeout(reloadBackOffice, msUntil(this.viewing.until) + 1000)
29
+ }
30
+
31
+ disconnectedCallback() {
32
+ super.disconnectedCallback()
33
+ clearTimeout(this._timer)
34
+ }
35
+
36
+ async #switchBack() {
37
+ this._busy = true
38
+ try {
39
+ await stopViewAs()
40
+ reloadBackOffice()
41
+ } catch (error) {
42
+ this._message = error.message
43
+ this._busy = false
44
+ }
45
+ }
46
+
47
+ render() {
48
+ if (!this.viewing) return nothing
49
+ return html`<div role="status">
50
+ <span>${viewingLabel(this.viewing)}</span>
51
+ ${this._message ? html`<span class="problem">${this._message}</span>` : nothing}
52
+ <uui-button
53
+ look="primary"
54
+ color="default"
55
+ compact
56
+ label="Switch back"
57
+ ?disabled=${this._busy}
58
+ @click=${() => this.#switchBack()}
59
+ >Switch back</uui-button
60
+ >
61
+ </div>`
62
+ }
63
+
64
+ static styles = css`
65
+ :host {
66
+ position: fixed;
67
+ top: 0;
68
+ left: 0;
69
+ right: 0;
70
+ height: var(--crombolo-view-as-height, 40px);
71
+ z-index: 10000;
72
+ display: block;
73
+ }
74
+ div {
75
+ box-sizing: border-box;
76
+ height: 100%;
77
+ display: flex;
78
+ align-items: center;
79
+ justify-content: center;
80
+ gap: var(--uui-size-space-4, 12px);
81
+ padding: 0 var(--uui-size-space-4, 12px);
82
+ background: var(--uui-color-warning, #fbd142);
83
+ color: var(--uui-color-warning-contrast, #000);
84
+ font-weight: bold;
85
+ }
86
+ .problem {
87
+ font-weight: normal;
88
+ }
89
+ `
90
+ }
91
+
92
+ customElements.define('crombolo-view-as-banner', CromboloViewAsBannerElement)
@@ -0,0 +1,110 @@
1
+ /**
2
+ * The panel behind "View as a group…" (story 314): the groups to tick, and a
3
+ * button that starts the view and reloads the back office into it.
4
+ */
5
+ import { css, html, nothing } from '@umbraco-cms/backoffice/external/lit'
6
+ import { UmbModalBaseElement } from '@umbraco-cms/backoffice/modal'
7
+ import { readViewAs, reloadBackOffice, startViewAs } from './view-as.js'
8
+ import { PANEL_NOTE, startButton } from './view-as-words.js'
9
+
10
+ export default class CromboloViewAsModalElement extends UmbModalBaseElement {
11
+ static properties = {
12
+ _groups: { state: true },
13
+ _chosen: { state: true },
14
+ _busy: { state: true },
15
+ _message: { state: true },
16
+ }
17
+
18
+ constructor() {
19
+ super()
20
+ this._groups = undefined
21
+ this._chosen = []
22
+ this._busy = false
23
+ this._message = ''
24
+ }
25
+
26
+ connectedCallback() {
27
+ super.connectedCallback()
28
+ this.#load()
29
+ }
30
+
31
+ async #load() {
32
+ try {
33
+ const status = await readViewAs()
34
+ this._groups = status.groups
35
+ this._chosen = status.viewing?.groups.map((g) => g.key) ?? []
36
+ } catch (error) {
37
+ this._message = error.message
38
+ }
39
+ }
40
+
41
+ #toggle(key, on) {
42
+ this._chosen = on ? [...this._chosen, key] : this._chosen.filter((k) => k !== key)
43
+ }
44
+
45
+ async #start() {
46
+ this._busy = true
47
+ this._message = ''
48
+ try {
49
+ await startViewAs(this._chosen)
50
+ reloadBackOffice()
51
+ } catch (error) {
52
+ this._message = error.message
53
+ this._busy = false
54
+ }
55
+ }
56
+
57
+ render() {
58
+ const button = startButton(this._chosen)
59
+ return html`<umb-body-layout headline="View as a group">
60
+ <p>${PANEL_NOTE}</p>
61
+ ${
62
+ this._groups
63
+ ? html`<ul>
64
+ ${this._groups.map(
65
+ (group) => html`<li>
66
+ <uui-checkbox
67
+ label=${group.name}
68
+ ?checked=${this._chosen.includes(group.key)}
69
+ @change=${(event) => this.#toggle(group.key, event.target.checked)}
70
+ >${group.name}</uui-checkbox
71
+ >
72
+ </li>`,
73
+ )}
74
+ </ul>`
75
+ : this._message
76
+ ? nothing
77
+ : html`<uui-loader></uui-loader>`
78
+ }
79
+ ${this._message ? html`<p class="problem" role="alert">${this._message}</p>` : nothing}
80
+ <uui-button slot="actions" label="Cancel" @click=${() => this._rejectModal()}
81
+ >Cancel</uui-button
82
+ >
83
+ <uui-button
84
+ slot="actions"
85
+ look="primary"
86
+ color="positive"
87
+ label=${button.label}
88
+ ?disabled=${this._busy || !button.enabled}
89
+ @click=${() => this.#start()}
90
+ >${button.label}</uui-button
91
+ >
92
+ </umb-body-layout>`
93
+ }
94
+
95
+ static styles = css`
96
+ ul {
97
+ list-style: none;
98
+ margin: var(--uui-size-space-4, 12px) 0;
99
+ padding: 0;
100
+ }
101
+ li {
102
+ padding: var(--uui-size-space-2, 6px) 0;
103
+ }
104
+ .problem {
105
+ color: var(--uui-color-danger);
106
+ }
107
+ `
108
+ }
109
+
110
+ customElements.define('crombolo-view-as-modal', CromboloViewAsModalElement)
@@ -0,0 +1,56 @@
1
+ /** What the back office says while an administrator views it as user groups (story 314). Pure. */
2
+
3
+ /**
4
+ * The groups' names as a list: "Editors, Writers".
5
+ * @param {Array<{ name: string }>} groups
6
+ * @returns {string}
7
+ */
8
+ export function groupNames(groups) {
9
+ return groups.map((g) => g.name).join(', ')
10
+ }
11
+
12
+ /**
13
+ * The time of day a view ends, as the browser writes times: "14:32".
14
+ * @param {string} iso
15
+ * @param {string} [locale]
16
+ * @returns {string}
17
+ */
18
+ export function endsAt(iso, locale) {
19
+ return new Date(iso).toLocaleTimeString(locale, { hour: '2-digit', minute: '2-digit' })
20
+ }
21
+
22
+ /**
23
+ * The banner's words: "Viewing as Editors, Writers until 14:32".
24
+ * @param {{ groups: Array<{ name: string }>, until: string }} viewing
25
+ * @param {string} [locale]
26
+ * @returns {string}
27
+ */
28
+ export function viewingLabel(viewing, locale) {
29
+ return `Viewing as ${groupNames(viewing.groups)} until ${endsAt(viewing.until, locale)}`
30
+ }
31
+
32
+ /**
33
+ * Whether Start may be pressed, and what it says.
34
+ * @param {string[]} chosen the keys ticked
35
+ * @returns {{ enabled: boolean, label: string }}
36
+ */
37
+ export function startButton(chosen) {
38
+ return chosen.length === 0
39
+ ? { enabled: false, label: 'Choose a group' }
40
+ : { enabled: true, label: chosen.length === 1 ? 'View as this group' : 'View as these groups' }
41
+ }
42
+
43
+ /** What the panel says it does. */
44
+ export const PANEL_NOTE =
45
+ 'For the next hour, the back office in this browser shows and allows only what the groups ' +
46
+ 'you choose can do. Your other devices are not affected. Anything you change is recorded as you.'
47
+
48
+ /**
49
+ * Milliseconds until the view ends, never less than zero.
50
+ * @param {string} iso
51
+ * @param {number} [now]
52
+ * @returns {number}
53
+ */
54
+ export function msUntil(iso, now = Date.now()) {
55
+ return Math.max(0, Date.parse(iso) - now)
56
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Viewing the back office as user groups (story 314): "View as a group…" in an
3
+ * administrator's user menu opens a panel to choose them, and while it is on a
4
+ * banner across the top says so, with Switch back. The server keeps it on this
5
+ * session at <backoffice>/crombolo/api/view-as and decides what it allows.
6
+ */
7
+
8
+ export const VIEW_AS_MODAL = 'Crombolo.Modal.ViewAs'
9
+
10
+ const endpoint = () =>
11
+ `${(document.querySelector('base')?.getAttribute('href') ?? '/umbraco/').replace(/\/$/, '')}/crombolo/api/view-as`
12
+
13
+ const call = async (method, body) => {
14
+ const response = await fetch(endpoint(), {
15
+ method,
16
+ credentials: 'include',
17
+ headers: {
18
+ accept: 'application/json',
19
+ ...(body ? { 'content-type': 'application/json' } : {}),
20
+ },
21
+ body: body ? JSON.stringify(body) : undefined,
22
+ })
23
+ const answer = await response.json().catch(() => ({}))
24
+ if (!response.ok) throw new Error(answer.message ?? 'That did not work. Try again.')
25
+ return answer
26
+ }
27
+
28
+ /** Whether this session is viewing as groups, whether it may, and the groups to choose from. */
29
+ export const readViewAs = () => call('GET')
30
+
31
+ /** Starts viewing as the groups with these keys. */
32
+ export const startViewAs = (groups) => call('POST', { groups })
33
+
34
+ /** Switches back to the person's own rights. */
35
+ export const stopViewAs = () => call('DELETE')
36
+
37
+ /** Reloads the back office, so its sections, trees and actions are fetched again. */
38
+ export const reloadBackOffice = () => window.location.reload()
39
+
40
+ export const VIEW_AS_ACTION = {
41
+ type: 'currentUserAction',
42
+ kind: 'default',
43
+ alias: 'Crombolo.CurrentUser.Button.ViewAs',
44
+ name: 'Crombolo View As A Group',
45
+ weight: 800,
46
+ api: () => import('./view-as-action.js'),
47
+ meta: { label: 'View as a group…', icon: 'icon-users' },
48
+ conditions: [{ alias: 'Umb.Condition.CurrentUser.IsAdmin' }],
49
+ }
50
+
51
+ export const VIEW_AS_PANEL = {
52
+ type: 'modal',
53
+ alias: VIEW_AS_MODAL,
54
+ name: 'Crombolo View As A Group Panel',
55
+ element: () => import('./view-as-modal.js'),
56
+ }
57
+
58
+ /** Puts the banner above the back office, which moves down to make room. */
59
+ async function showBanner(viewing) {
60
+ await import('./view-as-banner.js')
61
+ if (document.querySelector('crombolo-view-as-banner')) return
62
+ const banner = document.createElement('crombolo-view-as-banner')
63
+ banner.viewing = viewing
64
+ document.body.prepend(banner)
65
+ const style = document.createElement('style')
66
+ style.id = 'crombolo-view-as-room'
67
+ style.textContent = `umb-app { display: block; position: fixed; inset: var(--crombolo-view-as-height, 40px) 0 0 0; }`
68
+ document.head.append(style)
69
+ }
70
+
71
+ export const onInit = async (_host, extensionRegistry) => {
72
+ extensionRegistry.register(VIEW_AS_ACTION)
73
+ extensionRegistry.register(VIEW_AS_PANEL)
74
+ try {
75
+ const status = await readViewAs()
76
+ if (status.viewing) await showBanner(status.viewing)
77
+ } catch {
78
+ // Not signed in yet, or the server cannot say; nothing to show.
79
+ }
80
+ }
@@ -7,7 +7,7 @@
7
7
  * bundle is.
8
8
  */
9
9
  import { existsSync, lstatSync, readdirSync, readFileSync, realpathSync, statSync } from 'node:fs'
10
- import { dirname, join, normalize } from 'node:path'
10
+ import { basename, dirname, join, normalize } from 'node:path'
11
11
  import { BACKOFFICE_CODE, permissionsProblem, readSettings } from './bundle-permissions.ts'
12
12
  import { packageInRoot, readRecord, rootOf } from './extension-roots.ts'
13
13
  import type { PackageManifestImportmap } from './manifests.ts'
@@ -616,7 +616,10 @@ export function discoverBundles(siteDir: string): DiscoveredBundles {
616
616
  /** Whether a package directory is a link to one elsewhere on this machine, as a workspace's is. */
617
617
  function isLinked(directory: string): boolean {
618
618
  try {
619
- return lstatSync(directory).isSymbolicLink() || realpathSync(directory) !== directory
619
+ if (lstatSync(directory).isSymbolicLink()) return true
620
+ // The package's own folder being elsewhere, as a junction is. A link further up
621
+ // the path, such as macOS's /var to /private/var, moves the site, not the package.
622
+ return realpathSync(directory) !== join(realpathSync(dirname(directory)), basename(directory))
620
623
  } catch {
621
624
  return false
622
625
  }