-
Notifications
You must be signed in to change notification settings - Fork 4
/
CSS3Slider.Dom.Clones.js
134 lines (109 loc) · 4.41 KB
/
CSS3Slider.Dom.Clones.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
/**
* handles clone nodes for endless slide effect
*
* @param {CSS3Slider} CSS3Slider
* @returns {CSS3Slider_Dom_Clone}
*/
function CSS3Slider_Dom_Clone(CSS3Slider) {
this.__CSS3Slider = null;
/**
* init tasks
*
* @param {CSS3Slider} CSS3Slider
* @returns {void}
*/
this.__construct = function(CSS3Slider) {
this.__CSS3Slider = CSS3Slider;
var cloneMode = this.__CSS3Slider._Config._getBaseConfig().cloneMode;
// set slider positioning to relative - to be able to absolute position clones
if(cloneMode) {
this.__CSS3Slider.getSlideTargetNode().style.position = 'relative';
}
};
/**
* will add clone nodes before the first slider node and after the last slider node
*
* @returns {void}
*/
this._addClones = function() {
var runtimeConfig = this.__CSS3Slider._Config.getRuntimeConfig();
var singleElementWidth = this.__CSS3Slider._Config._getSingleElementWidthInPercent();
var singleElementMargin = this.__CSS3Slider._Config._getSingleElementMarginInPx();
var slideTargetNode = this.__CSS3Slider.getSlideTargetNode();
// store all child nodes in an array cause nodeCollections are updated
var originalChildNodeArray = [];
for(var childNodeIndex = 0; childNodeIndex < runtimeConfig.slideChildrenCount; childNodeIndex++) {
originalChildNodeArray.push(slideTargetNode.children[childNodeIndex]);
}
// add so much clones as the config states are needed
for(var i = 0; i < runtimeConfig.slideClonesCount; i++) {
this.__addSingleClone(slideTargetNode, originalChildNodeArray, i, 'prepander', singleElementWidth, singleElementMargin);
this.__addSingleClone(slideTargetNode, originalChildNodeArray, i, 'apander', singleElementWidth, singleElementMargin);
}
};
/**
* adds a single clone to the slider
*
* @param {object} slideTargetNode
* @param {Array} originalChildNodeArray
* @param {Number} index
* @param {String} type
* @param {Number} singleElementWidth
* @returns {void}
*/
this.__addSingleClone = function(slideTargetNode, originalChildNodeArray, index, type, singleElementWidth, singleElementMargin) {
var nodeForCloningIndex = null;
// get the index of the child node that we want to clone
if(type === 'prepander') {
nodeForCloningIndex = originalChildNodeArray.length - 1 - index;
}else if(type === 'apander') {
nodeForCloningIndex = index;
}
// get the actual child node we want to clone
var nodeForCloning = originalChildNodeArray[nodeForCloningIndex];
// create the clone
var cloneNode = nodeForCloning.cloneNode(true);
cloneNode.classList.add('-css3Slider-' + type);
cloneNode.style.position = 'absolute';
cloneNode.style.top = '0%';
cloneNode.style.right = '100%';
if(singleElementMargin) {
cloneNode.style.width = 'calc(' + singleElementWidth + '% - ' + singleElementMargin + 'px)';
}else {
cloneNode.style.width = singleElementWidth + '%';
}
if(type === 'prepander') {
// prepander will be positioned in front
cloneNode.style.right = 100 + singleElementWidth * index + '%';
slideTargetNode.insertBefore(cloneNode, slideTargetNode.children[0]);
}else if(type === 'apander') {
// apander will be positioned at the end
cloneNode.style.left = 100 + singleElementWidth * index + '%';
slideTargetNode.appendChild(cloneNode);
}
};
/**
* will return all clone nodes from slider
*
* @returns {void}
*/
this._removeClones = function() {
var slideTargetNode = this.__CSS3Slider.getSlideTargetNode();
// get all prepander
var prepanderCollection = slideTargetNode.getElementsByClassName('-css3Slider-prepander');
var prepanderLength = prepanderCollection.length;
// get all apander
var apenderCollection = slideTargetNode.getElementsByClassName('-css3Slider-apander');
var apenderLength = apenderCollection.length;
// remove all prepander
for(var prepanderIndex = 0; prepanderIndex < prepanderLength; prepanderIndex++) {
slideTargetNode.removeChild(prepanderCollection[0]); // allways use index 0 due to updating nodeCollections
}
// remove all apander
for(var apenderIndex = 0; apenderIndex < apenderLength; apenderIndex++) {
slideTargetNode.removeChild(apenderCollection[0]); // allways use index 0 due to updating nodeCollections
}
};
this.__construct(CSS3Slider);
}
module.exports = CSS3Slider_Dom_Clone;