terrier-engine 4.0.20 → 4.1.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.
package/parts.ts DELETED
@@ -1,461 +0,0 @@
1
- import { Logger } from "tuff-core/logging"
2
- import { Part } from "tuff-core/parts"
3
- import {PartParent, PartTag, NoState} from "tuff-core/parts"
4
- import Fragments from "./fragments"
5
- import {Dropdown} from "./dropdowns"
6
- import {TerrierApp} from "./app"
7
- import Loading from "./loading"
8
- import Theme, {Action, RenderActionOptions, ThemeType} from "./theme"
9
- import Toasts, {ToastOptions} from "./toasts";
10
-
11
- const log = new Logger('Parts')
12
-
13
- export type PanelActions<TT extends ThemeType> = {
14
- primary: Array<Action<TT>>
15
- secondary: Array<Action<TT>>
16
- tertiary: Array<Action<TT>>
17
- }
18
-
19
- export type ActionLevel = keyof PanelActions<any>
20
-
21
-
22
- ////////////////////////////////////////////////////////////////////////////////
23
- // Terrier Part
24
- ////////////////////////////////////////////////////////////////////////////////
25
-
26
- /**
27
- * Base class for ALL parts in a Terrier application.
28
- */
29
- export abstract class TerrierPart<
30
- TState,
31
- TThemeType extends ThemeType,
32
- TApp extends TerrierApp<TThemeType, TApp, TTheme>,
33
- TTheme extends Theme<TThemeType>
34
- > extends Part<TState> {
35
-
36
- get app(): TApp {
37
- return this.root as TApp // this should always be true
38
- }
39
-
40
- get theme(): TTheme {
41
- return this.app.theme
42
- }
43
-
44
- /// Loading
45
-
46
- /**
47
- * This can be overloaded if the loading overlay should go
48
- * somewhere other than the part's root element.
49
- */
50
- getLoadingContainer(): Element | null | undefined {
51
- return this.element
52
- }
53
-
54
-
55
- /**
56
- * Shows the loading animation on top of the part.
57
- */
58
- startLoading() {
59
- const elem = this.getLoadingContainer()
60
- if (!elem) {
61
- return
62
- }
63
- Loading.showOverlay(elem, this.theme)
64
- }
65
-
66
- /**
67
- * Removes the loading animation from the part.
68
- */
69
- stopLoading() {
70
- const elem = this.getLoadingContainer()
71
- if (!elem) {
72
- return
73
- }
74
- Loading.removeOverlay(elem)
75
- }
76
-
77
- /**
78
- * Shows the loading overlay until the given function completes (either returns successfully or throws an exception)
79
- * @param func
80
- */
81
- showLoading(func: () => void): void
82
- showLoading(func: () => Promise<void>): Promise<void>
83
- showLoading(func: () => void | Promise<void>): void | Promise<void> {
84
- this.startLoading()
85
- let stopImmediately = true
86
- try {
87
- const res = func()
88
- if (res) {
89
- stopImmediately = false
90
- return res.finally(() => {
91
- this.stopLoading()
92
- })
93
- }
94
- } finally {
95
- if (stopImmediately) {
96
- this.stopLoading()
97
- }
98
- }
99
- }
100
-
101
-
102
- /// Toasts
103
-
104
- /**
105
- * Shows a toast message in a bubble in the upper right corner.
106
- * @param message the message text
107
- * @param options
108
- */
109
- showToast(message: string, options: ToastOptions<TThemeType>) {
110
- Toasts.show(message, options, this.theme)
111
- }
112
-
113
- }
114
-
115
-
116
- ////////////////////////////////////////////////////////////////////////////////
117
- // Content Part
118
- ////////////////////////////////////////////////////////////////////////////////
119
-
120
- /**
121
- * Base class for all Parts that render some main content, like pages, panels, and modals.
122
- */
123
- export abstract class ContentPart<
124
- TState,
125
- TThemeType extends ThemeType,
126
- TApp extends TerrierApp<TThemeType, TApp, TTheme>,
127
- TTheme extends Theme<TThemeType>
128
- > extends TerrierPart<TState, TThemeType, TApp, TTheme> {
129
-
130
- /**
131
- * All ContentParts must implement this to render their actual content.
132
- * @param parent
133
- */
134
- abstract renderContent(parent: PartTag): void
135
-
136
-
137
- protected _title = ''
138
-
139
- /**
140
- * Sets the page, panel, or modal title.
141
- * @param title
142
- */
143
- setTitle(title: string) {
144
- this._title = title
145
- }
146
-
147
- protected _icon: TThemeType['icons'] | null = null
148
-
149
- setIcon(icon: TThemeType['icons']) {
150
- this._icon = icon
151
- }
152
-
153
- protected _breadcrumbClasses: string[] = []
154
-
155
- addBreadcrumbClass(c: string) {
156
- this._breadcrumbClasses.push(c)
157
- }
158
-
159
-
160
- /// Actions
161
-
162
- // stored actions can be either an action object or a reference to a named action
163
- actions = {
164
- primary: Array<Action<TThemeType> | string>(),
165
- secondary: Array<Action<TThemeType> | string>(),
166
- tertiary: Array<Action<TThemeType> | string>()
167
- }
168
-
169
- namedActions: Record<string, { action: Action<TThemeType>, level: ActionLevel }> = {}
170
-
171
- /**
172
- * Add an action to the part, or replace a named action if it already exists.
173
- * @param action the action to add
174
- * @param level whether it's a primary, secondary, or tertiary action
175
- * @param name a name to be given to this action, so it can be accessed later
176
- */
177
- addAction(action: Action<TThemeType>, level: ActionLevel = 'primary', name?: string) {
178
- if (name?.length) {
179
- if (name in this.namedActions) {
180
- const currentLevel = this.namedActions[name].level
181
- if (level != currentLevel) {
182
- const index = this.actions[currentLevel].indexOf(name)
183
- this.actions[currentLevel].splice(index, 1)
184
- this.actions[level].push(name)
185
- }
186
- this.namedActions[name].action = action
187
- } else {
188
- this.namedActions[name] = { action, level }
189
- this.actions[level].push(name)
190
- }
191
- } else {
192
- this.actions[level].push(action)
193
- }
194
- }
195
-
196
- /**
197
- * Returns the action definition for the action with the given name, or undefined if there is no action with that name
198
- * @param name
199
- */
200
- getNamedAction(name: string): Action<TThemeType> | undefined {
201
- return this.namedActions[name].action
202
- }
203
-
204
- /**
205
- * Removes the action with the given name
206
- * @param name
207
- */
208
- removeNamedAction(name: string) {
209
- if (!(name in this.namedActions)) return
210
- const level = this.actions[this.namedActions[name].level]
211
- delete this.namedActions[name]
212
- const actionIndex = level.indexOf(name)
213
- if (actionIndex >= 0) {
214
- level.splice(actionIndex, 1)
215
- }
216
- }
217
-
218
- /**
219
- * Clears the actions for this part
220
- * @param level whether to clear the primary, secondary, or both sets of actions
221
- */
222
- clearActions(level: ActionLevel) {
223
- for (const action of this.actions[level]) {
224
- if (typeof action === 'string') {
225
- delete this.namedActions[action]
226
- }
227
- }
228
- this.actions[level] = []
229
- }
230
-
231
- getAllActions(): PanelActions<TThemeType> {
232
- return {
233
- primary: this.getActions('primary'),
234
- secondary: this.getActions('secondary'),
235
- tertiary: this.getActions('tertiary'),
236
- }
237
- }
238
-
239
- getActions(level: ActionLevel): Action<TThemeType>[] {
240
- return this.actions[level].map(action => {
241
- return (typeof action === 'string') ? this.namedActions[action].action : action
242
- })
243
- }
244
-
245
-
246
- /// Dropdowns
247
-
248
- /**
249
- * Shows the given dropdown part on the page.
250
- * It's generally better to call `toggleDropdown` instead so that the dropdown will be
251
- * hidden upon a subsequent click on the target.
252
- * @param constructor a constructor for a dropdown part
253
- * @param state the dropdown's state
254
- * @param target the target element around which to show the dropdown
255
- */
256
- makeDropdown<DropdownType extends Dropdown<DropdownStateType, TThemeType, TApp, TTheme>, DropdownStateType>(
257
- constructor: {new(p: PartParent, id: string, state: DropdownStateType): DropdownType;},
258
- state: DropdownStateType,
259
- target: EventTarget | null) {
260
- if (!(target && target instanceof HTMLElement)) {
261
- throw "Trying to show a dropdown without an element target!"
262
- }
263
- const dropdown = this.app.makeOverlay(constructor, state, 'dropdown')
264
- dropdown.parentPart = this
265
- dropdown.anchor(target)
266
- this.app.lastDropdownTarget = target
267
- }
268
-
269
- clearDropdown() {
270
- this.app.clearOverlay('dropdown')
271
- }
272
-
273
- /**
274
- * Calls `makeDropdown` only if there's not a dropdown currently originating from the target.
275
- * @param constructor a constructor for a dropdown part
276
- * @param state the dropdown's state
277
- * @param target the target element around which to show the dropdown
278
- */
279
- toggleDropdown<DropdownType extends Dropdown<DropdownStateType, TThemeType, TApp, TTheme>, DropdownStateType>(
280
- constructor: { new(p: PartParent, id: string, state: DropdownStateType): DropdownType; },
281
- state: DropdownStateType,
282
- target: EventTarget | null) {
283
- if (target && target instanceof HTMLElement && target == this.app.lastDropdownTarget) {
284
- this.clearDropdown()
285
- } else {
286
- this.makeDropdown(constructor, state, target)
287
- }
288
- }
289
-
290
- }
291
-
292
-
293
- ////////////////////////////////////////////////////////////////////////////////
294
- // Page
295
- ////////////////////////////////////////////////////////////////////////////////
296
-
297
- /**
298
- * Whether some content should be constrained to a reasonable width or span the entire screen.
299
- */
300
- export type ContentWidth = "normal" | "wide"
301
-
302
-
303
- /**
304
- * A part that renders content to a full page.
305
- */
306
- export abstract class PagePart<
307
- TState,
308
- TThemeType extends ThemeType,
309
- TApp extends TerrierApp<TThemeType, TApp, TTheme>,
310
- TTheme extends Theme<TThemeType>
311
- > extends ContentPart<TState, TThemeType, TApp, TTheme> {
312
-
313
- /// Breadcrumbs
314
-
315
- private _breadcrumbs = Array<Action<TThemeType>>()
316
-
317
- addBreadcrumb(crumb: Action<TThemeType>) {
318
- this._breadcrumbs.push(crumb)
319
- }
320
-
321
-
322
- /**
323
- * Sets both the page title and the last breadcrumb.
324
- * @param title
325
- */
326
- setTitle(title: string) {
327
- super.setTitle(title)
328
- document.title = `${title} :: Terrier Hub`
329
- }
330
-
331
- private _titleHref?: string
332
-
333
- /**
334
- * Adds an href to the title (last) breadcrumb.
335
- * @param href
336
- */
337
- setTitleHref(href: string) {
338
- this._titleHref = href
339
- }
340
-
341
- /**
342
- * Whether the main content should be constrained to a reasonable width (default) or span the entire screen.
343
- */
344
- protected mainContentWidth: ContentWidth = "normal"
345
-
346
- render(parent: PartTag) {
347
- parent.div(`.tt-page-part.content-width-${this.mainContentWidth}`, page => {
348
- page.div('.tt-flex.top-row', topRow => {
349
- this.renderBreadcrumbs(topRow);
350
-
351
- if (this.actions.tertiary.length) {
352
- this.renderActions(topRow, 'tertiary');
353
- }
354
- })
355
-
356
- page.div('.lighting')
357
- page.div('.page-main', main => {
358
- this.renderContent(main)
359
- main.div('.page-actions', actions => {
360
- this.renderActions(actions, 'secondary', {iconColor: null, defaultClass: 'secondary'})
361
- this.renderActions(actions, 'primary', {iconColor: null, defaultClass: 'primary'})
362
- })
363
- })
364
- })
365
- }
366
-
367
- protected renderActions(parent: PartTag, level: ActionLevel, options?: RenderActionOptions<TThemeType>) {
368
- parent.div(`.${level}-actions`, actions => {
369
- this.app.theme.renderActions(actions, this.getActions(level), options)
370
- })
371
- }
372
-
373
- protected renderBreadcrumbs(parent: PartTag) {
374
- if (!this._breadcrumbs.length && !this._title?.length) return
375
-
376
- parent.h1('.breadcrumbs', h1 => {
377
- const crumbs = Array.from(this._breadcrumbs)
378
-
379
- // add a breadcrumb for the page title
380
- const titleCrumb: Action<TThemeType> = {
381
- title: this._title,
382
- icon: this._icon || undefined,
383
- }
384
- if (this._titleHref) {
385
- titleCrumb.href = this._titleHref
386
- }
387
- if (this._breadcrumbClasses?.length) {
388
- titleCrumb.classes = this._breadcrumbClasses
389
- }
390
- crumbs.push(titleCrumb)
391
-
392
- this.app.theme.renderActions(h1, crumbs)
393
- })
394
- }
395
- }
396
-
397
-
398
-
399
- /**
400
- * Default page part if the router can't find the path.
401
- */
402
- export class NotFoundRoute<
403
- TT extends ThemeType,
404
- TApp extends TerrierApp<TT, TApp, TTheme>,
405
- TTheme extends Theme<TT>
406
- > extends PagePart<NoState, TT, TApp, TTheme> {
407
- async init() {
408
- this.setTitle("Page Not Found")
409
- }
410
-
411
- renderContent(parent: PartTag) {
412
- log.warn(`Not found: ${this.context.href}`)
413
- parent.h1({text: "Not Found"})
414
- }
415
-
416
- }
417
-
418
-
419
- ////////////////////////////////////////////////////////////////////////////////
420
- // Panel
421
- ////////////////////////////////////////////////////////////////////////////////
422
-
423
- /**
424
- * A part that renders content inside a panel.
425
- */
426
- export abstract class PanelPart<
427
- TState,
428
- TThemeType extends ThemeType,
429
- TApp extends TerrierApp<TThemeType, TApp, TTheme>,
430
- TTheme extends Theme<TThemeType>
431
- > extends ContentPart<TState, TThemeType, TApp, TTheme> {
432
-
433
- getLoadingContainer() {
434
- return this.element?.getElementsByClassName('tt-panel')[0]
435
- }
436
-
437
- protected get panelClasses(): string[] {
438
- return []
439
- }
440
-
441
- render(parent: PartTag) {
442
- parent.div('.tt-panel', panel => {
443
- panel.class(...this.panelClasses)
444
- if (this._title?.length || this.actions.tertiary.length) {
445
- panel.div('.panel-header', header => {
446
- header.h2(h2 => {
447
- if (this._icon) {
448
- this.app.theme.renderIcon(h2, this._icon, 'link')
449
- }
450
- h2.div('.title', {text: this._title || 'Call setTitle()'})
451
- })
452
- this.theme.renderActions(header, this.getActions('tertiary'))
453
- })
454
- }
455
- panel.div('.panel-content', content => {
456
- this.renderContent(content)
457
- })
458
- Fragments.panelActions(panel, this.getAllActions(), this.theme)
459
- })
460
- }
461
- }