-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauru.js
More file actions
196 lines (178 loc) · 5.23 KB
/
auru.js
File metadata and controls
196 lines (178 loc) · 5.23 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
'use strict';
var EventEmitter = require('eventemitter3');
var Hammer;
var touchEnabled = typeof AURU_TOUCH_ENABLED === 'undefined' || AURU_TOUCH_ENABLED === true;
// webkit/uglify don't understand the variable above for dead code removal, so it's repeated in the if statement
if (typeof AURU_TOUCH_ENABLED === 'undefined' || AURU_TOUCH_ENABLED === true) {
Hammer = require('hammerjs');
}
var defaultOptions = {
slideDuration: 5, // how many seconds to show each slide, false to not play automatically
classPrefix: 'auru-',
continuousLoop: true,
touch: false
};
function dumbMerge(first, second) {
var copy = {};
for (var key in first) {
copy[key] = first[key];
}
for (var key in second) {
copy[key] = second[key];
}
return copy;
}
export default class Auru extends EventEmitter {
constructor(element, options) {
super();
this.element = element;
this.options = dumbMerge(defaultOptions, options || {});
this.playing = false;
this.currentIndex = 0;
this.maxIndex = this.element.children.length - 1;
this.previousSlide = this.element.children[this.maxIndex];
this.currentSlide = this.element.children[this.currentIndex];
this._initializeClasses();
if (touchEnabled && this.options.touch) {
this._initializeTouchEvents();
}
if (this.options.slideDuration) {
this.play();
}
}
_initializeClasses() {
var prefix = this.options.classPrefix, i = 0;
this.element.classList.add(prefix + 'slideshow');
Array.prototype.forEach.call(this.element.children, childNode => {
childNode.classList.add(prefix + 'slide');
if (i === 0) {
childNode.classList.add(prefix + 'current');
} else {
childNode.classList.add(prefix + 'hidden');
}
i++;
});
}
_removeClasses() {
var prefix = this.options.classPrefix, i = 0;
this.element.classList.remove(prefix + 'slideshow');
Array.prototype.forEach.call(this.element.children, childNode => {
childNode.classList.remove(
prefix + 'slide',
prefix + 'current',
prefix + 'previous',
prefix + 'to-right',
prefix + 'to-left',
prefix + 'from-right',
prefix + 'from-left'
);
i++;
});
}
_initializeTouchEvents() {
this.hammer = new Hammer(this.element);
this.hammer.on('swipeleft', () => {
this.next();
this.emit('swipe');
this.emit('swipeleft');
});
this.hammer.on('swiperight', () => {
this.previous();
this.emit('swipe');
this.emit('swiperight');
});
this.hammer.on('tap', () => {
this.emit('tap');
});
}
_removeAnimationClasses(elem) {
var prefix = this.options.classPrefix;
elem.classList.remove(
prefix + 'to-right',
prefix + 'to-left',
prefix + 'from-right',
prefix + 'from-left'
);
}
play() {
if (this.playing || typeof this.options.slideDuration !== 'number' || this.options.slideDuration <= 0) {
return;
}
this.playing = true;
this.timer = setInterval(this.next.bind(this), this.options.slideDuration*1000);
}
stop() {
this.playing = false;
clearInterval(this.timer);
}
next() {
var nextIndex = this.currentIndex+1;
if (nextIndex > this.maxIndex) {
nextIndex = 0;
}
return this.goToSlide(nextIndex);
}
previous() {
var nextIndex = this.currentIndex-1;
if (nextIndex < 0) {
nextIndex = this.maxIndex;
}
return this.goToSlide(nextIndex);
}
goToSlide(index) {
var prefix, children, oldPrevious, previous, current, oldIndex;
if (index < 0 || index > this.maxIndex) {
throw new TypeError('Slide index out of bounds');
}
if (index === this.currentIndex) {
return;
}
prefix = this.options.classPrefix;
children = this.element.children;
oldPrevious = this.previousSlide;
previous = this.currentSlide;
oldIndex = this.currentIndex;
this.currentIndex = index;
current = children[index];
this._removeAnimationClasses(oldPrevious);
oldPrevious.classList.remove(prefix + 'previous');
oldPrevious.classList.add(prefix + 'hidden');
this._removeAnimationClasses(previous);
previous.classList.remove(prefix + 'current');
previous.classList.add(prefix + 'previous');
if (this.options.continuousLoop && this.currentIndex === 0 && oldIndex === this.maxIndex) {
previous.classList.add(prefix + 'to-left');
} else if (this.options.continuousLoop && this.currentIndex === this.maxIndex && oldIndex === 0) {
previous.classList.add(prefix + 'to-right');
} else if (this.currentIndex > oldIndex) {
previous.classList.add(prefix + 'to-left');
} else {
previous.classList.add(prefix + 'to-right');
}
current.classList.remove(prefix + 'hidden');
current.classList.add(prefix + 'current');
if (this.options.continuousLoop && this.currentIndex === 0 && oldIndex === this.maxIndex) {
current.classList.add(prefix + 'from-right');
} else if (this.options.continuousLoop && this.currentIndex === this.maxIndex && oldIndex === 0) {
current.classList.add(prefix + 'from-left');
} else if (this.currentIndex > oldIndex) {
current.classList.add(prefix + 'from-right');
} else {
current.classList.add(prefix + 'from-left');
}
this.previousSlide = previous;
this.currentSlide = current;
this.emit('change');
}
destroy(removeClasses) {
var prefix;
if (touchEnabled && this.options.touch) {
this.hammer.destroy();
}
if (removeClasses) {
this._removeClasses();
}
this.stop();
this.removeAllListeners();
}
}