|
32 | 32 | snapshot.data.title = snapshot.data.title || snapshot.data.uuid;
|
33 | 33 | api.state.create( 'changesetTitle', snapshot.data.title );
|
34 | 34 |
|
| 35 | + snapshot.extendPreviewerQuery(); |
35 | 36 | api.section( 'publish_settings', snapshot.addTitleControl );
|
36 | 37 | api.trigger( 'snapshots-ready', snapshot );
|
37 | 38 | } );
|
|
44 | 45 | * @return {void}
|
45 | 46 | */
|
46 | 47 | addTitleControl: function( section ) {
|
47 |
| - var snapshot = this, control, toggleControl, originalQuery; |
| 48 | + var snapshot = this, control, toggleControl; |
48 | 49 |
|
49 | 50 | control = new api.Control( 'changeset_title', {
|
50 | 51 | type: 'text',
|
|
67 | 68 | toggleControl( api.state( 'selectedChangesetStatus' ).get() );
|
68 | 69 | api.state( 'selectedChangesetStatus' ).bind( toggleControl );
|
69 | 70 |
|
70 |
| - originalQuery = api.previewer.query; |
71 |
| - |
72 |
| - api.previewer.query = function() { |
73 |
| - var retval = originalQuery.apply( this, arguments ); |
74 |
| - retval.customize_changeset_title = api.state( 'changesetTitle' ); |
75 |
| - return retval; |
76 |
| - }; |
77 |
| - |
78 | 71 | api.state( 'changesetTitle' ).bind( function() {
|
79 | 72 | api.state( 'saved' ).set( false );
|
80 | 73 | } );
|
|
85 | 78 | }
|
86 | 79 | return undefined;
|
87 | 80 | } );
|
| 81 | + }, |
| 82 | + |
| 83 | + /** |
| 84 | + * Amend the preview query so we can update the snapshot during `changeset_save`. |
| 85 | + * |
| 86 | + * @return {void} |
| 87 | + */ |
| 88 | + extendPreviewerQuery: function extendPreviewerQuery() { |
| 89 | + var snapshot = this, originalQuery = api.previewer.query; |
| 90 | + |
| 91 | + api.previewer.query = function() { |
| 92 | + var retval = originalQuery.apply( this, arguments ); |
| 93 | + |
| 94 | + if ( api.state( 'selectedChangesetStatus' ) && 'publish' !== api.state( 'selectedChangesetStatus' ) ) { |
| 95 | + retval.customizer_state_query_vars = JSON.stringify( snapshot.getStateQueryVars() ); |
| 96 | + retval.customize_changeset_title = api.state( 'changesetTitle' ); |
| 97 | + } |
| 98 | + |
| 99 | + return retval; |
| 100 | + }; |
| 101 | + }, |
| 102 | + |
| 103 | + /** |
| 104 | + * Get state query vars. |
| 105 | + * @todo Reuse method in compat mode? |
| 106 | + * |
| 107 | + * @return {{}} Query vars for scroll, device, url, and autofocus. |
| 108 | + */ |
| 109 | + getStateQueryVars: function() { |
| 110 | + var snapshot = this, queryVars; |
| 111 | + |
| 112 | + queryVars = { |
| 113 | + 'autofocus[control]': null, |
| 114 | + 'autofocus[section]': null, |
| 115 | + 'autofocus[panel]': null |
| 116 | + }; |
| 117 | + |
| 118 | + queryVars.scroll = parseInt( api.previewer.scroll, 10 ) || 0; |
| 119 | + queryVars.device = api.previewedDevice.get(); |
| 120 | + queryVars.url = api.previewer.previewUrl.get(); |
| 121 | + |
| 122 | + if ( ! api.state( 'activated' ).get() || snapshot.isNotSavedPreviewingTheme ) { |
| 123 | + queryVars.previewing_theme = true; |
| 124 | + } |
| 125 | + |
| 126 | + _.find( [ 'control', 'section', 'panel' ], function( constructType ) { |
| 127 | + var found = false; |
| 128 | + api[ constructType ].each( function( construct ) { // @todo Core needs to support more Backbone methods on wp.customize.Values(). |
| 129 | + if ( ! found && construct.expanded && construct.expanded.get() ) { |
| 130 | + queryVars[ 'autofocus[' + constructType + ']' ] = construct.id; |
| 131 | + found = true; |
| 132 | + } |
| 133 | + } ); |
| 134 | + return found; |
| 135 | + } ); |
| 136 | + |
| 137 | + return queryVars; |
88 | 138 | }
|
89 | 139 | } );
|
90 | 140 |
|
|
0 commit comments