Skip to content

Commit 425b13e

Browse files
author
Kai Schlamp
committed
beforeScroll, afterScroll and afterFade bindings. More tests.
1 parent 7528b3f commit 425b13e

File tree

3 files changed

+228
-7
lines changed

3 files changed

+228
-7
lines changed

tests/unit/ticker/ticker_events.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,66 @@
55

66
module("ticker: events");
77

8+
test("beforeScroll", function() {
9+
expect(4);
10+
stop();
11+
12+
$("#ticker").ticker({
13+
initialTimeout: 0,
14+
slidingTime: 0,
15+
fadeInTime: 0,
16+
next: function(lastItem) { return '<li>TestItem</li>'; },
17+
beforeScroll: function(event, ui) {
18+
ok(true, 'before scrolling fires beforeScroll callback');
19+
equals($("#ticker li").length, 6, "list does have all items");
20+
equals($("#ticker li:first").text(), "Item1", "Item1 still on first position");
21+
equals($("#ticker li:last").text(), "Item6", "last item still in the list");
22+
}
23+
});
24+
25+
window.setTimeout(function() { start(); }, 100);
26+
});
27+
28+
test("afterScroll", function() {
29+
expect(5);
30+
stop();
31+
32+
$("#ticker").ticker({
33+
initialTimeout: 0,
34+
slidingTime: 0,
35+
fadeInTime: 10000,
36+
next: function(lastItem) { return '<li>TestItem</li>'; },
37+
afterScroll: function(event, ui) {
38+
ok(true, 'after scrolling fires afterScroll callback');
39+
equals($("#ticker li").length, 6, "list does have all items");
40+
equals($("#ticker li:first").text(), "TestItem", "TestItem is first list item");
41+
ok($("#ticker li:first").css("opacity") < 1, "TestItem is not fully visible yet");
42+
equals($("#ticker li:last").text(), "Item5", "Item5 is last list item");
43+
}
44+
});
45+
46+
window.setTimeout(function() { start(); }, 100);
47+
});
48+
49+
test("afterFade", function() {
50+
expect(5);
51+
stop();
52+
53+
$("#ticker").ticker({
54+
initialTimeout: 0,
55+
slidingTime: 0,
56+
fadeInTime: 100,
57+
next: function(lastItem) { return '<li>TestItem</li>'; },
58+
afterFade: function(event, ui) {
59+
ok(true, 'after fade fires afterFade callback');
60+
equals($("#ticker li").length, 6, "list does have all items");
61+
equals($("#ticker li:first").text(), "TestItem", "TestItem is first list item");
62+
ok($("#ticker li:first").css("opacity") == 1, "TestItem is fully visible");
63+
equals($("#ticker li:last").text(), "Item5", "Item5 is last list item");
64+
}
65+
});
66+
67+
window.setTimeout(function() { start(); }, 200);
68+
});
69+
870
})(jQuery);

tests/unit/ticker/ticker_options.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,132 @@
55

66
module("ticker: options");
77

8+
test("{initalTimout: 200}", function() {
9+
expect(1);
10+
stop();
11+
var nextCalled = false;
12+
$("#ticker").ticker({
13+
initialTimeout: 200,
14+
next: function(lastItem) {
15+
nextCalled = true;
16+
return lastItem;
17+
}
18+
});
19+
20+
window.setTimeout(function() {
21+
if (nextCalled) {
22+
ok(false, "next called in initial timeout");
23+
}
24+
}, 100);
25+
26+
window.setTimeout(function() {
27+
if (nextCalled) {
28+
ok(true, "next called after timeout");
29+
}
30+
start();
31+
}, 200);
32+
});
33+
34+
test("{initialTimeout: 200} after calling start method", function() {
35+
expect(1);
36+
stop();
37+
var nextCalled = false;
38+
$("#ticker").ticker({
39+
initialTimeout: 200,
40+
next: function(lastItem) {
41+
nextCalled = true;
42+
return lastItem;
43+
}
44+
});
45+
46+
$("#ticker").ticker("stop");
47+
$("#ticker").ticker("start");
48+
49+
window.setTimeout(function() {
50+
if (nextCalled) {
51+
ok(false, "next called in initial timeout");
52+
}
53+
}, 100);
54+
55+
window.setTimeout(function() {
56+
if (nextCalled) {
57+
ok(true, "next called after timeout");
58+
}
59+
start();
60+
}, 200);
61+
});
62+
63+
test("{mouseOffTimeout: 100}", function() {
64+
expect(2);
65+
stop();
66+
67+
var counter = 0;
68+
69+
var nextCalled = false;
70+
$("#ticker").ticker({
71+
initialTimeout: 0,
72+
mouseOnTimeout: 10000,
73+
mouseOffTimeout: 100,
74+
next: function(lastItem) {
75+
ok(true, "Next called (one time after init and one time afterwards");
76+
if (counter == 1) {
77+
$("#ticker").ticker("stop");
78+
}
79+
counter++;
80+
return lastItem;
81+
}
82+
});
83+
84+
window.setTimeout(function() { start(); }, 200 );
85+
});
86+
87+
test("{mouseOnTimeout: 100}", function() {
88+
expect(2);
89+
stop();
90+
91+
var counter = 0;
92+
93+
var nextCalled = false;
94+
95+
$("#ticker").ticker({
96+
initialTimeout: 0,
97+
mouseOnTimeout: 100,
98+
mouseOffTimeout: 10000,
99+
next: function(lastItem) {
100+
ok(true, "Next called (one time after init and one time afterwards");
101+
if (counter == 1) {
102+
$("#ticker").ticker("stop");
103+
}
104+
counter++;
105+
return lastItem;
106+
}
107+
});
108+
109+
$("#ticker").simulate("mouseover");
110+
window.setTimeout(function() { start(); }, 200 );
111+
});
112+
113+
test('{next: function() {return "TestItem"}}', function() {
114+
expect(2);
115+
stop();
116+
117+
var nextCalled = false;
118+
119+
$("#ticker").ticker({
120+
initialTimeout: 0,
121+
slidingTime: 0,
122+
fadeInTime: 0,
123+
mouseOffTimeout: 10000,
124+
next: function(lastItem) {
125+
return "<li>TestItem</li>";
126+
}
127+
});
128+
129+
window.setTimeout(function() {
130+
equals($("#ticker li:first").html(), "TestItem", "new item was added");
131+
equals($("#ticker li:last").html(), "Item5", "last item was removed");
132+
start();
133+
}, 100 );
134+
});
135+
8136
})(jQuery);

