-
Notifications
You must be signed in to change notification settings - Fork 2
/
frontend-scroll-persistence.js
67 lines (55 loc) · 1.62 KB
/
frontend-scroll-persistence.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
/* global _ */
/* eslint no-magic-numbers: ["error", { "ignore": [0,1] }] */
/* exported CustomizerBrowserHistoryFrontendScrollPersistence */
var CustomizerBrowserHistoryFrontendScrollPersistence = (function( $ ) {
'use strict';
var component = {};
/**
* Update scroll param.
*
* @returns {void}
*/
component.updateScrollParam = function updateScrollParam() {
var query = $( this ).prop( 'search' ).substr( 1 );
// Remove existing scroll param.
query = _.filter( query.split( /&/ ), function( pair ) {
return ! /^scroll=/.test( pair );
}).join( '&' );
// Append new scroll param to query string.
if ( query.length > 0 ) {
query += '&';
}
query += 'scroll=' + String( $( window ).scrollTop() );
// Update query string.
$( this ).prop( 'search', query );
};
/**
* Restore the scroll position the user was last at in the Customizer preview.
*
* @returns {void}
*/
component.restoreScrollPosition = function restoreScrollPosition() {
if ( 'undefined' !== typeof sessionStorage && sessionStorage.getItem( 'lastCustomizerScrollPosition' ) ) {
$( window ).scrollTop( parseInt( sessionStorage.getItem( 'lastCustomizerScrollPosition' ), 10 ) );
sessionStorage.removeItem( 'lastCustomizerScrollPosition' );
}
};
/**
* Set up functionality.
*
* @returns {void}
*/
component.ready = function ready() {
component.restoreScrollPosition();
$( '#wp-admin-bar-customize > a' ).on( 'mouseover mousedown click', component.updateScrollParam );
};
/**
* Initialize.
*
* @returns {void}
*/
component.init = function init() {
$( component.ready );
};
return component;
})( jQuery );