-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCSS3Slider.Config.js
231 lines (182 loc) · 6.98 KB
/
CSS3Slider.Config.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
var imxQuery = require('imxquery');
/**
* the configuration of the CSS3Slider
*
* @param {CSS3Slider} CSS3Slider
* @param {object} baseConfig
* @returns {CSS3Slider_Config}
*/
function CSS3Slider_Config(CSS3Slider, baseConfig) {
/**
* fallback for baseConfig
*/
this.__defaultBaseConfig = {
maxSteps: 1, // how many elements in the slider should one direction slide step skip
singleStep: 100, // slide width in percent of the width of a single slide step
forceSingleElement: false, // better ignore this - can be used to force the slider only to show one element
cloneMode: false, // set to true to add clones for endless slider visuals
continiousSlide: false, // set to true to create an endless slider
overflowAllowed: false, // set to true to allow to slide the last element to the first visible position.
calcMethod: 'floor'
};
this.__CSS3Slider = null;
/**
* contains all user given config input
*/
this.__baseConfig = null;
/**
* contains calculated values
*/
this.__runtimeConfig = null;
/**
* init tasks
*
* @param {CSS3Slider} CSS3Slider
* @param {object} baseConfig
* @returns {void}
*/
this.__construct = function(CSS3Slider, baseConfig) {
this.__CSS3Slider = CSS3Slider;
if(baseConfig === undefined) {
baseConfig = [];
}
this._setBaseConfig(imxQuery.extendObject(baseConfig, this.__defaultBaseConfig));
this._createRuntimeConfig();
};
/**
* @param {object} baseConfig
* @returns {void}
*/
this._setBaseConfig = function(baseConfig) {
this.__baseConfig = baseConfig;
};
/**
* @returns {object}
*/
this._getBaseConfig = function() {
return this.__baseConfig;
};
/**
* @param {object} runtimeConfig
* @returns {object}
*/
this._setRuntimeConfig = function(runtimeConfig) {
this.__runtimeConfig = runtimeConfig;
return this.getRuntimeConfig();
};
/**
* @returns {object}
*/
this.getRuntimeConfig = function() {
return this.__runtimeConfig;
};
/**
* update the config, for example after a screen resize
*
* @param {object} baseConfig
* @returns {void}
*/
this.updateBaseConfig = function(baseConfig) {
// set the single step to the default value of 100
this.__baseConfig.singleStep = 100;
if(baseConfig !== undefined) {
this._setBaseConfig(imxQuery.extendObject(baseConfig, this.__defaultBaseConfig));
}
this.__CSS3Slider._Dom.resetSlideTargetNode();
// create a new clean runtime config
this._createRuntimeConfig();
this.__CSS3Slider._Dom.prepareSlideTarget();
this.__CSS3Slider.slideTo(this.getRuntimeConfig().slidePosition);
};
/**
* creates the runtime config
*
* @returns {object}
*/
this._createRuntimeConfig = function() {
var slideTargetNode = this.__CSS3Slider.getSlideTargetNode();
// look up how many childnodes without clones are present
var cloneChildrenCount = slideTargetNode.querySelectorAll('.-css3Slider-prepander').length * 2;
var slideChildrenCount = slideTargetNode.childElementCount - cloneChildrenCount;
// calculate how many slider nodes are visible at once
var slideChildrenVisible = this._getChildrenVisible();
var slideOverflowCount = this._getBaseConfig().overflowAllowed ? slideChildrenVisible - 1 : 0;
// calculate how many clones are needed
var slideClonesCount = 0;
if(this._getBaseConfig().cloneMode) {
slideClonesCount = this.__baseConfig.maxSteps + Math.floor((slideChildrenVisible - 1) / 2);
if(slideClonesCount > slideChildrenVisible) {
slideClonesCount = slideChildrenVisible;
}
if(slideClonesCount > slideChildrenCount) {
slideClonesCount = slideChildrenCount;
}
}
// calculate the movement for one single sliding attempt
this.__baseConfig.singleStep = this.__baseConfig.singleStep / slideChildrenVisible;
// general global check if animation is allowed
if(slideChildrenCount <= slideChildrenVisible) {
this.__CSS3Slider._forbidAnimation();
}else {
this.__CSS3Slider._allowAnimation();
}
var slidePosition = (this.getRuntimeConfig() !== null) ? this.getRuntimeConfig().slidePosition : 0;
return this._setRuntimeConfig({
slidePosition: slidePosition, // the current first visible element in the row
slideValue: 0, // the offset of the row in percent
slideChildrenCount: slideChildrenCount, // how many non clone elements are in the slider
slideChildrenVisible: slideChildrenVisible, // how many elements are visible at once in the slider
slideOverflowCount: slideOverflowCount, // how many overflow steps are possible
slideClonesCount: slideClonesCount // how many clones are needed
});
};
this._getChildrenVisible = function() {
var slideTargetNode = this.__CSS3Slider.getSlideTargetNode();
// the width of the container that holds the row
var canvasWidth = slideTargetNode.parentNode.getBoundingClientRect().width;
var slideChildrenVisible = 1;
// calculate how many slider nodes are visible at once
if(!this._getBaseConfig().forceSingleElement) {
var calcMethod = this.__baseConfig.calcMethod;
var singleElementWidthInPx = this._getSingleElementWidthInPx();
if(calcMethod == 'ceil') {
slideChildrenVisible = Math.ceil(canvasWidth / singleElementWidthInPx);
}else if(calcMethod == 'round') {
slideChildrenVisible = Math.round(canvasWidth / singleElementWidthInPx);
}else {
slideChildrenVisible = Math.floor(canvasWidth / singleElementWidthInPx);
}
}
if(slideChildrenVisible < 1) slideChildrenVisible = 1;
return slideChildrenVisible;
};
/**
* calculate the original widht of a single slider element in pixel
*
* @returns {Number|CSS3Slider_Config._getSingleElementWidthInPx.width}
*/
this._getSingleElementWidthInPx = function() {
var baseObject = this.__CSS3Slider.getSlideTargetNode().children[0];
var computedStyle = window.getComputedStyle(baseObject);
var width = parseFloat(computedStyle.getPropertyValue('width'));
var totalWidth = width + this._getSingleElementMarginInPx();
return totalWidth;
};
/**
* calculate the percentage width of a single slider element
*
* @returns {Number|CSS3Slider_Config.getRuntimeConfig.slideChildrenCount}
*/
this._getSingleElementWidthInPercent = function() {
return 100 / this.getRuntimeConfig().slideChildrenCount;
};
this._getSingleElementMarginInPx = function() {
var baseObject = this.__CSS3Slider.getSlideTargetNode().children[0];
var computedStyle = window.getComputedStyle(baseObject);
var marginLeft = parseInt(computedStyle.getPropertyValue('margin-left')) || 0;
var marginRight = parseInt(computedStyle.getPropertyValue('margin-right')) || 0;
return marginLeft + marginRight;
};
this.__construct(CSS3Slider, baseConfig);
}
module.exports = CSS3Slider_Config;