-
Notifications
You must be signed in to change notification settings - Fork 9
/
tools.tabs.slideshow.js
193 lines (143 loc) · 4.58 KB
/
tools.tabs.slideshow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* jQuery TOOLS plugin :: tabs.slideshow 1.0.2
*
* Copyright (c) 2009 Tero Piirainen
* http://flowplayer.org/tools/tabs.html#slideshow
*
* Dual licensed under MIT and GPL 2+ licenses
* http://www.opensource.org/licenses
*
* Launch : September 2009
* Date: ${date}
* Revision: ${revision}
*/
(function($) {
var t = $.tools.tabs;
t.plugins = t.plugins || {};
t.plugins.slideshow = {
version: '1.0.2',
// CALLBACKS: onBeforePlay, onPlay, onBeforePause, onPause,
conf: {
next: '.forward',
prev: '.backward',
disabledClass: 'disabled',
autoplay: false,
autopause: true,
interval: 3000,
clickable: true,
api: false
}
};
// jQuery plugin implementation
$.prototype.slideshow = function(conf) {
var globals = $.extend({}, t.plugins.slideshow.conf),
len = this.length,
ret;
conf = $.extend(globals, conf);
this.each(function() {
var tabs = $(this), api = tabs.tabs(), $api = $(api), ret = api;
// bind all callbacks from configuration
$.each(conf, function(name, fn) {
if ($.isFunction(fn)) { api.bind(name, fn); }
});
function find(query) {
return len == 1 ? $(query) : tabs.parent().find(query);
}
var nextButton = find(conf.next).click(function() {
api.next();
});
var prevButton = find(conf.prev).click(function() {
api.prev();
});
// interval stuff
var timer, hoverTimer, startTimer, stopped = false;
// extend the Tabs API with slideshow methods
$.extend(api, {
play: function() {
// do not start additional timer if already exists
if (timer) { return; }
// onBeforePlay
var e = $.Event("onBeforePlay");
$api.trigger(e);
if (e.isDefaultPrevented()) { return api; }
stopped = false;
// construct new timer
timer = setInterval(api.next, conf.interval);
// onPlay
$api.trigger("onPlay");
api.next();
},
pause: function() {
if (!timer) { return api; }
// onBeforePause
var e = $.Event("onBeforePause");
$api.trigger(e);
if (e.isDefaultPrevented()) { return api; }
timer = clearInterval(timer);
startTimer = clearInterval(startTimer);
// onPause
$api.trigger("onPause");
},
// when stopped - mouseover won't restart
stop: function() {
api.pause();
stopped = true;
},
onBeforePlay: function(fn) {
return api.bind("onBeforePlay", fn);
},
onPlay: function(fn) {
return api.bind("onPlay", fn);
},
onBeforePause: function(fn) {
return api.bind("onBeforePause", fn);
},
onPause: function(fn) {
return api.bind("onPause", fn);
}
});
/* when mouse enters, slideshow stops */
if (conf.autopause) {
var els = api.getTabs().add(nextButton).add(prevButton).add(api.getPanes());
els.hover(function() {
api.pause();
hoverTimer = clearInterval(hoverTimer);
}, function() {
if (!stopped) {
hoverTimer = setTimeout(api.play, conf.interval);
}
});
}
if (conf.autoplay) {
startTimer = setTimeout(api.play, conf.interval);
} else {
api.stop();
}
if (conf.clickable) {
api.getPanes().click(function() {
api.next();
});
}
// manage disabling of next/prev buttons
if (!api.getConf().rotate) {
var cls = conf.disabledClass;
if (!api.getIndex()) {
prevButton.addClass(cls);
}
api.onBeforeClick(function(e, i) {
if (!i) {
prevButton.addClass(cls);
} else {
prevButton.removeClass(cls);
if (i == api.getTabs().length -1) {
nextButton.addClass(cls);
} else {
nextButton.removeClass(cls);
}
}
});
}
});
return conf.api ? ret : this;
};
})(jQuery);