@crombolo/backoffice-host 0.51.0 → 0.52.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.
@@ -0,0 +1,308 @@
1
+ /**
2
+ * The tree beside forms and workflows (story 289): their folders and their items,
3
+ * with making, renaming and moving, or a word saying why they cannot be changed.
4
+ * Forms' root opens every submission; a folder is a real folder of files.
5
+ */
6
+ import { css, html, nothing, repeat } from '@umbraco-cms/backoffice/external/lit'
7
+ import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'
8
+ import { activeItem } from './content-strip-items.js'
9
+ import { definitionTree, readOnlyWords } from './definition-tree-model.js'
10
+ import { formsTree, moveForm, sendFormFolder } from './forms-client.js'
11
+ import { listWorkflows, moveWorkflow, sendWorkflowFolder } from './workflow-client.js'
12
+
13
+ const here = () => {
14
+ const base = new URL(document.baseURI).pathname
15
+ return location.pathname.startsWith(base) ? location.pathname.slice(base.length) : ''
16
+ }
17
+
18
+ const KINDS = {
19
+ Forms: {
20
+ label: 'Forms',
21
+ root: 'section/forms/view/entries',
22
+ rootLabel: 'Forms',
23
+ open: (item) => `section/forms/view/forms?form=${encodeURIComponent(item.key)}`,
24
+ create: (folder) => `section/forms/view/forms?new=${encodeURIComponent(folder)}`,
25
+ load: formsTree,
26
+ folder: sendFormFolder,
27
+ move: moveForm,
28
+ },
29
+ Workflows: {
30
+ label: 'Workflows',
31
+ root: 'section/forms/view/workflows',
32
+ rootLabel: 'Workflows',
33
+ open: (item) => `section/forms/view/workflows?workflow=${encodeURIComponent(item.alias)}`,
34
+ create: (folder) => `section/forms/view/workflows?new=${encodeURIComponent(folder)}`,
35
+ load: async () => {
36
+ const list = await listWorkflows()
37
+ return {
38
+ items: list.items ?? [],
39
+ folders: list.folders ?? [],
40
+ readOnlyBecause: list.readOnlyBecause,
41
+ }
42
+ },
43
+ folder: sendWorkflowFolder,
44
+ move: moveWorkflow,
45
+ },
46
+ }
47
+
48
+ const url = (path) =>
49
+ new URL(path, document.baseURI).pathname + new URL(path, document.baseURI).search
50
+
51
+ export default class CromboloDefinitionTreeElement extends UmbLitElement {
52
+ static properties = {
53
+ _kind: { state: true },
54
+ _tree: { state: true },
55
+ _because: { state: true },
56
+ _asking: { state: true },
57
+ _problem: { state: true },
58
+ }
59
+
60
+ #follow = () => {
61
+ const kind = activeItem(here()) === 'Workflows' ? 'Workflows' : 'Forms'
62
+ if (kind === this._kind) return
63
+ this._kind = kind
64
+ this.#load()
65
+ }
66
+
67
+ #reload = () => this.#load()
68
+
69
+ connectedCallback() {
70
+ super.connectedCallback()
71
+ this.#follow()
72
+ window.addEventListener('changestate', this.#follow)
73
+ window.addEventListener('popstate', this.#follow)
74
+ window.addEventListener('crombolo:definitions-changed', this.#reload)
75
+ }
76
+
77
+ disconnectedCallback() {
78
+ super.disconnectedCallback()
79
+ window.removeEventListener('changestate', this.#follow)
80
+ window.removeEventListener('popstate', this.#follow)
81
+ window.removeEventListener('crombolo:definitions-changed', this.#reload)
82
+ }
83
+
84
+ get #config() {
85
+ return KINDS[this._kind]
86
+ }
87
+
88
+ async #load() {
89
+ const { items, folders, readOnlyBecause } = await this.#config.load()
90
+ this._tree = definitionTree(items, folders)
91
+ this._folders = folders
92
+ this._because = readOnlyBecause
93
+ }
94
+
95
+ get #editable() {
96
+ return !this._because
97
+ }
98
+
99
+ /** A folder change, and what stopped it, if anything did. */
100
+ async #send(change) {
101
+ this._problem = undefined
102
+ const result = await change()
103
+ if (result.ok === false) this._problem = result.message ?? 'That could not be done.'
104
+ this._asking = undefined
105
+ await this.#load()
106
+ }
107
+
108
+ #submit(event) {
109
+ event.preventDefault()
110
+ const name = new FormData(event.target).get('name')?.toString().trim()
111
+ if (!name) return
112
+ const { verb, path } = this._asking
113
+ const parent = path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : ''
114
+ if (verb === 'new') {
115
+ const target = path ? `${path}/${name}` : name
116
+ this.#send(() => this.#config.folder('POST', { path: target }))
117
+ } else {
118
+ const to = parent ? `${parent}/${name}` : name
119
+ this.#send(() => this.#config.folder('PUT', { from: path, to }))
120
+ }
121
+ }
122
+
123
+ #asking() {
124
+ if (!this._asking) return nothing
125
+ return html`<form class="ask" @submit=${(event) => this.#submit(event)}>
126
+ <input
127
+ name="name"
128
+ aria-label="Folder name"
129
+ placeholder="Folder name"
130
+ .value=${this._asking.verb === 'rename' ? this._asking.path.split('/').pop() : ''}
131
+ autofocus
132
+ />
133
+ <button type="submit">${this._asking.verb === 'rename' ? 'Rename' : 'Add'}</button>
134
+ <button type="button" @click=${() => {
135
+ this._asking = undefined
136
+ }}>Cancel</button>
137
+ </form>`
138
+ }
139
+
140
+ #folderActions(path) {
141
+ if (!this.#editable) return nothing
142
+ return html`<span class="actions">
143
+ <a href=${url(this.#config.create(path))} title="New in this folder" aria-label="New in this folder">+</a>
144
+ <button
145
+ type="button"
146
+ title="New folder"
147
+ aria-label="New folder"
148
+ @click=${() => {
149
+ this._asking = { verb: 'new', path }
150
+ }}
151
+ >▸</button>
152
+ ${
153
+ path
154
+ ? html`<button
155
+ type="button"
156
+ title="Rename"
157
+ aria-label="Rename"
158
+ @click=${() => {
159
+ this._asking = { verb: 'rename', path }
160
+ }}
161
+ >✎</button>
162
+ <button
163
+ type="button"
164
+ title="Delete"
165
+ aria-label="Delete"
166
+ @click=${() => this.#send(() => this.#config.folder('DELETE', { path }))}
167
+ >×</button>`
168
+ : nothing
169
+ }
170
+ </span>`
171
+ }
172
+
173
+ #move(item) {
174
+ if (!this.#editable || (this._folders ?? []).length === 0) return nothing
175
+ return html`<select
176
+ aria-label="Move to a folder"
177
+ title="Move to a folder"
178
+ @change=${(event) => this.#send(() => this.#config.move(item.alias, event.target.value))}
179
+ >
180
+ <option value="" ?selected=${!item.folder}>Top</option>
181
+ ${this._folders.map(
182
+ (folder) =>
183
+ html`<option value=${folder} ?selected=${folder === item.folder}>${folder}</option>`,
184
+ )}
185
+ </select>`
186
+ }
187
+
188
+ #items(node) {
189
+ return html`<ul>
190
+ ${repeat(
191
+ node.folders,
192
+ (folder) => folder.path,
193
+ (folder) => html`<li>
194
+ <details open>
195
+ <summary><span>${folder.name}</span>${this.#folderActions(folder.path)}</summary>
196
+ ${this.#items(folder)}
197
+ </details>
198
+ </li>`,
199
+ )}
200
+ ${repeat(
201
+ node.items,
202
+ (item) => item.alias,
203
+ (item) => html`<li class="item">
204
+ <a href=${url(this.#config.open(item))}>${item.name}</a>${this.#move(item)}
205
+ </li>`,
206
+ )}
207
+ </ul>`
208
+ }
209
+
210
+ render() {
211
+ if (!this._tree) return html`<uui-loader></uui-loader>`
212
+ const words = readOnlyWords(this.#config.label, this._because)
213
+ return html`<div class="root">
214
+ <a href=${url(this.#config.root)}>${this.#config.rootLabel}</a>
215
+ ${this.#folderActions('')}
216
+ </div>
217
+ ${this.#asking()}
218
+ ${this._problem ? html`<p class="problem" role="alert">${this._problem}</p>` : nothing}
219
+ ${words ? html`<p class="note" role="status">${words}</p>` : nothing}
220
+ ${this.#items(this._tree)}`
221
+ }
222
+
223
+ static styles = css`
224
+ :host {
225
+ display: block;
226
+ padding: var(--uui-size-space-3) var(--uui-size-space-4);
227
+ }
228
+ a {
229
+ color: var(--uui-color-text);
230
+ text-decoration: none;
231
+ }
232
+ a:hover {
233
+ text-decoration: underline;
234
+ }
235
+ .root {
236
+ display: flex;
237
+ justify-content: space-between;
238
+ align-items: center;
239
+ font-weight: 700;
240
+ padding-bottom: var(--uui-size-space-2);
241
+ }
242
+ ul {
243
+ list-style: none;
244
+ margin: 0;
245
+ padding: 0 0 0 var(--uui-size-space-4);
246
+ }
247
+ :host > ul {
248
+ padding-left: 0;
249
+ }
250
+ li {
251
+ padding: var(--uui-size-space-1) 0;
252
+ }
253
+ .item {
254
+ display: flex;
255
+ justify-content: space-between;
256
+ gap: var(--uui-size-space-2);
257
+ }
258
+ summary {
259
+ display: flex;
260
+ justify-content: space-between;
261
+ cursor: pointer;
262
+ list-style-position: inside;
263
+ }
264
+ .actions {
265
+ display: inline-flex;
266
+ gap: var(--uui-size-space-1);
267
+ }
268
+ .actions button,
269
+ .actions a {
270
+ background: none;
271
+ border: 0;
272
+ padding: 0 var(--uui-size-space-1);
273
+ cursor: pointer;
274
+ color: var(--uui-color-text-alt);
275
+ font: inherit;
276
+ }
277
+ select {
278
+ max-width: 5rem;
279
+ font: inherit;
280
+ font-size: var(--uui-type-small-size);
281
+ color: var(--uui-color-text-alt);
282
+ background: none;
283
+ border: 0;
284
+ }
285
+ .ask {
286
+ display: flex;
287
+ flex-wrap: wrap;
288
+ gap: var(--uui-size-space-2);
289
+ padding-bottom: var(--uui-size-space-2);
290
+ }
291
+ .ask input {
292
+ flex: 1 1 100%;
293
+ font: inherit;
294
+ }
295
+ .note,
296
+ .problem {
297
+ margin: 0 0 var(--uui-size-space-3);
298
+ font-size: var(--uui-type-small-size);
299
+ line-height: 1.5;
300
+ color: var(--uui-color-text-alt);
301
+ }
302
+ .problem {
303
+ color: var(--uui-color-danger);
304
+ }
305
+ `
306
+ }
307
+
308
+ customElements.define('crombolo-definition-tree', CromboloDefinitionTreeElement)
@@ -0,0 +1,19 @@
1
+ /**
2
+ * From a page to what defines it (story 289): its content type or its template,
3
+ * as a tree action, for whoever may open Settings, where those live.
4
+ */
5
+ import { UmbDocumentDetailRepository } from '@umbraco-cms/backoffice/document'
6
+ import { UmbEntityActionBase } from '@umbraco-cms/backoffice/entity-action'
7
+
8
+ class CromboloDocumentLinkAction extends UmbEntityActionBase {
9
+ async execute() {
10
+ const target = this.args.meta.target
11
+ const { data } = await new UmbDocumentDetailRepository(this).requestByUnique(this.args.unique)
12
+ const unique = target === 'template' ? data?.template?.unique : data?.documentType?.unique
13
+ if (!unique) return
14
+ const path = `section/settings/workspace/${target}/edit/${unique}`
15
+ history.pushState(null, '', new URL(path, document.baseURI).pathname)
16
+ }
17
+ }
18
+
19
+ export { CromboloDocumentLinkAction as api }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * What the form editor's text boxes suggest (story 294). Pure, so each is tested
3
+ * on its own.
4
+ */
5
+ const ROWS = 8
6
+
7
+ /**
8
+ * The dictionary items to offer for what has been typed: names starting with it
9
+ * first, then those containing it, then those whose words do. Only after a `#`.
10
+ *
11
+ * @param {{ name: string, text: string }[]} words
12
+ * @param {string} value
13
+ */
14
+ export function suggestions(words, value) {
15
+ if (!/^#\S*$/.test(value)) return { rows: [], more: false }
16
+ const typed = value.slice(1).toLowerCase()
17
+ const rank = ({ name, text }) => {
18
+ const lower = name.toLowerCase()
19
+ if (lower.startsWith(typed)) return 0
20
+ if (lower.includes(typed)) return 1
21
+ return text.toLowerCase().includes(typed) ? 2 : 3
22
+ }
23
+ const matching = words
24
+ .map((word) => ({ word, rank: rank(word) }))
25
+ .filter((found) => found.rank < 3)
26
+ .sort((a, b) => a.rank - b.rank || a.word.name.localeCompare(b.word.name))
27
+ .map((found) => found.word)
28
+ return { rows: matching.slice(0, ROWS), more: matching.length > ROWS }
29
+ }
30
+
31
+ /**
32
+ * What goes under a box: what a `#name` reads as, or that nothing has that name.
33
+ * Nothing for ordinary text, or when `#` starts a few words, or before the list has loaded.
34
+ *
35
+ * @param {{ name: string, text: string }[] | undefined} words
36
+ * @param {string} value
37
+ */
38
+ export function wordLine(words, value) {
39
+ if (!words || !/^#\S+$/.test(value)) return undefined
40
+ const name = value.slice(1).toLowerCase()
41
+ const word = words.find((candidate) => candidate.name.toLowerCase() === name)
42
+ return word
43
+ ? `From the dictionary: ${word.text || word.name}`
44
+ : 'No dictionary item has this name, so it shows as written.'
45
+ }
@@ -0,0 +1,170 @@
1
+ /**
2
+ * A form text that can name a dictionary item (story 294): typing `#` offers the
3
+ * items, and what one reads as shows underneath. It sends `change`, with `value`,
4
+ * as `uui-input` does. The list is a popover so a table cell doesn't clip it.
5
+ */
6
+ import { css, html, LitElement, nothing } from '@umbraco-cms/backoffice/external/lit'
7
+ import { suggestions, wordLine } from './form-word-input-model.js'
8
+
9
+ export default class CromboloFormWordInputElement extends LitElement {
10
+ static properties = {
11
+ label: { type: String },
12
+ placeholder: { type: String },
13
+ value: { type: String },
14
+ readonly: { type: Boolean },
15
+ words: { attribute: false },
16
+ _open: { state: true },
17
+ _active: { state: true },
18
+ }
19
+
20
+ constructor() {
21
+ super()
22
+ this.value = ''
23
+ this.words = []
24
+ this._open = false
25
+ this._active = 0
26
+ }
27
+
28
+ get #found() {
29
+ return this.readonly ? { rows: [], more: false } : suggestions(this.words ?? [], this.value)
30
+ }
31
+
32
+ #choose(word) {
33
+ this.value = `#${word.name}`
34
+ this._open = false
35
+ this.#changed()
36
+ }
37
+
38
+ #changed() {
39
+ this.dispatchEvent(new Event('change', { bubbles: true }))
40
+ }
41
+
42
+ #onInput(event) {
43
+ event.stopPropagation()
44
+ this.value = event.target.value ?? ''
45
+ this._active = 0
46
+ this._open = this.#found.rows.length > 0
47
+ }
48
+
49
+ #onKeydown(event) {
50
+ const { rows } = this.#found
51
+ if (!this._open || rows.length === 0) return
52
+ if (event.key === 'Escape') {
53
+ event.stopPropagation()
54
+ this._open = false
55
+ } else if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
56
+ event.preventDefault()
57
+ const step = event.key === 'ArrowDown' ? 1 : rows.length - 1
58
+ this._active = (this._active + step) % rows.length
59
+ } else if (event.key === 'Enter') {
60
+ event.preventDefault()
61
+ this.#choose(rows[this._active] ?? rows[0])
62
+ }
63
+ }
64
+
65
+ updated() {
66
+ const list = this.renderRoot.querySelector('[popover]')
67
+ if (!list) return
68
+ const box = this.renderRoot.querySelector('uui-input').getBoundingClientRect()
69
+ list.style.top = `${box.bottom + 2}px`
70
+ list.style.left = `${box.left}px`
71
+ list.style.minWidth = `${box.width}px`
72
+ list.showPopover()
73
+ }
74
+
75
+ render() {
76
+ const { rows, more } = this.#found
77
+ const line = wordLine(this.words, this.value)
78
+ const open = this._open && rows.length > 0
79
+ return html`<uui-input
80
+ label=${this.label}
81
+ placeholder=${this.placeholder ?? ''}
82
+ .value=${this.value ?? ''}
83
+ ?readonly=${this.readonly}
84
+ @input=${this.#onInput}
85
+ @keydown=${this.#onKeydown}
86
+ @change=${(event) => {
87
+ event.stopPropagation()
88
+ this.#changed()
89
+ }}
90
+ @blur=${() => {
91
+ this._open = false
92
+ }}
93
+ ></uui-input>
94
+ ${line ? html`<p class="word">${line}</p>` : nothing}
95
+ <span class="status" role="status"
96
+ >${open ? `${rows.length} dictionary items. Use the arrow keys to choose one.` : ''}</span
97
+ >
98
+ ${
99
+ open
100
+ ? html`<div popover="manual" role="listbox" aria-label="Dictionary items">
101
+ ${rows.map(
102
+ (word, index) => html`<div
103
+ role="option"
104
+ aria-selected=${index === this._active ? 'true' : 'false'}
105
+ @pointerdown=${(event) => {
106
+ event.preventDefault()
107
+ this.#choose(word)
108
+ }}
109
+ >
110
+ <strong>${word.name}</strong> <span>${word.text}</span>
111
+ </div>`,
112
+ )}
113
+ ${more ? html`<p class="more">Keep typing to narrow this down.</p>` : nothing}
114
+ </div>`
115
+ : nothing
116
+ }`
117
+ }
118
+
119
+ static styles = css`
120
+ :host {
121
+ display: block;
122
+ }
123
+ uui-input {
124
+ width: 100%;
125
+ }
126
+ .word {
127
+ margin: var(--uui-size-space-1) 0 0;
128
+ color: var(--uui-color-text-alt);
129
+ font-size: var(--uui-type-small-size);
130
+ }
131
+ .status {
132
+ position: absolute;
133
+ width: 1px;
134
+ height: 1px;
135
+ overflow: hidden;
136
+ clip-path: inset(50%);
137
+ }
138
+ [popover] {
139
+ position: fixed;
140
+ inset: auto;
141
+ margin: 0;
142
+ padding: var(--uui-size-space-1);
143
+ max-width: 28rem;
144
+ background: var(--uui-color-surface);
145
+ color: var(--uui-color-text);
146
+ border: 1px solid var(--uui-color-border);
147
+ border-radius: var(--uui-border-radius);
148
+ box-shadow: var(--uui-shadow-depth-3);
149
+ }
150
+ [role='option'] {
151
+ padding: var(--uui-size-space-2) var(--uui-size-space-3);
152
+ border-radius: var(--uui-border-radius);
153
+ cursor: pointer;
154
+ }
155
+ [role='option'] span {
156
+ color: var(--uui-color-text-alt);
157
+ }
158
+ [role='option'][aria-selected='true'],
159
+ [role='option']:hover {
160
+ background: var(--uui-color-surface-emphasis);
161
+ }
162
+ .more {
163
+ margin: var(--uui-size-space-1) var(--uui-size-space-3);
164
+ color: var(--uui-color-text-alt);
165
+ font-size: var(--uui-type-small-size);
166
+ }
167
+ `
168
+ }
169
+
170
+ customElements.define('crombolo-form-word-input', CromboloFormWordInputElement)
@@ -27,6 +27,19 @@ const call = async (path, init) => {
27
27
 
28
28
  export const listForms = async () => (await call('')).body.items ?? []
29
29
 
30
+ /** The forms with their folders, and why they cannot be changed here, if they cannot. */
31
+ export const formsTree = async () => (await call('')).body
32
+
33
+ const treeCall = async (path, method, body) => {
34
+ const result = await call(path, { method, body: JSON.stringify(body) })
35
+ return { ok: result.ok, message: result.body.message }
36
+ }
37
+
38
+ /** A folder made (`POST`), renamed (`PUT`) or deleted (`DELETE`). */
39
+ export const sendFormFolder = (method, body) => treeCall('/tree-folders', method, body)
40
+
41
+ export const moveForm = (alias, folder) => treeCall('/tree-move', 'POST', { alias, folder })
42
+
30
43
  /** The content types made from forms (story 239), by key, with each form's name. */
31
44
  export const listLibraryTypes = async () => (await call('/library-types')).body.items ?? []
32
45
 
@@ -41,13 +54,19 @@ export const listEmailTemplates = async () => (await call('/email-templates')).b
41
54
 
42
55
  export const readForm = async (key) => (await call(`/${encodeURIComponent(key)}`)).body
43
56
 
44
- export const saveForm = (definition) =>
45
- call('', { method: 'PUT', body: JSON.stringify(definition) })
57
+ export const saveForm = (definition, folder) =>
58
+ call(folder ? `?folder=${encodeURIComponent(folder)}` : '', {
59
+ method: 'PUT',
60
+ body: JSON.stringify(definition),
61
+ })
46
62
 
47
63
  /** The options a choice source offers right now, as a visitor would see them. */
48
64
  export const previewChoices = (source) =>
49
65
  call('/choices', { method: 'POST', body: JSON.stringify({ source }) })
50
66
 
67
+ /** Every dictionary item, with its words in the default language. */
68
+ export const listWords = async () => (await call('/words')).body.words ?? []
69
+
51
70
  export const deleteForm = (key) => call(`/${encodeURIComponent(key)}`, { method: 'DELETE' })
52
71
 
53
72
  export const listEntries = async (key, query = {}) => {