Skip to content

Commit 01664fa

Browse files
committed
Editor: Warn before losing unsaved Quick Edit and Bulk Edit changes
1 parent a1c062c commit 01664fa

File tree

3 files changed

+156
-3
lines changed

3 files changed

+156
-3
lines changed

src/js/_enqueues/admin/edit-comments.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ window.commentReply = {
782782
cid : '',
783783
act : '',
784784
originalContent : '',
785+
_beforeUnloadHandler: null,
785786

786787
/**
787788
* Initializes the comment reply functionality.
@@ -808,9 +809,13 @@ window.commentReply = {
808809
commentReply.toggle($(this).parent());
809810
});
810811

811-
$('#doaction, #post-query-submit').on( 'click', function(){
812-
if ( $('#the-comment-list #replyrow').length > 0 )
812+
$('#doaction, #post-query-submit').on( 'click', function() {
813+
if ( $('#the-comment-list #replyrow').length > 0 ) {
814+
if ( ! commentReply.discardCommentChanges() ) {
815+
return false;
816+
}
813817
commentReply.close();
818+
}
814819
});
815820

816821
this.comments_listing = $('#comments-form > input[name="comment_status"]').val() || '';
@@ -891,6 +896,8 @@ window.commentReply = {
891896
return;
892897
}
893898

899+
this._unguardNavigation();
900+
894901
if ( this.cid ) {
895902
commentRow = $( '#comment-' + this.cid );
896903
}
@@ -1058,6 +1065,10 @@ window.commentReply = {
10581065
} );
10591066
}, 600);
10601067

1068+
$( ':input', editRow ).off( 'change.commentEdit input.commentEdit' ).one( 'change.commentEdit input.commentEdit', function() {
1069+
t._guardNavigation();
1070+
} );
1071+
10611072
return false;
10621073
},
10631074

@@ -1243,6 +1254,39 @@ window.commentReply = {
12431254
}
12441255

12451256
return window.confirm( __( 'Are you sure you want to do this?\nThe comment changes you made will be lost.' ) );
1257+
},
1258+
1259+
/**
1260+
* Sets a beforeunload handler to warn about unsaved comment edit changes.
1261+
*
1262+
* @since x.x.x
1263+
* @memberof commentReply
1264+
* @return {void}
1265+
*/
1266+
_guardNavigation: function() {
1267+
var t = this;
1268+
if ( t._beforeUnloadHandler ) {
1269+
return;
1270+
}
1271+
t._beforeUnloadHandler = function( event ) {
1272+
event.preventDefault();
1273+
event.returnValue = '';
1274+
};
1275+
window.addEventListener( 'beforeunload', t._beforeUnloadHandler );
1276+
},
1277+
1278+
/**
1279+
* Removes the beforeunload handler set by _guardNavigation().
1280+
*
1281+
* @since x.x.x
1282+
* @memberof commentReply
1283+
* @return {void}
1284+
*/
1285+
_unguardNavigation: function() {
1286+
if ( this._beforeUnloadHandler ) {
1287+
window.removeEventListener( 'beforeunload', this._beforeUnloadHandler );
1288+
this._beforeUnloadHandler = null;
1289+
}
12461290
}
12471291
};
12481292

src/js/_enqueues/admin/inline-edit-post.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ window.wp = window.wp || {};
2626

