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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
// prev/next buttons
( function( window, factory ) {
// universal module definition
/* jshint strict: false */
if ( typeof define == 'function' && define.amd ) {
// AMD
define( [
'./flickity',
'tap-listener/tap-listener',
'fizzy-ui-utils/utils'
], function( Flickity, TapListener, utils ) {
return factory( window, Flickity, TapListener, utils );
});
} else if ( typeof module == 'object' && module.exports ) {
// CommonJS
module.exports = factory(
window,
require('./flickity'),
require('tap-listener'),
require('fizzy-ui-utils')
);
} else {
// browser global
factory(
window,
window.Flickity,
window.TapListener,
window.fizzyUIUtils
);
}
}( window, function factory( window, Flickity, TapListener, utils ) {
'use strict';
var svgURI = 'http://www.w3.org/2000/svg';
// -------------------------- PrevNextButton -------------------------- //
function PrevNextButton( direction, parent ) {
this.direction = direction;
this.parent = parent;
this._create();
}
PrevNextButton.prototype = new TapListener();
PrevNextButton.prototype._create = function() {
// properties
this.isEnabled = true;
this.isPrevious = this.direction == -1;
var leftDirection = this.parent.options.rightToLeft ? 1 : -1;
this.isLeft = this.direction == leftDirection;
var element = this.element = document.createElement('button');
element.className = 'flickity-prev-next-button';
element.className += this.isPrevious ? ' previous' : ' next';
// prevent button from submitting form http://stackoverflow.com/a/10836076/182183
element.setAttribute( 'type', 'button' );
// init as disabled
this.disable();
element.setAttribute( 'aria-label', this.isPrevious ? 'previous' : 'next' );
// create arrow
var svg = this.createSVG();
element.appendChild( svg );
// events
this.on( 'tap', this.onTap );
this.parent.on( 'select', this.update.bind( this ) );
this.on( 'pointerDown', this.parent.childUIPointerDown.bind( this.parent ) );
};
PrevNextButton.prototype.activate = function() {
this.bindTap( this.element );
// click events from keyboard
this.element.addEventListener( 'click', this );
// add to DOM
this.parent.element.appendChild( this.element );
};
PrevNextButton.prototype.deactivate = function() {
// remove from DOM
this.parent.element.removeChild( this.element );
// do regular TapListener destroy
TapListener.prototype.destroy.call( this );
// click events from keyboard
this.element.removeEventListener( 'click', this );
};
PrevNextButton.prototype.createSVG = function() {
var svg = document.createElementNS( svgURI, 'svg');
svg.setAttribute( 'viewBox', '0 0 100 100' );
var path = document.createElementNS( svgURI, 'path');
var pathMovements = getArrowMovements( this.parent.options.arrowShape );
path.setAttribute( 'd', pathMovements );
path.setAttribute( 'class', 'arrow' );
// rotate arrow
if ( !this.isLeft ) {
path.setAttribute( 'transform', 'translate(100, 100) rotate(180) ' );
}
svg.appendChild( path );
return svg;
};
// get SVG path movmement
function getArrowMovements( shape ) {
// use shape as movement if string
if ( typeof shape == 'string' ) {
return shape;
}
// create movement string
return 'M ' + shape.x0 + ',50' +
' L ' + shape.x1 + ',' + ( shape.y1 + 50 ) +
' L ' + shape.x2 + ',' + ( shape.y2 + 50 ) +
' L ' + shape.x3 + ',50 ' +
' L ' + shape.x2 + ',' + ( 50 - shape.y2 ) +
' L ' + shape.x1 + ',' + ( 50 - shape.y1 ) +
' Z';
}
PrevNextButton.prototype.onTap = function() {
if ( !this.isEnabled ) {
return;
}
this.parent.uiChange();
var method = this.isPrevious ? 'previous' : 'next';
this.parent[ method ]();
};
PrevNextButton.prototype.handleEvent = utils.handleEvent;
PrevNextButton.prototype.onclick = function() {
// only allow clicks from keyboard
var focused = document.activeElement;
if ( focused && focused == this.element ) {
this.onTap();
}
};
// ----- ----- //
PrevNextButton.prototype.enable = function() {
if ( this.isEnabled ) {
return;
}
this.element.disabled = false;
this.isEnabled = true;
};
PrevNextButton.prototype.disable = function() {
if ( !this.isEnabled ) {
return;
}
this.element.disabled = true;
this.isEnabled = false;
};
PrevNextButton.prototype.update = function() {
// index of first or last slide, if previous or next
var slides = this.parent.slides;
// enable is wrapAround and at least 2 slides
if ( this.parent.options.wrapAround && slides.length > 1 ) {
this.enable();
return;
}
var lastIndex = slides.length ? slides.length - 1 : 0;
var boundIndex = this.isPrevious ? 0 : lastIndex;
var method = this.parent.selectedIndex == boundIndex ? 'disable' : 'enable';
this[ method ]();
};
PrevNextButton.prototype.destroy = function() {
this.deactivate();
};
// -------------------------- Flickity prototype -------------------------- //
utils.extend( Flickity.defaults, {
prevNextButtons: true,
arrowShape: {
x0: 10,
x1: 60, y1: 50,
x2: 70, y2: 40,
x3: 30
}
});
Flickity.createMethods.push('_createPrevNextButtons');
var proto = Flickity.prototype;
proto._createPrevNextButtons = function() {
if ( !this.options.prevNextButtons ) {
return;
}
this.prevButton = new PrevNextButton( -1, this );
this.nextButton = new PrevNextButton( 1, this );
this.on( 'activate', this.activatePrevNextButtons );
};
proto.activatePrevNextButtons = function() {
this.prevButton.activate();
this.nextButton.activate();
this.on( 'deactivate', this.deactivatePrevNextButtons );
};
proto.deactivatePrevNextButtons = function() {
this.prevButton.deactivate();
this.nextButton.deactivate();
this.off( 'deactivate', this.deactivatePrevNextButtons );
};
// -------------------------- -------------------------- //
Flickity.PrevNextButton = PrevNextButton;
return Flickity;
}));
|