| 1 |
jQuery(function($) { |
| 2 |
let LogtivityLogIndex = { |
| 3 |
init: function() { |
| 4 |
this.container = $('#logtivity-log-index'); |
| 5 |
|
| 6 |
if (this.container.length) { |
| 7 |
this.form = $('#logtivity-log-index-search-form'); |
| 8 |
this.listenForPagination(); |
| 9 |
this.listenForChange(); |
| 10 |
this.filter(); |
| 11 |
this.listenForViewLog() |
| 12 |
this.listenForCloseModal(); |
| 13 |
} |
| 14 |
}, |
| 15 |
|
| 16 |
listenForCloseModal: function() { |
| 17 |
let listenForCloseModal = this; |
| 18 |
|
| 19 |
$('body').on('click', '.js-logtivity-notice-dismiss', function(e) { |
| 20 |
e.preventDefault(); |
| 21 |
|
| 22 |
listenForCloseModal.hideModal(); |
| 23 |
}); |
| 24 |
|
| 25 |
$(document).on('keyup', function(e) { |
| 26 |
if (e.key === 'Escape' && listenForCloseModal.modalOpen) { |
| 27 |
listenForCloseModal.hideModal(); |
| 28 |
} |
| 29 |
|
| 30 |
}); |
| 31 |
|
| 32 |
$(document).on('keydown', function(e) { |
| 33 |
if (e.key !== 'Tab' || !listenForCloseModal.modalOpen) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
let $focusable = $('#logtivity-log-modal') |
| 38 |
.find('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])') |
| 39 |
.filter(':visible'); |
| 40 |
|
| 41 |
if (!$focusable.length) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
let firstFocusable = $focusable[0], |
| 46 |
lastFocusable = $focusable[$focusable.length - 1]; |
| 47 |
|
| 48 |
if (e.shiftKey && document.activeElement === firstFocusable) { |
| 49 |
e.preventDefault(); |
| 50 |
lastFocusable.focus(); |
| 51 |
} else if (!e.shiftKey && document.activeElement === lastFocusable) { |
| 52 |
e.preventDefault(); |
| 53 |
firstFocusable.focus(); |
| 54 |
} |
| 55 |
}); |
| 56 |
|
| 57 |
$(document).mouseup(function(e) { |
| 58 |
if (!listenForCloseModal.modalOpen) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
let $container = $('.logtivity-modal-dialog'); |
| 63 |
|
| 64 |
// if the target of the click isn't the container nor a descendant of the container |
| 65 |
if (!$container.is(e.target) && $container.has(e.target).length === 0) { |
| 66 |
listenForCloseModal.hideModal(); |
| 67 |
} |
| 68 |
}); |
| 69 |
}, |
| 70 |
|
| 71 |
listenForViewLog: function() { |
| 72 |
let listenForViewLog = this; |
| 73 |
|
| 74 |
$('body').on('click', '.js-logtivity-view-log', function(e) { |
| 75 |
e.preventDefault(); |
| 76 |
|
| 77 |
listenForViewLog.lastFocusedElement = this; |
| 78 |
listenForViewLog.showLogModal($(this).next().html()); |
| 79 |
}); |
| 80 |
}, |
| 81 |
|
| 82 |
showLogModal: function(modalContent) { |
| 83 |
$('.logtivity-modal').attr('aria-hidden', 'false').addClass('active'); |
| 84 |
|
| 85 |
this.modalOpen = true; |
| 86 |
|
| 87 |
$('.logtivity-modal-content').html(modalContent); |
| 88 |
$('.js-logtivity-notice-dismiss').trigger('focus'); |
| 89 |
}, |
| 90 |
|
| 91 |
hideModal: function() { |
| 92 |
$('.logtivity-modal').attr('aria-hidden', 'true').removeClass('active'); |
| 93 |
|
| 94 |
this.modalOpen = false; |
| 95 |
|
| 96 |
if (this.lastFocusedElement) { |
| 97 |
this.lastFocusedElement.focus(); |
| 98 |
} |
| 99 |
}, |
| 100 |
|
| 101 |
listenForChange: function() { |
| 102 |
let listenForChange = this, |
| 103 |
timeout = null; |
| 104 |
|
| 105 |
$('body').on('input', '#logtivity-log-index-search-form input', function(e) { |
| 106 |
e.preventDefault(); |
| 107 |
|
| 108 |
$('#logtivity_page').val(''); |
| 109 |
|
| 110 |
// Clear the timeout if it has already been set. |
| 111 |
// This will prevent the previous task from executing |
| 112 |
// if it has been less than <MILLISECONDS> |
| 113 |
clearTimeout(timeout); |
| 114 |
|
| 115 |
// Make a new timeout set to go off in 1000ms |
| 116 |
timeout = setTimeout(function() { |
| 117 |
listenForChange.filter(); |
| 118 |
}, 1000); |
| 119 |
}); |
| 120 |
}, |
| 121 |
|
| 122 |
loading: function() { |
| 123 |
let $loading = $('<div role="status" style="text-align: center; padding-bottom: 20px">'); |
| 124 |
|
| 125 |
$loading.append( |
| 126 |
'<div class="spinner is-active" style="float:none;width:auto;height:auto;padding:10px 0 10px 50px;background-position:20px 0;"></div>' |
| 127 |
); |
| 128 |
$loading.append( |
| 129 |
$('<span class="screen-reader-text">').text(logtivityAdminA11y.loadingLogs) |
| 130 |
); |
| 131 |
|
| 132 |
this.container.attr('aria-busy', 'true').empty().append($loading); |
| 133 |
}, |
| 134 |
|
| 135 |
listenForPagination: function() { |
| 136 |
let listenForPagination = this; |
| 137 |
|
| 138 |
$('body').on('click', '.js-logtivity-pagination', function(e) { |
| 139 |
e.preventDefault(); |
| 140 |
|
| 141 |
$('#logtivity_page').val($(this).attr('data-page')); |
| 142 |
|
| 143 |
listenForPagination.filter(); |
| 144 |
}); |
| 145 |
}, |
| 146 |
|
| 147 |
filter: function() { |
| 148 |
let filter = this; |
| 149 |
|
| 150 |
this.loading(); |
| 151 |
|
| 152 |
$.ajax({ |
| 153 |
url : filter.form.attr('action'), |
| 154 |
type : 'GET', |
| 155 |
data : filter.form.serialize(), |
| 156 |
success: function(result) { |
| 157 |
filter.container.attr('aria-busy', 'false').html(result.view); |
| 158 |
}, |
| 159 |
error : function(error) { |
| 160 |
let $error = $('<div class="notice notice-error" role="alert"><p></p></div>'); |
| 161 |
|
| 162 |
$error.find('p').text(logtivityAdminA11y.loadLogsError); |
| 163 |
filter.container.attr('aria-busy', 'false').empty().append($error); |
| 164 |
} |
| 165 |
}); |
| 166 |
} |
| 167 |
}; |
| 168 |
|
| 169 |
let DismissUrlHasChanged = { |
| 170 |
init: function() { |
| 171 |
$(document).on('click', '.notice-dismiss', function() { |
| 172 |
let type = $(this).closest('.is-dismissible').attr('notice'), |
| 173 |
dismissUntil = $(this).closest('.is-dismissible').attr('dismiss-until'); |
| 174 |
|
| 175 |
if (type) { |
| 176 |
$.ajax(ajaxurl, { |
| 177 |
type: 'POST', |
| 178 |
data: { |
| 179 |
action : 'logtivity_dismiss_notice', |
| 180 |
type : type, |
| 181 |
dismiss_until: dismissUntil, |
| 182 |
} |
| 183 |
}); |
| 184 |
} |
| 185 |
}); |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
LogtivityLogIndex.init(); |
| 190 |
DismissUrlHasChanged.init(); |
| 191 |
|
| 192 |
let registerMessage = function(message, type) { |
| 193 |
type = type || 'info'; |
| 194 |
|
| 195 |
let $messaging = $('#logtivity-register-response') |
| 196 |
.removeClass() |
| 197 |
.addClass('logtivity-notice logtivity-notice-' + type) |
| 198 |
.css('width', 'fit-content'); |
| 199 |
|
| 200 |
if (message) { |
| 201 |
$messaging |
| 202 |
.css('display', 'block') |
| 203 |
.html('<div>' + message + '</div>'); |
| 204 |
} else { |
| 205 |
$messaging |
| 206 |
.css('display', 'none') |
| 207 |
.html(); |
| 208 |
} |
| 209 |
}; |
| 210 |
|
| 211 |
$('#logtivity-register-site').on('submit', function(evt) { |
| 212 |
evt.preventDefault(); |
| 213 |
|
| 214 |
registerMessage(); |
| 215 |
|
| 216 |
$.post(this.getAttribute('action'), $(this).serialize()) |
| 217 |
.success(function(response) { |
| 218 |
let success = response.success || false, |
| 219 |
responseData = response.data || [], |
| 220 |
code = responseData.code || 200; |
| 221 |
|
| 222 |
console.log(responseData); |
| 223 |
console.log(response); |
| 224 |
|
| 225 |
if (success && code === 200) { |
| 226 |
registerMessage(responseData.message); |
| 227 |
|
| 228 |
} else if (success) { |
| 229 |
let message = '<h2>' + code + ': ' + responseData.error + '<h2>'; |
| 230 |
if (responseData.error !== responseData.message) { |
| 231 |
message += responseData.message; |
| 232 |
} |
| 233 |
|
| 234 |
registerMessage(message, 'danger'); |
| 235 |
|
| 236 |
} else if (typeof responseData == 'string') { |
| 237 |
registerMessage(responseData, 'danger'); |
| 238 |
|
| 239 |
} else if (typeof responseData.forEach !== 'undefined') { |
| 240 |
let message = ''; |
| 241 |
responseData.forEach(function(error) { |
| 242 |
message += '<p>' + error.message + '</p>'; |
| 243 |
}); |
| 244 |
registerMessage(message, 'danger'); |
| 245 |
|
| 246 |
} else { |
| 247 |
console.log(response); |
| 248 |
registerMessage('Unknown Response', 'danger') |
| 249 |
} |
| 250 |
|
| 251 |
}) |
| 252 |
.error(function($xhr) { |
| 253 |
let message = 'Unknown Error' |
| 254 |
if ($xhr.responseJSON) { |
| 255 |
message = $xhr.responseJSON.data || message; |
| 256 |
} else if ($xhr.responseText) { |
| 257 |
message = $xhr.responseText; |
| 258 |
} |
| 259 |
|
| 260 |
registerMessage(message, 'danger'); |
| 261 |
}); |
| 262 |
}); |
| 263 |
}); |
| 264 |
|