|
32 | 32 | return;
|
33 | 33 | }
|
34 | 34 | api.state.create( 'snapshot-saved', true );
|
| 35 | + api.state.create( 'snapshot-exists', Boolean( component.data.isPreview ) ); |
35 | 36 | api.state.create( 'snapshot-submitted', true );
|
36 | 37 | api.bind( 'change', function() {
|
37 | 38 | api.state( 'snapshot-saved' ).set( false );
|
38 | 39 | api.state( 'snapshot-submitted' ).set( false );
|
39 | 40 | } );
|
| 41 | + component.frontendPreviewUrl = new api.Value( api.previewer.previewUrl.get() ); |
| 42 | + component.frontendPreviewUrl.link( api.previewer.previewUrl ); |
40 | 43 |
|
41 | 44 | component.previewerQuery();
|
42 | 45 | component.addButtons();
|
43 | 46 |
|
44 | 47 | $( '#snapshot-save' ).on( 'click', function( event ) {
|
45 | 48 | event.preventDefault();
|
46 |
| - component.sendUpdateSnapshotRequest( { status: 'draft', openNewWindow: event.shiftKey } ); |
| 49 | + component.sendUpdateSnapshotRequest( { status: 'draft' } ); |
47 | 50 | } );
|
48 | 51 | $( '#snapshot-submit' ).on( 'click', function( event ) {
|
49 | 52 | event.preventDefault();
|
50 |
| - component.sendUpdateSnapshotRequest( { status: 'pending', openNewWindow: event.shiftKey } ); |
| 53 | + component.sendUpdateSnapshotRequest( { status: 'pending' } ); |
51 | 54 | } );
|
52 | 55 |
|
53 |
| - if ( component.data.isPreview ) { |
| 56 | + if ( api.state( 'snapshot-exists' ).get() ) { |
54 | 57 | api.state( 'saved' ).set( false );
|
55 | 58 | component.resetSavedStateQuietly();
|
56 | 59 | }
|
| 60 | + |
| 61 | + api.trigger( 'snapshots-ready', component ); |
57 | 62 | } );
|
58 | 63 |
|
59 | 64 | api.bind( 'save', function( request ) {
|
|
71 | 76 | if ( 0 === $( '#' + id ).length ) {
|
72 | 77 | $( 'body' ).append( snapshotDialogPublishError( {
|
73 | 78 | title: component.data.i18n.publish,
|
74 |
| - message: component.data.isPreview ? component.data.i18n.permsMsg.update : component.data.i18n.permsMsg.save |
| 79 | + message: api.state( 'snapshot-exists' ).get() ? component.data.i18n.permsMsg.update : component.data.i18n.permsMsg.save |
75 | 80 | } ) );
|
76 | 81 | }
|
77 | 82 |
|
|
95 | 100 |
|
96 | 101 | // Set the button text back to "Save".
|
97 | 102 | component.changeButton( component.data.i18n.saveButton, component.data.i18n.permsMsg.save );
|
| 103 | + api.state( 'snapshot-exists' ).set( false ); |
98 | 104 |
|
99 | 105 | request = wp.ajax.post( 'customize_get_snapshot_uuid', {
|
100 | 106 | nonce: component.data.nonce,
|
|
104 | 110 | // Update the UUID.
|
105 | 111 | request.done( function( response ) {
|
106 | 112 | component.data.uuid = response.uuid;
|
| 113 | + component.previewLink.attr( 'target', component.data.uuid ); |
107 | 114 | } );
|
108 | 115 |
|
109 | 116 | // Replace the history state with an updated Customizer URL that does not include the Snapshot UUID.
|
|
134 | 141 |
|
135 | 142 | retval = originalQuery.apply( this, arguments );
|
136 | 143 |
|
137 |
| - if ( component.data.isPreview ) { |
| 144 | + if ( api.state( 'snapshot-exists' ).get() ) { |
138 | 145 | api.each( function( value, key ) {
|
139 | 146 | if ( value._dirty ) {
|
140 | 147 | allCustomized[ key ] = {
|
|
150 | 157 | };
|
151 | 158 | };
|
152 | 159 |
|
| 160 | + /** |
| 161 | + * Get the preview URL with the snapshot UUID attached. |
| 162 | + * |
| 163 | + * @returns {string} URL. |
| 164 | + */ |
| 165 | + component.getSnapshotFrontendPreviewUrl = function getSnapshotFrontendPreviewUrl() { |
| 166 | + var a = document.createElement( 'a' ); |
| 167 | + a.href = component.frontendPreviewUrl.get(); |
| 168 | + if ( a.search ) { |
| 169 | + a.search += '&'; |
| 170 | + } |
| 171 | + a.search += 'customize_snapshot_uuid=' + component.data.uuid; |
| 172 | + return a.href; |
| 173 | + }; |
| 174 | + |
153 | 175 | /**
|
154 | 176 | * Create the snapshot buttons.
|
155 | 177 | *
|
|
158 | 180 | component.addButtons = function() {
|
159 | 181 | var header = $( '#customize-header-actions' ),
|
160 | 182 | publishButton = header.find( '#save' ),
|
161 |
| - snapshotButton, submitButton, data; |
| 183 | + snapshotButton, submitButton, data, setPreviewLinkHref; |
162 | 184 |
|
| 185 | + // Save/update button. |
163 | 186 | snapshotButton = wp.template( 'snapshot-save' );
|
164 | 187 | data = {
|
165 |
| - buttonText: component.data.isPreview ? component.data.i18n.updateButton : component.data.i18n.saveButton |
| 188 | + buttonText: api.state( 'snapshot-exists' ).get() ? component.data.i18n.updateButton : component.data.i18n.saveButton |
166 | 189 | };
|
167 | 190 | snapshotButton = $( $.trim( snapshotButton( data ) ) );
|
168 | 191 | if ( ! component.data.currentUserCanPublish ) {
|
169 |
| - snapshotButton.attr( 'title', component.data.isPreview ? component.data.i18n.permsMsg.update : component.data.i18n.permsMsg.save ); |
| 192 | + snapshotButton.attr( 'title', api.state( 'snapshot-exists' ).get() ? component.data.i18n.permsMsg.update : component.data.i18n.permsMsg.save ); |
170 | 193 | }
|
171 | 194 | snapshotButton.prop( 'disabled', true );
|
172 | 195 | snapshotButton.insertAfter( publishButton );
|
| 196 | + |
| 197 | + // Preview link. |
| 198 | + component.previewLink = $( $.trim( wp.template( 'snapshot-preview-link' )() ) ); |
| 199 | + component.previewLink.toggle( api.state( 'snapshot-saved' ).get() ); |
| 200 | + component.previewLink.attr( 'target', component.data.uuid ); |
| 201 | + setPreviewLinkHref = _.debounce( function() { |
| 202 | + if ( api.state( 'snapshot-exists' ).get() ) { |
| 203 | + component.previewLink.attr( 'href', component.getSnapshotFrontendPreviewUrl() ); |
| 204 | + } else { |
| 205 | + component.previewLink.attr( 'href', component.frontendPreviewUrl.get() ); |
| 206 | + } |
| 207 | + } ); |
| 208 | + component.frontendPreviewUrl.bind( setPreviewLinkHref ); |
| 209 | + setPreviewLinkHref(); |
| 210 | + api.state.bind( 'change', setPreviewLinkHref ); |
| 211 | + api.bind( 'saved', setPreviewLinkHref ); |
| 212 | + snapshotButton.after( component.previewLink ); |
173 | 213 | api.state( 'snapshot-saved' ).bind( function( saved ) {
|
174 | 214 | snapshotButton.prop( 'disabled', saved );
|
| 215 | + component.previewLink.toggle( saved ); |
175 | 216 | } );
|
176 | 217 |
|
| 218 | + // Submit for review button. |
177 | 219 | if ( ! component.data.currentUserCanPublish ) {
|
178 | 220 | publishButton.hide();
|
179 | 221 | submitButton = wp.template( 'snapshot-submit' );
|
|
230 | 272 | *
|
231 | 273 | * @param {object} options Options.
|
232 | 274 | * @param {string} options.status The post status for the snapshot.
|
233 |
| - * @param {boolean} options.openNewWindow Whether to open the frontend in a new window. |
234 | 275 | * @return {void}
|
235 | 276 | */
|
236 | 277 | component.sendUpdateSnapshotRequest = function( options ) {
|
|
239 | 280 |
|
240 | 281 | args = _.extend(
|
241 | 282 | {
|
242 |
| - status: 'draft', |
243 |
| - openNewWindow: false |
| 283 | + status: 'draft' |
244 | 284 | },
|
245 | 285 | options
|
246 | 286 | );
|
|
262 | 302 | snapshot_customized: JSON.stringify( customized ),
|
263 | 303 | customize_snapshot_uuid: component.data.uuid,
|
264 | 304 | status: args.status,
|
265 |
| - preview: ( component.data.isPreview ? 'on' : 'off' ) |
| 305 | + preview: ( api.state( 'snapshot-exists' ).get() ? 'on' : 'off' ) |
266 | 306 | } );
|
267 | 307 |
|
268 | 308 | request.done( function( response ) {
|
|
275 | 315 | // Set the UUID.
|
276 | 316 | if ( ! component.data.uuid ) {
|
277 | 317 | component.data.uuid = response.customize_snapshot_uuid;
|
| 318 | + component.previewLink.attr( 'target', component.data.uuid ); |
278 | 319 | }
|
279 | 320 |
|
280 | 321 | if ( url.match( regex ) ) {
|
|
285 | 326 |
|
286 | 327 | // Change the save button text to update.
|
287 | 328 | component.changeButton( component.data.i18n.updateButton, component.data.i18n.permsMsg.update );
|
288 |
| - component.data.isPreview = true; |
| 329 | + api.state( 'snapshot-exists' ).set( true ); |
289 | 330 |
|
290 | 331 | spinner.removeClass( 'is-active' );
|
291 | 332 |
|
|
301 | 342 | }
|
302 | 343 | component.resetSavedStateQuietly();
|
303 | 344 |
|
304 |
| - // Open the preview in a new window on shift+click. |
305 |
| - if ( args.openNewWindow ) { |
306 |
| - window.open( url, '_blank' ); |
307 |
| - } |
308 |
| - |
309 | 345 | // Trigger an event for plugins to use.
|
310 | 346 | api.trigger( 'customize-snapshots-update', {
|
311 | 347 | previewUrl: url,
|
|
0 commit comments