Skip to content

Commit 7528b3f

Browse files
author
Kai Schlamp
committed
Active option. Stop and start method. More tests.
1 parent 5cd1407 commit 7528b3f

File tree

3 files changed

+130
-36
lines changed

3 files changed

+130
-36
lines changed

tests/unit/ticker/ticker_defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
var ticker_defaults = {
66
disabled: false,
7+
active: true,
78
initialTimeout: 4000,
89
mouseOnTimeout: 8000,
910
mouseOffTimeout: 4000,

tests/unit/ticker/ticker_methods.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,73 @@ test("destroy", function() {
3333
equal( afterHtml, beforeHtml );
3434
});
3535

36+
test("initial stop", function() {
37+
expect(0);
38+
stop();
39+
40+
$("#ticker").ticker({
41+
initialTimeout: 100,
42+
next: function(lastItem) {
43+
ok(false, "ticker should not scroll after it was stopped");
44+
return lastItem;
45+
}
46+
});
47+
$("#ticker").ticker("stop");
48+
49+
setTimeout(function() { start(); }, 200);
50+
});
51+
52+
test("stop after scroll", function() {
53+
expect(1);
54+
stop();
55+
56+
var counter = 0;
57+
58+
$("#ticker").ticker({
59+
initialTimeout: 0,
60+
mouseOnTimeout: 100,
61+
mouseOffTimeout: 100,
62+
slidingTime: 0,
63+
fadeInTime: 0,
64+
next: function(lastItem) {
65+
if (counter == 0) {
66+
ok(true, "ticker scrolled one time");
67+
$("#ticker").ticker("stop");
68+
counter++;
69+
return lastItem;
70+
}
71+
else {
72+
ok(false, "ticker should not scroll after it was stopped");
73+
return lastItem;
74+
}
75+
}
76+
});
77+
78+
setTimeout(function() { start(); }, 300);
79+
});
80+
81+
test("start", function() {
82+
expect(1);
83+
stop();
84+
85+
var started = false;
86+
87+
$("#ticker").ticker({
88+
initialTimeout: 100,
89+
mouseOnTimeout: 100,
90+
mouseOffTimeout: 100,
91+
slidingTime: 0,
92+
fadeInTime: 0,
93+
next: function(lastItem) {
94+
if (started) {
95+
ok(true, "ticker scrolled after it was started");
96+
$("#ticker").ticker("stop");
97+
}
98+
}
99+
});
100+
$("#ticker").ticker("stop");
101+
window.setTimeout(function() { started = true; $("#ticker").ticker("start"); }, 200);
102+
window.setTimeout(function() { start(); }, 300);
103+
});
104+
36105
})(jQuery);

