nanosplash 2.5.2 → 3.0.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.
Files changed (80) hide show
  1. package/.github/workflows/ci.yml +48 -0
  2. package/.prettierrc +10 -9
  3. package/README.md +22 -89
  4. package/dist/main.css +1 -0
  5. package/dist/ns.cjs.js +1 -0
  6. package/dist/ns.es.js +330 -0
  7. package/dist/ns.iife.js +2 -0
  8. package/index.html +41 -122
  9. package/package.json +27 -34
  10. package/src/main.ts +8 -5
  11. package/src/sass/ns.sass +86 -0
  12. package/src/ts/core/Nanosplash/Nanosplash.ts +97 -0
  13. package/src/ts/core/Nanosplash/interfaces/NanosplashAPI.ts +50 -0
  14. package/src/ts/core/Nanosplash/interfaces/NanosplashInterface.ts +42 -0
  15. package/src/ts/core/Nanosplash/repositories/NanosplashRepository.ts +178 -0
  16. package/src/ts/core/Nanosplash/services/NanosplashService.ts +222 -0
  17. package/src/ts/types/Alias.d.ts +1 -0
  18. package/src/ts/types/Nanosplash.d.ts +18 -0
  19. package/src/ts/types/NanosplashService.d.ts +1 -0
  20. package/src/ts/util/Guid.ts +13 -0
  21. package/src/ts/util/Stack.ts +73 -0
  22. package/src/vite-env.d.ts +8 -0
  23. package/tests/Guid.spec.ts +21 -0
  24. package/tests/Nanosplash.spec.ts +64 -0
  25. package/tests/NanosplashRepository.spec.ts +91 -0
  26. package/tests/NanosplashService.spec.ts +119 -0
  27. package/tests/Stack.spec.ts +56 -0
  28. package/tsconfig.json +9 -5
  29. package/vite.config.ts +22 -14
  30. package/.github/workflows/production.yml +0 -60
  31. package/.github/workflows/release.yml +0 -61
  32. package/LICENSE +0 -21
  33. package/_config.yml +0 -1
  34. package/dist/iife/manifest.json +0 -7
  35. package/dist/iife/nanosplash.iife.js +0 -1
  36. package/dist/module/manifest.json +0 -7
  37. package/dist/module/nanosplash.cjs.js +0 -1
  38. package/dist/module/nanosplash.es.js +0 -371
  39. package/dist/module/style.css +0 -1
  40. package/docs/index.html +0 -131
  41. package/docs/nanosplash.iife.js +0 -6
  42. package/docs/style.css +0 -1
  43. package/docs/typedoc/.nojekyll +0 -1
  44. package/docs/typedoc/assets/highlight.css +0 -22
  45. package/docs/typedoc/assets/icons.css +0 -1043
  46. package/docs/typedoc/assets/icons.png +0 -0
  47. package/docs/typedoc/assets/icons@2x.png +0 -0
  48. package/docs/typedoc/assets/main.js +0 -52
  49. package/docs/typedoc/assets/search.js +0 -1
  50. package/docs/typedoc/assets/style.css +0 -1414
  51. package/docs/typedoc/assets/widgets.png +0 -0
  52. package/docs/typedoc/assets/widgets@2x.png +0 -0
  53. package/docs/typedoc/classes/Core_Nanosplash.default.html +0 -133
  54. package/docs/typedoc/index.html +0 -1
  55. package/docs/typedoc/modules/Core_Nanosplash.html +0 -1
  56. package/docs/typedoc/modules/types.html +0 -1
  57. package/favicon.svg +0 -15
  58. package/postcss.config.js +0 -8
  59. package/src/Core/Nanosplash.ts +0 -257
  60. package/src/Core/SplashInstance.ts +0 -298
  61. package/src/Exceptions/DestinationException.ts +0 -13
  62. package/src/Exceptions/Exception.ts +0 -9
  63. package/src/Exceptions/IllegalArgumentException.ts +0 -11
  64. package/src/Factory/NanosplashFactory.ts +0 -93
  65. package/src/Interfaces/ContextualAPIInterface.ts +0 -8
  66. package/src/Interfaces/IdentityInterface.ts +0 -3
  67. package/src/Interfaces/InsideInterface.ts +0 -5
  68. package/src/Interfaces/NanosplashInterface.ts +0 -8
  69. package/src/Interfaces/ProgressInterface.ts +0 -5
  70. package/src/Interfaces/ShowInterface.ts +0 -5
  71. package/src/Interfaces/WhileInterface.ts +0 -5
  72. package/src/Repositories/NanosplashRepository.ts +0 -82
  73. package/src/Tests/dom.spec.ts +0 -72
  74. package/src/Utilities/dom.ts +0 -49
  75. package/src/style.sass +0 -88
  76. package/src/types.d.ts +0 -59
  77. package/update-all.sh +0 -3
  78. package/vite.iife.config.js +0 -72
  79. package/vite.mod.config.js +0 -71
  80. package/vite.site.config.js +0 -22
