@@ -11,34 +11,21 @@ String.prototype.rightChars = function(n){
1111} ;
1212
1313( function ( $ ) {
14- var
15- options = {
16- highlightSpeed : 20 ,
17- typeSpeed : 100 ,
18- clearDelay : 500 ,
19- typeDelay : 200 ,
20- clearOnHighlight : true ,
21- highlightEverything : true ,
22- typerDataAttr : 'data-typer-targets' ,
23- typerInterval : 2000 ,
24- debug : false ,
25- tapeColor : 'auto' ,
26- typerOrder : 'random' ,
27- } ,
28- highlight ,
29- clearText ,
30- backspace ,
31- type ,
32- spanWithColor ,
33- clearDelay ,
34- typeDelay ,
35- clearData ,
36- isNumber ,
37- typeWithAttribute ,
38- getHighlightInterval ,
39- getTypeInterval ,
40- intervalHandle ,
41- typerInterval ;
14+ var opts ,
15+ highlight ,
16+ clearText ,
17+ backspace ,
18+ type ,
19+ spanWithColor ,
20+ clearDelay ,
21+ typeDelay ,
22+ clearData ,
23+ isNumber ,
24+ typeWithAttribute ,
25+ getHighlightInterval ,
26+ getTypeInterval ,
27+ intervalHandle ,
28+ typerInterval ;
4229
4330 spanWithColor = function ( color , backgroundColor ) {
4431 if ( color === 'rgba(0, 0, 0, 0)' ) {
@@ -83,7 +70,6 @@ String.prototype.rightChars = function(n){
8370 return ;
8471 }
8572
86-
8773 $e . text (
8874 oldLeft +
8975 text . charAt ( 0 ) +
@@ -136,7 +122,7 @@ String.prototype.rightChars = function(n){
136122 . append (
137123 spanWithColor (
138124 $e . data ( 'backgroundColor' ) ,
139- $ . typer . options . tapeColor === 'auto' ? $e . data ( 'primaryColor' ) : $ . typer . options . tapeColor
125+ opts . tapeColor === 'auto' ? $e . data ( 'primaryColor' ) : opts . tapeColor
140126 )
141127 . append ( highlightedText )
142128 )
@@ -160,47 +146,40 @@ String.prototype.rightChars = function(n){
160146 }
161147
162148 try {
163- targets = JSON . parse ( $e . attr ( $ . typer . options . typerDataAttr ) ) . targets ;
149+ targets = JSON . parse ( $e . attr ( opts . typerDataAttr ) ) . targets ;
164150 } catch ( e ) { }
165151
166152 if ( typeof targets === "undefined" ) {
167- targets = $ . map ( $e . attr ( $ . typer . options . typerDataAttr ) . split ( ',' ) , function ( e ) {
153+ targets = $ . map ( $e . attr ( opts . typerDataAttr ) . split ( ',' ) , function ( e ) {
168154 return $ . trim ( e ) ;
169155 } ) ;
170156 }
171157
172- if ( $ . typer . options . typerOrder == 'random' ) {
158+ if ( opts . typerOrder == 'random' ) {
173159 $e . typeTo ( targets [ Math . floor ( Math . random ( ) * targets . length ) ] ) ;
174160 }
175- else if ( $ . typer . options . typerOrder == 'sequential' ) {
161+ else if ( opts . typerOrder == 'sequential' ) {
176162 $e . typeTo ( targets [ last ] ) ;
177163 last = ( last < targets . length - 1 ) ? last + 1 : 0 ;
178164 }
179165 else {
180- console . error ( "Type order of '" + $ . typer . options . typerOrder + "' not supported" ) ;
166+ console . error ( "Type order of '" + opts . typerOrder + "' not supported" ) ;
181167 clearInterval ( intervalHandle ) ;
182168 }
183- }
184- } ) ( ) ;
185-
186- // Expose our options to the world.
187- $ . typer = ( function ( ) {
188- return { options : options } ;
169+ } ;
189170 } ) ( ) ;
190171
191- $ . extend ( $ . typer , {
192- options : options
193- } ) ;
194-
195172 //-- Methods to attach to jQuery sets
196173
197- $ . fn . typer = function ( ) {
174+ $ . fn . typer = function ( options ) {
198175 var $elements = $ ( this ) ;
199176
177+ opts = jQuery . extend ( { } , $ . fn . typer . defaults , options ) ;
178+
200179 return $elements . each ( function ( ) {
201180 var $e = $ ( this ) ;
202181
203- if ( typeof $e . attr ( $ . typer . options . typerDataAttr ) === "undefined" ) {
182+ if ( typeof $e . attr ( opts . typerDataAttr ) === "undefined" ) {
204183 return ;
205184 }
206185
@@ -219,22 +198,22 @@ String.prototype.rightChars = function(n){
219198 j = 0 ;
220199
221200 if ( currentText === newString ) {
222- if ( $ . typer . options . debug === true ) {
201+ if ( opts . debug === true ) {
223202 console . log ( "Our strings our equal, nothing to type" ) ;
224203 }
225204 return $e ;
226205 }
227206
228207 if ( currentText !== $e . html ( ) ) {
229- if ( $ . typer . options . debug === true ) {
208+ if ( opts . debug === true ) {
230209 console . error ( "Typer does not work on elements with child elements." ) ;
231210 }
232211 return $e ;
233212 }
234213
235214 $e . data ( 'typing' , true ) ;
236215
237- if ( $ . typer . options . highlightEverything !== true ) {
216+ if ( opts . highlightEverything !== true ) {
238217 while ( currentText . charAt ( i ) === newString . charAt ( i ) ) {
239218 i ++ ;
240219 }
@@ -251,7 +230,7 @@ String.prototype.rightChars = function(n){
251230 oldRight : currentText . rightChars ( j - 1 ) ,
252231 leftStop : i ,
253232 rightStop : currentText . length - j ,
254- primaryColor : $ . typer . options . tapeColor === 'auto' ? $e . data ( 'primaryColor' ) : $ . typer . options . tapeColor ,
233+ primaryColor : opts . tapeColor === 'auto' ? $e . data ( 'primaryColor' ) : opts . tapeColor ,
255234 backgroundColor : $e . css ( 'background-color' ) ,
256235 text : newString
257236 } ) ;
@@ -264,22 +243,37 @@ String.prototype.rightChars = function(n){
264243 //-- Helper methods. These can one day be customized further to include things like ranges of delays.
265244
266245 getHighlightInterval = function ( ) {
267- return $ . typer . options . highlightSpeed ;
246+ return opts . highlightSpeed ;
268247 } ;
269248
270249 getTypeInterval = function ( ) {
271- return $ . typer . options . typeSpeed ;
272- } ,
250+ return opts . typeSpeed ;
251+ } ;
273252
274253 clearDelay = function ( ) {
275- return $ . typer . options . clearDelay ;
276- } ,
254+ return opts . clearDelay ;
255+ } ;
277256
278257 typeDelay = function ( ) {
279- return $ . typer . options . typeDelay ;
258+ return opts . typeDelay ;
280259 } ;
281260
282261 typerInterval = function ( ) {
283- return $ . typer . options . typerInterval ;
262+ return opts . typerInterval ;
284263 } ;
264+
265+ $ . fn . typer . defaults = {
266+ highlightSpeed : 20 ,
267+ typeSpeed : 100 ,
268+ clearDelay : 500 ,
269+ typeDelay : 200 ,
270+ clearOnHighlight : true ,
271+ highlightEverything : true ,
272+ typerDataAttr : 'data-typer-targets' ,
273+ typerInterval : 2000 ,
274+ debug : false ,
275+ tapeColor : 'auto' ,
276+ typerOrder : 'random' ,
277+ } ;
278+
285279} ) ( jQuery ) ;
0 commit comments