This repository has been archived by the owner on Jan 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshower-next.js
196 lines (158 loc) · 5.56 KB
/
shower-next.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
/**
* @fileOverview
* Next plugin for Shower
*/
shower.modules.define('shower-next', [
'shower',
'Emitter',
'util.extend'
], function (provide, globalShower, EventEmitter, extend) {
var TIMER_PLUGIN_NAME = 'shower-timer';
var DEFAULT_SELECTOR = '.next';
/**
* @class
* @name plugin.Next
* @param {Shower} shower
* @param {Object} [options] Plugin options.
* @param {String} [options.selector = '.next']
* @constructor
*/
function Next(shower, options) {
options = options || {};
this.events = new EventEmitter({context: this});
this._shower = shower;
this._elementsSelector = options.selector || DEFAULT_SELECTOR;
this._elements = [];
this._innerComplete = 0;
this._setupListeners();
if (this._shower.player.getCurrentSlideIndex() != -1) {
this._onSlideActivate();
}
}
extend(Next.prototype, /** @lends plugin.Next.prototype */{
destroy: function () {
this._clearListeners();
this._elements = null;
this._elementsSelector = null;
this._innerComplete = null;
this._shower = null;
},
/**
* Activate next inner item.
* @return {plugin.Next}
*/
next: function () {
if (!this._elements.length) {
throw new Error('Inner nav elements not found.');
}
this._innerComplete++;
this._go();
this.events.emit('next');
return this;
},
prev: function () {
if (!this._elements.length) {
throw new Error('Inner nav elements not found.');
}
this._innerComplete--;
this._go();
this.events.emit('prev');
return this;
},
/**
* @returns {Number} Inner elements count.
*/
getLength: function () {
this._elements = this._getElements();
return this._elements.length;
},
/**
* @returns {Number} Completed inner elements count.
*/
getComplete: function () {
return this._innerComplete;
},
_setupListeners: function () {
var shower = this._shower;
this._showerListeners = shower.events.group()
.on('destroy', this.destroy, this);
this._playerListeners = shower.player.events.group()
.on('activate', this._onSlideActivate, this)
.on('next', this._onNext, this)
.on('prev', this._onPrev, this);
var timerPlugin = globalShower.plugins.get(TIMER_PLUGIN_NAME, shower);
if (timerPlugin) {
this._setupTimerPluginListener(timerPlugin);
} else {
this._pluginsListeners = globalShower.plugins.events.group()
.on('add', function (e) {
if (e.get('name') === TIMER_PLUGIN_NAME) {
this._setupTimerPluginListener();
this._pluginsListeners.offAll();
}
}, this);
}
},
_setupTimerPluginListener: function (plugin) {
if (!plugin) {
var timerPlugin = globalShower.plugins.get(TIMER_PLUGIN_NAME, this._shower);
}
plugin.events
.on('next', this._onNext, this, 100);
},
_clearListeners: function () {
this._showerListeners.offAll();
this._playerListeners.offAll();
if (this._pluginsListeners) {
this._pluginsListeners.offAll();
}
},
_getElements: function () {
var slideLayout = this._shower.player.getCurrentSlide().layout;
var slideElement = slideLayout.getElement();
return Array.prototype.slice.call(
slideElement.querySelectorAll(this._elementsSelector)
);
},
_onNext: function (e) {
var elementsLength = this._elements.length;
var isSlideMode = this._shower.container.isSlideMode();
if (isSlideMode && elementsLength && this._innerComplete < elementsLength) {
e.preventDefault();
this.next();
}
},
_onPrev: function (e) {
var elementsLength = this._elements.length;
var isSlideMode = this._shower.container.isSlideMode();
var completed = this._innerComplete;
if (elementsLength && completed < elementsLength && completed > 0) {
e.preventDefault();
this.prev();
}
},
_go: function () {
for (var i = 0, k = this._elements.length; i < k; i++) {
var element = this._elements[i];
if (i < this._innerComplete) {
element.classList.add('active');
} else {
element.classList.remove('active');
}
}
},
_onSlideActivate: function () {
this._elements = this._getElements();
this._innerComplete = this._getInnerComplete();
},
_getInnerComplete: function () {
return this._elements.filter(function (element) {
return element.classList.contains('active');
}).length;
}
});
provide(Next);
});
shower.modules.require(['shower'], function (sh) {
sh.plugins.add('shower-next');
});