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