-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathImageCarousel.js
357 lines (331 loc) · 8.83 KB
/
ImageCarousel.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
* Contains the declaration for the {@link module:layout/ImageCarousel~ImageCarousel} kind.
* @module layout/ImageCarousel
*/
var
kind = require('enyo/kind'),
utils = require('enyo/utils');
var
ImageView = require('./ImageView'),
Panels = require('./Panels'),
CarouselArranger = require('./CarouselArranger');
/**
* {@link module:layout/ImageCarousel~ImageCarousel} is a
* {@link module:layout/Panels~Panels} that uses
* {@link module:layout/CarouselArranger~CarouselArranger} as its arrangerKind.
* An ImageCarousel dynamically creates and loads instances of
* {@link module:layout/ImageView~ImageView} as needed, creating a gallery of images.
*
* ```javascript
* var
* kind = require('enyo/kind'),
* ImageCarousel = require('layout/ImageCarousel');
*
* {kind: ImageCarousel, images: [
* '@../assets/mercury.jpg',
* '@../assets/venus.jpg',
* '@../assets/earth.jpg',
* '@../assets/mars.jpg',
* '@../assets/jupiter.jpg',
* '@../assets/saturn.jpg',
* '@../assets/uranus.jpg',
* '@../assets/neptune.jpg'
* ], defaultScale: 'auto'},
* ```
*
* All of the events (`onload`, `onerror`, and `onZoom`) from the contained
* ImageView objects are bubbled up to the ImageCarousel, which also inherits
* the [onTransitionStart]{@link module:layout/Panels~Panels#onTransitionStart} and
* [onTransitionFinish]{@link module:layout/Panels~Panels#onTransitionFinish} events from
* {@link module:layout/Panels~Panels}.
*
* The [images]{@link module:layout/ImageCarousel~ImageCarousel#images} property is an array containing the
* file paths of the images in the gallery. The `images` array may be altered and
* updated at any time, and the current index may be manipulated at runtime via the
* inherited functions [getIndex()]{@link module:layout/Panels~Panels#getIndex} and
* [setIndex()]{@link module:layout/Panels~Panels#setIndex}.
*
* Note that it's best to specify a size for the ImageCarousel in order to
* avoid complications.
*
* @class ImageCarousel
* @extends module:layout/Panels~Panels
* @ui
* @public
*/
module.exports = kind(
/** @lends module:layout/ImageCarousel~ImageCarousel.prototype */ {
/**
* @private
*/
name: 'enyo.ImageCarousel',
/**
* @private
*/
kind: Panels,
/**
* @private
*/
arrangerKind: CarouselArranger,
/**
* The default scaling to be applied to each ImageView. Can be `'auto'`,
* `'width'`, `'height'`, or any positive numeric value.
*
* @type {String|Number}
* @default 'auto'
* @public
*/
defaultScale: 'auto',
/**
* If `true`, ImageView instances are created with zooming disabled.
*
* @type {Boolean}
* @default false
* @public
*/
disableZoom: false,
/**
* If `true`, any ImageViews that are not in the immediate viewing area
* (i.e., any images other than the currently active image and the first image
* on either side of it) will be destroyed to free up memory. This has the
* benefit of minimizing memory usage (which is good for mobile devices), but
* also has the downside that, if you want to view the images again, you'll need
* to recreate the ImageViews refetch the images (thus increasing the number of
* image-related GET calls). Defaults to `false`.
*
* @type {Boolean}
* @default false
* @public
*/
lowMemory: false,
/**
* @lends module:layout/ImageCarousel~ImageCarousel.prototype
* @private
*/
published: {
/**
* Array of paths to image files.
*
* @type {String[]}
* @default `[]`
* @public
*/
images: []
},
/**
* @private
*/
handlers: {
onTransitionStart: 'transitionStart',
onTransitionFinish: 'transitionFinish'
},
/**
* @method
* @private
*/
create: function () {
Panels.prototype.create.apply(this, arguments);
this.imageCount = this.images.length;
if (this.images.length > 0) {
this.initContainers();
this.loadNearby();
}
},
/**
* Builds a container for each image and destroys any extra containers and images.
*
* @private
*/
initContainers: function () {
for (var i=0; i<this.images.length; i++) {
if (!this.$['container' + i]) {
this.createComponent({
name: 'container' + i,
style: 'height:100%; width:100%;'
});
this.$['container' + i].render();
}
}
for (i=this.images.length; i<this.imageCount; i++) {
if (this.$['image' + i]) {
this.$['image' + i].destroy();
}
this.$['container' + i].destroy();
}
this.imageCount = this.images.length;
},
/**
* Loads images that are in view or may come into view soon.
*
* @private
*/
loadNearby: function () {
var range = this.getBufferRange();
for (var i in range) {
this.loadImageView(range[i]);
}
},
/**
* Determines which image indices are `'near'` the active image.
*
* @private
*/
getBufferRange: function () {
var range = [];
if (this.layout.containerBounds) {
var prefetchRange = 1;
var bounds = this.layout.containerBounds;
var c, i, x, xEnd;
// get the lower range
i=this.index-1;
x=0;
xEnd = bounds.width * prefetchRange;
while (i>=0 && x<=xEnd) {
c = this.$['container' + i];
x+= c.width + c.marginWidth;
range.unshift(i);
i--;
}
// get the upper range
i=this.index;
x=0;
xEnd = bounds.width * (prefetchRange + 1);
while (i<this.images.length && x<=xEnd) {
c = this.$['container' + i];
x+= c.width + c.marginWidth;
range.push(i);
i++;
}
}
return range;
},
/**
* @method
* @private
*/
reflow: function () {
Panels.prototype.reflow.apply(this, arguments);
this.loadNearby();
},
/**
* Loads the image whose path is found at the specified index in the
* [images]{@link module:layout/ImageCarousel~ImageCarousel#images} array.
*
* @param {Number} index - The index of the image to load.
* @private
*/
loadImageView: function (index) {
// NOTE: wrap bugged in enyo.CarouselArranger, but once fixed, wrap should work in this
if (this.wrap) {
// Used this modulo technique to get around javascript issue with negative values
// ref: http://javascript.about.com/od/problemsolving/a/modulobug.htm
index = ((index % this.images.length)+this.images.length)%this.images.length;
}
if (index>=0 && index<=this.images.length-1) {
if (!this.$['image' + index]) {
this.$['container' + index].createComponent({
name: 'image' + index,
kind: ImageView,
scale: this.defaultScale,
disableZoom: this.disableZoom,
src: this.images[index],
verticalDragPropagation: false,
style: 'height:100%; width:100%;'
}, {owner: this});
this.$['image' + index].render();
} else {
if (this.$['image' + index].src != this.images[index]) {
this.$['image' + index].setSrc(this.images[index]);
this.$['image' + index].setScale(this.defaultScale);
this.$['image' + index].setDisableZoom(this.disableZoom);
}
}
}
return this.$['image' + index];
},
/**
* Updates the array of images.
*
* @todo Probably a defect here. Simply calling `set()` won't force the observer to fire
* if `images` is a ref to the same array. Need to add the `force` parameter.
* @public
*/
setImages: function (images) {
// always invoke imagesChanged because this is an array property
// which might otherwise seem to be the same object
this.set('images', images);
},
/**
* @private
*/
imagesChanged: function () {
this.initContainers();
this.loadNearby();
},
/**
* @method
* @private
*/
indexChanged: function () {
this.loadNearby();
if (this.lowMemory) {
this.cleanupMemory();
}
Panels.prototype.indexChanged.apply(this, arguments);
},
/**
* @private
*/
transitionStart: function (sender, event) {
if (event.fromIndex==event.toIndex) {
return true; //prevent from bubbling if there's no change
}
},
/**
* @private
*/
transitionFinish: function (sender, event) {
this.loadNearby();
if (this.lowMemory) {
this.cleanupMemory();
}
},
/**
* Returns the currently displayed ImageView.
*
* @return {module:enyo/Control~Control} - The active image control.
* @public
*/
getActiveImage: function () {
return this.getImageByIndex(this.index);
},
/**
* Returns the ImageView with the specified index.
*
* @param {Number} index - The index of the image to be retrieved.
* @return {module:enyo/Control~Control} - The image control at `index`.
* @public
*/
getImageByIndex: function (index) {
return this.$['image' + index] || this.loadImageView(index);
},
/**
* Destroys any ImageView objects that are not in the immediate viewing area
* (i.e., any images other than the currently active image and the first
* image on either side of it) to free up memory. If you set the ImageCarousel's
* [lowMemory]{@link module:layout/ImageCarousel~ImageCarousel#lowMemory} property to `true`, this
* function will be called automatically as needed.
*
* @public
*/
cleanupMemory: function () {
var buffer = this.getBufferRange();
for (var i=0; i<this.images.length; i++) {
if (utils.indexOf(i, buffer) ===-1) {
if (this.$['image' + i]) {
this.$['image' + i].destroy();
}
}
}
}
});