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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
// -------------------------- prev/next button -------------------------- //
( function( window, factory ) {
'use strict';
// universal module definition
if ( typeof define == 'function' && define.amd ) {
// AMD
define( [
'eventie/eventie',
'./flickity',
'tap-listener/tap-listener',
'fizzy-ui-utils/utils'
], function( eventie, Flickity, TapListener, utils ) {
return factory( window, eventie, Flickity, TapListener, utils );
});
} else if ( typeof exports == 'object' ) {
// CommonJS
module.exports = factory(
window,
require('eventie'),
require('./flickity'),
require('tap-listener'),
require('fizzy-ui-utils')
);
} else {
// browser global
factory(
window,
window.eventie,
window.Flickity,
window.TapListener,
window.fizzyUIUtils
);
}
}( window, function factory( window, eventie, Flickity, TapListener, utils ) {
'use strict';
// ----- inline SVG support ----- //
var svgURI = 'http://www.w3.org/2000/svg';
// only check on demand, not on script load
var supportsInlineSVG = ( function() {
var supports;
function checkSupport() {
if ( supports !== undefined ) {
return supports;
}
var div = document.createElement('div');
div.innerHTML = '<svg/>';
supports = ( div.firstChild && div.firstChild.namespaceURI ) == svgURI;
return supports;
}
return checkSupport;
})();
// -------------------------- 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' );
Flickity.setUnselectable( element );
// create arrow
if ( supportsInlineSVG() ) {
var svg = this.createSVG();
element.appendChild( svg );
} else {
// SVG not supported, set button text
this.setArrowText();
element.className += ' no-svg';
}
// update on select
var _this = this;
this.onCellSelect = function() {
_this.update();
};
this.parent.on( 'cellSelect', this.onCellSelect );
// tap
this.on( 'tap', this.onTap );
// pointerDown
this.on( 'pointerDown', function onPointerDown( button, event ) {
_this.parent.childUIPointerDown( event );
});
};
PrevNextButton.prototype.activate = function() {
this.update();
this.bindTap( this.element );
// click events from keyboard
eventie.bind( this.element, '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
eventie.unbind( this.element, '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.setArrowText = function() {
var parentOptions = this.parent.options;
var arrowText = this.isLeft ? parentOptions.leftArrowText : parentOptions.rightArrowText;
utils.setText( this.element, arrowText );
};
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 cell, if previous or next
var cells = this.parent.cells;
// enable is wrapAround and at least 2 cells
if ( this.parent.options.wrapAround && cells.length > 1 ) {
this.enable();
return;
}
var lastIndex = cells.length ? cells.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,
leftArrowText: '‹',
rightArrowText: '›',
arrowShape: {
x0: 10,
x1: 60, y1: 50,
x2: 70, y2: 40,
x3: 30
}
});
Flickity.createMethods.push('_createPrevNextButtons');
Flickity.prototype._createPrevNextButtons = function() {
if ( !this.options.prevNextButtons ) {
return;
}
this.prevButton = new PrevNextButton( -1, this );
this.nextButton = new PrevNextButton( 1, this );
this.on( 'activate', this.activatePrevNextButtons );
};
Flickity.prototype.activatePrevNextButtons = function() {
this.prevButton.activate();
this.nextButton.activate();
this.on( 'deactivate', this.deactivatePrevNextButtons );
};
Flickity.prototype.deactivatePrevNextButtons = function() {
this.prevButton.deactivate();
this.nextButton.deactivate();
this.off( 'deactivate', this.deactivatePrevNextButtons );
};
// -------------------------- -------------------------- //
Flickity.PrevNextButton = PrevNextButton;
return Flickity;
}));
|