customize-preview-nav-menus.js
8.34 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
/* global JSON, _wpCustomizePreviewNavMenusExports */
( function( $, _, wp ) {
'use strict';
if ( ! wp || ! wp.customize ) { return; }
var api = wp.customize,
currentRefreshDebounced = {},
refreshDebounceDelay = 200,
settings = {},
defaultSettings = {
renderQueryVar: null,
renderNonceValue: null,
renderNoncePostKey: null,
previewCustomizeNonce: null,
requestUri: '/',
theme: {
active: false,
stylesheet: ''
},
navMenuInstanceArgs: {}
};
api.MenusCustomizerPreview = {
/**
* Bootstrap functionality.
*/
init : function() {
var self = this, initializedSettings = {};
settings = _.extend( {}, defaultSettings );
if ( 'undefined' !== typeof _wpCustomizePreviewNavMenusExports ) {
_.extend( settings, _wpCustomizePreviewNavMenusExports );
}
api.each( function( setting, id ) {
setting.id = id;
initializedSettings[ setting.id ] = true;
self.bindListener( setting );
} );
api.preview.bind( 'setting', function( args ) {
var id, value, setting;
args = args.slice();
id = args.shift();
value = args.shift();
setting = api( id );
if ( ! setting ) {
// Currently customize-preview.js is not creating settings for dynamically-created settings in the pane, so we have to do it.
setting = api.create( id, value ); // @todo This should be in core
}
if ( ! setting.id ) {
// Currently customize-preview.js doesn't set the id property for each setting, like customize-controls.js does.
setting.id = id;
}
if ( ! initializedSettings[ setting.id ] ) {
initializedSettings[ setting.id ] = true;
if ( self.bindListener( setting ) ) {
setting.callbacks.fireWith( setting, [ setting(), null ] );
}
}
} );
},
/**
*
* @param {wp.customize.Value} setting
* @returns {boolean} Whether the setting was bound.
*/
bindListener : function( setting ) {
var matches, themeLocation;
matches = setting.id.match( /^nav_menu\[(-?\d+)]$/ );
if ( matches ) {
setting.navMenuId = parseInt( matches[1], 10 );
setting.bind( this.onChangeNavMenuSetting );
return true;
}
matches = setting.id.match( /^nav_menu_item\[(-?\d+)]$/ );
if ( matches ) {
setting.navMenuItemId = parseInt( matches[1], 10 );
setting.bind( this.onChangeNavMenuItemSetting );
return true;
}
matches = setting.id.match( /^nav_menu_locations\[(.+?)]/ );
if ( matches ) {
themeLocation = matches[1];
setting.bind( _.bind( function() {
this.refreshMenuLocation( themeLocation );
}, this ) );
return true;
}
return false;
},
/**
* Handle changing of a nav_menu setting.
*
* @this {wp.customize.Setting}
*/
onChangeNavMenuSetting : function() {
var setting = this;
if ( ! setting.navMenuId ) {
throw new Error( 'Expected navMenuId property to be set.' );
}
api.MenusCustomizerPreview.refreshMenu( setting.navMenuId );
},
/**
* Handle changing of a nav_menu_item setting.
*
* @this {wp.customize.Setting}
* @param {object} to
* @param {object} from
*/
onChangeNavMenuItemSetting : function( to, from ) {
if ( from && from.nav_menu_term_id && ( ! to || from.nav_menu_term_id !== to.nav_menu_term_id ) ) {
api.MenusCustomizerPreview.refreshMenu( from.nav_menu_term_id );
}
if ( to && to.nav_menu_term_id ) {
api.MenusCustomizerPreview.refreshMenu( to.nav_menu_term_id );
}
},
/**
* Update a given menu rendered in the preview.
*
* @param {int} menuId
*/
refreshMenu : function( menuId ) {
var assignedLocations = [];
api.each(function( setting, id ) {
var matches = id.match( /^nav_menu_locations\[(.+?)]/ );
if ( matches && menuId === setting() ) {
assignedLocations.push( matches[1] );
}
});
_.each( settings.navMenuInstanceArgs, function( navMenuArgs, instanceNumber ) {
if ( menuId === navMenuArgs.menu || -1 !== _.indexOf( assignedLocations, navMenuArgs.theme_location ) ) {
this.refreshMenuInstanceDebounced( instanceNumber );
}
}, this );
},
/**
* Refresh the menu(s) associated with a given nav menu location.
*
* @param {string} location
*/
refreshMenuLocation : function( location ) {
var foundInstance = false;
_.each( settings.navMenuInstanceArgs, function( navMenuArgs, instanceNumber ) {
if ( location === navMenuArgs.theme_location ) {
this.refreshMenuInstanceDebounced( instanceNumber );
foundInstance = true;
}
}, this );
if ( ! foundInstance ) {
api.preview.send( 'refresh' );
}
},
/**
* Update a specific instance of a given menu on the page.
*
* @param {int} instanceNumber
*/
refreshMenuInstance : function( instanceNumber ) {
var data, menuId, customized, container, request, wpNavMenuArgs, instance, containerInstanceClassName;
if ( ! settings.navMenuInstanceArgs[ instanceNumber ] ) {
throw new Error( 'unknown_instance_number' );
}
instance = settings.navMenuInstanceArgs[ instanceNumber ];
containerInstanceClassName = 'partial-refreshable-nav-menu-' + String( instanceNumber );
container = $( '.' + containerInstanceClassName );
if ( _.isNumber( instance.menu ) ) {
menuId = instance.menu;
} else if ( instance.theme_location && api.has( 'nav_menu_locations[' + instance.theme_location + ']' ) ) {
menuId = api( 'nav_menu_locations[' + instance.theme_location + ']' ).get();
}
if ( ! menuId || ! instance.can_partial_refresh || 0 === container.length ) {
api.preview.send( 'refresh' );
return;
}
menuId = parseInt( menuId, 10 );
data = {
nonce: settings.previewCustomizeNonce, // for Customize Preview
wp_customize: 'on'
};
if ( ! settings.theme.active ) {
data.theme = settings.theme.stylesheet;
}
data[ settings.renderQueryVar ] = '1';
// Gather settings to send in partial refresh request.
customized = {};
api.each( function( setting, id ) {
var value = setting.get(), shouldSend = false;
// @todo Core should propagate the dirty state into the Preview as well so we can use that here.
// Send setting if it is a nav_menu_locations[] setting.
shouldSend = shouldSend || /^nav_menu_locations\[/.test( id );
// Send setting if it is the setting for this menu.
shouldSend = shouldSend || id === 'nav_menu[' + String( menuId ) + ']';
// Send setting if it is one that is associated with this menu, or it is deleted.
shouldSend = shouldSend || ( /^nav_menu_item\[/.test( id ) && ( false === value || menuId === value.nav_menu_term_id ) );
if ( shouldSend ) {
customized[ id ] = value;
}
} );
data.customized = JSON.stringify( customized );
data[ settings.renderNoncePostKey ] = settings.renderNonceValue;
wpNavMenuArgs = $.extend( {}, instance );
data.wp_nav_menu_args_hash = wpNavMenuArgs.args_hash;
delete wpNavMenuArgs.args_hash;
data.wp_nav_menu_args = JSON.stringify( wpNavMenuArgs );
container.addClass( 'customize-partial-refreshing' );
request = wp.ajax.send( null, {
data: data,
url: settings.requestUri
} );
request.done( function( data ) {
// If the menu is now not visible, refresh since the page layout may have changed.
if ( false === data ) {
api.preview.send( 'refresh' );
return;
}
var eventParam, previousContainer = container;
container = $( data );
container.addClass( containerInstanceClassName );
container.addClass( 'partial-refreshable-nav-menu customize-partial-refreshing' );
previousContainer.replaceWith( container );
eventParam = {
instanceNumber: instanceNumber,
wpNavArgs: wpNavMenuArgs, // @deprecated
wpNavMenuArgs: wpNavMenuArgs,
oldContainer: previousContainer,
newContainer: container
};
container.removeClass( 'customize-partial-refreshing' );
$( document ).trigger( 'customize-preview-menu-refreshed', [ eventParam ] );
} );
},
refreshMenuInstanceDebounced : function( instanceNumber ) {
if ( currentRefreshDebounced[ instanceNumber ] ) {
clearTimeout( currentRefreshDebounced[ instanceNumber ] );
}
currentRefreshDebounced[ instanceNumber ] = setTimeout(
_.bind( function() {
this.refreshMenuInstance( instanceNumber );
}, this ),
refreshDebounceDelay
);
}
};
api.bind( 'preview-ready', function() {
api.preview.bind( 'active', function() {
api.MenusCustomizerPreview.init();
} );
} );
}( jQuery, _, wp ) );