@@ -1,257 +0,0 @@
1
- import '../style.sass'
2
- import {get} from "../Utilities/dom";
3
- import {SplashInstance} from "./SplashInstance";
4
- import {ShowInterface} from "../Interfaces/ShowInterface";
5
- import {NanosplashFactory} from "../Factory/NanosplashFactory";
6
- import {NanosplashInterface} from "../Interfaces/NanosplashInterface";
7
- import {
8
- ContextualAPIObject,
9
- Destination,
10
- InstanceIterationCallback,
11
- NanosplashOptions,
12
- SplashJob,
13
- } from "../types";
14
- import Exception from "../Exceptions/Exception";
15
- import IllegalArgumentException from "../Exceptions/IllegalArgumentException";
16
-
17
- /**
18
- * # Nanosplash
19
- * @author Isak K. Hauge <isakhauge@gmail.com>
20
- */
21
- class Nanosplash implements NanosplashInterface {
22
- public static APP_NAME = 'Nanosplash'
23
- private static SPINNER_DEFAULT_VISIBILITY = true
24
- private static DEBUG = true
25
- public debug: boolean
26
- private imgSrc: string | undefined
27
- private spinner: boolean
28
- private fontSize: string
29
- public readonly instances: Map<string, SplashInstance>
30
-
31
- /**
32
- * Options passed to the constructor will persist in the instance.
33
- * @param {NanosplashOptions | undefined} options Nanosplash options object.
34
- */
35
- public constructor(options?: NanosplashOptions) {
36
- this.debug = ((options?.debug === undefined)
37
- ? Nanosplash.DEBUG
38
- : options.spinner) as boolean
39
- this.imgSrc = options?.imgSrc
40
- this.spinner = (options?.spinner === undefined)
41
- ? Nanosplash.SPINNER_DEFAULT_VISIBILITY
42
- : options.spinner
43
- this.fontSize = options?.fontSize ?? '18px'
44
- this.instances = new Map<string, SplashInstance>()
45
- }
46
-
47
- /**
48
- * # Set Image Source
49
- * Set the value as undefined to display no image.
50
- * @param {string | undefined} value
51
- * @return Nanosplash
52
- */
53
- public setImgSrc(value: string | undefined): Nanosplash {
54
- this.imgSrc = value
55
- return this
56
- }
57
-
58
- /**
59
- * # Set Spinner
60
- * Control spinner visibility.
61
- * @param {boolean} value
62
- * @return Nanosplash
63
- */
64
- public showSpinner(value: boolean): Nanosplash {
65
- this.spinner = value
66
- return this
67
- }
68
-
69
- /**
70
- * # Set Font Size
71
- * @param {string} value Use CSS compatible values.
72
- * @return Nanosplash
73
- */
74
- public setFontSize(value: string): Nanosplash {
75
- this.fontSize = value
76
- return this
77
- }
78
-
79
- /**
80
- * # Get Image Source
81
- * @return string | undefined
82
- */
83
- public getImgSrc(): string | undefined {
84
- return this.imgSrc
85
- }
86
-
87
- /**
88
- * # Spinner Is Visible
89
- * @return boolean
90
- */
91
- public spinnerIsVisible(): boolean {
92
- return this.spinner
93
- }
94
-
95
- /**
96
- * # Get Font Size
97
- * @return string
98
- */
99
- public getFontSize(): string {
100
- return this.fontSize
101
- }
102
-
103
- /**
104
- * # Show
105
- * @param {string} text
106
- */
107
- public show(text: string): ContextualAPIObject {
108
- return NanosplashFactory.createShowFunction(this, new SplashInstance(this, text, this.imgSrc))(text)
109
- }
110
-
111
- /**
112
- * # Progress
113
- * @param {SplashJob[]} jobs An array or list of Splash jobs.
114
- */
115
- public progress(...jobs: SplashJob[]): ContextualAPIObject {
116
- return NanosplashFactory.createProgressFunction(this, new SplashInstance(this, '', this.imgSrc))(...jobs)
117
- }
118
-
119
- /**
120
- * # While
121
- * @param {Promise<any>} asyncTask An asynchronous function or Promise.
122
- */
123
- public while(asyncTask: Promise<any>): ShowInterface {
124
- return NanosplashFactory.createWhileFunction(this, new SplashInstance(this, '', this.imgSrc))(asyncTask)
125
- }
126
-
127
- /**
128
- * # LIFO Iterate
129
- * This function will iterate through the splash instances in a LIFO (last in, first out) manner, invoke the
130
- * callback for each instance and pass the instance as an argument.
131
- * @param {InstanceIterationCallback} callback This callback will be invoked for each instance.
132
- * @private
133
- */
134
- private lifoIterate(callback: InstanceIterationCallback): void {
135
- const n = this.instances.size
136
- const instances = Array.from(this.instances.values())
137
- for (let i = n - 1; i >= 0; i--) {
138
- const instance = instances[i]
139
- const id = instance.getId()
140
- if (!callback(id, instance, i)) {
141
- break
142
- }
143
- }
144
- }
145
-
146
- /**
147
- * # Delete
148
- * @param {SplashInstance} splashInstance
149
- */
150
- public delete(splashInstance: SplashInstance): void {
151
- this.instances.delete(splashInstance.getId())
152
- }
153
-
154
- /**
155
- * # Hide All
156
- * Delete all Splash instances.
157
- */
158
- public hideAll(): void
159
- {
160
- this.instances.forEach((instance: SplashInstance) => instance.delete())
161
- }
162
-
163
- /**
164
- * # Hide
165
- * Delete specific Splash instance or the latest one. There are three different ways of using this function.
166
- *
167
- * ## Use SplashInstance IDs
168
- * Every new splash instance is given a unique, randomized ID. This ID is easily retrieved by using the getId
169
- * method.
170
- * <br>
171
- * <code>
172
- * const splashInstance = ns.show('Loading');<br>
173
- * ns.hide(splashInstance.getId());
174
- * </code>
175
- * <br>
176
- *
177
- * ## Use CSS Selector
178
- * Pass the selector referring to the element in which the splash instance resides.
179
- * <br>
180
- * <code>
181
- * ns.show('Loading').inside('#my-div');<br>
182
- * ns.hide('#my-div');
183
- * </code>
184
- * <br>
185
- *
186
- * ## Pass no argument
187
- * When calling the hide method without passing arguments, it will delete the latest splash instance added to the
188
- * DOM, in other words, in a LIFO fashion.<br>
189
- * <code>
190
- * ns.show('Loading');<br>
191
- * ns.hide();
192
- * </code>
193
- * <br>
194
- * @param ref This can either be a SplashInstance ID or a CSS selector referring to the element in which
195
- * the instance is residing.
196
- */
197
- public hide(ref?: Destination): void {
198
- try {
199
- if (ref) {
200
- const isString = typeof ref === 'string'
201
- const isNode = ref instanceof Node
202
- const deleteInstancesWhereDestination = (destination: Destination) => {
203
- Array.from(this.instances.values())
204
- .filter((v: SplashInstance) => v.getDestination() === destination)
205
- .forEach((v: SplashInstance) => v.delete())
206
- }
207
-
208
- if (isString) {
209
- const splashInstance = this.instances.get(ref)
210
- if (splashInstance) {
211
- splashInstance.delete()
212
- return
213
- } else {
214
- const element = get<HTMLElement>(ref)
215
- if (element) {
216
- deleteInstancesWhereDestination(element)
217
- }
218
- throw new Exception(`The CSS selector (${ref}) points to a non-existing DOM element.`)
219
- }
220
- } else if (isNode) {
221
- deleteInstancesWhereDestination(ref)
222
- } else {
223
- throw new IllegalArgumentException(
224
- 'The ref argument must be either a string or Node',
225
- ref
226
- )
227
- }
228
- } else {
229
- const n = this.instances.size
230
- this.lifoIterate((_: string, splashInstance: SplashInstance, i: number) => {
231
- const remove = i === n - 1
232
- if (remove) {
233
- splashInstance.delete()
234
- }
235
- return remove
236
- })
237
- }
238
- } catch (e) {
239
- if (this.debug) {
240
- console.warn(e)
241
- }
242
- }
243
- }
244
-
245
- /**
246
- * # Get From Destination Node
247
- * Returns Splash instances having the given destination node.
248
- * @param {HTMLElement} node
249
- */
250
- public getFromDestinationNode(node: HTMLElement): SplashInstance[]
251
- {
252
- const fnSameDestinationNode = (v: SplashInstance) => v.getDestination() === node
253
- return Array.from(this.instances.values()).filter(fnSameDestinationNode)
254
- }
255
- }
256
-
257
- export default Nanosplash
@@ -1,298 +0,0 @@
1
- import {Destination} from "../types";
2
- import Nanosplash from "./Nanosplash";
3
- import {addClass, mk, move, setAttr} from "../Utilities/dom";
4
- import {IdentityInterface} from "../Interfaces/IdentityInterface";
5
- import {NanosplashRepository} from "../Repositories/NanosplashRepository";
6
-
7
- /**
8
- * SplashInstance
9
- * @author Isak K. Hauge <isakhauge@gmail.com>
10
- */
11
- export class SplashInstance implements IdentityInterface {
12
- private readonly id: string
13
- private imgSrc: string | undefined
14
- private readonly nsInstance: Nanosplash
15
- private destinationNode: HTMLElement | undefined
16
- private readonly nsRootElement: HTMLDivElement = mk('div') as HTMLDivElement
17
- private readonly nsTextElement: HTMLDivElement = mk('div') as HTMLDivElement
18
- private readonly nsTextContainerElement: HTMLDivElement = mk('div') as HTMLDivElement
19
- private readonly nsSpinnerElement: HTMLDivElement = NanosplashRepository.createNanosplashSpinnerElement()
20
- private readonly nsWindowElement: HTMLDivElement = mk('div') as HTMLDivElement
21
- private readonly nsWrapperElement: HTMLDivElement = mk('div') as HTMLDivElement
22
- private readonly nsContentElement: HTMLDivElement = mk('div') as HTMLDivElement
23
- private readonly nsImageElement: HTMLImageElement = mk('img') as HTMLImageElement
24
-
25
- /**
26
- * @param {Nanosplash} ns Nanosplash instance
27
- * @param {string} text Text
28
- * @param {string|undefined} imgSrc Image source URL
29
- */
30
- public constructor(ns: Nanosplash, text: string, imgSrc?: string) {
31
- this.id = Math.random().toString(36).substring(2)
32
- this.nsInstance = ns
33
- this.nsTextElement.innerText = text
34
-
35
- this.imgSrc = imgSrc
36
- this.nsImageElement.src = this.imgSrc ?? ''
37
- this.nsImageElement.alt = Nanosplash.APP_NAME
38
-
39
- this.nsRootElement.style.fontSize = ns.getFontSize()
40
- this.nsSpinnerElement.style.display = ns.spinnerIsVisible() ? 'flex' : 'none'
41
-
42
- this.assembleNSComponent()
43
- this.setImgSrc(imgSrc)
44
- }
45
-
46
- /**
47
- * # Assign CSS Classes
48
- * Assigns CSS class names to all elements connected to this splash instance.
49
- * @private
50
- */
51
- private assignCSSClasses(): void
52
- {
53
- addClass(this.nsContentElement, 'ns-container')
54
- addClass(this.nsWrapperElement, 'ns-blur')
55
- addClass(this.nsImageElement, 'ns-img')
56
- addClass(this.nsTextElement, 'ns-text')
57
- addClass(this.nsTextContainerElement, 'ns-text-container')
58
- addClass(this.nsSpinnerElement, 'ns-spinner')
59
- addClass(this.nsWindowElement, 'ns', 'ns-window')
60
- addClass(this.nsRootElement, 'ns-wrapper')
61
- }
62
-
63
- /**
64
- * # Assemble Element Structure
65
- * Assembles all elements connected to this splash instance in a fixed structure.
66
- * @private
67
- */
68
- private assembleElementStructure(): void
69
- {
70
- this.nsTextContainerElement.append(this.nsTextElement, this.nsSpinnerElement)
71
- this.nsContentElement.append(this.nsImageElement, this.nsTextContainerElement)
72
- this.nsWindowElement.append(this.nsContentElement)
73
- this.nsRootElement.append(this.nsWrapperElement, this.nsWindowElement)
74
- }
75
-
76
- /**
77
- * # Assemble Nanosplash Component
78
- * Assembles all elements into one component that represents the Nanosplash component.
79
- * @private
80
- */
81
- private assembleNSComponent(): void {
82
- this.nsRootElement.id = this.getId()
83
- setAttr(this.nsRootElement, 'data-ctx', 'nanosplash')
84
- this.assembleElementStructure()
85
- this.assignCSSClasses()
86
- }
87
-
88
- /**
89
- * # Get ID
90
- * @return string
91
- */
92
- public getId(): string
93
- {
94
- return this.id
95
- }
96
-
97
- /**
98
- * Get Text
99
- * @return string
100
- */
101
- public getText(): string
102
- {
103
- return this.nsTextElement.innerText
104
- }
105
-
106
- /**
107
- * # Set Text
108
- * @param {string} text Text
109
- * @return SplashInstance
110
- */
111
- public setText(text: string): SplashInstance {
112
- this.nsTextElement.innerText = text
113
- return this
114
- }
115
-
116
- /**
117
- * # Get Image Source
118
- * @return string | undefined
119
- */
120
- public getImgSrc(): string | undefined
121
- {
122
- return this.imgSrc
123
- }
124
-
125
- /**
126
- * # Set Image Source
127
- * @param {string|undefined} src Source URL
128
- * @return SplashInstance
129
- */
130
- public setImgSrc(src?: string): SplashInstance
131
- {
132
- this.nsImageElement.src = src ?? ''
133
- this.nsImageElement.style.display = src ? 'block' : 'none'
134
- this.assembleElementStructure()
135
- return this
136
- }
137
-
138
- /**
139
- * # Get Destination
140
- * @return HTMLElement | undefined
141
- */
142
- public getDestination(): HTMLElement | undefined
143
- {
144
- return this.destinationNode
145
- }
146
-
147
- /**
148
- * # Clean And Restore
149
- * Cleans and restore the DOM structure.
150
- * @private
151
- */
152
- private cleanAndRestore(): void
153
- {
154
- const currentParent = this.nsRootElement.parentElement
155
- if (currentParent) {
156
- this.restoreDOMStructure(currentParent)
157
- }
158
- }
159
-
160
- /**
161
- * # Reset Fullscreen Attributes
162
- * Removes the fullscreen attributes and CSS class names.
163
- * @private
164
- */
165
- private resetFullscreenAttributes(): void
166
- {
167
- setAttr(this.nsRootElement, 'style', '')
168
- this.nsRootElement.classList.remove('ns-fs')
169
- }
170
-
171
- /**
172
- * # Move With Regular Strategy
173
- * Moves the Nanosplash root element to the target node's position and essentially wraps the target node with the
174
- * Nanosplash root element.
175
- * @param {Node} targetNode
176
- * @private
177
- */
178
- private moveWithRegularStrategy(targetNode: Node): void
179
- {
180
- const targetParentNode = targetNode.parentNode
181
- if (targetParentNode) {
182
- this.restoreDOMStructure(targetParentNode)
183
- targetParentNode.replaceChild(this.nsRootElement, targetNode)
184
- this.nsWrapperElement.appendChild(targetNode)
185
- }
186
- }
187
-
188
- /**
189
- * # Move With Fullscreen Strategy
190
- * Moves the Nanosplash root element inside the body element.
191
- * @private
192
- */
193
- private moveWithFullscreenStrategy(): void
194
- {
195
- this.nsRootElement.classList.add('ns-fs')
196
- move(this.nsRootElement, document.body, true)
197
- }
198
-
199
- /**
200
- * # Replace Splash Instances Having Same Destination
201
- * Delete all Splash instances residing inside the same destination node.
202
- * @param {HTMLElement} destinationNode
203
- * @private
204
- */
205
- private replaceSplashInstancesHavingSameDestination(destinationNode: HTMLElement): void
206
- {
207
- const fnNotSameInstance = (v: SplashInstance) => v.getId() !== this.getId()
208
- const fnDelete = (v: SplashInstance) => v.delete()
209
- this.nsInstance.getFromDestinationNode(destinationNode).filter(fnNotSameInstance).forEach(fnDelete)
210
- }
211
-
212
- /**
213
- * # Move To
214
- * This function moves the Nanosplash root element to a destination.
215
- * @param {Destination} destination
216
- */
217
- public moveTo(destination: Destination): void
218
- {
219
- try {
220
- // Clean up and restore previous location if any.
221
- this.cleanAndRestore()
222
-
223
- // Convert the destination into a DOM Node
224
- this.destinationNode = NanosplashRepository.destinationToNode(destination)
225
-
226
- // Replace splash instances having the same destination node
227
- this.replaceSplashInstancesHavingSameDestination(this.destinationNode)
228
-
229
- // Assess whether the destination node is the Document body or not
230
- const targetIsBody = this.destinationNode === document.body
231
-
232
- if (targetIsBody) {
233
- this.moveWithFullscreenStrategy()
234
- } else {
235
- this.resetFullscreenAttributes()
236
- this.moveWithRegularStrategy(this.destinationNode)
237
- }
238
-
239
- // Assemble the Nanosplash component
240
- this.assembleNSComponent()
241
- } catch (e) {
242
- this.delete()
243
- if (this.nsInstance.debug) {
244
- console.warn(e)
245
- }
246
- }
247
- }
248
-
249
- /**
250
- * # For Each Wrapped Node
251
- * Invokes a callback for each wrapped node.
252
- * @param {(node: Node) => void} callback
253
- * @private
254
- */
255
- private forEachWrappedNode(callback: (node: Node) => void): void
256
- {
257
- Array.from(this.nsWrapperElement.childNodes).forEach(callback)
258
- }
259
-
260
- /**
261
- * # Restore DOM Structure
262
- * Undo the node wrapping and restore the wrapped nodes' original DOM position.
263
- * @param {Node} parentNode
264
- * @private
265
- */
266
- private restoreDOMStructure(parentNode: Node): void
267
- {
268
- this.forEachWrappedNode((child: Node) => parentNode.insertBefore(child, this.nsRootElement))
269
- }
270
-
271
- /**
272
- * # Remove Elements From DOM
273
- * @private
274
- */
275
- private removeElementsFromDOM(): void
276
- {
277
- [
278
- this.nsTextElement,
279
- this.nsSpinnerElement,
280
- this.nsTextContainerElement,
281
- this.nsImageElement,
282
- this.nsContentElement,
283
- this.nsWrapperElement,
284
- this.nsWindowElement,
285
- this.nsRootElement
286
- ].forEach(v => v.remove())
287
- }
288
-
289
- /**
290
- * # Delete
291
- * Gracefully delete Splash instance and connected elements from DOM.
292
- */
293
- public delete(): void {
294
- this.cleanAndRestore()
295
- this.removeElementsFromDOM()
296
- this.nsInstance.delete(this)
297
- }
298
- }
@@ -1,13 +0,0 @@
1
- import Exception from "./Exception";
2
- import {Destination} from "../types";
3
-
4
- class DestinationException extends Exception {
5
- public destination: Destination
6
- public constructor(message: string, destination: Destination, cause: Exception) {
7
- super(message);
8
- this.destination = destination
9
- this.cause = cause
10
- }
11
- }
12
-
13
- export default DestinationException
@@ -1,9 +0,0 @@
1
- class Exception extends Error {
2
- public constructor(message?: string, cause?: Exception) {
3
- super(message);
4
- this.name = this.constructor.name
5
- this.cause = cause
6
- }
7
- }
8
-
9
- export default Exception
@@ -1,11 +0,0 @@
1
- import Exception from "./Exception";
2
-
3
- class IllegalArgumentException extends Exception {
4
- public argument: any
5
- public constructor(message: string, argument: any) {
6
- super(message);
7
- this.argument = argument
8
- }
9
- }
10
-
11
- export default IllegalArgumentException
@@ -1,93 +0,0 @@
1
- import Nanosplash from "../Core/Nanosplash";
2
- import {SplashInstance} from "../Core/SplashInstance";
3
- import {NanosplashRepository} from "../Repositories/NanosplashRepository";
4
- import {ContextualAPIObject, ProgressFunction, ShowFunction, SplashJob, WhileFunction} from "../types";
5
- import {ShowInterface} from "../Interfaces/ShowInterface";
6
-
7
- /**
8
- * # Nanosplash Factory
9
- * @author Isak K. Hauge
10
- */
11
- export class NanosplashFactory {
12
- /**
13
- * # Ensure Instance
14
- * Returns and modifies the splash instance, or creates a new one.
15
- * @param {SplashInstance | null} splash Splash instance
16
- * @param {Nanosplash} ns Nanosplash instance
17
- * @param {string} text Splash text
18
- * @param {string | undefined} imgSrc Splash image URL
19
- * @private
20
- */
21
- private static ensureInstance(
22
- splash: SplashInstance | null,
23
- ns: Nanosplash,
24
- text: string,
25
- imgSrc?: string
26
- ): SplashInstance
27
- {
28
- if (!splash) {
29
- splash = new SplashInstance(ns, text, imgSrc)
30
- }
31
- return splash.setText(text).setImgSrc(splash.getImgSrc() ?? imgSrc)
32
- }
33
-
34
- /**
35
- * # Create Show Function
36
- * @param ns
37
- * @param splash
38
- * @return {ShowFunction} A curried function made for a specific splash instance.
39
- */
40
- public static createShowFunction(ns: Nanosplash, splash: SplashInstance | null): ShowFunction
41
- {
42
- return (text: string): ContextualAPIObject => {
43
- splash = NanosplashFactory.ensureInstance(splash, ns, text)
44
- ns.instances.set(splash.getId(), splash)
45
- splash.moveTo(document.body)
46
- return NanosplashRepository.createContextualApiObject(splash)
47
- }
48
- }
49
-
50
- /**
51
- * # Create Progress Function
52
- * @param ns
53
- * @param splash
54
- * @return {ProgressFunction} A curried API function made for a specific splash instance.
55
- */
56
- public static createProgressFunction(ns: Nanosplash, splash: SplashInstance | null): ProgressFunction
57
- {
58
- return (...jobs: SplashJob[]) => {
59
- splash = NanosplashFactory.ensureInstance(splash, ns, '')
60
- splash.moveTo(document.body)
61
- ;(async () => {
62
- for (const [_, [job, text]] of jobs.entries()) {
63
- splash.setText(text)
64
- await job
65
- }
66
- splash.delete()
67
- })()
68
- return NanosplashRepository.createContextualApiObject(splash)
69
- }
70
- }
71
-
72
- /**
73
- * # Create While Function
74
- * @param ns
75
- * @param splash
76
- * @return {ProgressFunction} A curried API function made for a specific splash instance.
77
- */
78
- public static createWhileFunction(ns: Nanosplash, splash: SplashInstance): WhileFunction
79
- {
80
- return (asyncTask: Promise<any>): ShowInterface => {
81
- splash = NanosplashFactory.ensureInstance(splash, ns, '')
82
- return {
83
- show(text: string) {
84
- ns.instances.set(splash.getId(), splash)
85
- splash.moveTo(document.body)
86
- splash.setText(text)
87
- asyncTask.finally(() => (splash as SplashInstance).delete())
88
- return NanosplashRepository.createContextualApiObject(splash)
89
- }
90
- }
91
- }
92
- }
93
- }
@@ -1,8 +0,0 @@
1
- import {SplashInstance} from "../Core/SplashInstance";
2
-
3
- export interface ContextualAPIInterface {
4
- getId: () => string
5
- remove: () => void
6
- moveTo: (selector: string) => void,
7
- setText: (text: string) => SplashInstance
8
- }
@@ -1,3 +0,0 @@
1
- export interface IdentityInterface {
2
- getId: () => string
3
- }
@@ -1,5 +0,0 @@
1
- import {ContextualAPIInterface} from "./ContextualAPIInterface";
2
-
3
- export interface InsideInterface extends ContextualAPIInterface {
4
- inside: (selector: string) => ContextualAPIInterface
5
- }