1String.prototype.rightChars = function(n){ 2 if (n <= 0) { 3 return ""; 4 } 5 else if (n > this.length) { 6 return this; 7 } 8 else { 9 return this.substring(this.length, this.length - n); 10 } 11}; 12 13(function($) { 14 var 15 options = { 16 highlightSpeed : 20, 17 typeSpeed : 100, 18 clearDelay : 500, 19 typeDelay : 200, 20 clearOnHighlight : true, 21 typerDataAttr : 'data-typer-targets', 22 typerInterval : 2000, 23 highlightColor : null, // set to null if not customized 24 textColor : 'rgb(255, 255, 255)', // set to null if not customized 25 initialDelay : 1000 26 }, 27 highlight, 28 clearText, 29 backspace, 30 type, 31 spanWithColor, 32 clearDelay, 33 typeDelay, 34 clearData, 35 isNumber, 36 typeWithAttribute, 37 getHighlightInterval, 38 getTypeInterval, 39 typerInterval, 40 currentindex; 41 42 spanWithColor = function(color, backgroundColor) { 43 if (color === 'rgba(0, 0, 0, 0)') { 44 color = 'rgb(255, 255, 255)'; 45 } 46 47 return $('<span></span>') 48 .css('color', color) 49 .css('background-color', backgroundColor); 50 }; 51 52 isNumber = function (n) { 53 return !isNaN(parseFloat(n)) && isFinite(n); 54 }; 55 56 clearData = function ($e) { 57 $e.removeData([ 58 'typePosition', 59 'highlightPosition', 60 'leftStop', 61 'rightStop', 62 'primaryColor', 63 'backgroundColor', 64 'text', 65 'typing' 66 ]); 67 }; 68 69 type = function ($e) { 70 var 71 // position = $e.data('typePosition'), 72 text = $e.data('text'), 73 oldLeft = $e.data('oldLeft'), 74 oldRight = $e.data('oldRight'); 75 76 // if (!isNumber(position)) { 77 // position = $e.data('leftStop'); 78 // } 79 80 if (!text || text.length === 0) { 81 clearData($e); 82 return; 83 } 84 85 86 $e.text( 87 oldLeft + 88 text.charAt(0) + 89 oldRight 90 ).data({ 91 oldLeft: oldLeft + text.charAt(0), 92 text: text.substring(1) 93 }); 94 95 // $e.text($e.text() + text.substring(position, position + 1)); 96 97 // $e.data('typePosition', position + 1); 98 99 setTimeout(function () { 100 type($e); 101 }, getTypeInterval()); 102 }; 103 104 clearText = function ($e) { 105 $e.find('span').remove(); 106 107 setTimeout(function () { 108 type($e); 109 }, typeDelay()); 110 }; 111 112 highlight = function ($e) { 113 var 114 position = $e.data('highlightPosition'), 115 leftText, 116 highlightedText, 117 rightText; 118 119 if (!isNumber(position)) { 120 position = $e.data('rightStop') + 1; 121 } 122 123 if (position <= $e.data('leftStop')) { 124 setTimeout(function () { 125 clearText($e); 126 }, clearDelay()); 127 return; 128 } 129 130 leftText = $e.text().substring(0, position - 1); 131 highlightedText = $e.text().substring(position - 1, $e.data('rightStop') + 1); 132 rightText = $e.text().substring($e.data('rightStop') + 1); 133 134 $e.html(leftText) 135 .append( 136 spanWithColor( 137 $e.data('backgroundColor'), 138 $e.data('primaryColor') 139 ) 140 .append(highlightedText) 141 ) 142 .append(rightText); 143 144 $e.data('highlightPosition', position - 1); 145 146 setTimeout(function () { 147 return highlight($e); 148 }, getHighlightInterval()); 149 }; 150 151 typeWithAttribute = function ($e) { 152 var targets; 153 154 if ($e.data('typing')) { 155 return; 156 } 157 158 try { 159 targets = JSON.parse($e.attr($.typer.options.typerDataAttr)).targets; 160 } catch (e) {} 161 162 if (typeof targets === "undefined") { 163 targets = $.map($e.attr($.typer.options.typerDataAttr).split(','), function (e) { 164 return $.trim(e); 165 }); 166 } 167 168 if(currentindex>=0 && currentindex<(targets.length-1)) { 169 currentindex++; 170 } else { 171 currentindex = 0; 172 } 173 $e.typeTo(targets[currentindex]); 174 }; 175 176 // Expose our options to the world. 177 $.typer = (function () { 178 return { options: options }; 179 })(); 180 181 $.extend($.typer, { 182 options: options 183 }); 184 185 //-- Methods to attach to jQuery sets 186 187 $.fn.typer = function() { 188 var $elements = $(this); 189 190 return $elements.each(function () { 191 var $e = $(this); 192 193 if (typeof $e.attr($.typer.options.typerDataAttr) === "undefined") { 194 return; 195 } 196 197 typeWithAttribute($e); 198 setInterval(function () { 199 typeWithAttribute($e); 200 }, typerInterval()); 201 });
202 }; 203 204 $.fn.typeTo = function (newString) { 205 var 206 $e = $(this), 207 currentText = $e.text(), 208 i = 0, 209 j = 0; 210 211 if (currentText === newString) { 212 console.log("Our strings our equal, nothing to type"); 213 return $e; 214 } 215 216 if (currentText !== $e.html()) { 217 console.error("Typer does not work on elements with child elements."); 218 return $e; 219 } 220 221 $e.data('typing', true); 222 223 // while (currentText.charAt(i) === newString.charAt(i)) { 224 // i++; 225 // } 226 227 // while (currentText.rightChars(j) === newString.rightChars(j)) { 228 // j++; 229 // } 230 231 newString = newString.substring(i, newString.length - j + 1); 232 233 $e.data({ 234 oldLeft: currentText.substring(0, i), 235 oldRight: currentText.rightChars(j - 1), 236 leftStop: i, 237 rightStop: currentText.length - j, 238 primaryColor: $.typer.options.highlightColor || $e.css('color'), 239 backgroundColor: $.typer.options.textColor || $e.css('background-color'), 240 text: newString 241 }); 242 243 var initDelay = function() { 244 highlight($e); 245 return $e; 246 } 247 window.setTimeout(initDelay, $.typer.options.initialDelay) 248 249 }; 250 251 //-- Helper methods. These can one day be customized further to include things like ranges of delays. 252 253 getHighlightInterval = function () { 254 return $.typer.options.highlightSpeed; 255 }; 256 257 getTypeInterval = function () { 258 return $.typer.options.typeSpeed; 259 }, 260 261 clearDelay = function () { 262 return $.typer.options.clearDelay; 263 }, 264 265 typeDelay = function () { 266 return $.typer.options.typeDelay; 267 }; 268 269 typerInterval = function () { 270 return $.typer.options.typerInterval; 271 }; 272})(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.