-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyass.js
More file actions
600 lines (495 loc) · 15 KB
/
Copy pathyass.js
File metadata and controls
600 lines (495 loc) · 15 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*jslint white:false, plusplus: false, regexp:true */
/*global jQuery, window, document*/
/*
YASS.js -- Yet Another Slideshow
================================
Copyright 2011–2012 Adam Lett
License: MIT http://www.opensource.org/licenses/mit-license.php
Version: 0.4.0
Github: https://github.com/KaptajnKold/YASS
Dependencies:
-------------
* EcmaScript 5
* jQuery (v1.6 testet)
* Modernizr. Specifically, the plugin relies on these classes that Modernizr sets on the html tag:
* touch
* csstransforms
* csstransitions
TODO:
* There should an event for paging
*/
;(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
'use strict';
var
win = window,
doc = document,
prefixes = 'Webkit Moz O Ms Khtml'.split(' '),
translatePattern = /-?\d+/, // matches the number part of 'translate(300px)'
$html = $('html'),
cssTransformProperty,
cssTransitionProperty,
vendorPrefix,
cssTransformsSupported = $html.hasClass('csstransforms'),
cssTransitionsSupported = $html.hasClass('csstransitions'),
prefixedTransitionEndEvents = {
Webkit: 'webkitTransitionEnd',
Moz: 'transitionend',
O: 'oTransitionEnd',
Ms: 'msTransitionEnd',
'': 'transitionend'
},
transitionEndEvent,
// Classes that YASS will apply to elements
// TODO: Make configurable
cssClasses = {
disabled: 'yass-disabled'
},
// TODO: Make prefix configurable
defaultSelectors = {
content: '.yass-content',
noContent: '.yass-no-content',
viewport: '.yass-viewport',
next: '.yass-nav-next',
prev: '.yass-nav-prev',
paging: 'ul.yass-paging-links, ol.yass-paging-links',
currentPage: '.yass-current-page',
totalPages: '.yass-total-pages',
verticalPaging: '.yass-vertical-paging',
numberedPaging: '.yass-numbers',
snapTo: '.yass-snap-to'
},
defaultOptions = {
touch: true,
// Toggles fake touch on non-touch enabled devices using mouse events
debug: false
},
isTouchScreen = $html.hasClass('touch'),
buttonEvent = isTouchScreen ? 'touchstart' : 'click';
// Source: http://lea.verou.me/2009/02/find-the-vendor-prefix-of-the-current-browser/
function getVendorPrefix () {
var
prefixPattern = /^(Moz|Webkit|Khtml|O|ms|Icab)(?=[A-Z])/,
someScript = doc.getElementsByTagName('script')[0],
prop;
for(prop in someScript.style) {
if(prefixPattern.test(prop)) {
// test is faster than match, so it's better to perform
// that on the lot and match only when necessary
return prop.match(prefixPattern)[0];
}
}
// Nothing found so far? Webkit does not enumerate over the CSS properties of the style object.
// However (prop in style) returns the correct value, so we'll have to test for
// the precence of a specific property
if('WebkitOpacity' in someScript.style) { return 'Webkit'; }
if('KhtmlOpacity' in someScript.style) { return 'Khtml'; }
return '';
}
// FIXME: This implementation will 'swallow' the last invocation of fn.
function throttle (fn, context) {
var lastInvocation = 0, threshold = 10;
return function () {
var now = new Date().getTime();
if (now - lastInvocation > threshold) {
return fn.apply(context, arguments);
}
};
}
// From: http://javascript.crockford.com/remedial.html
function supplant (o) {
return this.replace(/\{([^\{\}]*)\}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
}
vendorPrefix = getVendorPrefix();
cssTransformProperty = vendorPrefix + 'Transform';
cssTransitionProperty = vendorPrefix + 'Transition';
transitionEndEvent = prefixedTransitionEndEvents[vendorPrefix] || prefixedTransitionEndEvents[''];
function Yass (el, userOptions) {
if (userOptions && typeof userOptions !== 'object') { throw new TypeError('Yass options must be an object'); }
var
//elements
$el = $(el),
options = $.extend({}, defaultOptions, $.fn.yass.options, (userOptions || {})),
selectors = $.extend({}, defaultSelectors, $.fn.yass.selectors, (options.selectors || {})),
$viewport = $(selectors.viewport, $el), // Maybe we should allow $el to be the viewport?
$content = $(selectors.content, $el),
$noContent = $(selectors.noContent, $el),
$pagingLinks = $(selectors.paging, $el),
$prev = $(selectors.prev, $el),
$next = $(selectors.next, $el),
$currentPage = $(selectors.currentPage),
$totalPages = $(selectors.totalPages),
paginationTemplate = supplant.bind( $pagingLinks.is(selectors.numberedPaging) ? '<li>{page}</li>' : '<li> </li>' ),
verticalPaging = $el.is(selectors.verticalPaging),
scrollDirection = verticalPaging ? 'top' : 'left',
size = verticalPaging ? 'height' : 'width',
translationDirection = verticalPaging ? 'translateY' : 'translateX',
pageSize = $viewport[size].bind($viewport),
scrollPos = cssTransformsSupported ? function () {
var
style = $content[0].style[cssTransformProperty] || '',
match = style.match(translatePattern);
return match ? -Number(match[0]): 0;
} : function () {
return -parseInt($content[0].style[scrollDirection], 10) || 0;
},
setScrollPos = cssTransformsSupported ? function (pos) {
$content.css(cssTransformProperty, translationDirection + '(' + (-pos) + 'px)');
} : function (pos) {
$content.css(scrollDirection, -pos);
};
function enableTransition () {
$content.css(cssTransitionProperty, '');
}
function disableTransition () {
$content.css(cssTransitionProperty, 'none');
}
function pageCount () {
return $content[size]()/pageSize();
}
function currentPage () {
return scrollPos()/pageSize();
}
function updatePaging () {
var
current = Math.ceil(currentPage()) + 1,
total = Math.ceil(pageCount()),
isFirstPage = current === 1,
isLastPage = current === total;
$('li.current', $pagingLinks).removeClass('current');
$('li:nth-child(' + current + ')', $pagingLinks).addClass('current'),
$prev.toggleClass(cssClasses.disabled, isFirstPage).attr('tabindex', isFirstPage ? -1 : 0);
$next.toggleClass(cssClasses.disabled, isLastPage).attr('tabindex', isLastPage ? -1 : 0);
$currentPage.text(current);
$totalPages.text(total);
}
function pos (el) {
// It's tempting to just return el.offset(Left|Top), but that only works if el is a child of $content
return el ? $(el).offset()[scrollDirection] - $content.offset()[scrollDirection] : 0;
}
function pagePos (page) {
var maxPageIndex = pageCount() - 1;
if (page < 0) {
return 0;
}
if (page > maxPageIndex) {
return maxPageIndex*pageSize();
}
return Math.round(page)*pageSize();
}
function maxScrollPos () {
var excessContent = $content[size]() - pageSize();
return excessContent > 0 ? excessContent : 0;
}
function targetAdjuster (targetPos) {
var max = maxScrollPos();
if (targetPos < 0) {
return 0;
} else if ( targetPos > max) {
return max;
}
return targetPos;
}
function scroll (targetPos) {
var pos = scrollPos();
targetPos = targetAdjuster(targetPos);
if ( pos === targetPos ) { return; }
setScrollPos(targetPos);
$el.trigger('scroll');
}
function scrollToElement (targetElem) {
scroll( Math.min( Math.max(0, pos(targetElem)), maxScrollPos() ) );
if (targetElem && typeof options.onScrollTo === 'function') {
options.onScrollTo(targetElem);
}
}
function scrollToPage (pageNumber) {
scroll(pagePos(pageNumber));
}
function scrollBy (delta) {
scroll(scrollPos() + delta);
}
function scrollIntoView (target) {
var
viewportEdge = scrollPos(),
targetEdge = $(target).position()[scrollDirection],
delta = targetEdge - viewportEdge;
// Check left/top edge first
if (delta < 0) {
scrollBy(delta);
return;
}
// Check right/bottom edge next
targetEdge += $(target).outerWidth(true);
viewportEdge += pageSize();
delta = targetEdge - viewportEdge;
if (0 < delta) {
scrollBy(delta);
}
}
function scrollPage (n) {
scroll(pagePos(currentPage() + n));
}
function prevSnapEl () {
var currentScrollPos = scrollPos();
return $(selectors.snapTo).filter(function () { return currentScrollPos > pos(this); }).last()[0];
}
function nextSnapEl () {
var currentScrollPos = scrollPos();
return $(selectors.snapTo).filter(function () {
return currentScrollPos < pos(this);
})[0] || $(selectors.snapTo).last()[0];
}
function nearestSnapEl () {
var currentScrollPos = scrollPos();
return $(selectors.snapTo).toArray().reduce(function (nearestSoFar, current) {
var
nearestSoFarDist = Math.abs(currentScrollPos - pos(nearestSoFar)),
currentDist = Math.abs(currentScrollPos - pos(current));
return nearestSoFarDist < currentDist ? nearestSoFar : current;
});
}
function prev () {
var hasSnap = $(selectors.snapTo).length > 0;
if (hasSnap) {
scrollToElement(prevSnapEl());
} else {
scrollPage(-0.51);
}
}
function next () {
var hasSnap = $(selectors.snapTo).length > 0;
if (hasSnap) {
scrollToElement(nextSnapEl());
} else {
scrollPage(0.51);
}
}
function scrollToNearestPage () {
var hasSnap = $(selectors.snapTo).length > 0;
if (hasSnap) {
scrollToElement(nearestSnapEl());
} else {
scrollPage(0);
}
}
function renderNav () {
if ($pagingLinks.length === 0) { return; }
var
wholePages = Math.ceil(pageCount()),
pagingHTML = "",
i;
if (1 < wholePages) {
for (i = 0; i < wholePages; i++) {
pagingHTML += paginationTemplate({ 'page': i + 1 });
}
$pagingLinks.html(pagingHTML);
$pagingLinks.show();
} else {
$pagingLinks.hide();
}
}
function toggleContentNoContent () {
$noContent.hide();
$content.show();
if ($(':visible', $content).length === 0) {
$content.hide();
$noContent.show();
}
}
function refresh () {
toggleContentNoContent();
disableTransition();
scroll(0);
enableTransition();
renderNav();
updatePaging();
}
function applyElasticEffect (targetPos) {
var max = maxScrollPos();
if (targetPos < 0) {
return -Math.round(Math.pow(-targetPos, 4/5));
} else if ( targetPos > max) {
return max + Math.round(Math.pow(targetPos - max, 4/5));
}
return targetPos;
}
function initTouch () {
// TODO: Support vertical scrolling
var initialX, initialY, latestDelta, initialScrollPos, hasMomentum, oldtargetAdjuster, wasIntentional;
function touchStart (e) {
disableTransition();
initialX = e.originalEvent.targetTouches[0].pageX;
initialY = e.originalEvent.targetTouches[0].pageY;
initialScrollPos = scrollPos();
oldtargetAdjuster = targetAdjuster;
targetAdjuster = applyElasticEffect;
}
function touchMove (e) {
var
x = e.originalEvent.targetTouches[0].pageX,
y = e.originalEvent.targetTouches[0].pageY,
deltaX = initialX - x,
deltaY = initialY - y;
wasIntentional = wasIntentional == null ? Math.abs(deltaX) >= Math.abs(deltaY) : wasIntentional;
if (!wasIntentional) {
return;
}
e.preventDefault();
hasMomentum = Math.abs(deltaX) - Math.abs(latestDelta) > 5;
latestDelta = deltaX;
scroll(initialScrollPos + deltaX);
}
function touchEnd (e) {
enableTransition();
initialX = null;
targetAdjuster = oldtargetAdjuster;
if (hasMomentum) {
if (latestDelta < 0) {
prev();
} else {
next();
}
} else {
scrollToNearestPage();
}
wasIntentional = null;
hasMomentum = false;
latestDelta = 0;
}
$content.bind('touchstart', touchStart);
$content.bind('touchmove', throttle(touchMove));
$content.bind('touchend', touchEnd);
}
function initFakeTouch () {
// TODO: Support vertical scrolling
var initialX, latestDelta, initialScrollPos, hasMomentum, touchMoveThrottled;
function touchStart (e) {
e.preventDefault();
disableTransition();
initialX = e.pageX;
initialScrollPos = scrollPos();
$content.bind('mousemove', touchMoveThrottled);
}
function touchMove (e) {
var
x = e.pageX,
delta = initialX - x;
e.preventDefault();
hasMomentum = Math.abs(delta) - Math.abs(latestDelta) > 5;
latestDelta = delta;
scroll(initialScrollPos + delta);
}
function touchEnd (e) {
enableTransition();
initialX = null;
$content.unbind('mousemove', touchMoveThrottled);
if (hasMomentum) {
if (latestDelta < 0) {
prev();
} else {
next();
}
} else {
scrollToNearestPage();
}
hasMomentum = false;
latestDelta = 0;
}
touchMoveThrottled = throttle(touchMove);
$content.bind('mousedown', touchStart);
$content.bind('mouseup', touchEnd);
}
function handleEnter (e) {
var keyCode = e.keyCode;
if (keyCode === 10 || keyCode === 13) {
$(this).trigger(buttonEvent);
e.preventDefault();
}
}
function init() {
if (isTouchScreen && options.touch) {
initTouch();
} else if (options.debug && options.touch) {
initFakeTouch();
}
$next.bind(buttonEvent, function (e) {
e.preventDefault();
next();
}).bind('keypress', handleEnter);
$prev.bind(buttonEvent, function (e) {
e.preventDefault();
prev();
}).bind('keypress', handleEnter);
$pagingLinks.delegate('li', buttonEvent, function (e) {
scroll($(this).index() * pageSize());
});
$el.bind('keyup', function handleArrows (e) {
var keyCode = e.keyCode;
if (keyCode === 37) {
prev();
e.preventDefault();
} else if (keyCode === 39) {
next();
e.preventDefault();
}
});
if (cssTransitionsSupported) {
$content.bind(transitionEndEvent, function (e) {
// We are not interested in events from other elements
if ( !$content.is(e.target) ) { return; }
updatePaging();
if (typeof options.onTransitionEnd === 'function') {
options.onTransitionEnd();
}
});
} else {
$el.bind('scroll', updatePaging);
}
refresh();
}
// Public methods;
this.scrollToElement = scrollToElement;
this.scrollToPage = scrollToPage;
this.scrollIntoView = scrollIntoView;
this.prev = prev;
this.next = next;
this.refresh = refresh;
this.currentPage = currentPage;
init();
return this;
}
$.fn.yass = function (method) {
var
args = [].slice.apply(arguments),
returnValue;
this.each(function () {
var plugin = $(this).data('yass');
if (plugin && typeof args[0] === 'string' && typeof plugin[args[0]] === "function") {
// If a method returns a value, we only return that value for the first element in the jQuery collection
returnValue = typeof returnValue === "undefined" ? plugin[args[0]].apply(plugin, args.slice(1)): returnValue;
} else {
plugin = new Yass(this, args[0]);
$(this).data('yass', plugin);
}
});
if (typeof returnValue === "undefined") {
return this;
}
return returnValue;
};
$.fn.yass.selectors = {};
$.fn.yass.options = {};
}));