1
|
|
|
/** global: GLSR */ |
2
|
|
|
;(function() { |
3
|
|
|
|
4
|
|
|
'use strict'; |
5
|
|
|
|
6
|
|
|
var Pagination = function( el ) { // HTMLElement |
7
|
|
|
this.el = el; |
8
|
|
|
this.initEvents_(); |
9
|
|
|
}; |
10
|
|
|
|
11
|
|
|
Pagination.prototype = { |
12
|
|
|
config: { |
13
|
|
|
hideClass: 'glsr-hide', |
14
|
|
|
linkSelector: '.glsr-navigation a', |
15
|
|
|
scrollTime: 468, |
16
|
|
|
}, |
17
|
|
|
|
18
|
|
|
/** @return string */ |
19
|
|
|
getElementClass_: function( el ) { // HTMLElement |
20
|
|
|
return el.className ? '.' + el.className.trim().replace( /\s+/g, '.' ) : ''; |
21
|
|
|
}, |
22
|
|
|
|
23
|
|
|
/** @return string */ |
24
|
|
|
getElementId_: function( el ) { // HTMLElement |
25
|
|
|
return el.id ? '#' + el.id.trim() : ''; |
26
|
|
|
}, |
27
|
|
|
|
28
|
|
|
/** @return void|string */ |
29
|
|
|
getSelectorOfElement_: function( el ) { // HTMLElement |
30
|
|
|
if( !el || el.nodeType !== el.ELEMENT_NODE )return; |
31
|
|
|
return el.nodeName.toLowerCase() + this.getElementId_( el ) + this.getElementClass_( el ); |
32
|
|
|
}, |
33
|
|
|
|
34
|
|
|
/** @return void */ |
35
|
|
|
handleResponse_: function( response, location ) { // string |
36
|
|
|
var parentSelector = this.getSelectorOfElement_( this.el ); |
37
|
|
|
var html = document.implementation.createHTMLDocument( 'new' ); |
38
|
|
|
html.documentElement.innerHTML = response; |
39
|
|
|
var newParentEl = parentSelector ? html.querySelectorAll( parentSelector ) : ''; |
40
|
|
|
if( newParentEl.length === 1 ) { |
41
|
|
|
this.el.innerHTML = newParentEl[0].innerHTML; |
42
|
|
|
this.scrollToTop_( this.el ); |
43
|
|
|
this.el.classList.remove( this.config.hideClass ); |
44
|
|
|
this.initEvents_(); |
45
|
|
|
window.history.pushState( null, '', location ); |
46
|
|
|
new GLSR.Excerpts( this.el ); |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
window.location = location; // @todo test location var |
50
|
|
|
}, |
51
|
|
|
|
52
|
|
|
/** @return void */ |
53
|
|
|
initEvents_: function() { |
54
|
|
|
var links = this.el.querySelectorAll( this.config.linkSelector ); |
55
|
|
|
for( var i = 0; i < links.length; i++ ) { |
56
|
|
|
links[i].addEventListener( 'click', this.onClick_.bind( this )); |
57
|
|
|
} |
58
|
|
|
}, |
59
|
|
|
|
60
|
|
|
/** @return void */ |
61
|
|
|
onClick_: function( ev ) { // MouseEvent |
62
|
|
|
ev.preventDefault(); |
63
|
|
|
this.el.classList.add( this.config.hideClass ); |
64
|
|
|
GLSR.Ajax.get( this.href, this.handleResponse_.bind( this, ev.target.href )); |
65
|
|
|
}, |
66
|
|
|
|
67
|
|
|
/** @return void */ |
68
|
|
|
scrollToTop_: function( el, offset ) { // HTMLElement, int |
69
|
|
|
offset = offset || 16; // 1rem |
70
|
|
|
var fixedElement; |
71
|
|
|
for( var i = 0; i < GLSR.ajaxpagination.length; i++ ) { |
72
|
|
|
fixedElement = document.querySelector( GLSR.ajaxpagination[i] ); |
73
|
|
|
if( !fixedElement || window.getComputedStyle( fixedElement ).getPropertyValue( 'position' ) !== 'fixed' )continue; |
74
|
|
|
offset = offset + fixedElement.clientHeight; |
75
|
|
|
} |
76
|
|
|
var clientBounds = el.getBoundingClientRect(); |
77
|
|
|
var offsetTop = clientBounds.top - offset; |
78
|
|
|
if( offsetTop > 0 )return; // if top is in view, don't scroll! |
79
|
|
|
this.scrollToTopStep_({ |
80
|
|
|
endY: offsetTop, |
81
|
|
|
offset: window.pageYOffset, |
82
|
|
|
startTime: window.performance.now(), |
83
|
|
|
startY: el.scrollTop, |
84
|
|
|
}); |
85
|
|
|
}, |
86
|
|
|
|
87
|
|
|
/** @return void */ |
88
|
|
|
scrollToTopStep_: function( context ) { // object |
89
|
|
|
var elapsed = ( window.performance.now() - context.startTime ) / this.config.scrollTime; |
90
|
|
|
elapsed = elapsed > 1 ? 1 : elapsed; |
91
|
|
|
var easedValue = 0.5 * ( 1 - Math.cos( Math.PI * elapsed )); |
92
|
|
|
var currentY = context.startY + ( context.endY - context.startY ) * easedValue; |
93
|
|
|
window.scroll( 0, context.offset + currentY ); // @todo what is this for again? |
94
|
|
|
if( currentY !== context.endY ) { |
95
|
|
|
window.requestAnimationFrame( this.scrollToTopStep_.bind( window, context )); |
96
|
|
|
} |
97
|
|
|
}, |
98
|
|
|
}; |
99
|
|
|
|
100
|
|
|
GLSR.Pagination = function() { |
101
|
|
|
var parentEl; |
102
|
|
|
var nodeList = document.querySelectorAll( '.glsr-ajax-pagination' ); |
103
|
|
|
this.navs = []; |
104
|
|
|
for( var i = 0; i < nodeList.length; i++ ) { |
105
|
|
|
parentEl = nodeList[i].querySelector( '.glsr-reviews' ); |
106
|
|
|
if( !parentEl )continue; |
107
|
|
|
this.navs.push( new Pagination( parentEl )); |
108
|
|
|
} |
109
|
|
|
}; |
110
|
|
|
})(); |
111
|
|
|
|