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