title-switcher 2.1.14 → 2.1.17
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/LICENSE.md +0 -0
- package/README.md +14 -0
- package/browser/titleSwitcher.js +1613 -5547
- package/browser/titleSwitcher.min.js +1 -1
- package/dist/main.d.ts +80 -90
- package/dist/main.js +409 -401
- package/dist/main.min.js +1 -1
- package/package.json +5 -8
package/dist/main.js
CHANGED
|
@@ -1,401 +1,409 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
require('core-js/modules/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
*
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
__classPrivateFieldSet(this,
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
if (
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
self.
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
}
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
require('core-js/modules/esnext.weak-map.delete-all.js')
|
|
4
|
+
const __classPrivateFieldSet = void 0 && (void 0).__classPrivateFieldSet || function (receiver, state, value, kind, f) {
|
|
5
|
+
if (kind === 'm') throw new TypeError('Private method is not writable')
|
|
6
|
+
if (kind === 'a' && !f) throw new TypeError('Private accessor was defined without a setter')
|
|
7
|
+
if (typeof state === 'function' ? receiver !== state || !f : !state.has(receiver)) throw new TypeError('Cannot write private member to an object whose class did not declare it')
|
|
8
|
+
return kind === 'a' ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value), value
|
|
9
|
+
}
|
|
10
|
+
const __classPrivateFieldGet = void 0 && (void 0).__classPrivateFieldGet || function (receiver, state, kind, f) {
|
|
11
|
+
if (kind === 'a' && !f) throw new TypeError('Private accessor was defined without a getter')
|
|
12
|
+
if (typeof state === 'function' ? receiver !== state || !f : !state.has(receiver)) throw new TypeError('Cannot read private member from an object whose class did not declare it')
|
|
13
|
+
return kind === 'm' ? f : kind === 'a' ? f.call(receiver) : f ? f.value : state.get(receiver)
|
|
14
|
+
}
|
|
15
|
+
let _TitleSwitcher_active, _TitleSwitcher_currentClass, _TitleSwitcher_currentIndex, _TitleSwitcher_delayEffect, _TitleSwitcher_delaySwitch, _TitleSwitcher_isRandom, _TitleSwitcher_titles, _TitleSwitcher_titlesContainer, _TitleSwitcher_switchStyle, _TitleSwitcher_typeSurface
|
|
16
|
+
Object.defineProperty(exports, '__esModule', {
|
|
17
|
+
value: true
|
|
18
|
+
})
|
|
19
|
+
class TitleSwitcher {
|
|
20
|
+
/**
|
|
21
|
+
* Instantiate this as a class to get an instance of TitleSwitcher
|
|
22
|
+
* @param titlesContainer - The selector where titles are stored
|
|
23
|
+
* @param switchStyle - The function or function name for the effect to apply
|
|
24
|
+
* @constructor
|
|
25
|
+
*/
|
|
26
|
+
constructor (titlesContainer = '', switchStyle = 'typingEffect') {
|
|
27
|
+
_TitleSwitcher_active.set(this, false)
|
|
28
|
+
_TitleSwitcher_currentClass.set(this, 'displayTitle')
|
|
29
|
+
_TitleSwitcher_currentIndex.set(this, 0)
|
|
30
|
+
_TitleSwitcher_delayEffect.set(this, 200)
|
|
31
|
+
_TitleSwitcher_delaySwitch.set(this, 400)
|
|
32
|
+
_TitleSwitcher_isRandom.set(this, false)
|
|
33
|
+
_TitleSwitcher_titles.set(this, [])
|
|
34
|
+
_TitleSwitcher_titlesContainer.set(this, '')
|
|
35
|
+
_TitleSwitcher_switchStyle.set(this, void 0)
|
|
36
|
+
_TitleSwitcher_typeSurface.set(this, void 0)
|
|
37
|
+
__classPrivateFieldSet(this, _TitleSwitcher_currentClass, 'displayTitle', 'f')
|
|
38
|
+
if (typeof switchStyle === 'string') {
|
|
39
|
+
// @ts-ignore Obnoxious error "type 'string' can't be used to index type 'TitleSwitcher'"
|
|
40
|
+
__classPrivateFieldSet(this, _TitleSwitcher_switchStyle, typeof this[switchStyle] === 'function' ? this[switchStyle] : this.typingEffect, 'f')
|
|
41
|
+
} else {
|
|
42
|
+
__classPrivateFieldSet(this, _TitleSwitcher_switchStyle, switchStyle, 'f')
|
|
43
|
+
}
|
|
44
|
+
__classPrivateFieldSet(this, _TitleSwitcher_titlesContainer, titlesContainer, 'f')
|
|
45
|
+
__classPrivateFieldSet(this, _TitleSwitcher_titles, [], 'f')
|
|
46
|
+
const foundContainers = titlesContainer ? document.querySelectorAll(titlesContainer) : []
|
|
47
|
+
if (foundContainers && foundContainers[0]) {
|
|
48
|
+
__classPrivateFieldSet(this, _TitleSwitcher_titlesContainer, foundContainers[0], 'f')
|
|
49
|
+
__classPrivateFieldSet(this, _TitleSwitcher_titles, foundContainers[0].children, 'f')
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Retrieve active
|
|
55
|
+
*/
|
|
56
|
+
get active () {
|
|
57
|
+
return __classPrivateFieldGet(this, _TitleSwitcher_active, 'f')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Retrieve currentClass
|
|
62
|
+
*/
|
|
63
|
+
get currentClass () {
|
|
64
|
+
return __classPrivateFieldGet(this, _TitleSwitcher_currentClass, 'f')
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Retrieve currentIndex
|
|
69
|
+
*/
|
|
70
|
+
get currentIndex () {
|
|
71
|
+
return __classPrivateFieldGet(this, _TitleSwitcher_currentIndex, 'f')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Retrieve delayEffect
|
|
76
|
+
*/
|
|
77
|
+
get delayEffect () {
|
|
78
|
+
return __classPrivateFieldGet(this, _TitleSwitcher_delayEffect, 'f')
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Retrieve delaySwitch
|
|
83
|
+
*/
|
|
84
|
+
get delaySwitch () {
|
|
85
|
+
return __classPrivateFieldGet(this, _TitleSwitcher_delaySwitch, 'f')
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Retrieve switchStyle
|
|
90
|
+
*/
|
|
91
|
+
get switchStyle () {
|
|
92
|
+
return __classPrivateFieldGet(this, _TitleSwitcher_switchStyle, 'f')
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Retrieve list of titles DOM elements
|
|
97
|
+
*/
|
|
98
|
+
get titles () {
|
|
99
|
+
return __classPrivateFieldGet(this, _TitleSwitcher_titles, 'f')
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Retrieve typeSurface used
|
|
104
|
+
*/
|
|
105
|
+
get typeSurface () {
|
|
106
|
+
return __classPrivateFieldGet(this, _TitleSwitcher_typeSurface, 'f')
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* This is the function to begin the switching titles
|
|
111
|
+
* @param settings
|
|
112
|
+
* @param settings.delaySwitch
|
|
113
|
+
* @param settings.delayEffect
|
|
114
|
+
* @param settings.isRandom
|
|
115
|
+
* @param settings.immediatePause
|
|
116
|
+
*/
|
|
117
|
+
startTitles ({
|
|
118
|
+
delaySwitch = 400,
|
|
119
|
+
delayEffect = 200,
|
|
120
|
+
isRandom = false,
|
|
121
|
+
immediatePause = false
|
|
122
|
+
} = {}) {
|
|
123
|
+
const typeSurface = 'typeSurface'
|
|
124
|
+
__classPrivateFieldSet(this, _TitleSwitcher_delaySwitch, delaySwitch, 'f')
|
|
125
|
+
__classPrivateFieldSet(this, _TitleSwitcher_delayEffect, delayEffect, 'f')
|
|
126
|
+
__classPrivateFieldSet(this, _TitleSwitcher_isRandom, isRandom, 'f')
|
|
127
|
+
__classPrivateFieldSet(this, _TitleSwitcher_active, !immediatePause, 'f')
|
|
128
|
+
if (__classPrivateFieldGet(this, _TitleSwitcher_currentIndex, 'f') >= __classPrivateFieldGet(this, _TitleSwitcher_titles, 'f').length || typeof __classPrivateFieldGet(this, _TitleSwitcher_titlesContainer, 'f') === 'string') {
|
|
129
|
+
__classPrivateFieldSet(this, _TitleSwitcher_active, false, 'f')
|
|
130
|
+
console.warn(`No titles found for '${__classPrivateFieldGet(this, _TitleSwitcher_titlesContainer, 'f')}'`)
|
|
131
|
+
return this
|
|
132
|
+
}
|
|
133
|
+
if (__classPrivateFieldGet(this, _TitleSwitcher_isRandom, 'f')) {
|
|
134
|
+
__classPrivateFieldSet(this, _TitleSwitcher_currentIndex, Math.round(Math.random() * (__classPrivateFieldGet(this, _TitleSwitcher_titles, 'f').length - 2)) + 1, 'f')
|
|
135
|
+
}
|
|
136
|
+
const currentTitle = __classPrivateFieldGet(this, _TitleSwitcher_titles, 'f')[__classPrivateFieldGet(this, _TitleSwitcher_currentIndex, 'f')]
|
|
137
|
+
if (currentTitle.classList) {
|
|
138
|
+
currentTitle.classList.add(__classPrivateFieldGet(this, _TitleSwitcher_currentClass, 'f'))
|
|
139
|
+
} else {
|
|
140
|
+
currentTitle.className += ' ' + __classPrivateFieldGet(this, _TitleSwitcher_currentClass, 'f')
|
|
141
|
+
}
|
|
142
|
+
// @ts-ignore The Node returned is of type Element, or it should be
|
|
143
|
+
const typeElement = __classPrivateFieldGet(this, _TitleSwitcher_titles, 'f')[0].cloneNode(true)
|
|
144
|
+
if (typeElement.classList) {
|
|
145
|
+
typeElement.classList.add(typeSurface)
|
|
146
|
+
} else {
|
|
147
|
+
typeElement.className += ' ' + typeSurface
|
|
148
|
+
}
|
|
149
|
+
if (typeElement.classList) {
|
|
150
|
+
typeElement.classList.remove(__classPrivateFieldGet(this, _TitleSwitcher_currentClass, 'f'))
|
|
151
|
+
} else {
|
|
152
|
+
typeElement.className = typeElement.className.replace(new RegExp('(^|\\b)' + typeElement.className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ')
|
|
153
|
+
}
|
|
154
|
+
__classPrivateFieldGet(this, _TitleSwitcher_titlesContainer, 'f').insertBefore(typeElement, __classPrivateFieldGet(this, _TitleSwitcher_titlesContainer, 'f').firstChild)
|
|
155
|
+
// @ts-ignore the returned Node is a type of HTMLElement
|
|
156
|
+
__classPrivateFieldSet(this, _TitleSwitcher_typeSurface, __classPrivateFieldGet(this, _TitleSwitcher_titlesContainer, 'f').querySelectorAll('.' + typeSurface)[0], 'f')
|
|
157
|
+
__classPrivateFieldGet(this, _TitleSwitcher_typeSurface, 'f').innerHTML = ''
|
|
158
|
+
__classPrivateFieldGet(this, _TitleSwitcher_typeSurface, 'f').style.display = 'block'
|
|
159
|
+
Array.prototype.forEach.call(__classPrivateFieldGet(this, _TitleSwitcher_titles, 'f'), function (title) {
|
|
160
|
+
title.style.display = 'none'
|
|
161
|
+
})
|
|
162
|
+
// currentTitle, not this.#titles[this.#currentIndex]: inserting typeElement above shifted every later index by
|
|
163
|
+
// one, so #currentIndex (computed before that insert) no longer points at the title it was set for - switchTitle
|
|
164
|
+
// would remove the 'displayTitle' class from whatever title now happens to sit at that stale index instead of
|
|
165
|
+
// from currentTitle, leaving more than one title carrying the class at once.
|
|
166
|
+
return this.switchTitle(currentTitle, __classPrivateFieldGet(this, _TitleSwitcher_switchStyle, 'f'), this)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* This is the function to pause between switching
|
|
171
|
+
*/
|
|
172
|
+
pause () {
|
|
173
|
+
__classPrivateFieldSet(this, _TitleSwitcher_active, false, 'f')
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* This is the function to resume after a pause.
|
|
178
|
+
*/
|
|
179
|
+
resume () {
|
|
180
|
+
if (!__classPrivateFieldGet(this, _TitleSwitcher_active, 'f')) {
|
|
181
|
+
__classPrivateFieldSet(this, _TitleSwitcher_active, true, 'f')
|
|
182
|
+
this.switchTitle(__classPrivateFieldGet(this, _TitleSwitcher_titles, 'f')[__classPrivateFieldGet(this, _TitleSwitcher_currentIndex, 'f')], __classPrivateFieldGet(this, _TitleSwitcher_switchStyle, 'f'), this)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* This is the core function for switching titles
|
|
188
|
+
* @param currentTitle
|
|
189
|
+
* @param callBackFunction
|
|
190
|
+
* @param self
|
|
191
|
+
* @param runOnce
|
|
192
|
+
*/
|
|
193
|
+
switchTitle (currentTitle, callBackFunction, self, runOnce = false) {
|
|
194
|
+
self = self || this
|
|
195
|
+
let currentIndex = 1
|
|
196
|
+
const size = __classPrivateFieldGet(self, _TitleSwitcher_titles, 'f').length
|
|
197
|
+
for (let i = 1; i < size; ++i) {
|
|
198
|
+
if (__classPrivateFieldGet(self, _TitleSwitcher_titles, 'f')[i] === currentTitle) {
|
|
199
|
+
currentIndex = i
|
|
200
|
+
break
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (!__classPrivateFieldGet(self, _TitleSwitcher_active, 'f')) {
|
|
204
|
+
__classPrivateFieldSet(self, _TitleSwitcher_currentIndex, currentIndex, 'f')
|
|
205
|
+
return self
|
|
206
|
+
}
|
|
207
|
+
const maxIndex = __classPrivateFieldGet(self, _TitleSwitcher_titles, 'f').length - 1
|
|
208
|
+
let nextIndex = 1
|
|
209
|
+
if (maxIndex === 1) {
|
|
210
|
+
return callBackFunction(currentTitle, runOnce ? () => self : self.switchTitle, self, runOnce)
|
|
211
|
+
}
|
|
212
|
+
if (__classPrivateFieldGet(self, _TitleSwitcher_isRandom, 'f')) {
|
|
213
|
+
if (!self.typeSurface.textContent.trim()) {
|
|
214
|
+
currentIndex = -1
|
|
215
|
+
}
|
|
216
|
+
do {
|
|
217
|
+
nextIndex = Math.round(Math.random() * (maxIndex - 1)) + 1
|
|
218
|
+
} while (nextIndex === currentIndex)
|
|
219
|
+
} else {
|
|
220
|
+
if (!self.typeSurface.textContent.trim()) {
|
|
221
|
+
currentIndex = maxIndex
|
|
222
|
+
}
|
|
223
|
+
nextIndex = currentIndex < maxIndex ? currentIndex + 1 : 1
|
|
224
|
+
}
|
|
225
|
+
const nextTitle = __classPrivateFieldGet(self, _TitleSwitcher_titles, 'f')[nextIndex]
|
|
226
|
+
if (currentTitle.classList) {
|
|
227
|
+
currentTitle.classList.remove(self.currentClass)
|
|
228
|
+
} else {
|
|
229
|
+
currentTitle.className = currentTitle.className.replace(new RegExp('(^|\\b)' + currentTitle.className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ')
|
|
230
|
+
}
|
|
231
|
+
if (nextTitle.classList) {
|
|
232
|
+
nextTitle.classList.add(self.currentClass)
|
|
233
|
+
} else {
|
|
234
|
+
nextTitle.className += ' ' + self.currentClass
|
|
235
|
+
}
|
|
236
|
+
return callBackFunction(nextTitle, runOnce ? () => self : self.switchTitle, self, runOnce)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
_TitleSwitcher_active = new WeakMap(), _TitleSwitcher_currentClass = new WeakMap(), _TitleSwitcher_currentIndex = new WeakMap(), _TitleSwitcher_delayEffect = new WeakMap(), _TitleSwitcher_delaySwitch = new WeakMap(), _TitleSwitcher_isRandom = new WeakMap(), _TitleSwitcher_titles = new WeakMap(), _TitleSwitcher_titlesContainer = new WeakMap(), _TitleSwitcher_switchStyle = new WeakMap(), _TitleSwitcher_typeSurface = new WeakMap()
|
|
240
|
+
/**
|
|
241
|
+
* This is a helper function to improve the default 'typingEffect'
|
|
242
|
+
* @param blinkOn
|
|
243
|
+
* @param self
|
|
244
|
+
*/
|
|
245
|
+
TitleSwitcher.prototype.cursorBlink = (blinkOn, self) => {
|
|
246
|
+
// display cursor effect
|
|
247
|
+
self = self || void 0
|
|
248
|
+
if (blinkOn) {
|
|
249
|
+
self.typeSurface.innerHTML = self.typeSurface.innerHTML.replace(/\|| *(<\/span>)?$/, '$1').trim()
|
|
250
|
+
self.typeSurface.innerHTML = self.typeSurface.innerHTML + '<span style="display: inline-block;font-weight: normal; color: black; text-decoration: none">|</span>'
|
|
251
|
+
} else {
|
|
252
|
+
self.typeSurface.innerHTML = self.typeSurface.innerHTML.replace(/\|(<\/span>)$/, ' $1')
|
|
253
|
+
}
|
|
254
|
+
return self
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Count how many typed "units" domObject's text takes to fully reveal via typedPartialHtml - one unit per
|
|
258
|
+
* non-whitespace character, but a whole run of consecutive whitespace (most often the newline and indentation
|
|
259
|
+
* left behind where a title's HTML source wraps onto a new line) counts as a single unit, matching how many
|
|
260
|
+
* steps typedPartialHtml actually needs to reveal it all. Typing through that whitespace one character at a
|
|
261
|
+
* time would type through several units a browser renders as a single collapsed space, with no visible change
|
|
262
|
+
* in between - not a slower typing speed, but a real stall followed by the next word suddenly catching up.
|
|
263
|
+
* @param domObject
|
|
264
|
+
*/
|
|
265
|
+
const typedLength = domObject => {
|
|
266
|
+
let length = 0
|
|
267
|
+
Array.prototype.forEach.call(domObject.childNodes, child => {
|
|
268
|
+
if (child.nodeType === 3) {
|
|
269
|
+
const text = child.textContent || ''
|
|
270
|
+
let position = 0
|
|
271
|
+
while (position < text.length) {
|
|
272
|
+
if (/\s/.test(text[position])) {
|
|
273
|
+
while (position < text.length && /\s/.test(text[position])) {
|
|
274
|
+
++position
|
|
275
|
+
}
|
|
276
|
+
} else {
|
|
277
|
+
++position
|
|
278
|
+
}
|
|
279
|
+
++length
|
|
280
|
+
}
|
|
281
|
+
} else if (child.nodeType === 1) {
|
|
282
|
+
length += typedLength(child)
|
|
283
|
+
}
|
|
284
|
+
})
|
|
285
|
+
return length
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Build the markup for the first `remaining` typed units of domObject's text (see typedLength), keeping
|
|
289
|
+
* whichever of its nested tags (em, strong, ...) that content falls under. This lets the typing effect reveal
|
|
290
|
+
* a title's formatting as each character is typed, instead of only applying it once the whole tag has been
|
|
291
|
+
* typed out.
|
|
292
|
+
* @param domObject
|
|
293
|
+
* @param remaining
|
|
294
|
+
*/
|
|
295
|
+
const typedPartialHtml = (domObject, remaining) => {
|
|
296
|
+
let html = ''
|
|
297
|
+
const children = Array.prototype.slice.call(domObject.childNodes)
|
|
298
|
+
for (let i = 0; i < children.length && remaining > 0; ++i) {
|
|
299
|
+
const child = children[i]
|
|
300
|
+
if (child.nodeType === 3) {
|
|
301
|
+
const text = child.textContent || ''
|
|
302
|
+
let position = 0
|
|
303
|
+
let taken = ''
|
|
304
|
+
while (position < text.length && remaining > 0) {
|
|
305
|
+
if (/\s/.test(text[position])) {
|
|
306
|
+
const runStart = position
|
|
307
|
+
while (position < text.length && /\s/.test(text[position])) {
|
|
308
|
+
++position
|
|
309
|
+
}
|
|
310
|
+
taken += text.slice(runStart, position)
|
|
311
|
+
} else {
|
|
312
|
+
taken += text[position]
|
|
313
|
+
++position
|
|
314
|
+
}
|
|
315
|
+
--remaining
|
|
316
|
+
}
|
|
317
|
+
html += taken
|
|
318
|
+
} else if (child.nodeType === 1) {
|
|
319
|
+
const result = typedPartialHtml(child, remaining)
|
|
320
|
+
if (result.html) {
|
|
321
|
+
const wrapper = child.cloneNode(false)
|
|
322
|
+
wrapper.innerHTML = result.html
|
|
323
|
+
html += wrapper.outerHTML
|
|
324
|
+
}
|
|
325
|
+
remaining = result.remaining
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return {
|
|
329
|
+
html,
|
|
330
|
+
remaining
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* This is the default and example of an effect being implemented when Titles are switched
|
|
335
|
+
* These functions take the currentElement in focus, the switchTitle function as a callback
|
|
336
|
+
* and an instance of the TitleSwitcher
|
|
337
|
+
* @param domObject
|
|
338
|
+
* @param callBackFunction
|
|
339
|
+
* @param self
|
|
340
|
+
* @param runOnce
|
|
341
|
+
*/
|
|
342
|
+
TitleSwitcher.prototype.typingEffect = (domObject, callBackFunction, self, runOnce = false) => {
|
|
343
|
+
self = self || void 0
|
|
344
|
+
const size = self.titles.length
|
|
345
|
+
let currentIndex = 0
|
|
346
|
+
for (let i = 1; i < size; ++i) {
|
|
347
|
+
if (self.titles[i] === domObject) {
|
|
348
|
+
currentIndex = i
|
|
349
|
+
break
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
domObject = domObject || self.titles[currentIndex + 1]
|
|
353
|
+
let blinkOn = true
|
|
354
|
+
const numBlinks = 4
|
|
355
|
+
if (self.typeSurface.hasAttribute('style')) {
|
|
356
|
+
self.typeSurface.removeAttribute('style')
|
|
357
|
+
}
|
|
358
|
+
if (domObject.hasAttribute('style')) {
|
|
359
|
+
self.typeSurface.setAttribute('style', domObject.getAttribute('style'))
|
|
360
|
+
}
|
|
361
|
+
// If we copied the title style, then display:none is set, so we need to ensure the surface is display:block
|
|
362
|
+
self.typeSurface.innerHTML = ''
|
|
363
|
+
self.typeSurface.style.display = 'block'
|
|
364
|
+
// Initialize with a few cursor blinks
|
|
365
|
+
for (let i = 0; i < numBlinks; ++i) {
|
|
366
|
+
setTimeout(() => {
|
|
367
|
+
self.cursorBlink(blinkOn, self)
|
|
368
|
+
blinkOn = !blinkOn
|
|
369
|
+
}, i * self.delaySwitch)
|
|
370
|
+
}
|
|
371
|
+
setTimeout(() => {
|
|
372
|
+
// Empty the surface, and display the cursor (cursor is always solid while typing / not flashing)
|
|
373
|
+
self.typeSurface.innerHTML = ''
|
|
374
|
+
self.cursorBlink(true, self)
|
|
375
|
+
// Copy each letter from the current title, keeping whichever tags (em, strong, ...) it falls under
|
|
376
|
+
const totalLength = typedLength(domObject)
|
|
377
|
+
for (let i = 0; i < totalLength; ++i) {
|
|
378
|
+
setTimeout(() => {
|
|
379
|
+
// Reveal one more character, wrapped in whatever tags its position in the title falls under,
|
|
380
|
+
// then append a formatted cursor on the end
|
|
381
|
+
self.typeSurface.innerHTML = typedPartialHtml(domObject, i + 1).html + '<span style="font-weight: normal; color: black; text-decoration: none">|</span>'
|
|
382
|
+
// If the text content equals the title content with a cursor appended then we reached the end.
|
|
383
|
+
if (domObject.textContent + '|' === self.typeSurface.textContent) {
|
|
384
|
+
// Replace html with old html on last letter, so we get all the html formatting applied
|
|
385
|
+
self.typeSurface.innerHTML = domObject.innerHTML + '<span style="font-weight: normal; color: black; text-decoration: none">|</span>'
|
|
386
|
+
// Run the blinking cursor two times the regular time in order to let the text be readable before switching
|
|
387
|
+
for (let j = 0; j < numBlinks * 2; ++j) {
|
|
388
|
+
setTimeout(() => {
|
|
389
|
+
--j
|
|
390
|
+
self.cursorBlink(blinkOn, self)
|
|
391
|
+
if (j === 0) {
|
|
392
|
+
callBackFunction(domObject, runOnce ? () => self : self.switchStyle, self, runOnce)
|
|
393
|
+
}
|
|
394
|
+
}, j * self.delaySwitch)
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}, i * self.delayEffect)
|
|
398
|
+
}
|
|
399
|
+
}, numBlinks * self.delaySwitch)
|
|
400
|
+
return self
|
|
401
|
+
}
|
|
402
|
+
exports.default = TitleSwitcher
|
|
403
|
+
if (void 0) {
|
|
404
|
+
// @ts-ignore 'this' is used in node as the global, and the key CAN be referenced by string
|
|
405
|
+
(void 0).TitleSwitcher = TitleSwitcher
|
|
406
|
+
} else if (typeof window !== 'undefined') {
|
|
407
|
+
// @ts-ignore YES, we can use a string to add a property to Window
|
|
408
|
+
window.TitleSwitcher = TitleSwitcher
|
|
409
|
+
}
|