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