-
Notifications
You must be signed in to change notification settings - Fork 2
/
customize-controls.js
400 lines (348 loc) · 13.3 KB
/
customize-controls.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
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
/* global wp */
/* exported CustomizerBrowserHistory */
/* eslint no-magic-numbers: ["error", { "ignore": [-1,0,1,2,10] }] */
/* eslint complexity: ["error", 8] */
var CustomizerBrowserHistory = (function( api, $ ) {
'use strict';
var component = {
defaultQueryParamValues: {},
previousQueryParams: {},
expandedPanel: new api.Value(),
expandedSection: new api.Value(),
expandedOuterSection: new api.Value(),
expandedControl: new api.Value(),
previewScrollPosition: new api.Value( 0 )
};
/**
* Get current query params.
*
* @param {string} url URL.
* @returns {object} Query params.
*/
component.getQueryParams = function getQueryParams( url ) {
var urlParser, queryParams, queryString;
urlParser = document.createElement( 'a' );
urlParser.href = url;
queryParams = {};
queryString = urlParser.search.substr( 1 );
if ( queryString ) {
queryParams = api.utils.parseQueryString( queryString );
}
// Cast scroll to integer.
if ( ! _.isUndefined( queryParams.scroll ) ) {
queryParams.scroll = parseInt( queryParams.scroll, 10 );
if ( isNaN( queryParams.scroll ) ) {
delete queryParams.scroll;
}
}
return queryParams;
};
/**
* Update the URL state with the current Customizer state, using pushState for url changes and replaceState for other changes.
*
* @returns {void}
*/
component.updateWindowLocation = _.debounce( function updateWindowLocation() { // eslint-disable-line complexity
var expandedPanel = '', expandedSection = '', expandedControl = '', values, urlParser,
oldQueryParams, newQueryParams, setQueryParams, urlChanged = false, expandedOuterSection = '';
api.panel.each( function( panel ) {
if ( panel.active() && panel.expanded() ) {
expandedPanel = panel.id;
}
} );
api.section.each( function( section ) {
if ( section.active() && section.expanded() ) {
if ( api.OuterSection && section.extended( api.OuterSection ) ) {
expandedOuterSection = section.id;
} else {
expandedSection = section.id;
}
}
} );
if ( expandedSection ) {
api.control.each( function( control ) {
if ( expandedSection && control.section() === expandedSection && control.active() && control.expanded && control.expanded() ) {
expandedControl = control.id;
}
} );
}
component.expandedPanel.set( expandedPanel );
component.expandedSection.set( expandedSection );
component.expandedOuterSection.set( expandedOuterSection );
component.expandedControl.set( expandedControl );
component.previewScrollPosition.set( api.previewer.scroll );
oldQueryParams = component.getQueryParams( location.href );
newQueryParams = {};
values = {
'url': api.previewer.previewUrl,
'autofocus[panel]': component.expandedPanel,
'autofocus[section]': component.expandedSection,
'autofocus[outer_section]': component.expandedOuterSection,
'autofocus[control]': component.expandedControl,
'device': api.previewedDevice,
'scroll': component.previewScrollPosition
};
// Preserve extra vars.
_.each( _.keys( oldQueryParams ), function( key ) {
if ( 'undefined' === typeof values[ key ] ) {
newQueryParams[ key ] = oldQueryParams[ key ];
}
} );
// Collect new query params.
_.each( values, function( valueObj, key ) {
var value = valueObj.get();
if ( null !== value ) {
newQueryParams[ key ] = value;
}
} );
/*
* There can only be one. Well, there should be. Let presence control override section,
* and section override panel. But if something was not registered in PHP, then include
* the autofocus parameter for its parent as it is likely lazy-loaded upon parent expanded.
*/
if ( newQueryParams['autofocus[section]'] && ! _.isUndefined( api.settings.sections[ newQueryParams['autofocus[section]'] ] ) ) {
delete newQueryParams['autofocus[panel]'];
}
if ( newQueryParams['autofocus[control]'] && ! _.isUndefined( api.settings.sections[ newQueryParams['autofocus[control]'] ] ) ) {
delete newQueryParams['autofocus[section]'];
}
// Delete the section if its parent panel is not expanded.
if (
component.expandedSection.get() &&
api.section.has( component.expandedSection.get() ) &&
api.section( component.expandedSection.get() ).panel() &&
api.panel.has( api.section( component.expandedSection.get() ).panel() ) &&
! api.panel( api.section( component.expandedSection.get() ).panel() ).expanded()
) {
delete newQueryParams['autofocus[section]'];
}
if ( ! _.isEqual( newQueryParams, oldQueryParams ) ) {
setQueryParams = {};
_.each( newQueryParams, function( value, key ) {
if ( value !== component.defaultQueryParamValues[ key ] ) {
setQueryParams[ key ] = value;
}
} );
urlParser = document.createElement( 'a' );
urlParser.href = location.href;
urlParser.search = _.map( setQueryParams, function( value, key ) {
var pair = encodeURIComponent( key );
if ( null !== value ) {
pair += '=' + encodeURIComponent( value );
}
pair = pair.replace( /%5B/g, '[' ).replace( /%5D/g, ']' ).replace( /%2F/g, '/' ).replace( /%3A/g, ':' );
return pair;
} ).join( '&' );
if ( newQueryParams.url !== ( oldQueryParams.url || component.defaultQueryParamValues.url ) ) {
urlChanged = true;
}
// Send the state to the parent window.
if ( urlChanged ) {
history.pushState( {}, '', urlParser.href );
} else {
history.replaceState( {}, '', urlParser.href );
}
component.previousQueryParams = newQueryParams;
}
} );
/**
* On history popstate, set the URL to match.
*
* @param {object} queryParams Query params.
* @returns {void}
*/
component.updatePreviewUrl = function updatePreviewUrl( queryParams ) {
var url = null;
// Preserve the old scroll position.
if ( queryParams.scroll ) {
api.previewer.scroll = queryParams.scroll;
} else {
api.previewer.scroll = 0;
}
// Update the url.
if ( queryParams.url ) {
url = queryParams.url;
} else {
url = api.settings.url.preview; // On pop to initial state, the state is null.
}
api.previewer.previewUrl.set( url );
};
/**
* Watch for changes to a construct's active and expanded states.
*
* @param {wp.customize.Panel|wp.customize.Section|wp.customize.Control} construct Construct.
* @returns {void}
*/
component.watchExpandedChange = function watchExpandedChange( construct ) {
if ( construct.active ) {
construct.active.bind( component.updateWindowLocation );
}
if ( construct.expanded ) {
construct.expanded.bind( component.updateWindowLocation );
}
component.updateWindowLocation();
};
/**
* Unwatch for changes to a construct's active and expanded states.
*
* @param {wp.customize.Panel|wp.customize.Section|wp.customize.Control} construct Construct.
* @returns {void}
*/
component.unwatchExpandedChange = function unwatchExpandedChange( construct ) {
if ( construct.active ) {
construct.active.unbind( component.updateWindowLocation );
}
if ( construct.expanded ) {
construct.expanded.unbind( component.updateWindowLocation );
}
// Because 'remove' event is triggered before the construct is removed. See #37269.
_.delay( function() {
component.updateWindowLocation();
} );
};
/*
* Strip out autofocus[section]=installed_themes and autofocus[panel]=themes before redirection.
* The user will not want to see the theme browser after switching a theme.
*/
if ( api.ThemesPanel ) {
api.ThemesPanel.prototype.loadThemePreview = (function( loadThemePreview ) {
return function( themeId ) {
var panel = this, promise, urlReplaced = false, queryParams, urlParser, originalUrl = location.href; // eslint-disable-line consistent-this
// Strip autofocus params from URL when they are for themes panel.
if ( panel.expanded() ) {
queryParams = component.getQueryParams( originalUrl );
delete queryParams['autofocus[panel]'];
delete queryParams['autofocus[control]'];
delete queryParams['autofocus[section]'];
urlParser = document.createElement( 'a' );
urlParser.href = location.href;
urlParser.search = $.param( queryParams );
if ( urlParser.href !== location.href ) {
history.replaceState( {}, '', urlParser.href );
urlReplaced = true;
}
}
promise = loadThemePreview.call( panel, themeId );
// Restore the original URL when loading of theme fails.
promise.fail( function() {
if ( urlReplaced ) {
history.replaceState( {}, '', originalUrl );
}
} );
return promise;
};
})( api.ThemesPanel.prototype.loadThemePreview );
}
/**
* Update window.location to sync with customizer state.
*
* @returns {void}
*/
component.startUpdatingWindowLocation = function startUpdatingWindowLocation() {
var outerSection, outerSectionParam = 'autofocus[outer_section]',
currentQueryParams = component.getQueryParams( location.href );
if ( currentQueryParams.scroll ) {
component.previewScrollPosition.set( currentQueryParams.scroll );
api.previewer.scroll = component.previewScrollPosition.get();
api.previewer.send( 'scroll', component.previewScrollPosition.get() );
}
if ( _.has( currentQueryParams, outerSectionParam ) && api.section.has( currentQueryParams[ outerSectionParam ] ) ) {
outerSection = api.section( currentQueryParams[ outerSectionParam ] );
outerSection.deferred.embedded.done( function() {
outerSection.focus();
} );
}
component.defaultQueryParamValues = {
'device': (function() {
var defaultPreviewedDevice = null;
_.find( api.settings.previewableDevices, function checkDefaultPreviewedDevice( params, device ) {
if ( true === params['default'] ) {
defaultPreviewedDevice = device;
return true;
}
return false;
} );
return defaultPreviewedDevice;
} )(),
'scroll': 0,
'url': api.settings.url.home,
'autofocus[panel]': '',
'autofocus[section]': '',
'autofocus[outer_section]': '',
'autofocus[control]': ''
};
component.previousQueryParams = _.extend( {}, currentQueryParams );
$( window ).on( 'popstate', function onPopState( event ) {
var urlParser, queryParams;
urlParser = document.createElement( 'a' );
urlParser.href = location.href;
queryParams = api.utils.parseQueryString( urlParser.search.substr( 1 ) );
component.updatePreviewUrl( queryParams );
// Make sure the current changeset_uuid is in the URL (if changesets are available).
if ( api.settings.changeset && queryParams.changeset_uuid !== api.settings.changeset.uuid ) {
queryParams.changeset_uuid = api.settings.changeset.uuid;
urlParser.search = $.param( queryParams ).replace( /%5B/g, '[' ).replace( /%5D/g, ']' ).replace( /%2F/g, '/' ).replace( /%3A/g, ':' );
history.replaceState( event.originalEvent.state, '', urlParser.href );
}
} );
component.expandedPanel.set( api.settings.autofocus.panel || '' );
component.expandedSection.set( api.settings.autofocus.section || '' );
component.expandedControl.set( api.settings.autofocus.control || '' );
api.control.each( component.watchExpandedChange );
api.section.each( component.watchExpandedChange );
api.panel.each( component.watchExpandedChange );
api.control.bind( 'add', component.watchExpandedChange );
api.section.bind( 'add', component.watchExpandedChange );
api.panel.bind( 'add', component.watchExpandedChange );
api.control.bind( 'remove', component.unwatchExpandedChange );
api.section.bind( 'remove', component.unwatchExpandedChange );
api.panel.bind( 'remove', component.unwatchExpandedChange );
api.previewedDevice.bind( component.updateWindowLocation );
api.previewer.previewUrl.bind( component.updateWindowLocation );
api.previewer.bind( 'scroll', component.updateWindowLocation );
component.previewScrollPosition.bind( component.updateWindowLocation );
component.updateWindowLocation();
};
/**
* Set up functionality.
*
* @returns {void}
*/
component.ready = function ready() {
var closeLink = $( '.customize-controls-close' ), rememberScrollPosition;
// Short-circuit if not supported or if using customize-loader.
if ( ! history.replaceState || ! history.pushState || top !== window ) {
return;
}
rememberScrollPosition = function() {
sessionStorage.setItem( 'lastCustomizerScrollPosition', api.previewer.scroll );
};
// Update close link URL to be preview URL and remember scroll position if close link is not back to WP Admin.
if ( -1 === closeLink.prop( 'pathname' ).indexOf( '/wp-admin/' ) ) {
// Sync the preview URL to the close button so the URL navigated to upon closing is the last URL which was previewed.
api.previewer.previewUrl.bind( function( newPreviewUrl ) {
var urlParser = document.createElement( 'a' );
urlParser.href = newPreviewUrl;
// Only copy the path and query vars since the frontend URL may have a different domain.
closeLink.prop( 'pathname', urlParser.pathname );
closeLink.prop( 'search', urlParser.search );
});
// Remember scroll position if we're going back to the frontend.
closeLink.on( 'click', rememberScrollPosition );
}
/*
* Start syncing state once the preview loads so that the active panels/sections/controls
* have been set to prevent the URL from being momentarily having autofocus params removed.
*/
api.previewer.deferred.active.done( component.startUpdatingWindowLocation );
};
/**
* Initialize component.
*
* @returns {void}
*/
component.init = function init() {
api.bind( 'ready', component.ready );
};
return component;
})( wp.customize, jQuery );