]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | /**\r |
2 | * Carousels, like [tabs](#!/guide/tabs), are a great way to allow the user to swipe through multiple full-screen pages.\r | |
3 | * A Carousel shows only one of its pages at a time but allows you to swipe through with your finger.\r | |
4 | *\r | |
5 | * Carousels can be oriented either horizontally or vertically and are easy to configure - they just work like any other\r | |
6 | * Container. Here's how to set up a simple horizontal Carousel:\r | |
7 | *\r | |
8 | * @example\r | |
9 | * Ext.create('Ext.Carousel', {\r | |
10 | * fullscreen: true,\r | |
11 | *\r | |
12 | * defaults: {\r | |
13 | * styleHtmlContent: true\r | |
14 | * },\r | |
15 | *\r | |
16 | * items: [\r | |
17 | * {\r | |
18 | * html : 'Item 1',\r | |
19 | * style: 'background-color: #5E99CC'\r | |
20 | * },\r | |
21 | * {\r | |
22 | * html : 'Item 2',\r | |
23 | * style: 'background-color: #759E60'\r | |
24 | * },\r | |
25 | * {\r | |
26 | * html : 'Item 3'\r | |
27 | * }\r | |
28 | * ]\r | |
29 | * });\r | |
30 | *\r | |
31 | * We can also make Carousels orient themselves vertically:\r | |
32 | *\r | |
33 | * @example preview\r | |
34 | * Ext.create('Ext.Carousel', {\r | |
35 | * fullscreen: true,\r | |
36 | * direction: 'vertical',\r | |
37 | *\r | |
38 | * defaults: {\r | |
39 | * styleHtmlContent: true\r | |
40 | * },\r | |
41 | *\r | |
42 | * items: [\r | |
43 | * {\r | |
44 | * html : 'Item 1',\r | |
45 | * style: 'background-color: #759E60'\r | |
46 | * },\r | |
47 | * {\r | |
48 | * html : 'Item 2',\r | |
49 | * style: 'background-color: #5E99CC'\r | |
50 | * }\r | |
51 | * ]\r | |
52 | * });\r | |
53 | *\r | |
54 | * ### Common Configurations\r | |
55 | * * {@link #ui} defines the style of the carousel\r | |
56 | * * {@link #direction} defines the direction of the carousel\r | |
57 | * * {@link #indicator} defines if the indicator show be shown\r | |
58 | *\r | |
59 | * ### Useful Methods\r | |
60 | * * {@link #next} moves to the next card\r | |
61 | * * {@link #previous} moves to the previous card\r | |
62 | * * {@link #setActiveItem} moves to the passed card\r | |
63 | */\r | |
64 | Ext.define('Ext.carousel.Carousel', {\r | |
65 | extend: 'Ext.Container',\r | |
66 | \r | |
67 | alternateClassName: 'Ext.Carousel',\r | |
68 | \r | |
69 | xtype: 'carousel',\r | |
70 | \r | |
71 | requires: [\r | |
72 | 'Ext.fx.easing.EaseOut',\r | |
73 | 'Ext.carousel.Item',\r | |
74 | 'Ext.carousel.Indicator',\r | |
75 | 'Ext.util.TranslatableGroup'\r | |
76 | ],\r | |
77 | \r | |
78 | config: {\r | |
79 | /**\r | |
80 | * @cfg layout\r | |
81 | * Hide layout config in Carousel. It only causes confusion.\r | |
82 | * @accessor\r | |
83 | * @private\r | |
84 | */\r | |
85 | \r | |
86 | /**\r | |
87 | * @cfg\r | |
88 | * @inheritdoc\r | |
89 | */\r | |
90 | baseCls: 'x-carousel',\r | |
91 | \r | |
92 | /**\r | |
93 | * @cfg {String} direction\r | |
94 | * The direction of the Carousel, either 'horizontal' or 'vertical'.\r | |
95 | * @accessor\r | |
96 | */\r | |
97 | direction: 'horizontal',\r | |
98 | \r | |
99 | directionLock: false,\r | |
100 | \r | |
101 | animation: {\r | |
102 | duration: 250,\r | |
103 | easing: {\r | |
104 | type: 'ease-out'\r | |
105 | }\r | |
106 | },\r | |
107 | \r | |
108 | /**\r | |
109 | * @cfg draggable\r | |
110 | * @hide\r | |
111 | */\r | |
112 | \r | |
113 | /**\r | |
114 | * @cfg {Boolean} indicator\r | |
115 | * Provides an indicator while toggling between child items to let the user\r | |
116 | * know where they are in the card stack.\r | |
117 | * @accessor\r | |
118 | */\r | |
119 | indicator: true,\r | |
120 | \r | |
121 | /**\r | |
122 | * @cfg {String} ui\r | |
123 | * Style options for Carousel. Default is 'dark'. 'light' is also available.\r | |
124 | * @accessor\r | |
125 | */\r | |
126 | ui: 'dark',\r | |
127 | \r | |
128 | itemConfig: {},\r | |
129 | \r | |
130 | bufferSize: 1,\r | |
131 | \r | |
132 | itemLength: null\r | |
133 | },\r | |
134 | \r | |
135 | itemLength: 0,\r | |
136 | \r | |
137 | offset: 0,\r | |
138 | \r | |
139 | flickStartOffset: 0,\r | |
140 | \r | |
141 | flickStartTime: 0,\r | |
142 | \r | |
143 | dragDirection: 0,\r | |
144 | \r | |
145 | count: 0,\r | |
146 | \r | |
147 | painted: false,\r | |
148 | \r | |
149 | activeIndex: -1,\r | |
150 | \r | |
151 | beforeInitialize: function() {\r | |
152 | var me = this;\r | |
153 | \r | |
154 | me.element.on({\r | |
155 | resize: 'onSizeChange',\r | |
156 | dragstart: 'onDragStart',\r | |
157 | drag: 'onDrag',\r | |
158 | dragend: 'onDragEnd',\r | |
159 | scope: me\r | |
160 | });\r | |
161 | \r | |
162 | me.carouselItems = [];\r | |
163 | \r | |
164 | me.orderedCarouselItems = [];\r | |
165 | \r | |
166 | me.inactiveCarouselItems = [];\r | |
167 | \r | |
168 | me.hiddenTranslation = 0;\r | |
169 | },\r | |
170 | \r | |
171 | updateBufferSize: function(size) {\r | |
172 | var ItemClass = Ext.carousel.Item,\r | |
173 | total = size * 2 + 1,\r | |
174 | isRendered = this.isRendered(),\r | |
175 | innerElement = this.innerElement,\r | |
176 | items = this.carouselItems,\r | |
177 | ln = items.length,\r | |
178 | itemConfig = this.getItemConfig(),\r | |
179 | itemLength = this.getItemLength(),\r | |
180 | direction = this.getDirection(),\r | |
181 | setterName = direction === 'horizontal' ? 'setWidth' : 'setHeight',\r | |
182 | i, item;\r | |
183 | \r | |
184 | for (i = ln; i < total; i++) {\r | |
185 | item = Ext.factory(itemConfig, ItemClass);\r | |
186 | \r | |
187 | if (itemLength) {\r | |
188 | item[setterName].call(item, itemLength);\r | |
189 | }\r | |
190 | item.setLayoutSizeFlags(this.LAYOUT_BOTH);\r | |
191 | items.push(item);\r | |
192 | innerElement.append(item.renderElement);\r | |
193 | \r | |
194 | if (isRendered && item.setRendered(true)) {\r | |
195 | item.fireEvent('renderedchange', this, item, true);\r | |
196 | }\r | |
197 | }\r | |
198 | \r | |
199 | this.getTranslatable().setActiveIndex(size);\r | |
200 | },\r | |
201 | \r | |
202 | setRendered: function(rendered) {\r | |
203 | var wasRendered = this.rendered;\r | |
204 | \r | |
205 | if (rendered !== wasRendered) {\r | |
206 | this.rendered = rendered;\r | |
207 | \r | |
208 | var items = this.items.items,\r | |
209 | carouselItems = this.carouselItems,\r | |
210 | i, ln, item;\r | |
211 | \r | |
212 | for (i = 0,ln = items.length; i < ln; i++) {\r | |
213 | item = items[i];\r | |
214 | \r | |
215 | if (!item.isInnerItem()) {\r | |
216 | item.setRendered(rendered);\r | |
217 | }\r | |
218 | }\r | |
219 | \r | |
220 | for (i = 0,ln = carouselItems.length; i < ln; i++) {\r | |
221 | carouselItems[i].setRendered(rendered);\r | |
222 | }\r | |
223 | \r | |
224 | return true;\r | |
225 | }\r | |
226 | \r | |
227 | return false;\r | |
228 | },\r | |
229 | \r | |
230 | onSizeChange: function() {\r | |
231 | this.refreshSizing();\r | |
232 | this.refreshCarouselItems();\r | |
233 | this.refreshActiveItem();\r | |
234 | },\r | |
235 | \r | |
236 | onItemAdd: function(item, index) {\r | |
237 | this.callParent(arguments);\r | |
238 | \r | |
239 | var innerIndex = this.getInnerItems().indexOf(item),\r | |
240 | indicator = this.getIndicator();\r | |
241 | \r | |
242 | if (indicator && item.isInnerItem()) {\r | |
243 | indicator.addIndicator();\r | |
244 | }\r | |
245 | \r | |
246 | if (innerIndex <= this.getActiveIndex()) {\r | |
247 | this.refreshActiveIndex();\r | |
248 | }\r | |
249 | \r | |
250 | if (this.isIndexDirty(innerIndex) && !this.isItemsInitializing) {\r | |
251 | this.refreshActiveItem();\r | |
252 | }\r | |
253 | },\r | |
254 | \r | |
255 | doItemLayoutAdd: function(item) {\r | |
256 | if (item.isInnerItem()) {\r | |
257 | return;\r | |
258 | }\r | |
259 | \r | |
260 | this.callParent(arguments);\r | |
261 | },\r | |
262 | \r | |
263 | onItemRemove: function(item, index) {\r | |
264 | this.callParent(arguments);\r | |
265 | \r | |
266 | var innerIndex = this.getInnerItems().indexOf(item),\r | |
267 | indicator = this.getIndicator(),\r | |
268 | carouselItems = this.carouselItems,\r | |
269 | i, ln, carouselItem;\r | |
270 | \r | |
271 | if (item.isInnerItem() && indicator) {\r | |
272 | indicator.removeIndicator();\r | |
273 | }\r | |
274 | \r | |
275 | if (innerIndex <= this.getActiveIndex()) {\r | |
276 | this.refreshActiveIndex();\r | |
277 | }\r | |
278 | \r | |
279 | if (this.isIndexDirty(innerIndex)) {\r | |
280 | for (i = 0,ln = carouselItems.length; i < ln; i++) {\r | |
281 | carouselItem = carouselItems[i];\r | |
282 | \r | |
283 | if (carouselItem.getComponent() === item) {\r | |
284 | carouselItem.setComponent(null);\r | |
285 | }\r | |
286 | }\r | |
287 | \r | |
288 | this.refreshActiveItem();\r | |
289 | }\r | |
290 | },\r | |
291 | \r | |
292 | doItemLayoutRemove: function(item) {\r | |
293 | if (item.isInnerItem()) {\r | |
294 | return;\r | |
295 | }\r | |
296 | \r | |
297 | this.callParent(arguments);\r | |
298 | },\r | |
299 | \r | |
300 | onInnerItemMove: function(item, toIndex, fromIndex) {\r | |
301 | if ((this.isIndexDirty(toIndex) || this.isIndexDirty(fromIndex))) {\r | |
302 | this.refreshActiveItem();\r | |
303 | }\r | |
304 | },\r | |
305 | \r | |
306 | doItemLayoutMove: function(item) {\r | |
307 | if (item.isInnerItem()) {\r | |
308 | return;\r | |
309 | }\r | |
310 | \r | |
311 | this.callParent(arguments);\r | |
312 | },\r | |
313 | \r | |
314 | isIndexDirty: function(index) {\r | |
315 | var activeIndex = this.getActiveIndex(),\r | |
316 | bufferSize = this.getBufferSize();\r | |
317 | \r | |
318 | return (index >= activeIndex - bufferSize && index <= activeIndex + bufferSize);\r | |
319 | },\r | |
320 | \r | |
321 | getTranslatable: function() {\r | |
322 | var me = this,\r | |
323 | translatable = me.translatable;\r | |
324 | \r | |
325 | if (!translatable) {\r | |
326 | me.translatable = translatable = new Ext.util.TranslatableGroup();\r | |
327 | translatable.setItems(me.orderedCarouselItems);\r | |
328 | translatable.on('animationend', 'onAnimationEnd', me);\r | |
329 | }\r | |
330 | \r | |
331 | return translatable;\r | |
332 | },\r | |
333 | \r | |
334 | onDragStart: function(e) {\r | |
335 | var direction = this.getDirection(),\r | |
336 | absDeltaX = e.absDeltaX,\r | |
337 | absDeltaY = e.absDeltaY,\r | |
338 | directionLock = this.getDirectionLock();\r | |
339 | \r | |
340 | this.isDragging = true;\r | |
341 | \r | |
342 | if (directionLock) {\r | |
343 | if ((direction === 'horizontal' && absDeltaX > absDeltaY) ||\r | |
344 | (direction === 'vertical' && absDeltaY > absDeltaX)) {\r | |
345 | e.stopPropagation();\r | |
346 | }\r | |
347 | else {\r | |
348 | this.isDragging = false;\r | |
349 | return;\r | |
350 | }\r | |
351 | }\r | |
352 | \r | |
353 | this.getTranslatable().stopAnimation();\r | |
354 | \r | |
355 | this.dragStartOffset = this.offset;\r | |
356 | this.dragDirection = 0;\r | |
357 | },\r | |
358 | \r | |
359 | onDrag: function(e) {\r | |
360 | if (!this.isDragging) {\r | |
361 | return;\r | |
362 | }\r | |
363 | \r | |
364 | var startOffset = this.dragStartOffset,\r | |
365 | direction = this.getDirection(),\r | |
366 | delta = direction === 'horizontal' ? e.deltaX : e.deltaY,\r | |
367 | lastOffset = this.offset,\r | |
368 | flickStartTime = this.flickStartTime,\r | |
369 | dragDirection = this.dragDirection,\r | |
370 | now = Ext.Date.now(),\r | |
371 | currentActiveIndex = this.getActiveIndex(),\r | |
372 | maxIndex = this.getMaxItemIndex(),\r | |
373 | lastDragDirection = dragDirection,\r | |
374 | offset;\r | |
375 | \r | |
376 | if ((currentActiveIndex === 0 && delta > 0) || (currentActiveIndex === maxIndex && delta < 0)) {\r | |
377 | delta *= 0.5;\r | |
378 | }\r | |
379 | \r | |
380 | offset = startOffset + delta;\r | |
381 | \r | |
382 | if (offset > lastOffset) {\r | |
383 | dragDirection = 1;\r | |
384 | }\r | |
385 | else if (offset < lastOffset) {\r | |
386 | dragDirection = -1;\r | |
387 | }\r | |
388 | \r | |
389 | if (dragDirection !== lastDragDirection || (now - flickStartTime) > 300) {\r | |
390 | this.flickStartOffset = lastOffset;\r | |
391 | this.flickStartTime = now;\r | |
392 | }\r | |
393 | \r | |
394 | this.dragDirection = dragDirection;\r | |
395 | \r | |
396 | this.setOffset(offset);\r | |
397 | },\r | |
398 | \r | |
399 | onDragEnd: function(e) {\r | |
400 | if (!this.isDragging) {\r | |
401 | return;\r | |
402 | }\r | |
403 | \r | |
404 | this.onDrag(e);\r | |
405 | \r | |
406 | this.isDragging = false;\r | |
407 | \r | |
408 | var now = Ext.Date.now(),\r | |
409 | itemLength = this.itemLength,\r | |
410 | threshold = itemLength / 2,\r | |
411 | offset = this.offset,\r | |
412 | activeIndex = this.getActiveIndex(),\r | |
413 | maxIndex = this.getMaxItemIndex(),\r | |
414 | animationDirection = 0,\r | |
415 | flickDistance = offset - this.flickStartOffset,\r | |
416 | flickDuration = now - this.flickStartTime,\r | |
417 | indicator = this.getIndicator(),\r | |
418 | velocity;\r | |
419 | \r | |
420 | if (flickDuration > 0 && Math.abs(flickDistance) >= 10) {\r | |
421 | velocity = flickDistance / flickDuration;\r | |
422 | \r | |
423 | if (Math.abs(velocity) >= 1) {\r | |
424 | if (velocity < 0 && activeIndex < maxIndex) {\r | |
425 | animationDirection = -1;\r | |
426 | }\r | |
427 | else if (velocity > 0 && activeIndex > 0) {\r | |
428 | animationDirection = 1;\r | |
429 | }\r | |
430 | }\r | |
431 | }\r | |
432 | \r | |
433 | if (animationDirection === 0) {\r | |
434 | if (activeIndex < maxIndex && offset < -threshold) {\r | |
435 | animationDirection = -1;\r | |
436 | }\r | |
437 | else if (activeIndex > 0 && offset > threshold) {\r | |
438 | animationDirection = 1;\r | |
439 | }\r | |
440 | }\r | |
441 | \r | |
442 | if (indicator) {\r | |
443 | indicator.setActiveIndex(activeIndex - animationDirection);\r | |
444 | }\r | |
445 | \r | |
446 | this.animationDirection = animationDirection;\r | |
447 | \r | |
448 | this.setOffsetAnimated(animationDirection * itemLength);\r | |
449 | },\r | |
450 | \r | |
451 | applyAnimation: function(animation) {\r | |
452 | animation.easing = Ext.factory(animation.easing, Ext.fx.easing.EaseOut);\r | |
453 | \r | |
454 | return animation;\r | |
455 | },\r | |
456 | \r | |
457 | updateDirection: function(direction) {\r | |
458 | var indicator = this.getIndicator();\r | |
459 | \r | |
460 | this.currentAxis = (direction === 'horizontal') ? 'x' : 'y';\r | |
461 | \r | |
462 | if (indicator) {\r | |
463 | indicator.setDirection(direction);\r | |
464 | }\r | |
465 | },\r | |
466 | \r | |
467 | /**\r | |
468 | * @private\r | |
469 | * @chainable\r | |
470 | */\r | |
471 | setOffset: function(offset) {\r | |
472 | this.offset = offset;\r | |
473 | \r | |
474 | if (Ext.isNumber(this.itemOffset)) {\r | |
475 | this.getTranslatable().translateAxis(this.currentAxis, offset + this.itemOffset);\r | |
476 | }\r | |
477 | \r | |
478 | return this;\r | |
479 | },\r | |
480 | \r | |
481 | /**\r | |
482 | * @private\r | |
483 | * @return {Ext.carousel.Carousel} this\r | |
484 | * @chainable\r | |
485 | */\r | |
486 | setOffsetAnimated: function(offset) {\r | |
487 | var indicator = this.getIndicator();\r | |
488 | \r | |
489 | if (indicator) {\r | |
490 | indicator.setActiveIndex(this.getActiveIndex() - this.animationDirection);\r | |
491 | }\r | |
492 | \r | |
493 | this.offset = offset;\r | |
494 | \r | |
495 | this.getTranslatable().translateAxis(this.currentAxis, offset + this.itemOffset, this.getAnimation());\r | |
496 | \r | |
497 | return this;\r | |
498 | },\r | |
499 | \r | |
500 | onAnimationEnd: function(translatable) {\r | |
501 | var currentActiveIndex = this.getActiveIndex(),\r | |
502 | animationDirection = this.animationDirection,\r | |
503 | axis = this.currentAxis,\r | |
504 | currentOffset = translatable[axis],\r | |
505 | itemLength = this.itemLength,\r | |
506 | offset;\r | |
507 | \r | |
508 | if (animationDirection === -1) {\r | |
509 | offset = itemLength + currentOffset;\r | |
510 | }\r | |
511 | else if (animationDirection === 1) {\r | |
512 | offset = currentOffset - itemLength;\r | |
513 | }\r | |
514 | else {\r | |
515 | offset = currentOffset;\r | |
516 | }\r | |
517 | \r | |
518 | offset -= this.itemOffset;\r | |
519 | this.offset = offset;\r | |
520 | this.setActiveItem(currentActiveIndex - animationDirection);\r | |
521 | },\r | |
522 | \r | |
523 | refresh: function() {\r | |
524 | this.refreshSizing();\r | |
525 | this.refreshActiveItem();\r | |
526 | },\r | |
527 | \r | |
528 | refreshSizing: function() {\r | |
529 | var element = this.element,\r | |
530 | itemLength = this.getItemLength(),\r | |
531 | translatableItemLength = {\r | |
532 | x: 0,\r | |
533 | y: 0\r | |
534 | },\r | |
535 | itemOffset, containerSize;\r | |
536 | \r | |
537 | if (this.getDirection() === 'horizontal') {\r | |
538 | containerSize = element.getWidth();\r | |
539 | }\r | |
540 | else {\r | |
541 | containerSize = element.getHeight();\r | |
542 | }\r | |
543 | \r | |
544 | this.hiddenTranslation = -containerSize;\r | |
545 | \r | |
546 | if (itemLength === null) {\r | |
547 | itemLength = containerSize;\r | |
548 | itemOffset = 0;\r | |
549 | }\r | |
550 | else {\r | |
551 | itemOffset = (containerSize - itemLength) / 2;\r | |
552 | }\r | |
553 | \r | |
554 | this.itemLength = itemLength;\r | |
555 | this.itemOffset = itemOffset;\r | |
556 | translatableItemLength[this.currentAxis] = itemLength;\r | |
557 | this.getTranslatable().setItemLength(translatableItemLength);\r | |
558 | },\r | |
559 | \r | |
560 | refreshOffset: function() {\r | |
561 | this.setOffset(this.offset);\r | |
562 | },\r | |
563 | \r | |
564 | refreshActiveItem: function() {\r | |
565 | this.updateActiveItem(this.getActiveItem());\r | |
566 | },\r | |
567 | \r | |
568 | /**\r | |
569 | * Returns the index of the currently active card.\r | |
570 | * @return {Number} The index of the currently active card.\r | |
571 | */\r | |
572 | getActiveIndex: function() {\r | |
573 | return this.activeIndex;\r | |
574 | },\r | |
575 | \r | |
576 | refreshActiveIndex: function() {\r | |
577 | this.activeIndex = this.getInnerItemIndex(this.getActiveItem());\r | |
578 | },\r | |
579 | \r | |
580 | refreshCarouselItems: function() {\r | |
581 | var items = this.carouselItems,\r | |
582 | i, ln, item;\r | |
583 | \r | |
584 | for (i = 0,ln = items.length; i < ln; i++) {\r | |
585 | item = items[i];\r | |
586 | item.getTranslatable().refresh();\r | |
587 | }\r | |
588 | \r | |
589 | this.refreshInactiveCarouselItems();\r | |
590 | },\r | |
591 | \r | |
592 | refreshInactiveCarouselItems: function() {\r | |
593 | var items = this.inactiveCarouselItems,\r | |
594 | hiddenTranslation = this.hiddenTranslation,\r | |
595 | axis = this.currentAxis,\r | |
596 | i, ln, item;\r | |
597 | \r | |
598 | for (i = 0,ln = items.length; i < ln; i++) {\r | |
599 | item = items[i];\r | |
600 | item.translateAxis(axis, hiddenTranslation);\r | |
601 | }\r | |
602 | },\r | |
603 | \r | |
604 | /**\r | |
605 | * @private\r | |
606 | * @return {Number}\r | |
607 | */\r | |
608 | getMaxItemIndex: function() {\r | |
609 | return this.innerItems.length - 1;\r | |
610 | },\r | |
611 | \r | |
612 | /**\r | |
613 | * @private\r | |
614 | * @return {Number}\r | |
615 | */\r | |
616 | getInnerItemIndex: function(item) {\r | |
617 | return this.innerItems.indexOf(item);\r | |
618 | },\r | |
619 | \r | |
620 | /**\r | |
621 | * @private\r | |
622 | * @return {Object}\r | |
623 | */\r | |
624 | getInnerItemAt: function(index) {\r | |
625 | return this.innerItems[index];\r | |
626 | },\r | |
627 | \r | |
628 | /**\r | |
629 | * @private\r | |
630 | * @return {Object}\r | |
631 | */\r | |
632 | applyActiveItem: function() {\r | |
633 | var activeItem = this.callParent(arguments),\r | |
634 | activeIndex;\r | |
635 | \r | |
636 | if (activeItem) {\r | |
637 | activeIndex = this.getInnerItemIndex(activeItem);\r | |
638 | \r | |
639 | if (activeIndex !== -1) {\r | |
640 | this.activeIndex = activeIndex;\r | |
641 | return activeItem;\r | |
642 | }\r | |
643 | }\r | |
644 | },\r | |
645 | \r | |
646 | updateActiveItem: function(activeItem) {\r | |
647 | var activeIndex = this.getActiveIndex(),\r | |
648 | maxIndex = this.getMaxItemIndex(),\r | |
649 | indicator = this.getIndicator(),\r | |
650 | bufferSize = this.getBufferSize(),\r | |
651 | carouselItems = this.carouselItems.slice(),\r | |
652 | orderedCarouselItems = this.orderedCarouselItems,\r | |
653 | visibleIndexes = {},\r | |
654 | visibleItems = {},\r | |
655 | visibleItem, component, id, i, index, ln, carouselItem;\r | |
656 | \r | |
657 | if (carouselItems.length === 0) {\r | |
658 | return;\r | |
659 | }\r | |
660 | \r | |
661 | this.callParent(arguments);\r | |
662 | \r | |
663 | orderedCarouselItems.length = 0;\r | |
664 | \r | |
665 | if (activeItem) {\r | |
666 | id = activeItem.getId();\r | |
667 | visibleItems[id] = activeItem;\r | |
668 | visibleIndexes[id] = bufferSize;\r | |
669 | \r | |
670 | if (activeIndex > 0) {\r | |
671 | for (i = 1; i <= bufferSize; i++) {\r | |
672 | index = activeIndex - i;\r | |
673 | if (index >= 0) {\r | |
674 | visibleItem = this.getInnerItemAt(index);\r | |
675 | id = visibleItem.getId();\r | |
676 | visibleItems[id] = visibleItem;\r | |
677 | visibleIndexes[id] = bufferSize - i;\r | |
678 | }\r | |
679 | else {\r | |
680 | break;\r | |
681 | }\r | |
682 | }\r | |
683 | }\r | |
684 | \r | |
685 | if (activeIndex < maxIndex) {\r | |
686 | for (i = 1; i <= bufferSize; i++) {\r | |
687 | index = activeIndex + i;\r | |
688 | if (index <= maxIndex) {\r | |
689 | visibleItem = this.getInnerItemAt(index);\r | |
690 | id = visibleItem.getId();\r | |
691 | visibleItems[id] = visibleItem;\r | |
692 | visibleIndexes[id] = bufferSize + i;\r | |
693 | }\r | |
694 | else {\r | |
695 | break;\r | |
696 | }\r | |
697 | }\r | |
698 | }\r | |
699 | \r | |
700 | for (i = 0,ln = carouselItems.length; i < ln; i++) {\r | |
701 | carouselItem = carouselItems[i];\r | |
702 | component = carouselItem.getComponent();\r | |
703 | \r | |
704 | if (component) {\r | |
705 | id = component.getId();\r | |
706 | \r | |
707 | if (visibleIndexes.hasOwnProperty(id)) {\r | |
708 | carouselItems.splice(i, 1);\r | |
709 | i--;\r | |
710 | ln--;\r | |
711 | delete visibleItems[id];\r | |
712 | orderedCarouselItems[visibleIndexes[id]] = carouselItem;\r | |
713 | }\r | |
714 | }\r | |
715 | }\r | |
716 | \r | |
717 | for (id in visibleItems) {\r | |
718 | if (visibleItems.hasOwnProperty(id)) {\r | |
719 | visibleItem = visibleItems[id];\r | |
720 | carouselItem = carouselItems.pop();\r | |
721 | carouselItem.setComponent(visibleItem);\r | |
722 | orderedCarouselItems[visibleIndexes[id]] = carouselItem;\r | |
723 | }\r | |
724 | }\r | |
725 | }\r | |
726 | \r | |
727 | this.inactiveCarouselItems.length = 0;\r | |
728 | this.inactiveCarouselItems = carouselItems;\r | |
729 | this.refreshOffset();\r | |
730 | this.refreshInactiveCarouselItems();\r | |
731 | \r | |
732 | if (indicator) {\r | |
733 | indicator.setActiveIndex(activeIndex);\r | |
734 | }\r | |
735 | },\r | |
736 | \r | |
737 | /**\r | |
738 | * Switches to the next card.\r | |
739 | * @return {Ext.carousel.Carousel} this\r | |
740 | * @chainable\r | |
741 | */\r | |
742 | next: function() {\r | |
743 | this.setOffset(0);\r | |
744 | \r | |
745 | if (this.activeIndex === this.getMaxItemIndex()) {\r | |
746 | return this;\r | |
747 | }\r | |
748 | \r | |
749 | this.animationDirection = -1;\r | |
750 | this.setOffsetAnimated(-this.itemLength);\r | |
751 | return this;\r | |
752 | },\r | |
753 | \r | |
754 | /**\r | |
755 | * Switches to the previous card.\r | |
756 | * @return {Ext.carousel.Carousel} this\r | |
757 | * @chainable\r | |
758 | */\r | |
759 | previous: function() {\r | |
760 | this.setOffset(0);\r | |
761 | \r | |
762 | if (this.activeIndex === 0) {\r | |
763 | return this;\r | |
764 | }\r | |
765 | \r | |
766 | this.animationDirection = 1;\r | |
767 | this.setOffsetAnimated(this.itemLength);\r | |
768 | return this;\r | |
769 | },\r | |
770 | \r | |
771 | /**\r | |
772 | * @private\r | |
773 | */\r | |
774 | applyIndicator: function(indicator, currentIndicator) {\r | |
775 | return Ext.factory(indicator, Ext.carousel.Indicator, currentIndicator);\r | |
776 | },\r | |
777 | \r | |
778 | /**\r | |
779 | * @private\r | |
780 | */\r | |
781 | updateIndicator: function(indicator) {\r | |
782 | if (indicator) {\r | |
783 | this.insertFirst(indicator);\r | |
784 | \r | |
785 | indicator.setUi(this.getUi());\r | |
786 | indicator.on({\r | |
787 | next: 'next',\r | |
788 | previous: 'previous',\r | |
789 | scope: this\r | |
790 | });\r | |
791 | }\r | |
792 | },\r | |
793 | \r | |
794 | destroy: function() {\r | |
795 | var me = this,\r | |
796 | carouselItems = me.carouselItems.slice();\r | |
797 | \r | |
798 | me.carouselItems.length = 0;\r | |
799 | \r | |
800 | Ext.destroy(carouselItems, me.getIndicator(), me.translatable);\r | |
801 | \r | |
802 | me.callParent();\r | |
803 | delete me.carouselItems;\r | |
804 | }\r | |
805 | });\r |