Skip to content

Commit 4aa54d2

Browse files
committed
Merge pull request layervault#14 from bfontaine/chaining-fix
jQuery chaining fixed
2 parents 08f5f28 + 6a66b50 commit 4aa54d2

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

src/jquery.typer.js

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@ String.prototype.rightChars = function(n){
5050
};
5151

5252
clearData = function ($e) {
53-
$e
54-
.data('typePosition', null)
55-
.data('highlightPosition', null)
56-
.data('leftStop', null)
57-
.data('rightStop', null)
58-
.data('primaryColor', null)
59-
.data('backgroundColor', null)
60-
.data('text', null)
61-
.data('typing', null);
53+
$e.removeData([
54+
'typePosition',
55+
'highlightPosition',
56+
'leftStop',
57+
'rightStop',
58+
'primaryColor',
59+
'backgroundColor',
60+
'text',
61+
'typing'
62+
]);
6263
};
6364

6465
type = function ($e) {
@@ -82,10 +83,11 @@ String.prototype.rightChars = function(n){
8283
oldLeft +
8384
text.charAt(0) +
8485
oldRight
85-
);
86+
).data({
87+
oldLeft: oldLeft + text.charAt(0),
88+
text: text.substring(1)
89+
});
8690

87-
$e.data('oldLeft', oldLeft + text.charAt(0));
88-
$e.data('text', text.substring(1));
8991
// $e.text($e.text() + text.substring(position, position + 1));
9092

9193
// $e.data('typePosition', position + 1);
@@ -125,8 +127,7 @@ String.prototype.rightChars = function(n){
125127
highlightedText = $e.text().substring(position - 1, $e.data('rightStop') + 1);
126128
rightText = $e.text().substring($e.data('rightStop') + 1);
127129

128-
$e.html(leftText);
129-
$e
130+
$e.html(leftText)
130131
.append(
131132
spanWithColor(
132133
$e.data('backgroundColor'),
@@ -177,13 +178,13 @@ String.prototype.rightChars = function(n){
177178
$.fn.typer = function() {
178179
var $elements = $(this);
179180

180-
$elements = $elements.filter(function () {
181-
return typeof $(this).attr($.typer.options.typerDataAttr) !== "undefined"
182-
});
183-
184-
$elements.each(function () {
181+
return $elements.each(function () {
185182
var $e = $(this);
186183

184+
if (typeof $e.attr($.typer.options.typerDataAttr) === "undefined") {
185+
return;
186+
}
187+
187188
typeWithAttribute($e);
188189
setInterval(function () {
189190
typeWithAttribute($e);
@@ -200,12 +201,12 @@ String.prototype.rightChars = function(n){
200201

201202
if (currentText === newString) {
202203
console.log("Our strings our equal, nothing to type");
203-
return;
204+
return $e;
204205
}
205206

206207
if (currentText !== $e.html()) {
207208
console.error("Typer does not work on elements with child elements.");
208-
return;
209+
return $e;
209210
}
210211

211212
$e.data('typing', true);
@@ -220,14 +221,19 @@ String.prototype.rightChars = function(n){
220221

221222
newString = newString.substring(i, newString.length - j + 1);
222223

223-
$e.data('oldLeft', currentText.substring(0, i));
224-
$e.data('oldRight', currentText.rightChars(j - 1));
225-
$e.data('leftStop', i);
226-
$e.data('rightStop', currentText.length - j);
227-
$e.data('primaryColor', $e.css('color'));
228-
$e.data('backgroundColor', $e.css('background-color'));
229-
$e.data('text', newString);
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+
230234
highlight($e);
235+
236+
return $e;
231237
};
232238

233239
//-- Helper methods. These can one day be customized further to include things like ranges of delays.

0 commit comments

Comments
 (0)