-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlideService.js
128 lines (103 loc) · 3.72 KB
/
SlideService.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
import Slide from "./Slide.js";
const DirectionEnum = Object.freeze({"left": 0, "right": 1})
class SlideService extends Slide {
constructor(contentArea) {
super();
this._components = [];
this._currentIndex = 0;
this._isAnimationRunning = false;
this._contentArea = contentArea;
this._contentCount = this._contentArea.children.length;
this._appendAdditionalElementsForLoop(this._contentArea);
this._registerEventListenerOnBottomContentArea(this._contentArea);
const generatedNumber = 1 + Math.floor(Math.random() * (this._contentCount));
this._setCurrentIndex(generatedNumber);
}
registerComponent(component) {
this._components.push(component);
component.onNotifyIndexChanged(this._convertIndex(this._contentCount, this._currentIndex) - 1);
}
mediate(message, index) {
if ('increaseCurrentIndex' === message) {
this._increaseCurrentIndex();
}
else if ('decreaseCurrentIndex' === message) {
this._decreaseCurrentIndex();
}
else if ('changeCurrentIndex' === message) {
this._changeCurrentIndex(index);
}
else {
throw new Error("Undefined Interface");
}
}
_increaseCurrentIndex() {
if (this._currentIndex < this._contentCount + 1) {
this._setCurrentIndex(this._currentIndex + 1);
}
else {
this._setCurrentIndex(0);
}
}
_decreaseCurrentIndex() {
if (this._currentIndex > 0) {
this._setCurrentIndex(this._currentIndex - 1);
}
else {
this._setCurrentIndex(this._contentCount - 1);
}
}
_changeCurrentIndex(index) {
this._setCurrentIndex(index + 1);
}
_appendAdditionalElementsForLoop(elements) {
const clonedFirstElementChild = elements.firstElementChild.cloneNode(true);
const clonedLastElementChild = elements.lastElementChild.cloneNode(true);
elements.appendChild(clonedFirstElementChild);
elements.insertBefore(clonedLastElementChild, elements.firstElementChild);
}
_registerEventListenerOnBottomContentArea(element) {
element.addEventListener('transitionstart', event => {
this._onTransitionStart(event);
});
element.addEventListener('transitionend', event => {
this._onTransitionEnd(event);
});
}
_setCurrentIndex(index) {
if (this._isAnimationRunning || this._currentIndex === index)
return;
this._components.forEach(element => {
element.onNotifyIndexChanged(this._convertIndex(this._contentCount, index) - 1);
});
this._changeContentArea(index);
this._currentIndex = this._convertIndex(this._contentCount, index);
}
_convertIndex(menuCount, index) {
let convertedIndex = 0;
if (menuCount < index) {
convertedIndex = 1;
}
else if (1 > index) {
convertedIndex = menuCount;
}
else {
convertedIndex = index;
}
return convertedIndex;
}
_changeContentArea(index) {
this._contentArea.style.transition = "margin-left 0.25s ease";
const offsetWidth = -(index * this._contentArea.offsetWidth);
this._contentArea.style.marginLeft = offsetWidth + 'px';
}
_onTransitionStart(event) {
this._isAnimationRunning = true;
}
_onTransitionEnd(event) {
this._contentArea.style.transition = "none";
this._contentArea.style.marginLeft = -(this._currentIndex * this._contentArea.offsetWidth) + 'px';
this._isAnimationRunning = false;
}
}
export default SlideService;