ui/jquery.ui.ticker.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ $.widget( "ui.ticker", {
7171
},
7272

7373
destroy: function() {
74-
this.element.unbind(".ticker");
75-
this.element.children( "li" ).unbind(".ticker");
76-
this.element.removeClass( "ui-ticker ui-widget ui-corner-all" );
77-
this.element.children( "li" ).removeClass(itemClasses + " ui-state-hover ui-state-focus");
74+
var self = this;
75+
76+
self.element.unbind(".ticker");
77+
self.element.children( "li" ).unbind(".ticker");
78+
self.element.removeClass( "ui-ticker ui-widget ui-corner-all" );
79+
self.element.children( "li" ).removeClass(itemClasses + " ui-state-hover ui-state-focus");
80+
if (self.timeoutId !== null) {
81+
window.clearTimeout(self.timeoutId);
82+
self.timeoutId = null;
83+
}
7884

79-
return $.Widget.prototype.destroy.call( this );
85+
return $.Widget.prototype.destroy.call( self );
8086
},
8187

8288
_addItemBindings: function(item) {
@@ -115,6 +121,10 @@ $.widget( "ui.ticker", {
115121
newItem,
116122
lastItem;
117123

124+
if (false === self._trigger('beforeScroll')) {
125+
return;
126+
}
127+
118128
lastItem = self.element.children().last().clone();
119129
lastItem.removeClass(itemClasses + " ui-state-hover ui-state-focus");
120130

@@ -129,11 +139,17 @@ $.widget( "ui.ticker", {
129139
.prependTo(self.element)
130140
.css('visibility', 'hidden')
131141
.slideDown(options.slidingTime, function() {
132-
$( this ).fadeTo(0, 0).css('visibility', 'visible').fadeTo(options.fadeInTime, 1);
142+
$( this )
143+
.fadeTo(0, 0)
144+
.css('visibility', 'visible')
145+
.fadeTo(options.fadeInTime, 1, function() {
146+
self._trigger('afterFade');
147+
});
133148
});
134149

135150
self.element.children().last().slideUp(options.slidingTime, function() {
136151
$( this ).remove();
152+
self._trigger('afterScroll');
137153
});
138154
}
139155
}
@@ -143,6 +159,21 @@ $.widget( "ui.ticker", {
143159
}
144160
},
145161

162+
_setOption: function( key, value ) {
163+
$.Widget.prototype._setOption.apply( this, arguments );
164+
165+
switch (key) {
166+
case "active":
167+
if (value) {
168+
this.start();
169+
}
170+
else {
171+
this.stop();
172+
}
173+
break;
174+
}
175+
},
176+
146177
stop: function() {
147178
var self = this,
148179
options = self.options;
@@ -160,7 +191,7 @@ $.widget( "ui.ticker", {
160191

161192
options.active = true;
162193
if (self.timeoutId === null) {
163-
self.timeoutId = window.setTimeout(function() { self._scroll(); }, self.speed);
194+
self.timeoutId = window.setTimeout(function() { self._scroll(); }, options.initialTimeout);
164195
}
165196
}
166197
});

0 commit comments

Comments
 (0)