-
Notifications
You must be signed in to change notification settings - Fork 0
/
typed.min.js
1 lines (1 loc) · 3.74 KB
/
typed.min.js
1
!function(o){"use strict";const r=function(t,s){this.el=o(t),this.options=o.extend({},o.fn.typed.defaults,s),this.isInput=this.el.is("input"),this.attr=this.options.attr,this.showCursor=!this.isInput&&this.options.showCursor,this.elContent=this.attr?this.el.attr(this.attr):this.el.text(),this.contentType=this.options.contentType,this.typeSpeed=this.options.typeSpeed,this.startDelay=this.options.startDelay,this.backSpeed=this.options.backSpeed,this.backDelay=this.options.backDelay,this.stringsElement=this.options.stringsElement,this.strings=this.options.strings,this.strPos=0,this.arrayPos=0,this.stopNum=0,this.loop=this.options.loop,this.loopCount=this.options.loopCount,this.curLoop=0,this.stop=!1,this.cursorChar=this.options.cursorChar,this.shuffle=this.options.shuffle,this.sequence=[],this.build()};r.prototype={constructor:r,init(){const s=this;s.timeout=setTimeout(function(){for(let t=0;t<s.strings.length;++t)s.sequence[t]=t;s.shuffle&&(s.sequence=s.shuffleArray(s.sequence)),s.typewrite(s.strings[s.sequence[s.arrayPos]],s.strPos)},s.startDelay)},build(){const e=this;var t;!0===this.showCursor&&(this.cursor=o('<span class="typed-cursor">'+this.cursorChar+"</span>"),this.el.after(this.cursor)),this.stringsElement&&(this.strings=[],this.stringsElement.hide(),console.log(this.stringsElement.children()),t=this.stringsElement.children(),o.each(t,function(t,s){e.strings.push(o(s).html())})),this.init()},typewrite(o,r){if(!0!==this.stop){var t=Math.round(70*Math.random())+this.typeSpeed;const n=this;n.timeout=setTimeout(function(){let s=0,e=o.substr(r);if("^"===e.charAt(0)){let t=1;/^\^\d+/.test(e)&&(e=/\d+/.exec(e)[0],t+=e.length,s=parseInt(e)),o=o.substring(0,r)+o.substring(r+t)}if("html"===n.contentType){var i=o.substr(r).charAt(0);if("<"===i){let t="",s="";for(s="<"===i?">":";";o.substr(r+1).charAt(0)!==s&&(t+=o.substr(r).charAt(0),!(++r+1>o.length)););r++,t+=s}}n.timeout=setTimeout(function(){var t;r===o.length?(n.options.onStringTyped(n.arrayPos),n.arrayPos===n.strings.length-1&&(n.options.callback(),n.curLoop++,!1===n.loop||n.curLoop===n.loopCount)||(n.timeout=setTimeout(function(){n.backspace(o,r)},n.backDelay))):(0===r&&n.options.preStringTyped(n.arrayPos),t=o.substr(0,r+1),n.attr?n.el.attr(n.attr,t):n.isInput?n.el.val(t):"html"===n.contentType?n.el.html(t):n.el.text(t),r++,n.typewrite(o,r))},s)},t)}},backspace(s,e){if(!0!==this.stop){var t=Math.round(70*Math.random())+this.backSpeed;const i=this;i.timeout=setTimeout(function(){if("html"===i.contentType&&">"===s.substr(e).charAt(0)){let t="";for(;"<"!==s.substr(e-1).charAt(0)&&(t-=s.substr(e).charAt(0),!(--e<0)););e--,t+="<"}var t=s.substr(0,e);i.attr?i.el.attr(i.attr,t):i.isInput?i.el.val(t):"html"===i.contentType?i.el.html(t):i.el.text(t),e>i.stopNum?(e--,i.backspace(s,e)):e<=i.stopNum&&(i.arrayPos++,i.arrayPos===i.strings.length?(i.arrayPos=0,i.shuffle&&(i.sequence=i.shuffleArray(i.sequence)),i.init()):i.typewrite(i.strings[i.sequence[i.arrayPos]],e))},t)}},shuffleArray(t){let s,e,i=t.length;if(i)for(;--i;)s=t[e=Math.floor(Math.random()*(i+1))],t[e]=t[i],t[i]=s;return t},reset(){clearInterval(this.timeout);this.el.attr("id");this.el.empty(),void 0!==this.cursor&&this.cursor.remove(),this.strPos=0,this.arrayPos=0,this.curLoop=0,this.options.resetCallback()}},o.fn.typed=function(i){return this.each(function(){let t=o(this),s=t.data("typed"),e="object"==typeof i&&i;s&&s.reset(),t.data("typed",s=new r(this,e)),"string"==typeof i&&s[i]()})},o.fn.typed.defaults={strings:["These are the default values...","You know what you should do?","Use your own!","Have a great day!"],stringsElement:null,typeSpeed:0,startDelay:0,backSpeed:0,shuffle:!1,backDelay:500,loop:!1,loopCount:!1,showCursor:!0,cursorChar:"|",attr:null,contentType:"html",callback(){},preStringTyped(){},onStringTyped(){},resetCallback(){}}}(window.jQuery);