ui/jquery.ui.ticker.js

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
* jquery.ui.widget.js
1313
*/
1414
(function( $, undefined ) {
15+
16+
var itemClasses = "ui-ticker-content ui-widget-content ui-helper-reset ui-state-default ui-corner-all";
1517

1618
$.widget( "ui.ticker", {
1719
options: {
20+
active: true,
1821
initialTimeout: 4000,
1922
mouseOnTimeout: 8000,
2023
mouseOffTimeout: 4000,
@@ -27,6 +30,8 @@ $.widget( "ui.ticker", {
2730
var self = this,
2831
options = self.options;
2932

33+
self.timeoutId = null;
34+
3035
self.speed = options.mouseOffTimeout;
3136

3237
self.element
@@ -36,50 +41,44 @@ $.widget( "ui.ticker", {
3641
return;
3742
}
3843
self.speed = options.mouseOnTimeout;
39-
if (self.timeoutId !== undefined) {
40-
window.clearTimeout(timeoutId);
41-
self.timeoutId = window.setTimeout(function() { self.rotate(); }, self.speed);
44+
if (options.active && self.timeoutId !== null) {
45+
window.clearTimeout(self.timeoutId);
46+
self.timeoutId = window.setTimeout(function() { self._scroll(); }, self.speed);
4247
}
4348
})
4449
.bind( "mouseleave.ticker", function() {
4550
if ( options.disabled ) {
4651
return;
4752
}
4853
self.speed = options.mouseOffTimeout;
49-
if (self.timeoutId !== undefined) {
50-
window.clearTimeout(timeoutId);
51-
self.timeoutId = window.setTimeout(function() { self.rotate(); }, self.speed);
54+
if (options.active && self.timeoutId !== null) {
55+
window.clearTimeout(self.timeoutId);
56+
self.timeoutId = window.setTimeout(function() { self._scroll(); }, self.speed);
5257
}
5358
});
5459

55-
self._addItemClasses(self.element.children( "li" ));
60+
self.element.children( "li" ).addClass(itemClasses);
5661
self._addItemBindings(self.element.children( "li" ));
5762
},
5863

5964
_init: function() {
6065
var self = this,
6166
options = self.options;
6267

63-
window.setTimeout(function() { self.rotate() }, options.initialTimeout);
68+
if (options.active) {
69+
self.timeoutId = window.setTimeout(function() { self._scroll() }, options.initialTimeout);
70+
}
6471
},
6572

6673
destroy: function() {
6774
this.element.unbind(".ticker");
6875
this.element.children( "li" ).unbind(".ticker");
6976
this.element.removeClass( "ui-ticker ui-widget ui-corner-all" );
70-
this._removeItemClasses(this.element.children( "li" ));
77+
this.element.children( "li" ).removeClass(itemClasses + " ui-state-hover ui-state-focus");
7178

7279
return $.Widget.prototype.destroy.call( this );
7380
},
7481

75-
_addItemClasses: function(item) {
76-
item.addClass( "ui-ticker-content ui-widget-content ui-helper-reset ui-state-default ui-corner-all" )
77-
},
78-
79-
_removeItemClasses: function(item) {
80-
item.removeClass( "ui-ticker-content ui-widget-content ui-helper-reset ui-state-default ui-state-hover ui-state-focus ui-corner-all" )
81-
},
82-
8382
_addItemBindings: function(item) {
8483
var options = this.options;
8584

@@ -110,34 +109,59 @@ $.widget( "ui.ticker", {
110109
});
111110
},
112111

113-
rotate: function() {
112+
_scroll: function() {
114113
var self = this,
115114
options = self.options,
116115
newItem,
117116
lastItem;
118117

119-
lastItem = lastItem = self.element.children().last().clone();
120-
self._removeItemClasses(lastItem);
121-
122-
newItem = $( self.options.next(lastItem.get()) );
118+
lastItem = self.element.children().last().clone();
119+
lastItem.removeClass(itemClasses + " ui-state-hover ui-state-focus");
123120

124-
if (newItem !== null) {
125-
self._addItemClasses(newItem);
126-
self._addItemBindings(newItem);
127-
newItem
128-
.hide()
129-
.prependTo(self.element)
130-
.css('visibility', 'hidden')
131-
.slideDown(options.slidingTime, function() {
132-
$( this ).fadeTo(0, 0).css('visibility', 'visible').fadeTo(options.fadeInTime, 1);
133-
});
121+
if (self.options.next !== null) {
122+
newItem = $( self.options.next(lastItem.get()) );
123+
124+
if (newItem.length > 0) {
125+
newItem.addClass(itemClasses);
126+
self._addItemBindings(newItem);
127+
newItem
128+
.hide()
129+
.prependTo(self.element)
130+
.css('visibility', 'hidden')
131+
.slideDown(options.slidingTime, function() {
132+
$( this ).fadeTo(0, 0).css('visibility', 'visible').fadeTo(options.fadeInTime, 1);
133+
});
134134

135-
self.element.children().last().slideUp(options.slidingTime, function() {
136-
$( this ).remove();
137-
});
135+
self.element.children().last().slideUp(options.slidingTime, function() {
136+
$( this ).remove();
137+
});
138+
}
138139
}
139140

140-
self.timeoutId = window.setTimeout(function() { self.rotate(); }, self.speed);
141+
if (options.active) {
142+
self.timeoutId = window.setTimeout(function() { self._scroll(); }, self.speed);
143+
}
144+
},
145+
146+
stop: function() {
147+
var self = this,
148+
options = self.options;
149+
150+
options.active = false;
151+
if (self.timeoutId !== null) {
152+
window.clearTimeout(self.timeoutId);
153+
self.timeoutId = null;
154+
}
155+
},
156+
157+
start: function() {
158+
var self = this,
159+
options = self.options;
160+
161+
options.active = true;
162+
if (self.timeoutId === null) {
163+
self.timeoutId = window.setTimeout(function() { self._scroll(); }, self.speed);
164+
}
141165
}
142166
});
143167

0 commit comments

Comments
 (0)