nanosplash 2.3.2 → 2.5.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/index.html CHANGED
@@ -13,8 +13,23 @@
13
13
  </head>
14
14
  <body class="flex flex-col justify-between">
15
15
  <div class="w-100 min-h-screen flex flex-col justify-center items-center">
16
- <div class="text-3xl">Nanosplash</div>
17
- <div class="mt-3">Small. Simple. Reliable.</div>
16
+ <div class="text-4xl text-slate-900 drop-shadow-lg">Nanosplash</div>
17
+ <div class="text-pink-500 mt-3 font-mono text-sm">ns.show('text').inside('#id')</div>
18
+ <div class="flex gap-5">
19
+ <div
20
+ id="demo"
21
+ class="bg-pink-500 text-pink-200 p-3 cursor-pointer hover:text-white rounded-xl shadow-xl mt-8"
22
+ >
23
+ <i class="lni lni-play"></i>
24
+ Display demo
25
+ </div>
26
+ <a
27
+ href="#example"
28
+ class="bg-slate-500 text-slate-200 p-3 cursor-pointer hover:text-white rounded-xl shadow-xl mt-8"
29
+ >
30
+ Show examples
31
+ </a>
32
+ </div>
18
33
  <div class="flex flex-col space-y-2 my-10">
19
34
  <div class="flex space-x-2 justify-center">
20
35
  <img src="https://badgen.net/npm/v/nanosplash" />
@@ -28,13 +43,23 @@
28
43
  />
29
44
  </div>
30
45
  </div>
31
- <div class="text-white bg-slate-700 p-3 rounded shadow-xl font-mono text-sm">ns.show('text').inside('#myDiv')</div>
46
+ <div class="flex rounded-xl text-slate-400 shadow-xl overflow-hidden">
47
+ <div data-copy class="p-3 bg-slate-800 text-slate-400 font-mono">
48
+ &lt;script src=&quot;https://unpkg.com/nanosplash/dist/iife/nanosplash.iife.js&quot;&gt;
49
+ </div>
50
+ <div id="copy" class="bg-pink-500 text-pink-200 p-3 cursor-pointer hover:text-white">Copy</div>
51
+ </div>
52
+ <div class="mt-10 flex justify-content items-center divide-x-2 text-slate-400">
53
+ <a href="https://www.npmjs.com/package/nanosplash" target="_blank"><i class="lni lni-npm text-4xl hover:text-pink-500 px-5"></i></a>
54
+ <a href="https://github.com/isakhauge/nanosplash" target="_blank"><i class="lni lni-github-original text-2xl hover:text-pink-500 px-5"></i></a>
55
+ </div>
56
+ <a class="mt-10 text-pink-300 hover:text-pink-500" href="https://rawcdn.githack.com/isakhauge/nanosplash/0a6cdef58b0e47c0cad911952b1a329df58d2c24/docs/typedoc/index.html">API Reference Documentation</a>
32
57
  </div>
33
- <div class="grid grid-cols-2 gap-1">
58
+ <div id="example" class="grid grid-cols-1 xl:grid-cols-2 gap-1">
34
59
  <div id="a" class="p-20 bg-slate-100 flex flex-col items-center">
35
60
  <div class="text-xl mb-10">Give user progress feedback</div>
