PluginProbe
Stream – Activity Log & Audit Trail / 4.0.2
Stream – Activity Log & Audit Trail v4.0.2
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
stream / ui / js / admin.js

admin.js in Stream – Activity Log & Audit Trail 4.0.2, at ui/js/admin.js

562 lines 15.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* globals wp_stream, ajaxurl, jQuery */
2 jQuery(
3 function( $ ) {
4 // Shorter timeago strings for English locale
5 if ( 'en' === wp_stream.locale && 'undefined' !== typeof $.timeago ) {
6 $.timeago.settings.strings.seconds = 'seconds';
7 $.timeago.settings.strings.minute = 'a minute';
8 $.timeago.settings.strings.hour = 'an hour';
9 $.timeago.settings.strings.hours = '%d hours';
10 $.timeago.settings.strings.month = 'a month';
11 $.timeago.settings.strings.year = 'a year';
12 }
13
14 $( 'li.toplevel_page_wp_stream ul li.wp-first-item.current' ).parent().parent().find( '.update-plugins' ).remove();
15
16 $( '.toplevel_page_wp_stream :input.chosen-select' ).each(
17 function( i, el ) {
18 var args = {},
19 templateResult = function( record ) {
20 var $result = $( '<span>' ),
21 $elem = $( record.element ),
22 icon = '';
23
24 if ( '- ' === record.text.substring( 0, 2 ) ) {
25 record.text = record.text.substring( 2 );
26 }
27
28 if ( 'undefined' !== typeof record.id && 'string' === typeof record.id ) {
29 if ( record.id.indexOf( 'group-' ) === 0 ) {
30 $result.addClass( 'parent' );
31 } else if ( $elem.hasClass( 'level-2' ) ) {
32 $result.addClass( 'child' );
33 }
34 }
35
36 if ( undefined !== record.icon ) {
37 icon = record.icon;
38 } else if ( undefined !== $elem && '' !== $elem.data( 'icon' ) ) {
39 icon = $elem.data( 'icon' );
40 }
41
42 if ( icon ) {
43 $result.html( '<img src="' + icon + '" class="wp-stream-select2-icon">' );
44 }
45 $result.append( record.text );
46
47 return $result;
48 },
49 templateSelection = function( record ) {
50 if ( '- ' === record.text.substring( 0, 2 ) ) {
51 record.text = record.text.substring( 2 );
52 }
53 return record.text;
54 };
55
56 if ( $( el ).find( 'option' ).not( ':selected' ).not( ':empty' ).length > 0 ) {
57 args = {
58 minimumResultsForSearch: 10,
59 templateResult: templateResult,
60 templateSelection: templateSelection,
61 allowClear: true,
62 width: '165px',
63 };
64 } else {
65 args = {
66 minimumInputLength: 3,
67 allowClear: true,
68 width: '165px',
69 ajax: {
70 url: ajaxurl,
71 delay: 500,
72 dataType: 'json',
73 quietMillis: 100,
74 data: function( term ) {
75 return {
76 action: 'wp_stream_filters',
77 nonce: $( '#stream_filters_user_search_nonce' ).val(),
78 filter: $( el ).attr( 'name' ),
79 q: term.term,
80 };
81 },
82 processResults: function( data ) {
83 var results = [];
84 $.each(
85 data, function( index, item ) {
86 results.push(
87 {
88 id: item.id,
89 text: item.label,
90 }
91 );
92 }
93 );
94 return {
95 results: results,
96 };
97 },
98 },
99 templateResult: templateResult,
100 templateSelection: templateSelection,
101 };
102 }
103
104 $( el ).select2( args );
105 }
106 );
107
108 var $queryVars = $.streamGetQueryVars();
109 var $contextInput = $( '.toplevel_page_wp_stream select.chosen-select[name="context"]' );
110
111 if ( ( 'undefined' === typeof $queryVars.context || '' === $queryVars.context ) && 'undefined' !== typeof $queryVars.connector ) {
112 $contextInput.val( 'group-' + $queryVars.connector );
113 $contextInput.trigger( 'change' );
114 }
115
116 $( 'input[type=submit]', '#record-filter-form' ).click(
117 function() {
118 $( 'input[type=submit]', $( this ).parents( 'form' ) ).removeAttr( 'clicked' );
119 $( this ).attr( 'clicked', 'true' );
120 }
121 );
122
123 $( '#record-filter-form' ).submit(
124 function() {
125 var $context = $( '.toplevel_page_wp_stream :input.chosen-select[name="context"]' ),
126 $option = $context.find( 'option:selected' ),
127 $connector = $context.parent().find( '.record-filter-connector' ),
128 optionConnector = $option.data( 'group' ),
129 optionClass = $option.prop( 'class' ),
130 $recordAction = $( '.recordactions select' );
131
132 if ( $( '#record-actions-submit' ).attr( 'clicked' ) !== 'true' ) {
133 $recordAction.val( '' );
134 }
135
136 $connector.val( optionConnector );
137
138 if ( 'level-1' === optionClass ) {
139 $option.val( '' );
140 }
141 }
142 );
143
144 $( window ).on(
145 'load',
146 function() {
147 $( '.toplevel_page_wp_stream input[type="search"]' ).off( 'mousedown' );
148 }
149 );
150
151 // Confirmation on some important actions
152 $( 'body' ).on(
153 'click', '#wp_stream_advanced_delete_all_records, #wp_stream_network_advanced_delete_all_records', function( e ) {
154 if ( ! window.confirm( wp_stream.i18n.confirm_purge ) ) {
155 e.preventDefault();
156 }
157 }
158 );
159
160 $( 'body' ).on(
161 'click', '#wp_stream_advanced_reset_site_settings, #wp_stream_network_advanced_reset_site_settings', function( e ) {
162 if ( ! window.confirm( wp_stream.i18n.confirm_defaults ) ) {
163 e.preventDefault();
164 }
165 }
166 );
167
168 // Admin page tabs
169 var $tabs = $( '.wp_stream_screen .nav-tab-wrapper' ),
170 $panels = $( '.wp_stream_screen .nav-tab-content table.form-table' ),
171 $activeTab = $tabs.find( '.nav-tab-active' ),
172 defaultIndex = $activeTab.length > 0 ? $tabs.find( 'a' ).index( $activeTab ) : 0,
173 hashIndexStart = window.location.hash.match( /^#(\d+)$/ ),
174 currentHash = ( null !== hashIndexStart ? hashIndexStart[ 1 ] : defaultIndex ),
175 syncFormAction = function( index ) {
176 var $optionsForm = $( 'input[name="option_page"][value^="wp_stream"]' ).closest( 'form' );
177 if ( $optionsForm.length === 0 ) {
178 return;
179 }
180 var currentAction = $optionsForm.attr( 'action' );
181
182 $optionsForm.prop( 'action', currentAction.replace( /(^[^#]*).*$/, '$1#' + index ) );
183 };
184
185 $tabs.on(
186 'click', 'a', function() {
187 var index = $tabs.find( 'a' ).index( $( this ) ),
188 hashIndex = window.location.hash.match( /^#(\d+)$/ );
189
190 $panels.hide().eq( index ).show();
191 $tabs
192 .find( 'a' )
193 .removeClass( 'nav-tab-active' )
194 .filter( $( this ) )
195 .addClass( 'nav-tab-active' );
196
197 if ( '' === window.location.hash || null !== hashIndex ) {
198 window.location.hash = index;
199 }
200
201 syncFormAction( index );
202
203 return false;
204 }
205 );
206
207 $tabs.children().eq( currentHash ).trigger( 'click' );
208
209 // Live Updates screen option
210 $( document ).ready(
211 function() {
212 // Enable Live Updates checkbox ajax
213 $( '#enable_live_update' ).click(
214 function() {
215 var nonce = $( '#stream_live_update_nonce' ).val(),
216 user = $( '#enable_live_update_user' ).val(),
217 checked = 'unchecked',
218 heartbeat = 'true';
219
220 if ( $( '#enable_live_update' ).is( ':checked' ) ) {
221 checked = 'checked';
222 }
223
224 heartbeat = $( '#enable_live_update' ).data( 'heartbeat' );
225
226 $.ajax(
227 {
228 type: 'POST',
229 url: ajaxurl,
230 data: {
231 action: 'stream_enable_live_update',
232 nonce: nonce,
233 user: user,
234 checked: checked,
235 heartbeat: heartbeat,
236 },
237 dataType: 'json',
238 beforeSend: function() {
239 $( '.stream-live-update-checkbox .spinner' ).show().css( { display: 'inline-block' } );
240 },
241 success: function( response ) {
242 $( '.stream-live-update-checkbox .spinner' ).hide();
243
244 if ( false === response.success ) {
245 $( '#enable_live_update' ).prop( 'checked', false );
246
247 if ( response.data ) {
248 window.alert( response.data );
249 }
250 }
251 },
252 }
253 );
254 }
255 );
256
257 function toggle_filter_submit() {
258 var all_hidden = true;
259
260 // If all filters are hidden, hide the button
261 if ( $( 'div.metabox-prefs [name="date-hide"]' ).is( ':checked' ) ) {
262 all_hidden = false;
263 }
264
265 var divs = $( 'div.alignleft.actions div.select2-container' );
266
267 divs.each(
268 function() {
269 if ( ! $( this ).is( ':hidden' ) ) {
270 all_hidden = false;
271 return false;
272 }
273 }
274 );
275
276 if ( all_hidden ) {
277 $( 'input#record-query-submit' ).hide();
278 $( 'span.filter_info' ).show();
279 } else {
280 $( 'input#record-query-submit' ).show();
281 $( 'span.filter_info' ).hide();
282 }
283 }
284
285 if ( $( 'div.metabox-prefs [name="date-hide"]' ).is( ':checked' ) ) {
286 $( 'div.date-interval' ).show();
287 } else {
288 $( 'div.date-interval' ).hide();
289 }
290
291 $( 'div.actions select.chosen-select' ).each(
292 function() {
293 var name = $( this ).prop( 'name' );
294
295 if ( $( 'div.metabox-prefs [name="' + name + '-hide"]' ).is( ':checked' ) ) {
296 $( this ).prev( '.select2-container' ).show();
297 } else {
298 $( this ).prev( '.select2-container' ).hide();
299 }
300 }
301 );
302
303 toggle_filter_submit();
304
305 $( 'div.metabox-prefs [type="checkbox"]' ).click(
306 function() {
307 var id = $( this ).prop( 'id' );
308
309 if ( 'date-hide' === id ) {
310 if ( $( this ).is( ':checked' ) ) {
311 $( 'div.date-interval' ).show();
312 } else {
313 $( 'div.date-interval' ).hide();
314 }
315 } else {
316 id = id.replace( '-hide', '' );
317
318 if ( $( this ).is( ':checked' ) ) {
319 $( '[name="' + id + '"]' ).prev( '.select2-container' ).show();
320 } else {
321 $( '[name="' + id + '"]' ).prev( '.select2-container' ).hide();
322 }
323 }
324
325 toggle_filter_submit();
326 }
327 );
328
329 $( '#ui-datepicker-div' ).addClass( 'stream-datepicker' );
330 }
331 );
332
333 // Relative time
334 $( 'table.wp-list-table' ).on(
335 'updated', function() {
336 var timeObjects = $( this ).find( 'time.relative-time' );
337 timeObjects.each(
338 function( i, el ) {
339 var timeEl = $( el );
340 timeEl.removeClass( 'relative-time' );
341 $( '<strong><time datetime="' + timeEl.attr( 'datetime' ) + '" class="timeago"/></time></strong><br/>' )
342 .prependTo( timeEl.parent().parent() )
343 .find( 'time.timeago' )
344 .timeago();
345 }
346 );
347 }
348 ).trigger( 'updated' );
349
350 var intervals = {
351 init: function( $wrapper ) {
352 this.wrapper = $wrapper;
353 this.save_interval( this.wrapper.find( '.button-primary' ), this.wrapper );
354
355 this.$ = this.wrapper.each(
356 function( i, val ) {
357 var container = $( val ),
358 dateinputs = container.find( '.date-inputs' ),
359 from = container.find( '.field-from' ),
360 to = container.find( '.field-to' ),
361 to_remove = to.prev( '.date-remove' ),
362 from_remove = from.prev( '.date-remove' ),
363 predefined = container.children( '.field-predefined' ),
364 datepickers = $( '' ).add( to ).add( from );
365
366 if ( jQuery.datepicker ) {
367 // Apply a GMT offset due to Date() using the visitor's local time
368 var siteGMTOffsetHours = parseFloat( wp_stream.gmt_offset ),
369 localGMTOffsetHours = new Date().getTimezoneOffset() / 60 * -1,
370 totalGMTOffsetHours = siteGMTOffsetHours - localGMTOffsetHours,
371 localTime = new Date(),
372 siteTime = new Date( localTime.getTime() + ( totalGMTOffsetHours * 60 * 60 * 1000 ) ),
373 maxOffset = 0,
374 minOffset = null;
375
376 // Check if the site date is different from the local date, and set a day offset
377 if ( localTime.getDate() !== siteTime.getDate() || localTime.getMonth() !== siteTime.getMonth() ) {
378 if ( localTime.getTime() < siteTime.getTime() ) {
379 maxOffset = '+1d';
380 } else {
381 maxOffset = '-1d';
382 }
383 }
384
385 datepickers.datepicker(
386 {
387 dateFormat: 'yy/mm/dd',
388 minDate: minOffset,
389 maxDate: maxOffset,
390 defaultDate: siteTime,
391 beforeShow: function() {
392 $( this ).prop( 'disabled', true );
393 },
394 onClose: function() {
395 $( this ).prop( 'disabled', false );
396 },
397 }
398 );
399
400 datepickers.datepicker( 'widget' ).addClass( 'stream-datepicker' );
401 }
402
403 predefined.select2(
404 {
405 allowClear: true,
406 }
407 );
408
409 if ( '' !== from.val() ) {
410 from_remove.show();
411 }
412
413 if ( '' !== to.val() ) {
414 to_remove.show();
415 }
416
417 predefined.on(
418 {
419 change: function() {
420 var value = $( this ).val(),
421 option = predefined.find( '[value="' + value + '"]' ),
422 to_val = option.data( 'to' ),
423 from_val = option.data( 'from' );
424
425 if ( 'custom' === value ) {
426 dateinputs.show();
427 return false;
428 }
429 dateinputs.hide();
430 datepickers.datepicker( 'hide' );
431
432 from.val( from_val ).trigger( 'change', [ true ] );
433 to.val( to_val ).trigger( 'change', [ true ] );
434
435 if ( jQuery.datepicker && datepickers.datepicker( 'widget' ).is( ':visible' ) ) {
436 datepickers.datepicker( 'refresh' ).datepicker( 'hide' );
437 }
438 },
439 'select2-removed': function() {
440 predefined.val( '' ).trigger( 'change' );
441 },
442 check_options: function() {
443 if ( '' !== to.val() && '' !== from.val() ) {
444 var option = predefined
445 .find( 'option' )
446 .filter( '[data-to="' + to.val() + '"]' )
447 .filter( '[data-from="' + from.val() + '"]' );
448 if ( 0 !== option.length ) {
449 predefined.val( option.attr( 'value' ) ).trigger( 'change', [ true ] );
450 } else {
451 predefined.val( 'custom' ).trigger( 'change', [ true ] );
452 }
453 } else if ( '' === to.val() && '' === from.val() ) {
454 predefined.val( '' ).trigger( 'change', [ true ] );
455 } else {
456 predefined.val( 'custom' ).trigger( 'change', [ true ] );
457 }
458 },
459 }
460 );
461
462 from.on(
463 'change', function() {
464 if ( '' !== from.val() ) {
465 from_remove.show();
466 to.datepicker( 'option', 'minDate', from.val() );
467 } else {
468 from_remove.hide();
469 }
470
471 if ( true === arguments[ arguments.length - 1 ] ) {
472 return false;
473 }
474
475 predefined.trigger( 'check_options' );
476 }
477 );
478
479 to.on(
480 'change', function() {
481 if ( '' !== to.val() ) {
482 to_remove.show();
483 from.datepicker( 'option', 'maxDate', to.val() );
484 } else {
485 to_remove.hide();
486 }
487
488 if ( true === arguments[ arguments.length - 1 ] ) {
489 return false;
490 }
491
492 predefined.trigger( 'check_options' );
493 }
494 );
495
496 // Trigger change on load
497 predefined.trigger( 'change' );
498
499 $( '' ).add( from_remove ).add( to_remove ).on(
500 'click', function() {
501 $( this ).next( 'input' ).val( '' ).trigger( 'change' );
502 }
503 );
504 }
505 );
506 },
507
508 save_interval: function( $btn ) {
509 var $wrapper = this.wrapper;
510 $btn.click(
511 function() {
512 var data = {
513 key: $wrapper.find( 'select.field-predefined' ).find( ':selected' ).val(),
514 start: $wrapper.find( '.date-inputs .field-from' ).val(),
515 end: $wrapper.find( '.date-inputs .field-to' ).val(),
516 };
517
518 // Add params to URL
519 $( this ).attr( 'href', $( this ).attr( 'href' ) + '&' + $.param( data ) );
520 }
521 );
522 },
523 };
524
525 $( document ).ready(
526 function() {
527 intervals.init( $( '.date-interval' ) );
528
529 // Disable option groups whose children are all disabled
530 $( 'select[name="context"] .level-1' ).each(
531 function() {
532 var all_disabled = true;
533
534 $( this ).nextUntil( '.level-1' ).each(
535 function() {
536 if ( $( this ).is( ':not(:disabled)' ) ) {
537 all_disabled = false;
538 return false;
539 }
540 }
541 );
542
543 if ( true === all_disabled ) {
544 $( this ).prop( 'disabled', true );
545 }
546 }
547 );
548 }
549 );
550 }
551 );
552
553 jQuery.extend(
554 {
555 streamGetQueryVars: function( str ) {
556 return ( str || document.location.search ).replace( /(^\?)/, '' ).split( '&' ).map( function( n ) {
557 return n = n.split( '=' ), this[n[0]] = n[1], this;
558 }.bind( {} ) )[0];
559 },
560 }
561 );
562