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