11( function ( $ ) {
22 var
33 options = {
4- highlightSpeed : 20 ,
5- typeSpeed : 100 ,
6- clearDelay : 500 ,
7- typeDelay : 200 ,
8- clearOnHighlight : true
4+ highlightSpeed : 20 ,
5+ typeSpeed : 100 ,
6+ clearDelay : 500 ,
7+ typeDelay : 200 ,
8+ clearOnHighlight : true ,
9+ typerDataAttr : 'data-typer-targets' ,
10+ typerInterval : 2000
911 } ,
1012 highlight ,
1113 clearText ,
1517 clearDelay ,
1618 typeDelay ,
1719 clearData ,
18- isNumber ;
19-
20- getHighlightInterval = function ( ) {
21- return options . highlightSpeed ;
22- } ;
23-
24- getTypeInterval = function ( ) {
25- return options . typeSpeed ;
26- } ,
27-
28- clearDelay = function ( ) {
29- return options . clearDelay ;
30- } ,
31-
32- typeDelay = function ( ) {
33- return options . typeDelay ;
34- }
20+ isNumber ,
21+ typeWithAttribute ;
3522
3623 spanWithColor = function ( color , backgroundColor ) {
3724 if ( color === 'rgba(0, 0, 0, 0)' ) {
5441 . data ( 'stopAt' , null )
5542 . data ( 'primaryColor' , null )
5643 . data ( 'backgroundColor' , null )
57- . data ( 'text' , null ) ;
44+ . data ( 'text' , null )
45+ . data ( 'typing' , null ) ;
5846 } ;
5947
6048 type = function ( $e ) {
125113 } , getHighlightInterval ( ) ) ;
126114 } ;
127115
116+ typeWithAttribute = function ( $e ) {
117+ var targets ;
118+
119+ if ( $e . data ( 'typing' ) ) {
120+ return ;
121+ }
122+
123+ try {
124+ targets = JSON . parse ( $e . attr ( options . typerDataAttr ) ) ;
125+ } catch ( e ) { }
126+
127+ if ( typeof targets === "undefined" ) {
128+ targets = $ . map ( $e . attr ( options . typerDataAttr ) . split ( ',' ) , function ( e ) {
129+ return $ . trim ( e ) ;
130+ } ) ;
131+ }
132+
133+ $e . typeTo ( targets [ Math . floor ( Math . random ( ) * targets . length ) ] ) ;
134+ } ;
135+
136+ //-- Methods to attach to jQuery sets
137+
128138 $ . fn . typer = function ( ) {
139+ var $e = $ ( this ) ;
129140
141+ if ( typeof $e . attr ( options . typerDataAttr ) === "undefined" ) {
142+ return ;
143+ }
144+
145+ typeWithAttribute ( $e ) ;
146+ setInterval ( function ( ) {
147+ typeWithAttribute ( $e ) ;
148+ } , typerInterval ( ) ) ;
130149 } ;
131150
132151 $ . fn . typeTo = function ( newString ) {
135154 currentText = $e . text ( ) ,
136155 i = 0 ;
137156
157+ if ( currentText === newString ) {
158+ console . log ( "Our strings our equal, nothing to type" ) ;
159+ return ;
160+ }
161+
138162 if ( currentText !== $e . html ( ) ) {
139163 console . error ( "Typer does not work on elements with child elements." ) ;
140164 return ;
141165 }
142166
167+ $e . data ( 'typing' , true ) ;
168+
143169 while ( currentText . charAt ( i ) === newString . charAt ( i ) ) {
144170 i ++ ;
145171 }
150176 $e . data ( 'text' , newString ) ;
151177 highlight ( $e ) ;
152178 } ;
179+
180+ //-- Helper methods. These can one day be customized further to include things like ranges of delays.
181+
182+ getHighlightInterval = function ( ) {
183+ return options . highlightSpeed ;
184+ } ;
185+
186+ getTypeInterval = function ( ) {
187+ return options . typeSpeed ;
188+ } ,
189+
190+ clearDelay = function ( ) {
191+ return options . clearDelay ;
192+ } ,
193+
194+ typeDelay = function ( ) {
195+ return options . typeDelay ;
196+ } ;
197+
198+ typerInterval = function ( ) {
199+ return options . typerInterval ;
200+ } ;
153201} ) ( jQuery ) ;
0 commit comments