|
25 | 25 | (function(window, $) {
|
26 | 26 |
|
27 | 27 | var NI = window.NI,
|
28 |
| - generate, |
29 |
| - events; |
| 28 | + generate; |
30 | 29 |
|
31 | 30 | generate = {
|
32 | 31 | carousel: function() {
|
|
60 | 59 | }
|
61 | 60 | };
|
62 | 61 |
|
63 |
| - events = { |
64 |
| - keydown: function(e) { |
65 |
| - var key; |
66 |
| - key = e.keyCode || e.which; |
67 |
| - switch (key) { |
68 |
| - case NI.co.keyboard.LEFT: |
69 |
| - e.preventDefault(); |
70 |
| - e.data.instance.prev(); |
71 |
| - break; |
72 |
| - case NI.co.keyboard.RIGHT: |
73 |
| - e.preventDefault(); |
74 |
| - e.data.instance.next(); |
75 |
| - break; |
76 |
| - } |
77 |
| - } |
78 |
| - }; |
79 |
| - |
80 | 62 | function Carousel(options) {
|
81 |
| - var me, o, $c, $elements; |
| 63 | + var me, o, $c, $elements, events; |
82 | 64 |
|
83 | 65 | me = this;
|
84 | 66 | o = $.extend({
|
|
139 | 121 | me.begin(true);
|
140 | 122 | }
|
141 | 123 |
|
| 124 | + function current() { |
| 125 | + return $elements.scroll.children("."+o.panelClass + "."+ o.activeClass); |
| 126 | + } |
| 127 | + |
142 | 128 | function targetPosition($panel) {
|
143 | 129 | if (o.viewportDimensions && typeof o.viewportDimensions.width === "number") {
|
144 | 130 | return -($panel.index()*o.viewportDimensions.width);
|
|
147 | 133 | }
|
148 | 134 | }
|
149 | 135 |
|
150 |
| - function moveTo($panel, speed) { |
| 136 | + function moveTo($panel, noAnimate) { |
151 | 137 |
|
152 | 138 | if (!$panel.length) {
|
153 | 139 | return me;
|
154 | 140 | }
|
155 | 141 |
|
156 | 142 | $elements.scroll.animate({left: targetPosition($panel)},
|
157 |
| - speed, o.easing, function() { |
| 143 | + (noAnimate ? 0 : o.speed), o.easing, function() { |
158 | 144 | var info;
|
159 | 145 |
|
160 | 146 | if ($panel.hasClass(o.cloneClass)) {
|
161 |
| - if ($panel.hasClass("beginning")) { |
| 147 | + if ($panel.hasClass("head")) { |
162 | 148 | $panel = $elements.scroll.children("."+o.panelClass).not("."+o.cloneClass).last();
|
163 | 149 | } else {
|
164 | 150 | $panel = $elements.scroll.children("."+o.panelClass).not("."+o.cloneClass).first();
|
|
168 | 154 |
|
169 | 155 | $panel.addClass(o.activeClass).siblings().removeClass(o.activeClass);
|
170 | 156 |
|
171 |
| - info = me.getInfo(); |
| 157 | + info = me.info(); |
172 | 158 |
|
173 | 159 | if (!o.circular) {
|
174 | 160 | if ($elements.prevBtn.length) {
|
|
187 | 173 | return me;
|
188 | 174 | }
|
189 | 175 |
|
190 |
| - function current() { |
191 |
| - return $elements.scroll.children("."+o.panelClass + "."+ o.activeClass); |
192 |
| - } |
| 176 | + events = { |
| 177 | + keydown: function(e) { |
| 178 | + var key; |
| 179 | + key = e.keyCode || e.which; |
| 180 | + switch (key) { |
| 181 | + case NI.co.keyboard.LEFT: |
| 182 | + e.preventDefault(); |
| 183 | + e.data.instance.prev(); |
| 184 | + break; |
| 185 | + case NI.co.keyboard.RIGHT: |
| 186 | + e.preventDefault(); |
| 187 | + e.data.instance.next(); |
| 188 | + break; |
| 189 | + } |
| 190 | + } |
| 191 | + }; |
193 | 192 |
|
194 | 193 | function registerBtn(key) {
|
195 | 194 | var $btn;
|
|
198 | 197 | return $btn;
|
199 | 198 | }
|
200 | 199 |
|
201 |
| - this.registerPrevBtn = function() { |
202 |
| - return registerBtn("prevBtn"); |
203 |
| - }; |
204 |
| - |
205 |
| - this.registerNextBtn = function() { |
206 |
| - return registerBtn("nextBtn"); |
207 |
| - }; |
208 |
| - |
209 |
| - this.get = function() { |
210 |
| - return $c; |
| 200 | + this.info = function() { |
| 201 | + var $panels, index; |
| 202 | + $panels = $elements.scroll.children("."+o.panelClass).not("."+o.cloneClass); |
| 203 | + $panels.each(function(i, panel) { |
| 204 | + if ($(panel).hasClass(o.activeClass)) { |
| 205 | + index = i; |
| 206 | + return false; |
| 207 | + } |
| 208 | + }); |
| 209 | + return { |
| 210 | + index: index, |
| 211 | + total: $panels.length |
| 212 | + }; |
211 | 213 | };
|
212 | 214 |
|
213 | 215 | this.add = function($panel) {
|
|
221 | 223 | }
|
222 | 224 | }
|
223 | 225 | $elements.scroll.append($panel);
|
| 226 | + return this; |
224 | 227 | };
|
225 | 228 |
|
226 |
| - this.getInfo = function() { |
227 |
| - var $panels, index; |
228 |
| - $panels = $elements.scroll.children("."+o.panelClass).not("."+o.cloneClass); |
229 |
| - $panels.each(function(i, panel) { |
230 |
| - if ($(panel).hasClass(o.activeClass)) { |
231 |
| - index = i; |
232 |
| - return false; |
233 |
| - } |
234 |
| - }); |
235 |
| - return { |
236 |
| - index: index, |
237 |
| - total: $panels.length |
238 |
| - }; |
| 229 | + this.registerPrevBtn = function() { |
| 230 | + return registerBtn("prevBtn"); |
| 231 | + }; |
| 232 | + |
| 233 | + this.registerNextBtn = function() { |
| 234 | + return registerBtn("nextBtn"); |
239 | 235 | };
|
240 | 236 |
|
| 237 | + |
| 238 | + this.get = function() { |
| 239 | + return $c; |
| 240 | + }; |
| 241 | + |
241 | 242 | this.refresh = function() {
|
242 | 243 | var $panels;
|
243 | 244 | if (o.circular) {
|
244 | 245 | $elements.scroll.children("."+o.cloneClass).remove();
|
245 | 246 | $panels = $elements.scroll.children("."+o.panelClass);
|
246 |
| - $panels.last().clone(false).addClass(o.cloneClass +" beginning").prependTo($elements.scroll); |
247 |
| - $panels.first().clone(false).addClass(o.cloneClass +" end").appendTo($elements.scroll); |
| 247 | + $panels.last().clone(false).addClass(o.cloneClass +" head").prependTo($elements.scroll); |
| 248 | + $panels.first().clone(false).addClass(o.cloneClass +" tail").appendTo($elements.scroll); |
248 | 249 | }
|
249 | 250 | return this;
|
250 | 251 | };
|
| 252 | + |
251 | 253 |
|
252 |
| - this.begin = function(no_animate) { |
| 254 | + this.begin = function(noAnimate) { |
253 | 255 | this.refresh();
|
254 |
| - return moveTo($elements.scroll.children("."+o.panelClass).not("."+o.cloneClass).first(), no_animate ? 0 : o.speed); |
| 256 | + return moveTo($elements.scroll.children("."+o.panelClass).not("."+o.cloneClass).first(), noAnimate); |
255 | 257 | };
|
256 | 258 |
|
257 |
| - this.next = function(no_animate) { |
258 |
| - return moveTo(current().next("."+o.panelClass), no_animate ? 0 : o.speed); |
| 259 | + this.end = function(noAnimate) { |
| 260 | + return moveTo($elements.scroll.children("."+o.panelClass).not("."+o.cloneClass).last(), noAnimate); |
259 | 261 | };
|
260 | 262 |
|
261 |
| - this.prev = function(no_animate) { |
262 |
| - return moveTo(current().prev("."+o.panelClass), no_animate ? 0 : o.speed); |
| 263 | + this.toIndex = function(index, noAnimate) { |
| 264 | + $elements.scroll.children("."+o.panelClass).not("."+o.cloneClass).each(function(i, panel) { |
| 265 | + if (i === index) { |
| 266 | + moveTo($(panel), noAnimate); |
| 267 | + return false; |
| 268 | + } |
| 269 | + }); |
| 270 | + return this; |
263 | 271 | };
|
264 | 272 |
|
265 |
| - //TODO |
| 273 | + this.next = function(noAnimate) { |
| 274 | + return moveTo(current().next("."+o.panelClass), noAnimate); |
| 275 | + }; |
| 276 | + |
| 277 | + this.prev = function(noAnimate) { |
| 278 | + return moveTo(current().prev("."+o.panelClass), noAnimate); |
| 279 | + }; |
| 280 | + |
| 281 | + |
266 | 282 | this.destroy = function() {
|
267 |
| - |
| 283 | + if (o.keyboard) { |
| 284 | + $(window.document).unbind("keydown.carousel", events.keydown); |
| 285 | + } |
| 286 | + if ($elements.prevBtn.length) { |
| 287 | + $elements.prevBtn.unbind("click"); |
| 288 | + } |
| 289 | + if ($elements.nextBtn.length) { |
| 290 | + $elements.nextBtn.unbind("click"); |
| 291 | + } |
| 292 | + $c.remove(); |
268 | 293 | };
|
269 | 294 |
|
270 | 295 | init();
|
|
0 commit comments