|
1 |
| -/* global jQuery, _customizeSnapshots, JSON */ |
| 1 | +/* global jQuery, _customizeSnapshots */ |
2 | 2 | /* eslint-disable no-extra-parens */
|
3 | 3 |
|
4 | 4 | ( function( api, $ ) {
|
|
28 | 28 | window._wpCustomizeControlsL10n.saved = component.data.i18n.published;
|
29 | 29 |
|
30 | 30 | api.bind( 'ready', function() {
|
31 |
| - if ( ! api.settings.theme.active || ( component.data.theme && component.data.theme !== api.settings.theme.stylesheet ) ) { |
32 |
| - return; |
33 |
| - } |
| 31 | + api.state.create( 'snapshot-exists', component.data.snapshotExists ); |
34 | 32 | api.state.create( 'snapshot-saved', true );
|
35 |
| - api.state.create( 'snapshot-exists', Boolean( component.data.isPreview ) ); |
36 | 33 | api.state.create( 'snapshot-submitted', true );
|
37 | 34 | api.bind( 'change', function() {
|
38 | 35 | api.state( 'snapshot-saved' ).set( false );
|
|
41 | 38 | component.frontendPreviewUrl = new api.Value( api.previewer.previewUrl.get() );
|
42 | 39 | component.frontendPreviewUrl.link( api.previewer.previewUrl );
|
43 | 40 |
|
44 |
| - component.previewerQuery(); |
| 41 | + component.extendPreviewerQuery(); |
45 | 42 | component.addButtons();
|
46 | 43 |
|
47 | 44 | $( '#snapshot-save' ).on( 'click', function( event ) {
|
|
92 | 89 | return request;
|
93 | 90 | } );
|
94 | 91 |
|
95 |
| - api.bind( 'saved', function() { |
| 92 | + api.bind( 'saved', function( response ) { |
96 | 93 | var url = window.location.href,
|
97 |
| - request, |
98 | 94 | updatedUrl,
|
99 | 95 | urlParts;
|
100 | 96 |
|
101 |
| - // Set the button text back to "Save". |
102 |
| - component.changeButton( component.data.i18n.saveButton, component.data.i18n.permsMsg.save ); |
103 |
| - api.state( 'snapshot-exists' ).set( false ); |
104 |
| - |
105 |
| - request = wp.ajax.post( 'customize_get_snapshot_uuid', { |
106 |
| - nonce: component.data.nonce, |
107 |
| - wp_customize: 'on' |
108 |
| - } ); |
109 |
| - |
110 | 97 | // Update the UUID.
|
111 |
| - request.done( function( response ) { |
112 |
| - component.data.uuid = response.uuid; |
| 98 | + if ( response.new_customize_snapshot_uuid ) { |
| 99 | + component.data.uuid = response.new_customize_snapshot_uuid; |
113 | 100 | component.previewLink.attr( 'target', component.data.uuid );
|
114 |
| - } ); |
| 101 | + } |
| 102 | + if ( response.edit_link ) { |
| 103 | + component.data.editLink = response.edit_link; |
| 104 | + } |
| 105 | + |
| 106 | + api.state( 'snapshot-exists' ).set( false ); |
115 | 107 |
|
116 | 108 | // Replace the history state with an updated Customizer URL that does not include the Snapshot UUID.
|
117 | 109 | urlParts = url.split( '?' );
|
|
132 | 124 | *
|
133 | 125 | * @return {void}
|
134 | 126 | */
|
135 |
| - component.previewerQuery = function() { |
| 127 | + component.extendPreviewerQuery = function() { |
136 | 128 | var originalQuery = api.previewer.query;
|
137 | 129 |
|
138 | 130 | api.previewer.query = function() {
|
139 |
| - var allCustomized = {}, |
140 |
| - retval; |
141 |
| - |
142 |
| - retval = originalQuery.apply( this, arguments ); |
143 |
| - |
| 131 | + var retval = originalQuery.apply( this, arguments ); |
144 | 132 | if ( api.state( 'snapshot-exists' ).get() ) {
|
145 |
| - api.each( function( value, key ) { |
146 |
| - if ( value._dirty ) { |
147 |
| - allCustomized[ key ] = { |
148 |
| - 'value': value() |
149 |
| - }; |
150 |
| - } |
151 |
| - } ); |
152 |
| - retval.snapshot_customized = JSON.stringify( allCustomized ); |
153 |
| - retval.snapshot_uuid = component.data.uuid; |
| 133 | + retval.customize_snapshot_uuid = component.data.uuid; |
154 | 134 | }
|
155 |
| - |
156 | 135 | return retval;
|
157 | 136 | };
|
158 | 137 | };
|
|
180 | 159 | component.addButtons = function() {
|
181 | 160 | var header = $( '#customize-header-actions' ),
|
182 | 161 | publishButton = header.find( '#save' ),
|
183 |
| - snapshotButton, submitButton, data, setPreviewLinkHref; |
| 162 | + snapshotEditLinkTemplate = wp.template( 'snapshot-edit-link' ), |
| 163 | + snapshotButton, submitButton, data, setPreviewLinkHref, snapshotEditLinkEl; |
184 | 164 |
|
185 | 165 | // Save/update button.
|
186 | 166 | snapshotButton = wp.template( 'snapshot-save' );
|
|
194 | 174 | snapshotButton.prop( 'disabled', true );
|
195 | 175 | snapshotButton.insertAfter( publishButton );
|
196 | 176 |
|
| 177 | + snapshotEditLinkEl = $( $.trim( snapshotEditLinkTemplate( component.data ) ) ); |
| 178 | + snapshotEditLinkEl.insertAfter( snapshotButton ); |
| 179 | + if ( ! component.data.editLink ) { |
| 180 | + snapshotEditLinkEl.hide(); |
| 181 | + } |
| 182 | + api.state.bind( 'change', function() { |
| 183 | + snapshotEditLinkEl.toggle( api.state( 'snapshot-saved' ).get() && api.state( 'snapshot-exists' ).get() ); |
| 184 | + } ); |
| 185 | + |
| 186 | + api.state( 'snapshot-saved' ).bind( function( saved ) { |
| 187 | + snapshotButton.prop( 'disabled', saved ); |
| 188 | + if ( saved ) { |
| 189 | + snapshotEditLinkEl.attr( 'href', component.data.editLink ); |
| 190 | + } |
| 191 | + } ); |
| 192 | + |
| 193 | + api.state( 'saved' ).bind( function( saved ) { |
| 194 | + if ( saved ) { |
| 195 | + snapshotButton.prop( 'disabled', true ); |
| 196 | + } |
| 197 | + } ); |
| 198 | + api.bind( 'change', function() { |
| 199 | + snapshotButton.prop( 'disabled', false ); |
| 200 | + } ); |
| 201 | + |
| 202 | + api.state( 'snapshot-exists' ).bind( function( exists ) { |
| 203 | + var buttonText, permsMsg; |
| 204 | + if ( exists ) { |
| 205 | + buttonText = component.data.i18n.updateButton; |
| 206 | + permsMsg = component.data.i18n.permsMsg.update; |
| 207 | + if ( component.data.editLink ) { |
| 208 | + snapshotEditLinkEl.attr( 'href', component.data.editLink ); |
| 209 | + } |
| 210 | + } else { |
| 211 | + buttonText = component.data.i18n.saveButton; |
| 212 | + permsMsg = component.data.i18n.permsMsg.save; |
| 213 | + } |
| 214 | + |
| 215 | + snapshotButton.text( buttonText ); |
| 216 | + if ( ! component.data.currentUserCanPublish ) { |
| 217 | + snapshotButton.attr( 'title', permsMsg ); |
| 218 | + } |
| 219 | + } ); |
| 220 | + |
197 | 221 | // Preview link.
|
198 | 222 | component.previewLink = $( $.trim( wp.template( 'snapshot-preview-link' )() ) );
|
199 | 223 | component.previewLink.toggle( api.state( 'snapshot-saved' ).get() );
|
|
211 | 235 | api.bind( 'saved', setPreviewLinkHref );
|
212 | 236 | snapshotButton.after( component.previewLink );
|
213 | 237 | api.state( 'snapshot-saved' ).bind( function( saved ) {
|
214 |
| - snapshotButton.prop( 'disabled', saved ); |
215 | 238 | component.previewLink.toggle( saved );
|
216 | 239 | } );
|
217 | 240 |
|
|
232 | 255 | header.addClass( 'button-added' );
|
233 | 256 | };
|
234 | 257 |
|
235 |
| - /** |
236 |
| - * Change the snapshot button. |
237 |
| - * |
238 |
| - * @param {string} buttonText The button text. |
239 |
| - * @param {string} permsMsg The permissions message. |
240 |
| - * @return {void} |
241 |
| - */ |
242 |
| - component.changeButton = function( buttonText, permsMsg ) { |
243 |
| - var snapshotButton = $( '#customize-header-actions' ).find( '#snapshot-save' ); |
244 |
| - |
245 |
| - if ( snapshotButton.length ) { |
246 |
| - snapshotButton.text( buttonText ); |
247 |
| - if ( ! component.data.currentUserCanPublish ) { |
248 |
| - snapshotButton.attr( 'title', permsMsg ); |
249 |
| - } |
250 |
| - } |
251 |
| - }; |
252 |
| - |
253 | 258 | /**
|
254 | 259 | * Silently update the saved state to be true without triggering the
|
255 | 260 | * changed event so that the AYS beforeunload dialog won't appear
|
|
276 | 281 | */
|
277 | 282 | component.sendUpdateSnapshotRequest = function( options ) {
|
278 | 283 | var spinner = $( '#customize-header-actions .spinner' ),
|
279 |
| - request, customized, args; |
| 284 | + request, data, args; |
280 | 285 |
|
281 | 286 | args = _.extend(
|
282 | 287 | {
|
|
285 | 290 | options
|
286 | 291 | );
|
287 | 292 |
|
288 |
| - spinner.addClass( 'is-active' ); |
| 293 | + data = _.extend( |
| 294 | + {}, |
| 295 | + api.previewer.query(), |
| 296 | + { |
| 297 | + nonce: api.settings.nonce.snapshot, |
| 298 | + customize_snapshot_uuid: component.data.uuid, |
| 299 | + status: args.status |
| 300 | + } |
| 301 | + ); |
| 302 | + request = wp.ajax.post( 'customize_update_snapshot', data ); |
289 | 303 |
|
290 |
| - customized = {}; |
291 |
| - api.each( function( value, key ) { |
292 |
| - if ( value._dirty ) { |
293 |
| - customized[ key ] = { |
294 |
| - 'value': value() |
295 |
| - }; |
| 304 | + spinner.addClass( 'is-active' ); |
| 305 | + request.always( function( response ) { |
| 306 | + spinner.removeClass( 'is-active' ); |
| 307 | + if ( response.edit_link ) { |
| 308 | + component.data.editLink = response.edit_link; |
296 | 309 | }
|
297 |
| - } ); |
298 | 310 |
|
299 |
| - request = wp.ajax.post( 'customize_update_snapshot', { |
300 |
| - nonce: component.data.nonce, |
301 |
| - wp_customize: 'on', |
302 |
| - snapshot_customized: JSON.stringify( customized ), |
303 |
| - customize_snapshot_uuid: component.data.uuid, |
304 |
| - status: args.status, |
305 |
| - preview: ( api.state( 'snapshot-exists' ).get() ? 'on' : 'off' ) |
| 311 | + // @todo Remove privateness from _handleSettingValidities in Core. |
| 312 | + if ( api._handleSettingValidities && response.setting_validities ) { |
| 313 | + api._handleSettingValidities( { |
| 314 | + settingValidities: response.setting_validities, |
| 315 | + focusInvalidControl: true |
| 316 | + } ); |
| 317 | + } |
306 | 318 | } );
|
307 | 319 |
|
308 |
| - request.done( function( response ) { |
| 320 | + request.done( function() { |
309 | 321 | var url = api.previewer.previewUrl(),
|
310 | 322 | regex = new RegExp( '([?&])customize_snapshot_uuid=.*?(&|$)', 'i' ),
|
311 | 323 | separator = url.indexOf( '?' ) !== -1 ? '&' : '?',
|
312 | 324 | customizeUrl = window.location.href,
|
313 | 325 | customizeSeparator = customizeUrl.indexOf( '?' ) !== -1 ? '&' : '?';
|
314 | 326 |
|
315 |
| - // Set the UUID. |
316 |
| - if ( ! component.data.uuid ) { |
317 |
| - component.data.uuid = response.customize_snapshot_uuid; |
318 |
| - component.previewLink.attr( 'target', component.data.uuid ); |
319 |
| - } |
320 |
| - |
321 | 327 | if ( url.match( regex ) ) {
|
322 | 328 | url = url.replace( regex, '$1customize_snapshot_uuid=' + encodeURIComponent( component.data.uuid ) + '$2' );
|
323 | 329 | } else {
|
324 | 330 | url = url + separator + 'customize_snapshot_uuid=' + encodeURIComponent( component.data.uuid );
|
325 | 331 | }
|
326 | 332 |
|
327 | 333 | // Change the save button text to update.
|
328 |
| - component.changeButton( component.data.i18n.updateButton, component.data.i18n.permsMsg.update ); |
329 | 334 | api.state( 'snapshot-exists' ).set( true );
|
330 | 335 |
|
331 |
| - spinner.removeClass( 'is-active' ); |
332 |
| - |
333 | 336 | // Replace the history state with an updated Customizer URL that includes the Snapshot UUID.
|
334 | 337 | if ( history.replaceState && ! customizeUrl.match( regex ) ) {
|
335 | 338 | customizeUrl += customizeSeparator + 'customize_snapshot_uuid=' + encodeURIComponent( component.data.uuid );
|
|
350 | 353 | } );
|
351 | 354 | } );
|
352 | 355 |
|
353 |
| - request.fail( function() { |
| 356 | + request.fail( function( response ) { |
354 | 357 | var id = 'snapshot-dialog-error',
|
355 |
| - snapshotDialogShareError = wp.template( id ); |
| 358 | + snapshotDialogShareError = wp.template( id ), |
| 359 | + messages = component.data.i18n.errorMsg, |
| 360 | + invalidityCount = 0, |
| 361 | + dialogElement; |
| 362 | + |
| 363 | + if ( response.setting_validities ) { |
| 364 | + invalidityCount = _.size( response.setting_validities, function( validity ) { |
| 365 | + return true !== validity; |
| 366 | + } ); |
| 367 | + } |
| 368 | + |
| 369 | + /* |
| 370 | + * Short-circuit if there are setting validation errors, since the error messages |
| 371 | + * will be displayed with the controls themselves. Eventually, once we have |
| 372 | + * a global notification area in the Customizer, we can eliminate this |
| 373 | + * short-circuit and instead display the messages in there. |
| 374 | + * See https://core.trac.wordpress.org/ticket/35210 |
| 375 | + */ |
| 376 | + if ( invalidityCount > 0 ) { |
| 377 | + return; |
| 378 | + } |
| 379 | + |
| 380 | + if ( response.errors ) { |
| 381 | + messages += ' ' + _.pluck( response.errors, 'message' ).join( ' ' ); |
| 382 | + } |
356 | 383 |
|
357 | 384 | // Insert the snapshot dialog error template.
|
358 |
| - if ( 0 === $( '#' + id ).length ) { |
359 |
| - $( 'body' ).append( snapshotDialogShareError( { |
| 385 | + dialogElement = $( '#' + id ); |
| 386 | + if ( ! dialogElement.length ) { |
| 387 | + dialogElement = $( snapshotDialogShareError( { |
360 | 388 | title: component.data.i18n.errorTitle,
|
361 |
| - message: component.data.i18n.errorMsg |
| 389 | + message: messages |
362 | 390 | } ) );
|
| 391 | + $( 'body' ).append( dialogElement ); |
363 | 392 | }
|
364 | 393 |
|
365 | 394 | // Open the dialog.
|
366 |
| - $( '#' + id ).dialog( { |
| 395 | + dialogElement.dialog( { |
367 | 396 | autoOpen: true,
|
368 | 397 | modal: true
|
369 | 398 | } );
|
370 |
| - |
371 |
| - spinner.removeClass( 'is-active' ); |
372 | 399 | } );
|
373 | 400 | };
|
374 | 401 |
|
|
0 commit comments