Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

Commit e27ed55

Browse files
committed
Move populateSetting logic to addButtons
1 parent ece6c06 commit e27ed55

File tree

1 file changed

+45
-19
lines changed

1 file changed

+45
-19
lines changed

js/customize-snapshots.js

+45-19
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@
198198
component.addButtons = function() {
199199
var header = $( '#customize-header-actions' ),
200200
publishButton = header.find( '#save' ),
201-
submitButton, data, setPreviewLinkHref, snapshotButtonText;
201+
submitButton, data, setPreviewLinkHref, snapshotButtonText, changeButtonText;
202+
203+
component.dirtySnapshotPostSetting = new api.Value();
204+
component.dirtyScheduleDate = new api.Value();
202205

203206
// Save/update button.
204207
component.snapshotButton = wp.template( 'snapshot-save' );
@@ -267,6 +270,36 @@
267270
}
268271
} );
269272

273+
changeButtonText = function() {
274+
var date = component.getDateFromInputs();
275+
if ( component.isFutureDate() && date && component.data.currentUserCanPublish ) {
276+
component.snapshotButton.text( component.data.i18n.scheduleButton );
277+
} else {
278+
component.snapshotButton.text( api.state( 'snapshot-exists' ).get() ? component.data.i18n.updateButton : component.data.i18n.saveButton );
279+
}
280+
};
281+
282+
component.dirtySnapshotPostSetting.bind( function( dirty ) {
283+
if ( dirty ) {
284+
component.snapshotButton.prop( 'disabled', false );
285+
} else {
286+
component.snapshotButton.prop( 'disabled', ! component.data.dirty );
287+
}
288+
changeButtonText();
289+
} );
290+
component.dirtyScheduleDate.bind( function( dirty ) {
291+
var date;
292+
if ( dirty ) {
293+
date = component.getDateFromInputs();
294+
if ( ! date || ! component.data.currentUserCanPublish ) {
295+
return;
296+
}
297+
component.snapshotButton.text( component.data.i18n.scheduleButton );
298+
} else {
299+
changeButtonText();
300+
}
301+
} );
302+
270303
// Preview link.
271304
component.previewLink = $( $.trim( wp.template( 'snapshot-preview-link' )() ) );
272305
component.previewLink.toggle( api.state( 'snapshot-saved' ).get() );
@@ -772,32 +805,25 @@
772805
var date = component.getDateFromInputs(),
773806
scheduled;
774807

775-
if ( ! date ) {
776-
component.snapshotButton.prop( 'disabled', component.data.title === component.snapshotTitle.val() );
808+
if ( ! date || ! component.data.currentUserCanPublish ) {
809+
component.dirtySnapshotPostSetting.set( component.data.title !== component.snapshotTitle.val() );
777810
return;
778811
}
779812

780813
date.setSeconds( 0 );
781814
scheduled = component.formatDate( date ) !== component.data.publishDate;
782815

783-
if ( component.snapshotButton.length ) {
784-
785-
// Change update button to schedule.
786-
if ( component.isFutureDate() ) {
787-
component.snapshotButton.text( component.data.i18n.scheduleButton );
788-
} else if ( api.state( 'snapshot-exists' ).get() ) {
789-
component.snapshotButton.text( component.data.i18n.updateButton );
790-
} else {
791-
component.snapshotButton.text( component.data.i18n.updateButton );
792-
}
793-
794-
if ( scheduled || component.data.dirty || component.data.title !== component.snapshotTitle.val() ) {
795-
component.snapshotButton.prop( 'disabled', false );
796-
} else {
797-
component.snapshotButton.prop( 'disabled', true );
798-
}
816+
if ( component.data.title !== component.snapshotTitle.val() || scheduled ) {
817+
component.dirtySnapshotPostSetting.set( true );
818+
} else {
819+
component.dirtySnapshotPostSetting.set( false );
799820
}
800821

822+
if ( scheduled && component.isFutureDate() ) {
823+
component.dirtyScheduleDate.set( true );
824+
} else {
825+
component.dirtyScheduleDate.set( false );
826+
}
801827
component.updateCountdown();
802828
component.editContainer.find( '.reset-time' ).toggle( scheduled );
803829
};

0 commit comments

Comments
 (0)