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