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