PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 3.3.9
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v3.3.9
3.3.9 3.3.8 trunk 1.0 1.1.0 1.10.0 1.11.0 1.11.1 1.12.0 1.13.0 1.14.0 1.15.0 1.16.0 1.17.0 1.17.1 1.18.0 1.19.0 1.2.0 1.20.0 1.20.1 1.3.0 1.3.1 1.4.0 1.5.0 1.6.0 All 67 releases
logtivity / assets / app.js

app.js in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 3.3.9, at assets/app.js

265 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 _ajax_nonce : logtivityAdmin.dismissNonce,
181 postType : type,
182 dismissUntil: dismissUntil,
183 }
184 });
185 }
186 });
187 }
188 };
189
190 LogtivityLogIndex.init();
191 DismissUrlHasChanged.init();
192
193 let registerMessage = function(message, type) {
194 type = type || 'info';
195
196 let $messaging = $('#logtivity-register-response')
197 .removeClass()
198 .addClass('logtivity-notice logtivity-notice-' + type)
199 .css('width', 'fit-content');
200
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 };
211
212 $('#logtivity-register-site').on('submit', function(evt) {
213 evt.preventDefault();
214
215 registerMessage();
216
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;
222
223 console.log(responseData);
224 console.log(response);
225
226 if (success && code === 200) {
227 registerMessage(responseData.message);
228
229 } else if (success) {
230 let message = '<h2>' + code + ': ' + responseData.error + '<h2>';
231 if (responseData.error !== responseData.message) {
232 message += responseData.message;
233 }
234
235 registerMessage(message, 'danger');
236
237 } else if (typeof responseData == 'string') {
238 registerMessage(responseData, 'danger');
239
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');
246
247 } else {
248 console.log(response);
249 registerMessage('Unknown Response', 'danger')
250 }
251
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 }
260
261 registerMessage(message, 'danger');
262 });
263 });
264 });
265