-
Notifications
You must be signed in to change notification settings - Fork 0
/
failsafe.js
52 lines (42 loc) · 1.73 KB
/
failsafe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
( function() {
if (typeof document.querySelectorAll === 'undefined') {
console.warn('Browser support for querySelectorAll is required for Trash Fail Safe');
return;
}
const postForm = document.getElementById('posts-filter');
if (typeof postForm === 'undefined' || !postForm) {
return;
}
//Media list has native confirmation, so we don't need to double up on it.
function isMediaListTable(eventTarget) {
return jQuery(eventTarget).closest('table.media').length === 1
|| jQuery(eventTarget).find('table.media').length === 1;
}
//Allow normal click handling for elements other than span.delete a.submitdelete
//(i.e. don't halt on move to trash)
postForm.addEventListener('click', function(ev) {
if (ev.target.matches('input#delete_all') && !confirm('Are you sure you want to empty trash and delete all trashed items?')) {
ev.preventDefault();
return;
}
if (!ev.target.matches('a.submitdelete') || !ev.target.parentElement.matches('span.delete') || isMediaListTable(ev.target)) {
return;
}
if (!confirm('Are you sure you want to permanently delete this item?')) {
ev.preventDefault();
}
});
//Confirm before bulk delete action
postForm.addEventListener('submit', function(ev) {
if (isMediaListTable(ev.target)) {
return;
}
const bulkAction = document.getElementById('bulk-action-selector-top');
if (!bulkAction || bulkAction.value !== 'delete') {
return;
}
if (!confirm('Are you sure you want to permanently delete all selected items?')) {
ev.preventDefault();
}
});
} )();