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