-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitializer.js
207 lines (171 loc) · 5.31 KB
/
initializer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import defaults from './defaults.js';
/**
* Initialize the Typed object
*/
export default class Initializer {
/**
* Load up defaults & options on the Typed instance
* @param {Typed} self instance of Typed
* @param {object} options options object
* @param {string} elementId HTML element ID _OR_ instance of HTML element
* @private
*/
load(self, options, elementId) {
// chosen element to manipulate text
if (typeof elementId === 'string') {
self.el = document.querySelector(elementId);
} else {
self.el = elementId;
}
self.options = { ...defaults, ...options };
// attribute to type into
self.isInput = self.el.tagName.toLowerCase() === 'input';
self.attr = self.options.attr;
self.bindInputFocusEvents = self.options.bindInputFocusEvents;
// show cursor
self.showCursor = self.isInput ? false : self.options.showCursor;
// custom cursor
self.cursorChar = self.options.cursorChar;
// Is the cursor blinking
self.cursorBlinking = true;
// text content of element
self.elContent = self.attr
? self.el.getAttribute(self.attr)
: self.el.textContent;
// html or plain text
self.contentType = self.options.contentType;
// typing speed
self.typeSpeed = self.options.typeSpeed;
// add a delay before typing starts
self.startDelay = self.options.startDelay;
// backspacing speed
self.backSpeed = self.options.backSpeed;
// only backspace what doesn't match the previous string
self.smartBackspace = self.options.smartBackspace;
// amount of time to wait before backspacing
self.backDelay = self.options.backDelay;
// Fade out instead of backspace
self.fadeOut = self.options.fadeOut;
self.fadeOutClass = self.options.fadeOutClass;
self.fadeOutDelay = self.options.fadeOutDelay;
// variable to check whether typing is currently paused
self.isPaused = false;
// input strings of text
self.strings = self.options.strings.map((s) => s.trim());
// div containing strings
if (typeof self.options.stringsElement === 'string') {
self.stringsElement = document.querySelector(self.options.stringsElement);
} else {
self.stringsElement = self.options.stringsElement;
}
if (self.stringsElement) {
self.strings = [];
self.stringsElement.style.display = 'none';
const strings = Array.prototype.slice.apply(self.stringsElement.children);
const stringsLength = strings.length;
if (stringsLength) {
for (let i = 0; i < stringsLength; i += 1) {
const stringEl = strings[i];
self.strings.push(stringEl.innerHTML.trim());
}
}
}
// character number position of current string
self.strPos = 0;
// current array position
self.arrayPos = 0;
// index of string to stop backspacing on
self.stopNum = 0;
// Looping logic
self.loop = self.options.loop;
self.loopCount = self.options.loopCount;
self.curLoop = 0;
// shuffle the strings
self.shuffle = self.options.shuffle;
// the order of strings
self.sequence = [];
self.pause = {
status: false,
typewrite: true,
curString: '',
curStrPos: 0
};
// When the typing is complete (when not looped)
self.typingComplete = false;
// Set the order in which the strings are typed
for (let i in self.strings) {
self.sequence[i] = i;
}
// If there is some text in the element
self.currentElContent = this.getCurrentElContent(self);
self.autoInsertCss = self.options.autoInsertCss;
this.appendAnimationCss(self);
}
getCurrentElContent(self) {
let elContent = '';
if (self.attr) {
elContent = self.el.getAttribute(self.attr);
} else if (self.isInput) {
elContent = self.el.value;
} else if (self.contentType === 'html') {
elContent = self.el.innerHTML;
} else {
elContent = self.el.textContent;
}
return elContent;
}
appendAnimationCss(self) {
const cssDataName = 'data-typed-js-css';
if (!self.autoInsertCss) {
return;
}
if (!self.showCursor && !self.fadeOut) {
return;
}
if (document.querySelector(`[${cssDataName}]`)) {
return;
}
let css = document.createElement('style');
css.type = 'text/css';
css.setAttribute(cssDataName, true);
let innerCss = '';
if (self.showCursor) {
innerCss += `
.typed-cursor{
opacity: 1;
}
.typed-cursor.typed-cursor--blink{
animation: typedjsBlink 0.7s infinite;
-webkit-animation: typedjsBlink 0.7s infinite;
animation: typedjsBlink 0.7s infinite;
}
@keyframes typedjsBlink{
50% { opacity: 0.0; }
}
@-webkit-keyframes typedjsBlink{
0% { opacity: 1; }
50% { opacity: 0.0; }
100% { opacity: 1; }
}
`;
}
if (self.fadeOut) {
innerCss += `
.typed-fade-out{
opacity: 0;
transition: opacity .25s;
}
.typed-cursor.typed-cursor--blink.typed-fade-out{
-webkit-animation: 0;
animation: 0;
}
`;
}
if (css.length === 0) {
return;
}
css.innerHTML = innerCss;
document.body.appendChild(css);
}
}
export let initializer = new Initializer();