36
- <div class="text-white bg-slate-800 p-5 rounded-lg shadow-2xl font-mono text-sm">
37
- ns.img(src).progress(<br>
61
+ <div class="text-white bg-slate-800 p-5 rounded-xl shadow-2xl font-mono text-sm break-all">
62
+ ns.progress(<br>
38
63
  &nbsp;&nbsp;[promise1, 'Loading A'],<br>
39
64
  &nbsp;&nbsp;[promise2, 'Loading B'],<br>
40
65
  &nbsp;&nbsp;[promise3, 'Loading C'],<br>
@@ -43,41 +68,64 @@
43
68
  </div>
44
69
  <div id="b" class="p-20 bg-slate-100 flex flex-col items-center">
45
70
  <div class="text-xl mb-10">Handle async functions</div>
46
- <div class="text-white bg-slate-800 p-5 rounded-lg shadow-2xl font-mono text-sm">
71
+ <div class="text-white bg-slate-800 p-5 rounded-xl shadow-2xl font-mono text-sm break-all">
47
72
  ns.while(promise).show('Loading ...').inside('#myDiv')
48
73
  </div>
49
74
  </div>
50
75
  <div id="c" class="p-20 bg-slate-100 flex flex-col items-center">
51
76
  <div class="text-xl mb-10">Display fullscreen</div>
52
- <div class="text-white bg-slate-800 p-5 rounded-lg shadow-2xl font-mono text-sm">
53
- const splash = ns.show('Fullscreen text')<br>
54
- splash.moveTo('#myDiv')<br>
55
- splash.remove()
77
+ <div class="text-white bg-slate-800 p-5 rounded-xl shadow-2xl font-mono text-sm break-all">
78
+ ns.show('Fullscreen text')
56
79
  </div>
57
80
  </div>
58
81
  <div id="d" class="p-20 bg-slate-100 flex flex-col items-center">
59
82
  <div class="text-xl mb-10">Display multiple, independent instances</div>
60
- <div class="text-white bg-slate-800 p-5 rounded-lg shadow-2xl font-mono text-sm">
61
- ns.img(src).show('Loading component A').inside('#a')<br>
62
- ns.show('Loading component A').inside('#b')<br>
63
- ns.show('Loading component A').inside('#c')<br>
64
- <br><br>
65
- ns.hide() // Removes instances in a FIFO manner.<br>
66
- ns.hideAll() // Removes all instances.
83
+ <div class="text-white bg-slate-800 p-5 rounded-xl shadow-2xl font-mono text-sm break-all">
84
+ ns.show('Loading component A').inside('#a')<br>
85
+ ns.show('Loading component B').inside('#b')<br>
86
+ ns.show('Loading component C').inside('#c')
87
+ </div>
88
+ </div>
89
+ <div id="e" class="p-20 bg-slate-100 flex flex-col items-center">
90
+ <div class="text-xl mb-10">Move splash instance to another destination</div>
91
+ <div class="text-white bg-slate-800 p-5 rounded-xl shadow-2xl font-mono text-sm break-all">
92
+ const splash = ns.show('Loading').inside('#a')<br>
93
+ splash.moveTo('#b')
94
+ </div>
95
+ </div>
96
+ <div id="f" class="p-20 bg-slate-100 flex flex-col items-center">
97
+ <div class="text-xl mb-10">Hide splash instances</div>
98
+ <div class="text-white bg-slate-800 p-5 rounded-xl shadow-2xl font-mono text-sm break-all">
99
+ // Hides one instance at a time, in a LIFO-fashion.<br>
100
+ ns.hide()<br><br>
101
+ // Hides all instances.<br>
102
+ ns.hideAll()
67
103
  </div>
68
104
  </div>
69
105
  </div>
70
106
  <footer>
71
107
  <script type="module" src="/src/main.ts"></script>
108
+ <script>
109
+ document.getElementById('demo').onclick = function () {
110
+ const wait = ms => new Promise(r => setTimeout(r, ms))
111
+ window.ns.progress(
112
+ [wait(2000), 'Loading data from API'],
113
+ [wait(4000), 'Restructuring'],
114
+ [wait(6000), 'Indexing'],
115
+ [wait(8000), 'Give this repo a star']
116
+ )
117
+ }
118
+ document.getElementById('copy').onclick = function () {
119
+ const text = document.querySelector('[data-copy]').innerText
120
+ navigator.clipboard.writeText(text)
121
+ alert('Copied to clipboard!')
122
+ }
123
+ </script>
72
124
  </footer>
73
125
  <style>
74
126
  body {
75
127
  min-height: 100vh;
76
128
  }
77
- .pill {
78
- font-size: 10px;
79
- padding: 0.2em 0.5em;
80
- }
81
129
  </style>
82
130
  </body>
83
131
  </html>
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nanosplash",
3
- "description": "The simple, 2KB loading screen",
4
- "version": "2.3.2",
3
+ "description": "The simple, tiny loading screen",
4
+ "version": "2.5.0",
5
5
  "author": {
6
6
  "name": "Isak K. Hauge",
7
7
  "email": "isakhauge@gmail.com",
@@ -55,19 +55,19 @@
55
55
  "test": "jest"
56
56
  },
57
57
  "devDependencies": {
58
- "@types/jest": "^27.5.0",
58
+ "@types/jest": "^28.1.1",
59
59
  "@types/jsdom": "^16.2.14",
60
60
  "autoprefixer": "^10.4.7",
61
- "cssnano": "^5.1.7",
62
- "jest": "^28.1.0",
63
- "jest-environment-jsdom": "^28.1.0",
61
+ "cssnano": "^5.1.11",
62
+ "jest": "^28.1.1",
63
+ "jest-environment-jsdom": "^28.1.1",
64
64
  "jsdom": "^19.0.0",
65
- "postcss": "^8.4.13",
66
- "sass": "^1.51.0",
67
- "ts-jest": "^28.0.2",
68
- "ts-node": "^10.7.0",
69
- "typedoc": "^0.22.15",
70
- "typescript": "^4.6.4",
71
- "vite": "^2.9.8"
65
+ "postcss": "^8.4.14",
66
+ "sass": "^1.52.3",
67
+ "ts-jest": "^28.0.4",
68
+ "ts-node": "^10.8.1",
69
+ "typedoc": "^0.22.17",
70
+ "typescript": "^4.7.3",
71
+ "vite": "^2.9.12"
72
72
  }
73
73
  }
@@ -1,69 +1,205 @@
1
1
  import '../style.sass'
2
+ import {get} from "../utilities/dom";
2
3
  import {SplashInstance} from "./SplashInstance";
3
- import {SplashJob, StrategyObject} from "../types";
4
4
  import {ShowInterface} from "../Interfaces/ShowInterface";
5
5
  import {NanosplashFactory} from "../Factory/NanosplashFactory";
6
6
  import {NanosplashInterface} from "../Interfaces/NanosplashInterface";
7
7
  import {ContextualAPIInterface} from "../Interfaces/ContextualAPIInterface";
8
+ import {InstanceIterationCallback, NanosplashOptions, SplashJob} from "../types";
8
9
 
9
- export class Nanosplash implements NanosplashInterface {
10
+ /**
11
+ * # Nanosplash
12
+ * @author Isak K. Hauge <isakhauge@gmail.com>
13
+ */
14
+ class Nanosplash implements NanosplashInterface {
10
15
  public static APP_NAME = 'Nanosplash'
16
+ private static SPINNER_DEFAULT_VISIBILITY = true
17
+ private imgSrc: string | undefined
18
+ private spinner: boolean
19
+ private fontSize: string
11
20
  public readonly instances: Map<string, SplashInstance>
12
21
 
13
- public constructor() {
22
+ /**
23
+ * Options passed to the constructor will persist in the instance.
24
+ * @param {NanosplashOptions | undefined} options Nanosplash options object.
25
+ */
26
+ public constructor(options?: NanosplashOptions) {
27
+ this.imgSrc = options?.imgSrc
28
+ this.spinner = (options?.spinner === undefined)
29
+ ? Nanosplash.SPINNER_DEFAULT_VISIBILITY
30
+ : options.spinner
31
+ this.fontSize = options?.fontSize ?? '18px'
14
32
  this.instances = new Map<string, SplashInstance>()
15
33
  }
16
34
 
17
- public img(src: string): StrategyObject {
18
- return NanosplashFactory.createImgFunction(this, new SplashInstance(this, '', src))(src)
35
+ /**
36
+ * # Set Image Source
37
+ * Set the value as undefined to display no image.
38
+ * @param {string | undefined} value
39
+ * @return Nanosplash
40
+ */
41
+ public setImgSrc(value: string | undefined): Nanosplash {
42
+ this.imgSrc = value
43
+ return this
19
44
  }
20
45
 
46
+ /**
47
+ * # Set Spinner
48
+ * Control spinner visibility.
49
+ * @param {boolean} value
50
+ * @return Nanosplash
51
+ */
52
+ public showSpinner(value: boolean): Nanosplash {
53
+ this.spinner = value
54
+ return this
55
+ }
56
+
57
+ /**
58
+ * # Set Font Size
59
+ * @param {string} value Use CSS compatible values.
60
+ * @return Nanosplash
61
+ */
62
+ public setFontSize(value: string): Nanosplash {
63
+ this.fontSize = value
64
+ return this
65
+ }
66
+
67
+ /**
68
+ * # Get Image Source
69
+ * @return string | undefined
70
+ */
71
+ public getImgSrc(): string | undefined {
72
+ return this.imgSrc
73
+ }
74
+
75
+ /**
76
+ * # Spinner Is Visible
77
+ * @return boolean
78
+ */
79
+ public spinnerIsVisible(): boolean {
80
+ return this.spinner
81
+ }
82
+
83
+ /**
84
+ * # Get Font Size
85
+ * @return string
86
+ */
87
+ public getFontSize(): string {
88
+ return this.fontSize
89
+ }
90
+
91
+ /**
92
+ * # Show
93
+ * @param {string} text
94
+ */
21
95
  public show(text: string): ContextualAPIInterface {
22
- return NanosplashFactory.createShowFunction(this, new SplashInstance(this, text))(text)
96
+ return NanosplashFactory.createShowFunction(this, new SplashInstance(this, text, this.imgSrc))(text)
23
97
  }
24
98
 
99
+ /**
100
+ * # Progress
101
+ * @param {SplashJob[]} jobs An array or list of Splash jobs.
102
+ * @return ContextualAPIInterface
103
+ */
25
104
  public progress(...jobs: SplashJob[]): ContextualAPIInterface {
26
- return NanosplashFactory.createProgressFunction(this, new SplashInstance(this, ''))(...jobs)
105
+ return NanosplashFactory.createProgressFunction(this, new SplashInstance(this, '', this.imgSrc))(...jobs)
27
106
  }
28
107
 
108
+ /**
109
+ * # While
110
+ * @param {Promise<any>} asyncTask An asynchronous function or Promise.
111
+ * @return ContextualAPIInterface
112
+ */
29
113
  public while(asyncTask: Promise<any>): ShowInterface {
30
- return NanosplashFactory.createWhileFunction(this, new SplashInstance(this, ''))(asyncTask)
114
+ return NanosplashFactory.createWhileFunction(this, new SplashInstance(this, '', this.imgSrc))(asyncTask)
31
115
  }
32
116
 
33
- private fifoIterate(callback: (id: string, splashInstance: SplashInstance, i: number) => boolean): void {
34
- let i = 0
35
- const instanceEntries = this.instances.entries()
36
- for (const [id, splashInstance] of instanceEntries) {
37
- if (!callback(id, splashInstance, i++)) {
117
+ /**
118
+ * # LIFO Iterate
119
+ * This function will iterate through the splash instances in a LIFO (last in, first out) manner, invoke the
120
+ * callback for each instance and pass the instance as an argument.
121
+ * @param {InstanceIterationCallback} callback This callback will be invoked for each instance.
122
+ * @private
123
+ */
124
+ private lifoIterate(callback: InstanceIterationCallback): void {
125
+ const n = this.instances.size
126
+ const instances = Array.from(this.instances.values())
127
+ for (let i = n - 1; i >= 0; i--) {
128
+ const instance = instances[i]
129
+ const id = instance.getId()
130
+ if (!callback(id, instance, i)) {
38
131
  break
39
132
  }
40
133
  }
41
134
  }
42
135
 
136
+ /**
137
+ * # Delete
138
+ * @param {SplashInstance} splashInstance
139
+ */
43
140
  public delete(splashInstance: SplashInstance): void {
44
141
  this.instances.delete(splashInstance.getId())
45
142
  }
46
143
 
144
+ /**
145
+ * # Hide All
146
+ * Delete all Splash instances.
147
+ */
47
148
  public hideAll(): void
48
149
  {
49
- this.instances.forEach(
50
- (instance: SplashInstance) => instance.delete()
51
- )
150
+ this.instances.forEach((instance: SplashInstance) => instance.delete())
52
151
  }
53
152
 
54
- public hide(id?: string): void {
55
- if (id) {
56
- const splashInstance = this.instances.get(id)
153
+ /**
154
+ * # Hide
155
+ * Delete specific Splash instance or the latest one. There are three different ways of using this function.
156
+ *
157
+ * ## Use SplashInstance IDs
158
+ * Every new splash instance is given a unique, randomized ID. This ID is easily retrieved by using the getId
159
+ * method.
160
+ * <br>
161
+ * <code>
162
+ * const splashInstance = ns.show('Loading');<br>
163
+ * ns.hide(splashInstance.getId());
164
+ * </code>
165
+ * <br>
166
+ *
167
+ * ## Use CSS Selector
168
+ * Pass the selector referring to the element in which the splash instance resides.
169
+ * <br>
170
+ * <code>
171
+ * ns.show('Loading').inside('#my-div');<br>
172
+ * ns.hide('#my-div');
173
+ * </code>
174
+ * <br>
175
+ *
176
+ * ## Pass no argument
177
+ * When calling the hide method without passing arguments, it will delete the latest splash instance added to the
178
+ * DOM, in other words, in a LIFO fashion.<br>
179
+ * <code>
180
+ * ns.show('Loading');<br>
181
+ * ns.hide();
182
+ * </code>
183
+ * <br>
184
+ * @param idOrSelector This can either be a SplashInstance ID or a CSS selector referring to the element in which
185
+ * the instance is residing.
186
+ */
187
+ public hide(idOrSelector?: string): void {
188
+ if (idOrSelector) {
189
+ const splashInstance = this.instances.get(idOrSelector)
57
190
  if (splashInstance) {
58
191
  splashInstance.delete()
59
192
  } else {
60
- throw new Error(
61
- `Could not find element with id: ${id}`
62
- )
193
+ Array.from(this.instances.values())
194
+ .filter((splashInstance: SplashInstance) => {
195
+ return splashInstance.getDestination() === get<HTMLElement>(idOrSelector)
196
+ })
197
+ .forEach((splashInstance: SplashInstance) => splashInstance.delete())
63
198
  }
64
199
  } else {
65
- this.fifoIterate((_: string, splashInstance: SplashInstance, i: number) => {
66
- const remove = i === 0
200
+ const n = this.instances.size
201
+ this.lifoIterate((_: string, splashInstance: SplashInstance, i: number) => {
202
+ const remove = i === n - 1
67
203
  if (remove) {
68
204
  splashInstance.delete()
69
205
  }
@@ -71,4 +207,17 @@ export class Nanosplash implements NanosplashInterface {
71
207
  })
72
208
  }
73
209
  }
210
+
211
+ /**
212
+ * # Get From Destination Node
213
+ * Returns Splash instances having the given destination node.
214
+ * @param {HTMLElement} node
215
+ */
216
+ public getFromDestinationNode(node: HTMLElement): SplashInstance[]
217
+ {
218
+ const fnSameDestinationNode = (v: SplashInstance) => v.getDestination() === node
219
+ return Array.from(this.instances.values()).filter(fnSameDestinationNode)
220
+ }
74
221
  }
222
+
223
+ export default Nanosplash