2727
window.inlineEditPost = {
2828

29+
_beforeUnloadHandler: null,
30+
2931
/**
3032
* Initializes the inline and bulk post editor.
3133
*
@@ -162,6 +164,10 @@ window.wp = window.wp || {};
162164
e.preventDefault();
163165
t.setBulk();
164166
} else if ( $('form#posts-filter tr.inline-editor').length > 0 ) {
167+
if ( t._beforeUnloadHandler && ! window.confirm( wp.i18n.__( 'The changes you made will be lost. Do you want to continue?' ) ) ) {
168+
e.preventDefault();
169+
return;
170+
}
165171
t.revert();
166172
}
167173
});
@@ -193,6 +199,11 @@ window.wp = window.wp || {};
193199
var te = '', type = this.type, c = true;
194200
var checkedPosts = $( 'tbody th.check-column input[type="checkbox"]:checked' );
195201
var categories = {};
202+
203+
if ( this._beforeUnloadHandler && ! window.confirm( wp.i18n.__( 'The changes you made will be lost. Do you want to continue?' ) ) ) {
204+
return;
205+
}
206+
196207
this.revert();
197208

198209
$( '#bulk-edit td' ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
@@ -318,6 +329,10 @@ window.wp = window.wp || {};
318329
$( '#bulk-edit .inline-edit-wrapper' ).attr( 'tabindex', '-1' ).focus();
319330
// Scrolls to the top of the table where the editor is rendered.
320331
$('html, body').animate( { scrollTop: 0 }, 'fast' );
332+
333+
$( ':input', '#bulk-edit' ).off( 'change.inlineEdit input.inlineEdit' ).one( 'change.inlineEdit input.inlineEdit', function() {
334+
inlineEditPost._guardNavigation();
335+
} );
321336
},
322337

323338
/**
@@ -333,6 +348,11 @@ window.wp = window.wp || {};
333348
*/
334349
edit : function(id) {
335350
var t = this, fields, editRow, rowData, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, f, val, pw;
351+
352+
if ( t._beforeUnloadHandler && ! window.confirm( wp.i18n.__( 'The changes you made will be lost. Do you want to continue?' ) ) ) {
353+
return false;
354+
}
355+
336356
t.revert();
337357

338358
if ( typeof(id) === 'object' ) {
@@ -472,6 +492,10 @@ window.wp = window.wp || {};
472492
$(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
473493
$('.ptitle', editRow).trigger( 'focus' );
474494

495+
$( ':input', editRow ).one( 'change.inlineEdit input.inlineEdit', function() {
496+
t._guardNavigation();
497+
} );
498+
475499
return false;
476500
},
477501

@@ -515,6 +539,7 @@ window.wp = window.wp || {};
515539

516540
if (r) {
517541
if ( -1 !== r.indexOf( '<tr' ) ) {
542+
inlineEditPost._unguardNavigation();
518543
$(inlineEditPost.what+id).siblings('tr.hidden').addBack().remove();
519544
$('#edit-'+id).before(r).remove();
520545
$( inlineEditPost.what + id ).hide().fadeIn( 400, function() {
@@ -556,6 +581,7 @@ window.wp = window.wp || {};
556581
id = $( '.inline-editor', $tableWideFat ).attr( 'id' );
557582

558583
if ( id ) {
584+
this._unguardNavigation();
559585
$( '.spinner', $tableWideFat ).removeClass( 'is-active' );
560586

561587
if ( 'bulk-edit' === id ) {
@@ -600,6 +626,39 @@ window.wp = window.wp || {};
600626
var id = $(o).closest('tr').attr('id'),
601627
parts = id.split('-');
602628
return parts[parts.length - 1];
629+
},
630+
631+
/**
632+
* Sets a beforeunload handler to warn about unsaved Quick Edit or Bulk Edit changes.
633+
*
634+
* @since x.x.x
635+
* @memberof inlineEditPost
636+
* @return {void}
637+
*/
638+
_guardNavigation: function() {
639+
var t = this;
640+
if ( t._beforeUnloadHandler ) {
641+
return;
642+
}
643+
t._beforeUnloadHandler = function( event ) {
644+
event.preventDefault();
645+
event.returnValue = '';
646+
};
647+
window.addEventListener( 'beforeunload', t._beforeUnloadHandler );
648+
},
649+
650+
/**
651+
* Removes the beforeunload handler set by _guardNavigation().
652+
*
653+
* @since x.x.x
654+
* @memberof inlineEditPost
655+
* @return {void}
656+
*/
657+
_unguardNavigation: function() {
658+
if ( this._beforeUnloadHandler ) {
659+
window.removeEventListener( 'beforeunload', this._beforeUnloadHandler );
660+
this._beforeUnloadHandler = null;
661+
}
603662
}
604663
};
605664

src/js/_enqueues/admin/inline-edit-tax.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ window.wp = window.wp || {};
2121

2222
window.inlineEditTax = {
2323

24+
_beforeUnloadHandler: null,
25+
2426
/**
2527
* Initializes the inline taxonomy editor by adding event handlers to be able to
2628
* quick edit.
@@ -81,7 +83,11 @@ window.inlineEditTax = {
8183
/**
8284
* Saves the inline edits on submitting the inline edit form.
8385
*/
84-
$( '#posts-filter input[type="submit"]' ).on( 'mousedown', function() {
86+
$( '#posts-filter' ).on( 'submit', function( e ) {
87+
if ( t._beforeUnloadHandler && ! window.confirm( wp.i18n.__( 'The changes you made will be lost. Do you want to continue?' ) ) ) {
88+
e.preventDefault();
89+
return;
90+
}
8591
t.revert();
8692
});
8793
},
@@ -120,6 +126,11 @@ window.inlineEditTax = {
120126
edit : function(id) {
121127
var editRow, rowData, val,
122128
t = this;
129+
130+
if ( t._beforeUnloadHandler && ! window.confirm( wp.i18n.__( 'The changes you made will be lost. Do you want to continue?' ) ) ) {
131+
return false;
132+
}
133+
123134
t.revert();
124135

125136
// Makes sure we can pass an HTMLElement as the ID.
@@ -145,6 +156,10 @@ window.inlineEditTax = {
145156
$(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
146157
$('.ptitle', editRow).eq(0).trigger( 'focus' );
147158

159+
$( ':input', editRow ).off( 'change.inlineEdit input.inlineEdit' ).one( 'change.inlineEdit input.inlineEdit', function() {
160+
t._guardNavigation();
161+
} );
162+
148163
return false;
149164
},
150165

@@ -203,6 +218,7 @@ window.inlineEditTax = {
203218

204219
if (r) {
205220
if ( -1 !== r.indexOf( '<tr' ) ) {
221+
inlineEditTax._unguardNavigation();
206222
$(inlineEditTax.what+id).siblings('tr.hidden').addBack().remove();
207223
new_id = $(r).attr('id');
208224

@@ -261,6 +277,7 @@ window.inlineEditTax = {
261277
var id = $('table.widefat tr.inline-editor').attr('id');
262278

263279
if ( id ) {
280+
this._unguardNavigation();
264281
$( 'table.widefat .spinner' ).removeClass( 'is-active' );
265282
$('#'+id).siblings('tr.hidden').addBack().remove();
266283
id = id.substr( id.lastIndexOf('-') + 1 );
@@ -286,6 +303,39 @@ window.inlineEditTax = {
286303
var id = o.tagName === 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-');
287304

288305
return parts[parts.length - 1];
306+
},
307+
308+
/**
309+
* Sets a beforeunload handler to warn about unsaved Quick Edit changes.
310+
*
311+
* @since x.x.x
312+
* @memberof inlineEditTax
313+
* @return {void}
314+
*/
315+
_guardNavigation: function() {
316+
var t = this;
317+
if ( t._beforeUnloadHandler ) {
318+
return;
319+
}
320+
t._beforeUnloadHandler = function( event ) {
321+
event.preventDefault();
322+
event.returnValue = '';
323+
};
324+
window.addEventListener( 'beforeunload', t._beforeUnloadHandler );
325+
},
326+
327+
/**
328+
* Removes the beforeunload handler set by _guardNavigation().
329+
*
330+
* @since x.x.x
331+
* @memberof inlineEditTax
332+
* @return {void}
333+
*/
334+
_unguardNavigation: function() {
335+
if ( this._beforeUnloadHandler ) {
336+
window.removeEventListener( 'beforeunload', this._beforeUnloadHandler );
337+
this._beforeUnloadHandler = null;
338+
}
289339
}
290340
};
291341

0 commit comments

Comments
 (0)