-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCSS3Slider.SlidePosition.js
More file actions
116 lines (94 loc) · 3.67 KB
/
Copy pathCSS3Slider.SlidePosition.js
File metadata and controls
116 lines (94 loc) · 3.67 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
/**
* handles slides to a specific position
*
* @param {CSS3Slider} CSS3Slider
* @returns {CSS3Slider_SlidePosition}
*/
function CSS3Slider_SlidePosition(CSS3Slider) {
this.__CSS3Slider = null;
/**
* init tasks
*
* @param {CSS3Slider} CSS3Slider
* @returns {void}
*/
this.__construct = function(CSS3Slider) {
this.__CSS3Slider = CSS3Slider;
};
/**
* slide to a specific position
*
* @param {Number} position - the index of the slider child to slide to
* @param {Boolean} testCalculation - set to true if you only want to calculate the runtimeconfig without actualy sliding
* @returns {object}
*/
this.positionSlide = function(position, testCalculation) {
if(this.__CSS3Slider.isAnimationAllowed()) {
this.__CSS3Slider._forbidAnimation();
// check if the position is inside the slider bounds
position = this.__getActualPositionInsideBounds(position);
// set the new slider position in percent
var newSlideValue = (position * this.__CSS3Slider._Config._getBaseConfig().singleStep) * -1;
// set the new runtime config
var newRuntimeConfig = {
slidePosition: position,
slideValue: newSlideValue,
slideChildrenCount: this.__CSS3Slider._Config.getRuntimeConfig().slideChildrenCount,
slideChildrenVisible: this.__CSS3Slider._Config.getRuntimeConfig().slideChildrenVisible,
slideOverflowCount: this.__CSS3Slider._Config.getRuntimeConfig().slideOverflowCount,
slideClonesCount: this.__CSS3Slider._Config.getRuntimeConfig().slideClonesCount
};
// check if call is not only a testCalculation
if(!testCalculation) {
// if so - actualy slide the slide target
this.__CSS3Slider.getSlideTargetNode().style.marginLeft = newSlideValue + '%';
// allow animation again, after the css transition took place
setTimeout(function() {
var runtimeConfig = this.__CSS3Slider._Config.getRuntimeConfig();
if(runtimeConfig.slideChildrenCount > runtimeConfig.slideChildrenVisible) {
this.__CSS3Slider._allowAnimation();
}
}.bind(this), 500);
return this.__CSS3Slider._Config._setRuntimeConfig(newRuntimeConfig);
}else {
// if not, only return the calculated but not stored runtimeconfig
this.__CSS3Slider._allowAnimation();
return newRuntimeConfig;
}
}else {
// simply return the runtime config without animation
return this.__CSS3Slider._Config.getRuntimeConfig();
}
};
/**
* return the next posible position inside the bounds of the slider
*
* @param {Number} position
* @returns {Number}
*/
this.__getActualPositionInsideBounds = function(position) {
var baseConfig = this.__CSS3Slider._Config._getBaseConfig();
var runtimeConfig = this.__CSS3Slider._Config.getRuntimeConfig();
var minPosition = 0;
var maxPosition = runtimeConfig.slideChildrenCount - runtimeConfig.slideChildrenVisible + runtimeConfig.slideOverflowCount;
if(maxPosition <= 0) {
maxPosition = 0;
}
// if continious slide is active, add the clones count to min and max positions
if(baseConfig.cloneMode) {
minPosition -= runtimeConfig.slideClonesCount;
maxPosition += runtimeConfig.slideClonesCount;
}
// slide to the first slider child node, if the target is below zero
if(position < minPosition) {
position = minPosition;
}
// slide to the last slider child node, if the target is beyond the last slider child node
if(position >= maxPosition) {
position = maxPosition;
}
return position;
};
this.__construct(CSS3Slider);
}
module.exports = CSS3Slider_SlidePosition;