frontend.js
4.8 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
(function($) {
/**
* Class for Post Carousel Module
*
* @since 1.6.1
*/
FLBuilderPostCarousel = function( settings ){
// set params
this.settings = settings.settings;
this.transitionType = settings.transition;
this.nodeClass = '.fl-node-' + settings.id;
this.wrapperClass = this.nodeClass + ' .fl-post-carousel-wrapper';
this.postClass = this.nodeClass + ' .fl-post-carousel-post';
this.prevCarouselBtn = $( this.nodeClass + ' .carousel-prev' );
this.nextCarouselBtn = $( this.nodeClass + ' .carousel-next' );
this.layout = settings.layout;
this.navigation = settings.navigationControls;
this.slideWidth = settings.slideWidth;
this.currentBrowserWidth = $( window ).width();
// check if module have posts
if( this._hasPosts() ) {
// initialize the slider
this._initCarousel();
// check if viewport is resizing
$( window ).on( 'resize', function( e ){
var width = $( window ).width();
// if screen width is resized, reload the carousel
if( width != this.currentBrowserWidth ){
this._resizeDebounce();
this.currentBrowserWidth = width;
}
}.bind( this ) );
}
};
FLBuilderPostCarousel.prototype = {
settings : {},
nodeClass : '',
wrapperClass : '',
postClass : '',
prevCarouselBtn : '',
nextCarouselBtn : '',
layout : '',
navigation : false,
slideWidth : 0,
carousel : '',
/**
* Check if the module have posts.
*
* @since 1.6.1
* @return bool
*/
_hasPosts: function(){
return $( this.postClass ).length > 0;
},
/**
* When screen is resized, reloads the carousel in a determinded interval.
*
* @see this._reloadCarousel()
* @since 1.6.1
* @return void
*/
_resizeDebounce: function(){
clearTimeout( this.resizeTimer );
this.resizeTimer = setTimeout( function() {
this._reloadCarousel();
}.bind( this ), 250 );
},
/**
* Checks screen size, and returns the number of slides for the current viewport.
*
* @since 1.6.1
* @return int The number of slides for the current screen size.
*/
_getSlidesNumber: function(){
var $wrapperWidth = this._getWrapperWidth(),
$slideWidth = $( this.postClass ).width(),
columns = Math.ceil( $wrapperWidth / this.slideWidth );
return columns;
},
/**
* Calculates individual slide with based on screen size.
*
* @see this._getSlidesNumber()
* @since 1.6.1
* @return int The correct slide width.
*/
_getSlideWidth: function(){
return Math.ceil( ( this._getWrapperWidth() - ( this.settings.slideMargin * ( this._getSlidesNumber() - 1 ) ) ) / this._getSlidesNumber() );
},
/**
* Get the carousel module real width both visible and hidden element
*
* @since 1.8.1
* @return int
*/
_getWrapperWidth: function()
{
var $wrapper = $( this.nodeClass + ' .fl-post-carousel' );
$width = $wrapper.width();
if ( $width === 0 && $wrapper.is(':hidden') ) {
$clone = $wrapper.clone()
.css("visibility","hidden")
.appendTo($('.fl-row-content'));
$width = $clone.outerWidth();
$clone.remove();
}
return $width;
},
/**
* Calculates slides variables and return an object with carousel options.
*
* @see this._getSlideWidth()
* @see this._getSlidesNumber()
* @since 1.6.1
* @return obj The carousel options object.
*/
_getSettings: function(){
var newSettings,
settings = {
slideWidth: this._getSlideWidth(),
minSlides: this._getSlidesNumber(),
maxSlides: this._getSlidesNumber(),
onSliderLoad: function() {
$( this.wrapperClass ).addClass( 'fl-post-carousel-loaded' );
}.bind( this ),
}
newSettings = $.extend( {}, this.settings, settings );
return newSettings;
},
/**
* Initialize the carousel.
*
* @see this._getSettings()
* @since 1.6.1
* @return void
*/
_initCarousel: function(){
this.carousel = $( this.wrapperClass ).bxSlider( this._getSettings() );
if( this.navigation ){
this.prevCarouselBtn.on( 'click', function( e ){
e.preventDefault();
this.carousel.goToPrevSlide();
}.bind( this ) );
this.nextCarouselBtn.on( 'click', function( e ){
e.preventDefault();
this.carousel.goToNextSlide();
}.bind( this ) );
}
},
/**
* Reloads the carousel.
*
* @see this._getSettings()
* @since 1.6.1
* @return void
*/
_reloadCarousel: function(){
var bxObject = this.carousel.data('bxSlider');
if ( bxObject ) {
bxObject.reloadSlider( this._getSettings() );
}
else {
this.carousel.reloadSlider( this._getSettings() );
}
},
};
})(jQuery);