ug-theme-carousel.js
8.76 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
if(typeof g_ugFunctions != "undefined")
g_ugFunctions.registerTheme("carousel");
else
jQuery(document).ready(function(){g_ugFunctions.registerTheme("carousel")});
/**
* Grid gallery theme
*/
function UGTheme_carousel(){
var t = this;
var g_gallery = new UniteGalleryMain(), g_objGallery, g_objects, g_objWrapper;
var g_lightbox = new UGLightbox(), g_carousel = new UGCarousel();
var g_functions = new UGFunctions(), g_objTileDesign = new UGTileDesign();;
var g_objNavWrapper, g_objButtonLeft, g_objButtonRight, g_objButtonPlay, g_objPreloader;
var g_apiDefine = new UG_API();
var g_options = {
theme_gallery_padding: 0, //the padding of the gallery wrapper
theme_carousel_align: "center", //the align of the carousel
theme_carousel_offset: 0, //the offset of the carousel from the align sides
theme_enable_navigation: true,
theme_navigation_position: "bottom", //top,bottom: the vertical position of the navigation reative to the carousel
theme_navigation_enable_play: true, //enable / disable the play button of the navigation
theme_navigation_align: "center", //the align of the navigation
theme_navigation_offset_hor: 0, //horizontal offset of the navigation
theme_navigation_margin: 20, //the space between the carousel and the navigation
theme_space_between_arrows: 5 //the space between arrows in the navigation
};
var g_defaults = {
gallery_width: "100%",
tile_width: 160,
tile_height: 160,
tile_enable_border: true,
tile_enable_outline: true,
carousel_vertical_scroll_ondrag: true
};
//temp variables
var g_temp = {
};
/**
* Init the theme
*/
function initTheme(gallery, customOptions){
g_gallery = gallery;
//set default options
g_options = jQuery.extend(g_options, g_defaults);
//set custom options
g_options = jQuery.extend(g_options, customOptions);
//modifyOptions();
//set gallery options
g_gallery.setOptions(g_options);
g_gallery.setFreestyleMode();
g_objects = gallery.getObjects();
//get some objects for local use
g_objGallery = jQuery(gallery);
g_objWrapper = g_objects.g_objWrapper;
//init objects
g_lightbox.init(gallery, g_options);
g_carousel.init(gallery, g_options);
g_objTileDesign = g_carousel.getObjTileDesign();
}
/**
* set gallery html elements
*/
function setHtml(){
//add html elements
g_objWrapper.addClass("ug-theme-carousel");
g_carousel.setHtml(g_objWrapper);
if(g_options.theme_enable_navigation == true){
var htmlAdd = "<div class='ug-tile-navigation-wrapper' style='position:absolute'>";
htmlAdd += "<div class='ug-button-tile-navigation ug-button-tile-left'></div>";
//put play/pause button
if(g_options.theme_navigation_enable_play == true)
htmlAdd += "<div class='ug-button-tile-navigation ug-button-tile-play'></div>";
htmlAdd += "<div class='ug-button-tile-navigation ug-button-tile-right'></div>";
htmlAdd += "</div>";
g_objWrapper.append(htmlAdd);
g_objNavWrapper = g_objWrapper.children(".ug-tile-navigation-wrapper");
g_objButtonLeft = g_objNavWrapper.children(".ug-button-tile-left");
g_objButtonRight = g_objNavWrapper.children(".ug-button-tile-right");
g_objButtonLeft.css("margin-right",g_options.theme_space_between_arrows+"px");
if(g_options.theme_navigation_enable_play == true){
g_objButtonPlay = g_objNavWrapper.children(".ug-button-tile-play");
g_objButtonPlay.css("margin-right",g_options.theme_space_between_arrows+"px");
}
}
g_lightbox.putHtml();
//add preloader
g_objWrapper.append("<div class='ug-tiles-preloader ug-preloader-trans'></div>");
g_objPreloader = g_objWrapper.children(".ug-tiles-preloader");
g_objPreloader.fadeTo(0,0);
}
/**
* get gallery width available for the grid
*/
function getGalleryWidth(){
var galleryWidth = g_gallery.getSize().width;
galleryWidth -= g_options.theme_gallery_padding * 2;
return(galleryWidth);
}
/**
* get estimated height of the carousel and the navigation
*/
function getEstimatedHeight(){
var height = g_carousel.getEstimatedHeight();
if(g_objNavWrapper){
var navHeight = g_objNavWrapper.height();
height += navHeight + g_options.theme_navigation_margin;
}
return(height);
}
/**
* actually run the theme
*/
function actualRun(){
//first set the height, maybe scrollbars will appear
var galleryHeight = getEstimatedHeight();
g_objWrapper.height(galleryHeight);
var galleryWidth = getGalleryWidth();
initEvents();
g_carousel.setMaxWidth(galleryWidth);
g_carousel.run();
g_lightbox.run();
positionElements();
}
/**
* run the theme
*/
function runTheme(){
setHtml();
actualRun();
}
/**
* position elements
*/
function positionElements(){
var carouselElement = g_carousel.getElement();
var sizeCar = g_functions.getElementSize(carouselElement);
var navHeight = 0;
if(g_objNavWrapper){
var sizeNav = g_functions.getElementSize(g_objNavWrapper);
navHeight = sizeNav.height;
}
var galleryHeight = sizeCar.height;
if(navHeight > 0)
galleryHeight += g_options.theme_navigation_margin + navHeight;
//vars for bottom nav position
var carouselTop = 0;
if(g_objNavWrapper){
var navTop = sizeCar.height + g_options.theme_navigation_margin;
//change vars for top nav position
if(g_options.theme_navigation_position == "top"){
carouselTop = sizeNav.height + g_options.theme_navigation_margin;
navTop = 0;
}
}
//align the carousel
g_functions.placeElement(carouselElement, g_options.theme_carousel_align, carouselTop, g_options.theme_carousel_offset, 0);
var sizeCar = g_functions.getElementSize(carouselElement);
//position buttons
if(g_objNavWrapper){
var navX = sizeCar.left + g_functions.getElementRelativePos(g_objNavWrapper, g_options.theme_navigation_align, g_options.theme_navigation_offset_hor, carouselElement);
g_functions.placeElement(g_objNavWrapper, navX, navTop);
}
g_objWrapper.height(galleryHeight); //temp height
//place preloader
g_functions.placeElement(g_objPreloader, "center", 50);
}
/**
* on tile click - open lightbox
*/
function onTileClick(data, objTile){
objTile = jQuery(objTile);
var objItem = g_objTileDesign.getItemByTile(objTile);
var index = objItem.index;
g_lightbox.open(index);
}
/**
* on gallery size change - resize the theme.
*/
function onSizeChange(){
var galleryWidth = getGalleryWidth();
g_carousel.setMaxWidth(galleryWidth);
g_carousel.run();
positionElements();
}
/**
* before items request: hide items, show preloader
*/
function onBeforeReqestItems(){
g_carousel.stopAutoplay();
g_carousel.getElement().hide();
if(g_objNavWrapper)
g_objNavWrapper.hide();
//show preloader:
g_objPreloader.fadeTo(0,1);
}
/**
* init api functions
*/
function initAPIFunctions(event, api){
api.carouselStartAutoplay = function(){
g_carousel.startAutoplay();
}
api.carouselStopAutoplay = function(){
g_carousel.stopAutoplay();
}
api.carouselPause = function(){
g_carousel.pauseAutoplay();
}
api.carouselUnpause = function(){
g_carousel.unpauseAutoplay();
}
api.scrollLeft = function(numTiles){
g_carousel.scrollLeft(numTiles);
}
api.scrollRight = function(numTiles){
g_carousel.scrollRight(numTiles);
}
}
/**
* init buttons functionality and events
*/
function initEvents(){
//set navigation buttons events
if(g_objNavWrapper){
g_carousel.setScrollLeftButton(g_objButtonRight);
g_carousel.setScrollRightButton(g_objButtonLeft);
if(g_objButtonPlay)
g_carousel.setPlayPauseButton(g_objButtonPlay);
}
g_objGallery.on(g_gallery.events.SIZE_CHANGE, onSizeChange);
g_objGallery.on(g_gallery.events.GALLERY_BEFORE_REQUEST_ITEMS, onBeforeReqestItems);
//on click events
jQuery(g_objTileDesign).on(g_objTileDesign.events.TILE_CLICK, onTileClick);
//init api
g_objGallery.on(g_apiDefine.events.API_INIT_FUNCTIONS, initAPIFunctions);
}
/**
* destroy the carousel events
*/
this.destroy = function(){
if(g_objNavWrapper){
g_functions.destroyButton(g_objButtonRight);
g_functions.destroyButton(g_objButtonLeft);
if(g_objButtonPlay)
g_functions.destroyButton(g_objButtonPlay);
}
g_objGallery.off(g_gallery.events.SIZE_CHANGE);
jQuery(g_objTileDesign).off(g_objTileDesign.events.TILE_CLICK);
g_objGallery.off(g_gallery.events.GALLERY_BEFORE_REQUEST_ITEMS);
g_carousel.destroy();
g_lightbox.destroy();
}
/**
* run the theme setting
*/
this.run = function(){
runTheme();
}
/**
* init
*/
this.init = function(gallery, customOptions){
initTheme(gallery, customOptions);
}
}