nanosplash 2.3.3 → 2.5.1
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/.github/workflows/production.yml +5 -2
- package/README.md +38 -42
- package/dist/iife/nanosplash.iife.js +1 -1
- package/dist/module/manifest.json +1 -4
- package/dist/module/nanosplash.cjs.js +1 -1
- package/dist/module/nanosplash.es.js +132 -74
- package/dist/module/style.css +1 -0
- package/docs/index.html +73 -19
- package/docs/nanosplash.iife.js +6 -1
- package/docs/style.css +1 -1
- package/docs/typedoc/assets/main.js +2 -2
- package/docs/typedoc/assets/search.js +1 -1
- package/docs/typedoc/classes/Core_Nanosplash.default.html +133 -0
- package/docs/typedoc/index.html +1 -1
- package/docs/typedoc/modules/Core_Nanosplash.html +1 -0
- package/docs/typedoc/modules/types.html +1 -0
- package/index.html +72 -22
- package/package.json +16 -15
- package/src/Core/Nanosplash.ts +188 -31
- package/src/Core/SplashInstance.ts +137 -21
- package/src/Exceptions/DestinationException.ts +13 -0
- package/src/Exceptions/Exception.ts +9 -0
- package/src/Exceptions/IllegalArgumentException.ts +11 -0
- package/src/Factory/NanosplashFactory.ts +37 -17
- package/src/Interfaces/NanosplashInterface.ts +6 -2
- package/src/Repositories/NanosplashRepository.ts +82 -0
- package/src/{utilities → Tests}/dom.spec.ts +7 -5
- package/src/Utilities/dom.ts +49 -0
- package/src/main.ts +5 -2
- package/src/style.sass +47 -17
- package/src/types.d.ts +23 -14
- package/tsconfig.json +1 -0
- package/update-all.sh +2 -2
- package/vite.config.ts +16 -0
- package/vite.mod.config.js +10 -11
- package/dist/module/Nanosplash.css +0 -1
- package/jest.config.ts +0 -194
- package/src/Interfaces/ImageInterface.ts +0 -5
- package/src/repositories/NanosplashRepository.ts +0 -39
- package/src/utilities/dom.ts +0 -26
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import {Destination} from "../types";
|
|
2
|
-
import
|
|
3
|
-
import {addClass, mk, move, setAttr} from "../
|
|
2
|
+
import Nanosplash from "./Nanosplash";
|
|
3
|
+
import {addClass, mk, move, setAttr} from "../Utilities/dom";
|
|
4
4
|
import {IdentityInterface} from "../Interfaces/IdentityInterface";
|
|
5
|
-
import {NanosplashRepository} from "../
|
|
5
|
+
import {NanosplashRepository} from "../Repositories/NanosplashRepository";
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* SplashInstance
|
|
9
|
+
* @author Isak K. Hauge <isakhauge@gmail.com>
|
|
10
|
+
*/
|
|
7
11
|
export class SplashInstance implements IdentityInterface {
|
|
8
12
|
private readonly id: string
|
|
9
13
|
private imgSrc: string | undefined
|
|
@@ -11,11 +15,18 @@ export class SplashInstance implements IdentityInterface {
|
|
|
11
15
|
private destinationNode: HTMLElement | undefined
|
|
12
16
|
private readonly nsRootElement: HTMLDivElement = mk('div') as HTMLDivElement
|
|
13
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()
|
|
14
20
|
private readonly nsWindowElement: HTMLDivElement = mk('div') as HTMLDivElement
|
|
15
21
|
private readonly nsWrapperElement: HTMLDivElement = mk('div') as HTMLDivElement
|
|
16
22
|
private readonly nsContentElement: HTMLDivElement = mk('div') as HTMLDivElement
|
|
17
23
|
private readonly nsImageElement: HTMLImageElement = mk('img') as HTMLImageElement
|
|
18
24
|
|
|
25
|
+
/**
|
|
26
|
+
* @param {Nanosplash} ns Nanosplash instance
|
|
27
|
+
* @param {string} text Text
|
|
28
|
+
* @param {string|undefined} imgSrc Image source URL
|
|
29
|
+
*/
|
|
19
30
|
public constructor(ns: Nanosplash, text: string, imgSrc?: string) {
|
|
20
31
|
this.id = Math.random().toString(36).substring(2)
|
|
21
32
|
this.nsInstance = ns
|
|
@@ -25,27 +36,48 @@ export class SplashInstance implements IdentityInterface {
|
|
|
25
36
|
this.nsImageElement.src = this.imgSrc ?? ''
|
|
26
37
|
this.nsImageElement.alt = Nanosplash.APP_NAME
|
|
27
38
|
|
|
39
|
+
this.nsRootElement.style.fontSize = ns.getFontSize()
|
|
40
|
+
this.nsSpinnerElement.style.display = ns.spinnerIsVisible() ? 'flex' : 'none'
|
|
41
|
+
|
|
28
42
|
this.assembleNSComponent()
|
|
29
43
|
this.setImgSrc(imgSrc)
|
|
30
44
|
}
|
|
31
45
|
|
|
46
|
+
/**
|
|
47
|
+
* # Assign CSS Classes
|
|
48
|
+
* Assigns CSS class names to all elements connected to this splash instance.
|
|
49
|
+
* @private
|
|
50
|
+
*/
|
|
32
51
|
private assignCSSClasses(): void
|
|
33
52
|
{
|
|
34
53
|
addClass(this.nsContentElement, 'ns-container')
|
|
35
54
|
addClass(this.nsWrapperElement, 'ns-blur')
|
|
36
55
|
addClass(this.nsImageElement, 'ns-img')
|
|
37
56
|
addClass(this.nsTextElement, 'ns-text')
|
|
57
|
+
addClass(this.nsTextContainerElement, 'ns-text-container')
|
|
58
|
+
addClass(this.nsSpinnerElement, 'ns-spinner')
|
|
38
59
|
addClass(this.nsWindowElement, 'ns', 'ns-window')
|
|
39
60
|
addClass(this.nsRootElement, 'ns-wrapper')
|
|
40
61
|
}
|
|
41
62
|
|
|
63
|
+
/**
|
|
64
|
+
* # Assemble Element Structure
|
|
65
|
+
* Assembles all elements connected to this splash instance in a fixed structure.
|
|
66
|
+
* @private
|
|
67
|
+
*/
|
|
42
68
|
private assembleElementStructure(): void
|
|
43
69
|
{
|
|
44
|
-
this.
|
|
70
|
+
this.nsTextContainerElement.append(this.nsTextElement, this.nsSpinnerElement)
|
|
71
|
+
this.nsContentElement.append(this.nsImageElement, this.nsTextContainerElement)
|
|
45
72
|
this.nsWindowElement.append(this.nsContentElement)
|
|
46
73
|
this.nsRootElement.append(this.nsWrapperElement, this.nsWindowElement)
|
|
47
74
|
}
|
|
48
75
|
|
|
76
|
+
/**
|
|
77
|
+
* # Assemble Nanosplash Component
|
|
78
|
+
* Assembles all elements into one component that represents the Nanosplash component.
|
|
79
|
+
* @private
|
|
80
|
+
*/
|
|
49
81
|
private assembleNSComponent(): void {
|
|
50
82
|
this.nsRootElement.id = this.getId()
|
|
51
83
|
setAttr(this.nsRootElement, 'data-ctx', 'nanosplash')
|
|
@@ -53,26 +85,48 @@ export class SplashInstance implements IdentityInterface {
|
|
|
53
85
|
this.assignCSSClasses()
|
|
54
86
|
}
|
|
55
87
|
|
|
88
|
+
/**
|
|
89
|
+
* # Get ID
|
|
90
|
+
* @return string
|
|
91
|
+
*/
|
|
56
92
|
public getId(): string
|
|
57
93
|
{
|
|
58
94
|
return this.id
|
|
59
95
|
}
|
|
60
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Get Text
|
|
99
|
+
* @return string
|
|
100
|
+
*/
|
|
61
101
|
public getText(): string
|
|
62
102
|
{
|
|
63
103
|
return this.nsTextElement.innerText
|
|
64
104
|
}
|
|
65
105
|
|
|
106
|
+
/**
|
|
107
|
+
* # Set Text
|
|
108
|
+
* @param {string} text Text
|
|
109
|
+
* @return SplashInstance
|
|
110
|
+
*/
|
|
66
111
|
public setText(text: string): SplashInstance {
|
|
67
112
|
this.nsTextElement.innerText = text
|
|
68
113
|
return this
|
|
69
114
|
}
|
|
70
115
|
|
|
116
|
+
/**
|
|
117
|
+
* # Get Image Source
|
|
118
|
+
* @return string | undefined
|
|
119
|
+
*/
|
|
71
120
|
public getImgSrc(): string | undefined
|
|
72
121
|
{
|
|
73
122
|
return this.imgSrc
|
|
74
123
|
}
|
|
75
124
|
|
|
125
|
+
/**
|
|
126
|
+
* # Set Image Source
|
|
127
|
+
* @param {string|undefined} src Source URL
|
|
128
|
+
* @return SplashInstance
|
|
129
|
+
*/
|
|
76
130
|
public setImgSrc(src?: string): SplashInstance
|
|
77
131
|
{
|
|
78
132
|
this.nsImageElement.src = src ?? ''
|
|
@@ -81,11 +135,20 @@ export class SplashInstance implements IdentityInterface {
|
|
|
81
135
|
return this
|
|
82
136
|
}
|
|
83
137
|
|
|
138
|
+
/**
|
|
139
|
+
* # Get Destination
|
|
140
|
+
* @return HTMLElement | undefined
|
|
141
|
+
*/
|
|
84
142
|
public getDestination(): HTMLElement | undefined
|
|
85
143
|
{
|
|
86
144
|
return this.destinationNode
|
|
87
145
|
}
|
|
88
146
|
|
|
147
|
+
/**
|
|
148
|
+
* # Clean And Restore
|
|
149
|
+
* Cleans and restore the DOM structure.
|
|
150
|
+
* @private
|
|
151
|
+
*/
|
|
89
152
|
private cleanAndRestore(): void
|
|
90
153
|
{
|
|
91
154
|
const currentParent = this.nsRootElement.parentElement
|
|
@@ -94,12 +157,24 @@ export class SplashInstance implements IdentityInterface {
|
|
|
94
157
|
}
|
|
95
158
|
}
|
|
96
159
|
|
|
160
|
+
/**
|
|
161
|
+
* # Reset Fullscreen Attributes
|
|
162
|
+
* Removes the fullscreen attributes and CSS class names.
|
|
163
|
+
* @private
|
|
164
|
+
*/
|
|
97
165
|
private resetFullscreenAttributes(): void
|
|
98
166
|
{
|
|
99
167
|
setAttr(this.nsRootElement, 'style', '')
|
|
100
168
|
this.nsRootElement.classList.remove('ns-fs')
|
|
101
169
|
}
|
|
102
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
|
+
*/
|
|
103
178
|
private moveWithRegularStrategy(targetNode: Node): void
|
|
104
179
|
{
|
|
105
180
|
const targetParentNode = targetNode.parentNode
|
|
@@ -110,59 +185,96 @@ export class SplashInstance implements IdentityInterface {
|
|
|
110
185
|
}
|
|
111
186
|
}
|
|
112
187
|
|
|
188
|
+
/**
|
|
189
|
+
* # Move With Fullscreen Strategy
|
|
190
|
+
* Moves the Nanosplash root element inside the body element.
|
|
191
|
+
* @private
|
|
192
|
+
*/
|
|
113
193
|
private moveWithFullscreenStrategy(): void
|
|
114
194
|
{
|
|
115
195
|
this.nsRootElement.classList.add('ns-fs')
|
|
116
196
|
move(this.nsRootElement, document.body, true)
|
|
117
197
|
}
|
|
118
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
|
+
*/
|
|
119
205
|
private replaceSplashInstancesHavingSameDestination(destinationNode: HTMLElement): void
|
|
120
206
|
{
|
|
121
207
|
const fnNotSameInstance = (v: SplashInstance) => v.getId() !== this.getId()
|
|
122
208
|
const fnDelete = (v: SplashInstance) => v.delete()
|
|
123
|
-
this.nsInstance.
|
|
209
|
+
this.nsInstance.getFromDestinationNode(destinationNode).filter(fnNotSameInstance).forEach(fnDelete)
|
|
124
210
|
}
|
|
125
211
|
|
|
212
|
+
/**
|
|
213
|
+
* # Move To
|
|
214
|
+
* This function moves the Nanosplash root element to a destination.
|
|
215
|
+
* @param {Destination} destination
|
|
216
|
+
*/
|
|
126
217
|
public moveTo(destination: Destination): void
|
|
127
218
|
{
|
|
128
|
-
|
|
129
|
-
|
|
219
|
+
try {
|
|
220
|
+
// Clean up and restore previous location if any.
|
|
221
|
+
this.cleanAndRestore()
|
|
130
222
|
|
|
131
|
-
|
|
132
|
-
|
|
223
|
+
// Convert the destination into a DOM Node
|
|
224
|
+
this.destinationNode = NanosplashRepository.destinationToNode(destination)
|
|
133
225
|
|
|
134
|
-
|
|
135
|
-
|
|
226
|
+
// Replace splash instances having the same destination node
|
|
227
|
+
this.replaceSplashInstancesHavingSameDestination(this.destinationNode)
|
|
136
228
|
|
|
137
|
-
|
|
138
|
-
|
|
229
|
+
// Assess whether the destination node is the Document body or not
|
|
230
|
+
const targetIsBody = this.destinationNode === document.body
|
|
139
231
|
|
|
232
|
+
if (targetIsBody) {
|
|
233
|
+
this.moveWithFullscreenStrategy()
|
|
234
|
+
} else {
|
|
235
|
+
this.resetFullscreenAttributes()
|
|
236
|
+
this.moveWithRegularStrategy(this.destinationNode)
|
|
237
|
+
}
|
|
140
238
|
|
|
141
|
-
|
|
142
|
-
this.
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
this.moveWithRegularStrategy(this.destinationNode)
|
|
239
|
+
// Assemble the Nanosplash component
|
|
240
|
+
this.assembleNSComponent()
|
|
241
|
+
} catch (e) {
|
|
242
|
+
console.warn(e)
|
|
146
243
|
}
|
|
147
|
-
|
|
148
|
-
// Assemble the Nanosplash component
|
|
149
|
-
this.assembleNSComponent()
|
|
150
244
|
}
|
|
151
245
|
|
|
246
|
+
/**
|
|
247
|
+
* # For Each Wrapped Node
|
|
248
|
+
* Invokes a callback for each wrapped node.
|
|
249
|
+
* @param {(node: Node) => void} callback
|
|
250
|
+
* @private
|
|
251
|
+
*/
|
|
152
252
|
private forEachWrappedNode(callback: (node: Node) => void): void
|
|
153
253
|
{
|
|
154
254
|
Array.from(this.nsWrapperElement.childNodes).forEach(callback)
|
|
155
255
|
}
|
|
156
256
|
|
|
257
|
+
/**
|
|
258
|
+
* # Restore DOM Structure
|
|
259
|
+
* Undo the node wrapping and restore the wrapped nodes' original DOM position.
|
|
260
|
+
* @param {Node} parentNode
|
|
261
|
+
* @private
|
|
262
|
+
*/
|
|
157
263
|
private restoreDOMStructure(parentNode: Node): void
|
|
158
264
|
{
|
|
159
265
|
this.forEachWrappedNode((child: Node) => parentNode.insertBefore(child, this.nsRootElement))
|
|
160
266
|
}
|
|
161
267
|
|
|
268
|
+
/**
|
|
269
|
+
* # Remove Elements From DOM
|
|
270
|
+
* @private
|
|
271
|
+
*/
|
|
162
272
|
private removeElementsFromDOM(): void
|
|
163
273
|
{
|
|
164
274
|
[
|
|
165
275
|
this.nsTextElement,
|
|
276
|
+
this.nsSpinnerElement,
|
|
277
|
+
this.nsTextContainerElement,
|
|
166
278
|
this.nsImageElement,
|
|
167
279
|
this.nsContentElement,
|
|
168
280
|
this.nsWrapperElement,
|
|
@@ -171,6 +283,10 @@ export class SplashInstance implements IdentityInterface {
|
|
|
171
283
|
].forEach(v => v.remove())
|
|
172
284
|
}
|
|
173
285
|
|
|
286
|
+
/**
|
|
287
|
+
* # Delete
|
|
288
|
+
* Gracefully delete Splash instance and connected elements from DOM.
|
|
289
|
+
*/
|
|
174
290
|
public delete(): void {
|
|
175
291
|
this.cleanAndRestore()
|
|
176
292
|
this.removeElementsFromDOM()
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
|
@@ -0,0 +1,11 @@
|
|
|
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,9 +1,23 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {Nanosplash} from "../Core/Nanosplash";
|
|
1
|
+
import Nanosplash from "../Core/Nanosplash";
|
|
3
2
|
import {SplashInstance} from "../Core/SplashInstance";
|
|
4
|
-
import {NanosplashRepository} from "../
|
|
3
|
+
import {NanosplashRepository} from "../Repositories/NanosplashRepository";
|
|
4
|
+
import {ContextualAPIObject, ProgressFunction, ShowFunction, SplashJob, WhileFunction} from "../types";
|
|
5
|
+
import {ShowInterface} from "../Interfaces/ShowInterface";
|
|
5
6
|
|
|
7
|
+
/**
|
|
8
|
+
* # Nanosplash Factory
|
|
9
|
+
* @author Isak K. Hauge
|
|
10
|
+
*/
|
|
6
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
|
+
*/
|
|
7
21
|
private static ensureInstance(
|
|
8
22
|
splash: SplashInstance | null,
|
|
9
23
|
ns: Nanosplash,
|
|
@@ -17,21 +31,15 @@ export class NanosplashFactory {
|
|
|
17
31
|
return splash.setText(text).setImgSrc(splash.getImgSrc() ?? imgSrc)
|
|
18
32
|
}
|
|
19
33
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
progress: NanosplashFactory.createProgressFunction(ns, splash),
|
|
27
|
-
while: NanosplashFactory.createWhileFunction(ns, splash)
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
34
|
+
/**
|
|
35
|
+
* # Create Show Function
|
|
36
|
+
* @param ns
|
|
37
|
+
* @param splash
|
|
38
|
+
* @return {ShowFunction} A curried function made for a specific splash instance.
|
|
39
|
+
*/
|
|
32
40
|
public static createShowFunction(ns: Nanosplash, splash: SplashInstance | null): ShowFunction
|
|
33
41
|
{
|
|
34
|
-
return (text: string) => {
|
|
42
|
+
return (text: string): ContextualAPIObject => {
|
|
35
43
|
splash = NanosplashFactory.ensureInstance(splash, ns, text)
|
|
36
44
|
ns.instances.set(splash.getId(), splash)
|
|
37
45
|
splash.moveTo(document.body)
|
|
@@ -39,6 +47,12 @@ export class NanosplashFactory {
|
|
|
39
47
|
}
|
|
40
48
|
}
|
|
41
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
|
+
*/
|
|
42
56
|
public static createProgressFunction(ns: Nanosplash, splash: SplashInstance | null): ProgressFunction
|
|
43
57
|
{
|
|
44
58
|
return (...jobs: SplashJob[]) => {
|
|
@@ -55,9 +69,15 @@ export class NanosplashFactory {
|
|
|
55
69
|
}
|
|
56
70
|
}
|
|
57
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
|
+
*/
|
|
58
78
|
public static createWhileFunction(ns: Nanosplash, splash: SplashInstance): WhileFunction
|
|
59
79
|
{
|
|
60
|
-
return (asyncTask: Promise<any>) => {
|
|
80
|
+
return (asyncTask: Promise<any>): ShowInterface => {
|
|
61
81
|
splash = NanosplashFactory.ensureInstance(splash, ns, '')
|
|
62
82
|
return {
|
|
63
83
|
show(text: string) {
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import {StrategyObject} from "../types";
|
|
2
|
-
import {
|
|
2
|
+
import {SplashInstance} from "../Core/SplashInstance";
|
|
3
3
|
|
|
4
|
-
export interface NanosplashInterface extends StrategyObject
|
|
4
|
+
export interface NanosplashInterface extends StrategyObject {
|
|
5
|
+
delete(splashInstance: SplashInstance): void
|
|
6
|
+
hideAll(): void
|
|
7
|
+
hide(id?: string): void
|
|
8
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import Exception from "../Exceptions/Exception";
|
|
2
|
+
import {addClass, get, mk} from "../Utilities/dom";
|
|
3
|
+
import {SplashInstance} from "../Core/SplashInstance";
|
|
4
|
+
import {ContextualAPIObject, Destination} from "../types";
|
|
5
|
+
import DestinationException from "../Exceptions/DestinationException";
|
|
6
|
+
import IllegalArgumentException from "../Exceptions/IllegalArgumentException";
|
|
7
|
+
|
|
8
|
+
export class NanosplashRepository {
|
|
9
|
+
/**
|
|
10
|
+
* # Destination To Node
|
|
11
|
+
* Converts a Destination type into an HTMLElement.
|
|
12
|
+
* @param {Destination} destination Either a node or a CSS selector.
|
|
13
|
+
* @throws DestinationException
|
|
14
|
+
* @throws IllegalArgumentException
|
|
15
|
+
*/
|
|
16
|
+
public static destinationToNode(destination: Destination): HTMLElement
|
|
17
|
+
{
|
|
18
|
+
if (typeof destination === 'string') {
|
|
19
|
+
try {
|
|
20
|
+
const element = get<HTMLElement>(destination)
|
|
21
|
+
if (!element) {
|
|
22
|
+
throw new Exception(`No DOM match with ${destination}`)
|
|
23
|
+
}
|
|
24
|
+
return element
|
|
25
|
+
} catch (e) {
|
|
26
|
+
throw new DestinationException(
|
|
27
|
+
`Destination (${destination}) is either invalid or non-existing in DOM`,
|
|
28
|
+
destination,
|
|
29
|
+
e as Exception
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
} else if (destination instanceof Node) {
|
|
33
|
+
return destination as HTMLElement
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
throw new IllegalArgumentException(
|
|
37
|
+
`Destination (${destination}) must be either a Node or a CSS selector`,
|
|
38
|
+
destination
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* # Create Contextual API Object
|
|
44
|
+
* Creates an object containing props that adhere to the ns API.
|
|
45
|
+
* @param {SplashInstance} splash
|
|
46
|
+
*/
|
|
47
|
+
public static createContextualApiObject(splash: SplashInstance): ContextualAPIObject
|
|
48
|
+
{
|
|
49
|
+
const ctx = {
|
|
50
|
+
getId: () => splash.getId(),
|
|
51
|
+
remove: () => splash.delete(),
|
|
52
|
+
moveTo: (selector: string) => splash.moveTo(selector),
|
|
53
|
+
getText: () => splash.getText(),
|
|
54
|
+
setText: (text: string) => splash.setText(text),
|
|
55
|
+
getImgSrc: () => splash.getImgSrc(),
|
|
56
|
+
setImgSrc: (src?: string) => splash.setImgSrc(src)
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
...ctx,
|
|
60
|
+
inside: (selector: string) => {
|
|
61
|
+
splash.moveTo(selector)
|
|
62
|
+
return ctx
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* # Create Nanosplash Spinner Element
|
|
69
|
+
* Returns a DIV element wrapping an SVG element.
|
|
70
|
+
*/
|
|
71
|
+
public static createNanosplashSpinnerElement(): HTMLDivElement
|
|
72
|
+
{
|
|
73
|
+
const div = mk('div') as HTMLDivElement
|
|
74
|
+
addClass(div, 'ns-spinner')
|
|
75
|
+
div.innerHTML = `
|
|
76
|
+
<svg viewBox="0 0 50 50">
|
|
77
|
+
<circle class="path" cx="25" cy="25" r="20" fill="none"></circle>
|
|
78
|
+
</svg>
|
|
79
|
+
`
|
|
80
|
+
return div
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {test} from "vitest"
|
|
2
|
+
import {addClass, get, mk, move, setAttr} from "../Utilities/dom"
|
|
2
3
|
|
|
3
4
|
const resetDOM = () => document.body.innerHTML = ''
|
|
4
5
|
|
|
5
|
-
test('
|
|
6
|
+
test('Get DOM element function', () => {
|
|
6
7
|
const element = document.createElement('div')
|
|
7
8
|
element.id = 'a'
|
|
8
9
|
element.classList.add('b')
|
|
@@ -48,9 +49,10 @@ test('Move Element from origin to destination', () => {
|
|
|
48
49
|
})
|
|
49
50
|
|
|
50
51
|
test('Make HTML Element', () => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
const span = mk('span')
|
|
53
|
+
document.body.appendChild(span)
|
|
54
|
+
const spanFromDom = document.body.children.item(0) as Node
|
|
55
|
+
expect(spanFromDom instanceof HTMLSpanElement).toBe(true)
|
|
54
56
|
resetDOM()
|
|
55
57
|
})
|
|
56
58
|
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {ElementTag} from "../types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* # Get
|
|
5
|
+
* Helper function to get Elements from the DOM using CSS selector.
|
|
6
|
+
* @param {string} selector CSS Selector.
|
|
7
|
+
*/
|
|
8
|
+
export function get<T>(selector: string): HTMLElement | T | null {
|
|
9
|
+
return document.querySelector(selector) as HTMLElement | T | null
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* # Move
|
|
14
|
+
* Helper function that can move a node inside another node.
|
|
15
|
+
* @param {Node} source The node to be moved.
|
|
16
|
+
* @param {Node} destination The destination node.
|
|
17
|
+
* @param {boolean} asFirstChild Append or prepend the new node inside the child list.
|
|
18
|
+
*/
|
|
19
|
+
export function move(source: Node, destination: Node, asFirstChild: boolean = false): void
|
|
20
|
+
{
|
|
21
|
+
if (source && destination) {
|
|
22
|
+
(destination.hasChildNodes() && asFirstChild)
|
|
23
|
+
? destination.insertBefore(source, destination.firstChild)
|
|
24
|
+
: destination.appendChild(source)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* # Make
|
|
30
|
+
* Short hand helper function that creates Nodes.
|
|
31
|
+
* @param {ElementTag} tag
|
|
32
|
+
*/
|
|
33
|
+
export const mk = (tag: ElementTag) => document.createElement(tag)
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* # Add Class
|
|
37
|
+
* Helper class that easily adds classes to an element.
|
|
38
|
+
* @param {HTMLElement} node The node
|
|
39
|
+
* @param {...string[]} classes
|
|
40
|
+
*/
|
|
41
|
+
export function addClass(node: HTMLElement, ...classes: string[]): void
|
|
42
|
+
{
|
|
43
|
+
node.classList.add(...classes)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function setAttr(node: HTMLElement, attribute: string, value: string): void
|
|
47
|
+
{
|
|
48
|
+
node.setAttribute(attribute, value)
|
|
49
|
+
}
|
package/src/main.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
window.addEventListener('load', () =>
|
|
1
|
+
import Nanosplash from "./Core/Nanosplash";
|
|
2
|
+
window.addEventListener('load', () => {
|
|
3
|
+
window.Nanosplash = Nanosplash
|
|
4
|
+
window.ns = new Nanosplash()
|
|
5
|
+
})
|