forked from ampproject/amphtml
/
base-carousel.js
198 lines (173 loc) · 5.09 KB
/
base-carousel.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
197
198
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {timerFor} from '../../../src/timer';
/**
* @abstract
*/
export class BaseCarousel extends AMP.BaseElement {
/** @param {!AmpElement} element */
constructor(element) {
super(element);
/** @private {?Element} */
this.prevButton_ = null;
/** @private {?Element} */
this.nextButton_ = null;
/** @private {boolean} */
this.showControls_ = false;
}
/** @override */
buildCallback() {
this.showControls_ = this.element.hasAttribute('controls');
if (this.showControls_) {
this.element.classList.add('-amp-carousel-has-controls');
}
this.buildCarousel();
this.buildButtons();
this.setupGestures();
this.setControlsState();
}
/** @override */
viewportCallback(inViewport) {
this.onViewportCallback(inViewport);
if (inViewport) {
this.hintControls();
}
}
/**
* Handles element specific viewport based events.
* @param {boolean} unusedInViewport.
* @protected
*/
onViewportCallback(unusedInViewport) {}
buildButtons() {
this.prevButton_ = this.element.ownerDocument.createElement('div');
this.prevButton_.classList.add('amp-carousel-button');
this.prevButton_.classList.add('amp-carousel-button-prev');
this.prevButton_.setAttribute('role', 'button');
// TODO(erwinm): Does label need i18n support in the future? or provide
// a way to be overridden.
this.prevButton_.setAttribute('aria-label', 'previous');
this.prevButton_.onclick = () => {
this.interactionPrev();
};
this.element.appendChild(this.prevButton_);
this.nextButton_ = this.element.ownerDocument.createElement('div');
this.nextButton_.classList.add('amp-carousel-button');
this.nextButton_.classList.add('amp-carousel-button-next');
this.nextButton_.setAttribute('role', 'button');
this.nextButton_.setAttribute('aria-label', 'next');
this.nextButton_.onclick = () => {
this.interactionNext();
};
this.element.appendChild(this.nextButton_);
}
/** @override */
prerenderAllowed() {
return true;
}
/** @override */
isRelayoutNeeded() {
return true;
}
/**
* Subclasses should override this method to build the UI for the carousel.
* @abstract
*/
buildCarousel() {
// Subclasses may override.
}
/**
* Subclasses may override this method to configure gestures for carousel.
*/
setupGestures() {
// Subclasses may override.
}
/**
* Calls `goCallback` and any additional work needed to proceed to next
* desired direction.
* @param {number} dir -1 or 1
* @param {boolean} animate
* @param {boolean=} opt_autoplay
*/
go(dir, animate, opt_autoplay = false) {
this.goCallback(dir, animate, opt_autoplay);
}
/**
* Proceeds to the next slide in the desired direction.
* @param {number} unusedDir -1 or 1
* @param {boolean} unusedAnimate
* @param {boolean=} opt_autoplay
*/
goCallback(unusedDir, unusedAnimate, opt_autoplay) {
// Subclasses may override.
}
/**
* Sets the previous and next button visual states.
*/
setControlsState() {
this.prevButton_.classList.toggle('amp-disabled', !this.hasPrev());
this.prevButton_.setAttribute('aria-disabled', !this.hasPrev());
this.nextButton_.classList.toggle('amp-disabled', !this.hasNext());
this.nextButton_.setAttribute('aria-disabled', !this.hasNext());
}
/**
* Shows the controls and then fades them away.
*/
hintControls() {
if (this.showControls_ || !this.isInViewport()) {
return;
}
this.getVsync().mutate(() => {
const className = '-amp-carousel-button-start-hint';
this.element.classList.add(className);
timerFor(this.win).delay(() => {
this.deferMutate(() => this.element.classList.remove(className));
}, 1000);
});
}
/** @override */
unlayoutCallback() {
return true;
}
/**
* @return {boolean}
*/
hasPrev() {
// Subclasses may override.
}
/**
* @return {boolean}
*/
hasNext() {
// Subclasses may override.
}
/**
* Called on user interaction to proceed to the next item/position.
*/
interactionNext() {
if (!this.nextButton_.classList.contains('amp-disabled')) {
this.go(/* dir */ 1, /* animate */ true, /* autoplay */ false);
}
}
/**
* Called on user interaction to proceed to the previous item/position.
*/
interactionPrev() {
if (!this.prevButton_.classList.contains('amp-disabled')) {
this.go(/* dir */ -1, /* animate */ true, /* autoplay */ false);
}
}
}