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/dist/main.js CHANGED
@@ -1,401 +1,409 @@
1
- 'use strict'
2
-
3
- require('core-js/modules/es.symbol.js')
4
- require('core-js/modules/es.symbol.description.js')
5
- require('core-js/modules/es.symbol.iterator.js')
6
- require('core-js/modules/es.symbol.to-primitive.js')
7
- require('core-js/modules/es.date.to-primitive.js')
8
- require('core-js/modules/es.number.constructor.js')
9
- function _typeof (o) { '@babel/helpers - typeof'; return _typeof = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ? function (o) { return typeof o } : function (o) { return o && typeof Symbol === 'function' && o.constructor === Symbol && o !== Symbol.prototype ? 'symbol' : typeof o }, _typeof(o) }
10
- require('core-js/modules/es.array.for-each.js')
11
- require('core-js/modules/es.array.iterator.js')
12
- require('core-js/modules/es.array.join.js')
13
- require('core-js/modules/es.object.define-property.js')
14
- require('core-js/modules/es.object.to-string.js')
15
- require('core-js/modules/es.regexp.constructor.js')
16
- require('core-js/modules/es.regexp.exec.js')
17
- require('core-js/modules/es.regexp.sticky.js')
18
- require('core-js/modules/es.regexp.to-string.js')
19
- require('core-js/modules/es.string.iterator.js')
20
- require('core-js/modules/es.string.replace.js')
21
- require('core-js/modules/es.string.trim.js')
22
- require('core-js/modules/es.weak-map.js')
23
- require('core-js/modules/esnext.iterator.constructor.js')
24
- require('core-js/modules/esnext.iterator.for-each.js')
25
- require('core-js/modules/esnext.weak-map.delete-all.js')
26
- require('core-js/modules/web.dom-collections.for-each.js')
27
- require('core-js/modules/web.dom-collections.iterator.js')
28
- require('core-js/modules/web.timers.js')
29
- var _this = void 0
30
- function _classCallCheck (a, n) { if (!(a instanceof n)) throw new TypeError('Cannot call a class as a function') }
31
- function _defineProperties (e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, 'value' in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o) } }
32
- function _createClass (e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, 'prototype', { writable: !1 }), e }
33
- function _toPropertyKey (t) { var i = _toPrimitive(t, 'string'); return _typeof(i) == 'symbol' ? i : i + '' }
34
- function _toPrimitive (t, r) { if (_typeof(t) != 'object' || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || 'default'); if (_typeof(i) != 'object') return i; throw new TypeError('@@toPrimitive must return a primitive value.') } return (r === 'string' ? String : Number)(t) }
35
- var __classPrivateFieldSet = void 0 && (void 0).__classPrivateFieldSet || function (receiver, state, value, kind, f) {
36
- if (kind === 'm') throw new TypeError('Private method is not writable')
37
- if (kind === 'a' && !f) throw new TypeError('Private accessor was defined without a setter')
38
- 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')
39
- return kind === 'a' ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value), value
40
- }
41
- var __classPrivateFieldGet = void 0 && (void 0).__classPrivateFieldGet || function (receiver, state, kind, f) {
42
- if (kind === 'a' && !f) throw new TypeError('Private accessor was defined without a getter')
43
- 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')
44
- return kind === 'm' ? f : kind === 'a' ? f.call(receiver) : f ? f.value : state.get(receiver)
45
- }
46
- var _TitleSwitcher_active, _TitleSwitcher_currentClass, _TitleSwitcher_currentIndex, _TitleSwitcher_delayEffect, _TitleSwitcher_delaySwitch, _TitleSwitcher_isRandom, _TitleSwitcher_titles, _TitleSwitcher_titlesContainer, _TitleSwitcher_switchStyle, _TitleSwitcher_typeSurface
47
- Object.defineProperty(exports, '__esModule', {
48
- value: true
49
- })
50
- var TitleSwitcher = /* #__PURE__ */(function () {
51
- /**
52
- * Instantiate this as a class to get an instance of TitleSwitcher
53
- * @param {string} [titlesContainer=''] - The selector where titles are stored
54
- * @param {Function|string} [switchStyle='typingEffect'] - The function or function name for the effect to apply
55
- * @constructor
56
- */
57
- function TitleSwitcher () {
58
- var titlesContainer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''
59
- var switchStyle = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'typingEffect'
60
- _classCallCheck(this, TitleSwitcher)
61
- _TitleSwitcher_active.set(this, false)
62
- _TitleSwitcher_currentClass.set(this, 'displayTitle')
63
- _TitleSwitcher_currentIndex.set(this, 0)
64
- _TitleSwitcher_delayEffect.set(this, 200)
65
- _TitleSwitcher_delaySwitch.set(this, 400)
66
- _TitleSwitcher_isRandom.set(this, false)
67
- _TitleSwitcher_titles.set(this, [])
68
- _TitleSwitcher_titlesContainer.set(this, '')
69
- _TitleSwitcher_switchStyle.set(this, void 0)
70
- _TitleSwitcher_typeSurface.set(this, void 0)
71
- __classPrivateFieldSet(this, _TitleSwitcher_currentClass, 'displayTitle', 'f')
72
- if (typeof switchStyle === 'string') {
73
- // @ts-ignore Obnoxious error "type 'string' can't be used to index type 'TitleSwitcher'"
74
- __classPrivateFieldSet(this, _TitleSwitcher_switchStyle, typeof this[switchStyle] === 'function' ? this[switchStyle] : this.typingEffect, 'f')
75
- } else {
76
- __classPrivateFieldSet(this, _TitleSwitcher_switchStyle, switchStyle, 'f')
77
- }
78
- __classPrivateFieldSet(this, _TitleSwitcher_titlesContainer, titlesContainer, 'f')
79
- __classPrivateFieldSet(this, _TitleSwitcher_titles, [], 'f')
80
- var foundContainers = titlesContainer ? document.querySelectorAll(titlesContainer) : []
81
- if (foundContainers && foundContainers[0]) {
82
- __classPrivateFieldSet(this, _TitleSwitcher_titlesContainer, foundContainers[0], 'f')
83
- __classPrivateFieldSet(this, _TitleSwitcher_titles, foundContainers[0].children, 'f')
84
- }
85
- }
86
- /**
87
- * Retrieve active
88
- * @return {boolean}
89
- */
90
- return _createClass(TitleSwitcher, [{
91
- key: 'active',
92
- get: function get () {
93
- return __classPrivateFieldGet(this, _TitleSwitcher_active, 'f')
94
- }
95
- /**
96
- * Retrieve currentClass
97
- * @return {string}
98
- */
99
- }, {
100
- key: 'currentClass',
101
- get: function get () {
102
- return __classPrivateFieldGet(this, _TitleSwitcher_currentClass, 'f')
103
- }
104
- /**
105
- * Retrieve currentIndex
106
- * @return {number}
107
- */
108
- }, {
109
- key: 'currentIndex',
110
- get: function get () {
111
- return __classPrivateFieldGet(this, _TitleSwitcher_currentIndex, 'f')
112
- }
113
- /**
114
- * Retrieve delayEffect
115
- * @return {number}
116
- */
117
- }, {
118
- key: 'delayEffect',
119
- get: function get () {
120
- return __classPrivateFieldGet(this, _TitleSwitcher_delayEffect, 'f')
121
- }
122
- /**
123
- * Retrieve delaySwitch
124
- * @return {number}
125
- */
126
- }, {
127
- key: 'delaySwitch',
128
- get: function get () {
129
- return __classPrivateFieldGet(this, _TitleSwitcher_delaySwitch, 'f')
130
- }
131
- /**
132
- * Retrieve switchStyle
133
- * @return {Function}
134
- */
135
- }, {
136
- key: 'switchStyle',
137
- get: function get () {
138
- return __classPrivateFieldGet(this, _TitleSwitcher_switchStyle, 'f')
139
- }
140
- /**
141
- * Retrieve list of titles DOM elements
142
- * @return {Array<HTMLElement>|HTMLCollection}
143
- */
144
- }, {
145
- key: 'titles',
146
- get: function get () {
147
- return __classPrivateFieldGet(this, _TitleSwitcher_titles, 'f')
148
- }
149
- /**
150
- * Retrieve typeSurface used
151
- * @return {HTMLElement|null}
152
- */
153
- }, {
154
- key: 'typeSurface',
155
- get: function get () {
156
- return __classPrivateFieldGet(this, _TitleSwitcher_typeSurface, 'f')
157
- }
158
- /**
159
- * This is the function to begin the switching titles
160
- * @param {Object} [settings={}]
161
- * @param {number} [settings.delaySwitch=400]
162
- * @param {number} [settings.delayEffect=200]
163
- * @param {boolean} [settings.isRandom=false]
164
- * @param {boolean} [settings.immediatePause=false]
165
- * @returns {TitleSwitcher}
166
- */
167
- }, {
168
- key: 'startTitles',
169
- value: function startTitles () {
170
- var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}
171
- var _ref$delaySwitch = _ref.delaySwitch
172
- var delaySwitch = _ref$delaySwitch === void 0 ? 400 : _ref$delaySwitch
173
- var _ref$delayEffect = _ref.delayEffect
174
- var delayEffect = _ref$delayEffect === void 0 ? 200 : _ref$delayEffect
175
- var _ref$isRandom = _ref.isRandom
176
- var isRandom = _ref$isRandom === void 0 ? false : _ref$isRandom
177
- var _ref$immediatePause = _ref.immediatePause
178
- var immediatePause = _ref$immediatePause === void 0 ? false : _ref$immediatePause
179
- var typeSurface = 'typeSurface'
180
- __classPrivateFieldSet(this, _TitleSwitcher_delaySwitch, delaySwitch, 'f')
181
- __classPrivateFieldSet(this, _TitleSwitcher_delayEffect, delayEffect, 'f')
182
- __classPrivateFieldSet(this, _TitleSwitcher_isRandom, isRandom, 'f')
183
- __classPrivateFieldSet(this, _TitleSwitcher_active, !immediatePause, 'f')
184
- if (__classPrivateFieldGet(this, _TitleSwitcher_currentIndex, 'f') >= __classPrivateFieldGet(this, _TitleSwitcher_titles, 'f').length || typeof __classPrivateFieldGet(this, _TitleSwitcher_titlesContainer, 'f') === 'string') {
185
- __classPrivateFieldSet(this, _TitleSwitcher_active, false, 'f')
186
- console.warn("No titles found for '".concat(__classPrivateFieldGet(this, _TitleSwitcher_titlesContainer, 'f'), "'"))
187
- return this
188
- }
189
- if (__classPrivateFieldGet(this, _TitleSwitcher_isRandom, 'f')) {
190
- __classPrivateFieldSet(this, _TitleSwitcher_currentIndex, Math.round(Math.random() * (__classPrivateFieldGet(this, _TitleSwitcher_titles, 'f').length - 2)) + 1, 'f')
191
- }
192
- var currentTitle = __classPrivateFieldGet(this, _TitleSwitcher_titles, 'f')[__classPrivateFieldGet(this, _TitleSwitcher_currentIndex, 'f')]
193
- if (currentTitle.classList) {
194
- currentTitle.classList.add(__classPrivateFieldGet(this, _TitleSwitcher_currentClass, 'f'))
195
- } else {
196
- currentTitle.className += ' ' + __classPrivateFieldGet(this, _TitleSwitcher_currentClass, 'f')
197
- }
198
- // @ts-ignore The Node returned is of type Element, or it should be
199
- var typeElement = __classPrivateFieldGet(this, _TitleSwitcher_titles, 'f')[0].cloneNode(true)
200
- if (typeElement.classList) {
201
- typeElement.classList.add(typeSurface)
202
- } else {
203
- typeElement.className += ' ' + typeSurface
204
- }
205
- if (typeElement.classList) {
206
- typeElement.classList.remove(__classPrivateFieldGet(this, _TitleSwitcher_currentClass, 'f'))
207
- } else {
208
- typeElement.className = typeElement.className.replace(new RegExp('(^|\\b)' + typeElement.className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ')
209
- }
210
- __classPrivateFieldGet(this, _TitleSwitcher_titlesContainer, 'f').insertBefore(typeElement, __classPrivateFieldGet(this, _TitleSwitcher_titlesContainer, 'f').firstChild)
211
- // @ts-ignore the returned Node is a type of HTMLElement
212
- __classPrivateFieldSet(this, _TitleSwitcher_typeSurface, __classPrivateFieldGet(this, _TitleSwitcher_titlesContainer, 'f').querySelectorAll('.' + typeSurface)[0], 'f')
213
- __classPrivateFieldGet(this, _TitleSwitcher_typeSurface, 'f').innerHTML = ''
214
- __classPrivateFieldGet(this, _TitleSwitcher_typeSurface, 'f').style.display = 'block'
215
- Array.prototype.forEach.call(__classPrivateFieldGet(this, _TitleSwitcher_titles, 'f'), function (title) {
216
- title.style.display = 'none'
217
- })
218
- return this.switchTitle(__classPrivateFieldGet(this, _TitleSwitcher_titles, 'f')[__classPrivateFieldGet(this, _TitleSwitcher_currentIndex, 'f')], __classPrivateFieldGet(this, _TitleSwitcher_switchStyle, 'f'), this)
219
- }
220
- /**
221
- * This is the function to pause between switching
222
- */
223
- }, {
224
- key: 'pause',
225
- value: function pause () {
226
- __classPrivateFieldSet(this, _TitleSwitcher_active, false, 'f')
227
- }
228
- /**
229
- * This is the function to resume after a pause.
230
- */
231
- }, {
232
- key: 'resume',
233
- value: function resume () {
234
- if (!__classPrivateFieldGet(this, _TitleSwitcher_active, 'f')) {
235
- __classPrivateFieldSet(this, _TitleSwitcher_active, true, 'f')
236
- this.switchTitle(__classPrivateFieldGet(this, _TitleSwitcher_titles, 'f')[__classPrivateFieldGet(this, _TitleSwitcher_currentIndex, 'f')], __classPrivateFieldGet(this, _TitleSwitcher_switchStyle, 'f'), this)
237
- }
238
- }
239
- /**
240
- * This is the core function for switching titles
241
- * @param {HTMLElement} currentTitle
242
- * @param {Function} callBackFunction
243
- * @param {TitleSwitcher} self
244
- * @param {boolean} [runOnce=false]
245
- * @returns {TitleSwitcher}
246
- */
247
- }, {
248
- key: 'switchTitle',
249
- value: function switchTitle (currentTitle, callBackFunction, self) {
250
- var runOnce = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false
251
- self = self || this
252
- var currentIndex = 1
253
- var size = __classPrivateFieldGet(self, _TitleSwitcher_titles, 'f').length
254
- for (var i = 1; i < size; ++i) {
255
- if (__classPrivateFieldGet(self, _TitleSwitcher_titles, 'f')[i] === currentTitle) {
256
- currentIndex = i
257
- break
258
- }
259
- }
260
- if (!__classPrivateFieldGet(self, _TitleSwitcher_active, 'f')) {
261
- __classPrivateFieldSet(self, _TitleSwitcher_currentIndex, currentIndex, 'f')
262
- return self
263
- }
264
- var maxIndex = __classPrivateFieldGet(self, _TitleSwitcher_titles, 'f').length - 1
265
- var nextIndex = 1
266
- if (maxIndex === 1) {
267
- return callBackFunction(currentTitle, runOnce ? function () {
268
- return self
269
- } : self.switchTitle, self, runOnce)
270
- }
271
- if (__classPrivateFieldGet(self, _TitleSwitcher_isRandom, 'f')) {
272
- if (!self.typeSurface.textContent.trim()) {
273
- currentIndex = -1
274
- }
275
- do {
276
- nextIndex = Math.round(Math.random() * (maxIndex - 1)) + 1
277
- } while (nextIndex === currentIndex)
278
- } else {
279
- if (!self.typeSurface.textContent.trim()) {
280
- currentIndex = maxIndex
281
- }
282
- nextIndex = currentIndex < maxIndex ? currentIndex + 1 : 1
283
- }
284
- var nextTitle = __classPrivateFieldGet(self, _TitleSwitcher_titles, 'f')[nextIndex]
285
- if (currentTitle.classList) {
286
- currentTitle.classList.remove(self.currentClass)
287
- } else {
288
- currentTitle.className = currentTitle.className.replace(new RegExp('(^|\\b)' + currentTitle.className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ')
289
- }
290
- if (nextTitle.classList) {
291
- nextTitle.classList.add(self.currentClass)
292
- } else {
293
- nextTitle.className += ' ' + self.currentClass
294
- }
295
- return callBackFunction(nextTitle, runOnce ? function () {
296
- return self
297
- } : self.switchTitle, self, runOnce)
298
- }
299
- }])
300
- }())
301
- _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()
302
- /**
303
- * This is a helper function to improve the default 'typingEffect'
304
- * @param {boolean} blinkOn
305
- * @param {TitleSwitcher} self
306
- * @return {TitleSwitcher}
307
- */
308
- TitleSwitcher.prototype.cursorBlink = function (blinkOn, self) {
309
- // display cursor effect
310
- self = self || _this
311
- if (blinkOn) {
312
- self.typeSurface.innerHTML = self.typeSurface.innerHTML.replace(/\||&nbsp;*(<\/span>)?$/, '$1').trim()
313
- self.typeSurface.innerHTML = self.typeSurface.innerHTML + '<span style="display: inline-block;font-weight: normal; color: black; text-decoration: none">&#124;</span>'
314
- } else {
315
- self.typeSurface.innerHTML = self.typeSurface.innerHTML.replace(/\|(<\/span>)$/, '&nbsp;$1')
316
- }
317
- return self
318
- }
319
- /**
320
- * This is the default and example of an effect being implemented when Titles are switched
321
- * These functions take the currentElement in focus, the switchTitle function as a callback
322
- * and an instance of the TitleSwitcher
323
- * @param {HTMLElement} domObject
324
- * @param {Function} callBackFunction
325
- * @param {TitleSwitcher} self
326
- * @param {boolean} [runOnce=false]
327
- * @returns {TitleSwitcher}
328
- */
329
- TitleSwitcher.prototype.typingEffect = function (domObject, callBackFunction, self) {
330
- var runOnce = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false
331
- self = self || _this
332
- var size = self.titles.length
333
- var currentIndex = 0
334
- for (var 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
- var blinkOn = true
342
- var 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 (var _i = 0; _i < numBlinks; ++_i) {
354
- setTimeout(function () {
355
- self.cursorBlink(blinkOn, self)
356
- blinkOn = !blinkOn
357
- }, _i * self.delaySwitch)
358
- }
359
- setTimeout(function () {
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 (text only)
364
- domObject.textContent.split('').forEach(function (letter, i) {
365
- setTimeout(function () {
366
- // Remove the previous cursor, place the new letter, then append a formatted cursor on the end
367
- self.typeSurface.innerHTML = self.typeSurface.textContent.replace('|', '') + letter + '<span style="font-weight: normal; color: black; text-decoration: none">&#124;</span>'
368
- // If the text content equals the title content with a cursor appended then we reached the end.
369
- if (domObject.textContent + '|' === self.typeSurface.textContent) {
370
- // Replace html with old html on last letter, so we get all the html formatting applied
371
- self.typeSurface.innerHTML = domObject.innerHTML + '<span style="font-weight: normal; color: black; text-decoration: none">&#124;</span>'
372
- // Run the blinking cursor two times the regular time in order to let the text be readable before switching
373
- var _loop = function _loop (_j) {
374
- setTimeout(function () {
375
- --_j
376
- self.cursorBlink(blinkOn, self)
377
- if (_j === 0) {
378
- callBackFunction(domObject, runOnce ? function () {
379
- return self
380
- } : self.switchStyle, self, runOnce)
381
- }
382
- }, _j * self.delaySwitch)
383
- j = _j
384
- }
385
- for (var j = 0; j < numBlinks * 2; ++j) {
386
- _loop(j)
387
- }
388
- }
389
- }, i * self.delayEffect)
390
- })
391
- }, numBlinks * self.delaySwitch)
392
- return self
393
- }
394
- exports.default = TitleSwitcher
395
- if (void 0) {
396
- // @ts-ignore 'this' is used in node as the global, and the key CAN be referenced by string
397
- (void 0).TitleSwitcher = TitleSwitcher
398
- } else if (typeof window !== 'undefined') {
399
- // @ts-ignore YES, we can use a string to add a property to Window
400
- window.TitleSwitcher = TitleSwitcher
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(/\||&nbsp;*(<\/span>)?$/, '$1').trim()
250
+ self.typeSurface.innerHTML = self.typeSurface.innerHTML + '<span style="display: inline-block;font-weight: normal; color: black; text-decoration: none">&#124;</span>'
251
+ } else {
252
+ self.typeSurface.innerHTML = self.typeSurface.innerHTML.replace(/\|(<\/span>)$/, '&nbsp;$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">&#124;</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">&#124;</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
+ }