1// The MIT License (MIT) 2 3// Typed.js | Copyright (c) 2014 Matt Boldt | www.mattboldt.com 4 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to deal 7// in the Software without restriction, including without limitation the rights 8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9// copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21// THE SOFTWARE. 22 23 24 25 26! function($) { 27 28 "use strict"; 29 30 var Typed = function(el, options) { 31 32 // chosen element to manipulate text 33 this.el = $(el); 34 35 // options 36 this.options = $.extend({}, $.fn.typed.defaults, options); 37 38 // attribute to type into 39 this.isInput = this.el.is('input'); 40 this.attr = this.options.attr; 41 42 // show cursor 43 this.showCursor = this.isInput ? false : this.options.showCursor; 44 45 // text content of element 46 this.elContent = this.attr ? this.el.attr(this.attr) : this.el.text() 47 48 // html or plain text 49 this.contentType = this.options.contentType; 50 51 // typing speed 52 this.typeSpeed = this.options.typeSpeed; 53 54 // add a delay before typing starts 55 this.startDelay = this.options.startDelay; 56 57 // backspacing speed 58 this.backSpeed = this.options.backSpeed; 59 60 // amount of time to wait before backspacing 61 this.backDelay = this.options.backDelay; 62 63 // input strings of text 64 this.strings = this.options.strings; 65 66 // character number position of current string 67 this.strPos = 0; 68 69 // current array position 70 this.arrayPos = 0; 71 72 // number to stop backspacing on. 73 // default 0, can change depending on how many chars 74 // you want to remove at the time 75 this.stopNum = 0; 76 77 // Looping logic 78 this.loop = this.options.loop; 79 this.loopCount = this.options.loopCount; 80 this.curLoop = 0; 81 82 // for stopping 83 this.stop = false; 84 85 // custom cursor 86 this.cursorChar = this.options.cursorChar; 87 88 // shuffle the strings 89 this.shuffle = this.options.shuffle; 90 // the order of strings 91 this.sequence = []; 92 93 // All systems go! 94 this.build(); 95 }; 96 97 Typed.prototype = { 98 99 constructor: Typed 100 101 , 102 init: function() { 103 // begin the loop w/ first current string (global self.string) 104 // current string will be passed as an argument each time after this 105 var self = this; 106 self.timeout = setTimeout(function() { 107 for (var i=0;i<self.strings.length;++i) self.sequence[i]=i; 108 109 // shuffle the array if true 110 if(self.shuffle) self.sequence = self.shuffleArray(self.sequence); 111 112 // Start typing 113 self.typewrite(self.strings[self.sequence[self.arrayPos]], self.strPos); 114 }, self.startDelay); 115 } 116 117 , 118 build: function() { 119 // Insert cursor 120 if (this.showCursor === true) { 121 this.cursor = $("<span class=\"typed-cursor\">" + this.cursorChar + "</span>"); 122 this.el.after(this.cursor); 123 } 124 this.init(); 125 } 126 127 // pass current string state to each function, types 1 char per call 128 , 129 typewrite: function(curString, curStrPos) { 130 // exit when stopped 131 if (this.stop === true) { 132 return; 133 } 134 135 // varying values for setTimeout during typing 136 // can't be global since number changes each time loop is executed 137 var humanize = Math.round(Math.random() * (100 - 30)) + this.typeSpeed; 138 var self = this; 139 140 // ------------- optional ------------- // 141 // backpaces a certain string faster 142 // ------------------------------------ // 143 // if (self.arrayPos == 1){ 144 // self.backDelay = 50; 145 // } 146 // else{ self.backDelay = 500; } 147 148 // contain typing function in a timeout humanize'd delay 149 self.timeout = setTimeout(function() { 150 // check for an escape character before a pause value 151 // format: \^\d+ .. eg: ^1000 .. should be able to print the ^ too using ^^ 152 // single ^ are removed from string 153 var charPause = 0; 154 var substr = curString.substr(curStrPos); 155 if (substr.charAt(0) === '^') { 156 var skip = 1;
156 // skip atleast 1 157 if (/^\^\d+/.test(substr)) { 158 substr = /\d+/.exec(substr)[0]; 159 skip += substr.length; 160 charPause = parseInt(substr); 161 } 162 163 // strip out the escape character and pause value so they're not printed 164 curString = curString.substring(0, curStrPos) + curString.substring(curStrPos + skip); 165 } 166 167 if (self.contentType === 'html') { 168 // skip over html tags while typing 169 var curChar = curString.substr(curStrPos).charAt(0) 170 if (curChar === '<' || curChar === '&') { 171 var tag = ''; 172 var endTag = ''; 173 if (curChar === '<') { 174 endTag = '>' 175 } else { 176 endTag = ';' 177 } 178 while (curString.substr(curStrPos).charAt(0) !== endTag) { 179 tag += curString.substr(curStrPos).charAt(0); 180 curStrPos++; 181 } 182 curStrPos++; 183 tag += endTag; 184 } 185 } 186 187 // timeout for any pause after a character 188 self.timeout = setTimeout(function() { 189 if (curStrPos === curString.length) { 190 // fires callback function 191 self.options.onStringTyped(self.arrayPos); 192 193 // is this the final string 194 if (self.arrayPos === self.strings.length - 1) { 195 // animation that occurs on the last typed string 196 self.options.callback(); 197 198 self.curLoop++; 199 200 // quit if we wont loop back 201 if (self.loop === false || self.curLoop === self.loopCount) 202 return; 203 } 204 205 self.timeout = setTimeout(function() { 206 self.backspace(curString, curStrPos); 207 }, self.backDelay); 208 } else { 209 210 /* call before functions if applicable */ 211 if (curStrPos === 0) 212 self.options.preStringTyped(self.arrayPos); 213 214 // start typing each new char into existing string 215 // curString: arg, self.el.html: original text inside element 216 var nextString = curString.substr(0, curStrPos + 1); 217 if (self.attr) { 218 self.el.attr(self.attr, nextString); 219 } else { 220 if (self.isInput) { 221 self.el.val(nextString); 222 } else if (self.contentType === 'html') { 223 self.el.html(nextString); 224 } else { 225 self.el.text(nextString); 226 } 227 } 228 229 // add characters one by one 230 curStrPos++; 231 // loop the function 232 self.typewrite(curString, curStrPos); 233 } 234 // end of character pause 235 }, charPause); 236 237 // humanized value for typing 238 }, humanize); 239 240 } 241 242 , 243 backspace: function(curString, curStrPos) { 244 // exit when stopped 245 if (this.stop === true) { 246 return; 247 } 248 249 // varying values for setTimeout during typing 250 // can't be global since number changes each time loop is executed 251 var humanize = Math.round(Math.random() * (100 - 30)) + this.backSpeed; 252 var self = this; 253 254 self.timeout = setTimeout(function() { 255 256 // ----- this part is optional ----- // 257 // check string array position 258 // on the first string, only delete one word 259 // the stopNum actually represents the amount of chars to 260 // keep in the current string. In my case it's 14. 261 // if (self.arrayPos == 1){ 262 // self.stopNum = 14; 263 // } 264 //every other time, delete the whole typed string 265 // else{ 266 // self.stopNum = 0; 267 // } 268 269 if (self.contentType === 'html') { 270 // skip over html tags while backspacing 271 if (curString.substr(curStrPos).charAt(0) === '>') { 272 var tag = ''; 273 while (curString.substr(curStrPos).charAt(0) !== '<') { 274 tag -= curString.substr(curStrPos).charAt(0); 275 curStrPos--; 276 } 277 curStrPos--; 278 tag += '<'; 279 } 280 } 281 282 // ----- continue important stuff ----- // 283 // replace text with base text + typed characters 284 var nextString = curString.substr(0, curStrPos); 285 if (self.attr) { 286 self.el.attr(self.attr, nextString); 287 } else { 288 if (self.isInput) { 289 self.el.val(nextString); 290 } else if (self.contentType === 'html') { 291 self.el.html(nextString); 292 } else { 293 self.el.text(nextString); 294 } 295 } 296 297 // if the number (id of character in current string) is 298 // less than the stop number, keep going 299 if (curStrPos > self.stopNum) { 300 // subtract characters one by one 301 curStrPos--; 302 // loop the function 303 self.backspace(curString, curStrPos); 304 } 305 // if the stop number has been reached, increase 306 // array position to next string 307 else if (curStrPos <= self.stopNum) { 308 self.arrayPos++; 309 310 if (self.arrayPos === self.strings.length) { 311 self.arrayPos = 0; 312 313 // Shuffle sequence again 314 if(self.shuffle) self.sequence = self.shuffleArray(self.sequence); 315 316 self.init(); 317 } else 318 self.typewrite(self.strings[self.sequence[self.arrayPos
318]], curStrPos); 319 } 320 321 // humanized value for typing 322 }, humanize); 323 324 } 325 /** 326 * Shuffles the numbers in the given array. 327 * @param {Array} array 328 * @returns {Array} 329 */ 330 ,shuffleArray: function(array) { 331 var tmp, current, top = array.length; 332 if(top) while(--top) { 333 current = Math.floor(Math.random() * (top + 1)); 334 tmp = array[current]; 335 array[current] = array[top]; 336 array[top] = tmp; 337 } 338 return array; 339 } 340 341 // Start & Stop currently not working 342 343 // , stop: function() { 344 // var self = this; 345 346 // self.stop = true; 347 // clearInterval(self.timeout); 348 // } 349 350 // , start: function() { 351 // var self = this; 352 // if(self.stop === false) 353 // return; 354 355 // this.stop = false; 356 // this.init(); 357 // } 358 359 // Reset and rebuild the element 360 , 361 reset: function() { 362 var self = this; 363 clearInterval(self.timeout); 364 var id = this.el.attr('id'); 365 this.el.after('<span id="' + id + '"/>') 366 this.el.remove(); 367 if (typeof this.cursor !== 'undefined') { 368 this.cursor.remove(); 369 } 370 // Send the callback 371 self.options.resetCallback(); 372 } 373 374 }; 375 376 $.fn.typed = function(option) { 377 return this.each(function() { 378 var $this = $(this), 379 data = $this.data('typed'), 380 options = typeof option == 'object' && option; 381 if (!data) $this.data('typed', (data = new Typed(this, options))); 382 if (typeof option == 'string') data[option](); 383 }); 384 }; 385 386 $.fn.typed.defaults = { 387 strings: ["These are the default values...", "You know what you should do?", "Use your own!", "Have a great day!"], 388 // typing speed 389 typeSpeed: 0, 390 // time before typing starts 391 startDelay: 0, 392 // backspacing speed 393 backSpeed: 0, 394 // shuffle the strings 395 shuffle: false, 396 // time before backspacing 397 backDelay: 500, 398 // loop 399 loop: false, 400 // false = infinite 401 loopCount: false, 402 // show cursor 403 showCursor: true, 404 // character for cursor 405 cursorChar: "|", 406 // attribute to type (null == text) 407 attr: null, 408 // either html or text 409 contentType: 'html', 410 // call when done callback function 411 callback: function() {}, 412 // starting callback function before each string 413 preStringTyped: function() {}, 414 //callback for every typed string 415 onStringTyped: function() {}, 416 // callback for reset 417 resetCallback: function() {} 418 }; 419 420 421}(window.jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.