PluginProbe
Stream – Activity Log & Audit Trail / 3.9.0
Stream – Activity Log & Audit Trail v3.9.0
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 3.9.0, at ui/js/admin.js

570 lines 15.6 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 $( 'body' ).on(
169 'click', '#wp_stream_uninstall', function( e ) {
170 if ( ! window.confirm( wp_stream.i18n.confirm_uninstall ) ) {
171 e.preventDefault();
172 }
173 }
174 );
175
176 // Admin page tabs
177 var $tabs = $( '.wp_stream_screen .nav-tab-wrapper' ),
178 $panels = $( '.wp_stream_screen .nav-tab-content table.form-table' ),
179 $activeTab = $tabs.find( '.nav-tab-active' ),
180 defaultIndex = $activeTab.length > 0 ? $tabs.find( 'a' ).index( $activeTab ) : 0,
181 hashIndexStart = window.location.hash.match( /^#(\d+)$/ ),
182 currentHash = ( null !== hashIndexStart ? hashIndexStart[ 1 ] : defaultIndex ),
183 syncFormAction = function( index ) {
184 var $optionsForm = $( 'input[name="option_page"][value^="wp_stream"]' ).closest( 'form' );
185 if ( $optionsForm.length === 0 ) {
186 return;
187 }
188 var currentAction = $optionsForm.attr( 'action' );
189
190 $optionsForm.prop( 'action', currentAction.replace( /(^[^#]*).*$/, '$1#' + index ) );
191 };
192
193 $tabs.on(
194 'click', 'a', function() {
195 var index = $tabs.find( 'a' ).index( $( this ) ),
196 hashIndex = window.location.hash.match( /^#(\d+)$/ );
197
198 $panels.hide().eq( index ).show();
199 $tabs
200 .find( 'a' )
201 .removeClass( 'nav-tab-active' )
202 .filter( $( this ) )
203 .addClass( 'nav-tab-active' );
204
205 if ( '' === window.location.hash || null !== hashIndex ) {
206 window.location.hash = index;
207 }
208
209 syncFormAction( index );
210
211 return false;
212 }
213 );
214
215 $tabs.children().eq( currentHash ).trigger( 'click' );
216
217 // Live Updates screen option
218 $( document ).ready(
219 function() {
220 // Enable Live Updates checkbox ajax
221 $( '#enable_live_update' ).click(
222 function() {
223 var nonce = $( '#stream_live_update_nonce' ).val(),
224 user = $( '#enable_live_update_user' ).val(),
225 checked = 'unchecked',
226 heartbeat = 'true';
227
228 if ( $( '#enable_live_update' ).is( ':checked' ) ) {
229 checked = 'checked';
230 }
231
232 heartbeat = $( '#enable_live_update' ).data( 'heartbeat' );
233
234 $.ajax(
235 {
236 type: 'POST',
237 url: ajaxurl,
238 data: {
239 action: 'stream_enable_live_update',
240 nonce: nonce,
241 user: user,
242 checked: checked,
243 heartbeat: heartbeat,
244 },
245 dataType: 'json',
246 beforeSend: function() {
247 $( '.stream-live-update-checkbox .spinner' ).show().css( { display: 'inline-block' } );
248 },
249 success: function( response ) {
250 $( '.stream-live-update-checkbox .spinner' ).hide();
251
252 if ( false === response.success ) {
253 $( '#enable_live_update' ).prop( 'checked', false );
254
255 if ( response.data ) {
256 window.alert( response.data );
257 }
258 }
259 },
260 }
261 );
262 }
263 );
264
265 function toggle_filter_submit() {
266 var all_hidden = true;
267
268 // If all filters are hidden, hide the button
269 if ( $( 'div.metabox-prefs [name="date-hide"]' ).is( ':checked' ) ) {
270 all_hidden = false;
271 }
272
273 var divs = $( 'div.alignleft.actions div.select2-container' );
274
275 divs.each(
276 function() {
277 if ( ! $( this ).is( ':hidden' ) ) {
278 all_hidden = false;
279 return false;
280 }
281 }
282 );
283
284 if ( all_hidden ) {
285 $( 'input#record-query-submit' ).hide();
286 $( 'span.filter_info' ).show();
287 } else {
288 $( 'input#record-query-submit' ).show();
289 $( 'span.filter_info' ).hide();
290 }
291 }
292
293 if ( $( 'div.metabox-prefs [name="date-hide"]' ).is( ':checked' ) ) {
294 $( 'div.date-interval' ).show();
295 } else {
296 $( 'div.date-interval' ).hide();
297 }
298
299 $( 'div.actions select.chosen-select' ).each(
300 function() {
301 var name = $( this ).prop( 'name' );
302
303 if ( $( 'div.metabox-prefs [name="' + name + '-hide"]' ).is( ':checked' ) ) {
304 $( this ).prev( '.select2-container' ).show();
305 } else {
306 $( this ).prev( '.select2-container' ).hide();
307 }
308 }
309 );
310
311 toggle_filter_submit();
312
313 $( 'div.metabox-prefs [type="checkbox"]' ).click(
314 function() {
315 var id = $( this ).prop( 'id' );
316
317 if ( 'date-hide' === id ) {
318 if ( $( this ).is( ':checked' ) ) {
319 $( 'div.date-interval' ).show();
320 } else {
321 $( 'div.date-interval' ).hide();
322 }
323 } else {
324 id = id.replace( '-hide', '' );
325
326 if ( $( this ).is( ':checked' ) ) {
327 $( '[name="' + id + '"]' ).prev( '.select2-container' ).show();
328 } else {
329 $( '[name="' + id + '"]' ).prev( '.select2-container' ).hide();
330 }
331 }
332
333 toggle_filter_submit();
334 }
335 );
336
337 $( '#ui-datepicker-div' ).addClass( 'stream-datepicker' );
338 }
339 );
340
341 // Relative time
342 $( 'table.wp-list-table' ).on(
343 'updated', function() {
344 var timeObjects = $( this ).find( 'time.relative-time' );
345 timeObjects.each(
346 function( i, el ) {
347 var timeEl = $( el );
348 timeEl.removeClass( 'relative-time' );
349 $( '<strong><time datetime="' + timeEl.attr( 'datetime' ) + '" class="timeago"/></time></strong><br/>' )
350 .prependTo( timeEl.parent().parent() )
351 .find( 'time.timeago' )
352 .timeago();
353 }
354 );
355 }
356 ).trigger( 'updated' );
357
358 var intervals = {
359 init: function( $wrapper ) {
360 this.wrapper = $wrapper;
361 this.save_interval( this.wrapper.find( '.button-primary' ), this.wrapper );
362
363 this.$ = this.wrapper.each(
364 function( i, val ) {
365 var container = $( val ),
366 dateinputs = container.find( '.date-inputs' ),
367 from = container.find( '.field-from' ),
368 to = container.find( '.field-to' ),
369 to_remove = to.prev( '.date-remove' ),
370 from_remove = from.prev( '.date-remove' ),
371 predefined = container.children( '.field-predefined' ),
372 datepickers = $( '' ).add( to ).add( from );
373
374 if ( jQuery.datepicker ) {
375 // Apply a GMT offset due to Date() using the visitor's local time
376 var siteGMTOffsetHours = parseFloat( wp_stream.gmt_offset ),
377 localGMTOffsetHours = new Date().getTimezoneOffset() / 60 * -1,
378 totalGMTOffsetHours = siteGMTOffsetHours - localGMTOffsetHours,
379 localTime = new Date(),
380 siteTime = new Date( localTime.getTime() + ( totalGMTOffsetHours * 60 * 60 * 1000 ) ),
381 maxOffset = 0,
382 minOffset = null;
383
384 // Check if the site date is different from the local date, and set a day offset
385 if ( localTime.getDate() !== siteTime.getDate() || localTime.getMonth() !== siteTime.getMonth() ) {
386 if ( localTime.getTime() < siteTime.getTime() ) {
387 maxOffset = '+1d';
388 } else {
389 maxOffset = '-1d';
390 }
391 }
392
393 datepickers.datepicker(
394 {
395 dateFormat: 'yy/mm/dd',
396 minDate: minOffset,
397 maxDate: maxOffset,
398 defaultDate: siteTime,
399 beforeShow: function() {
400 $( this ).prop( 'disabled', true );
401 },
402 onClose: function() {
403 $( this ).prop( 'disabled', false );
404 },
405 }
406 );
407
408 datepickers.datepicker( 'widget' ).addClass( 'stream-datepicker' );
409 }
410
411 predefined.select2(
412 {
413 allowClear: true,
414 }
415 );
416
417 if ( '' !== from.val() ) {
418 from_remove.show();
419 }
420
421 if ( '' !== to.val() ) {
422 to_remove.show();
423 }
424
425 predefined.on(
426 {
427 change: function() {
428 var value = $( this ).val(),
429 option = predefined.find( '[value="' + value + '"]' ),
430 to_val = option.data( 'to' ),
431 from_val = option.data( 'from' );
432
433 if ( 'custom' === value ) {
434 dateinputs.show();
435 return false;
436 }
437 dateinputs.hide();
438 datepickers.datepicker( 'hide' );
439
440 from.val( from_val ).trigger( 'change', [ true ] );
441 to.val( to_val ).trigger( 'change', [ true ] );
442
443 if ( jQuery.datepicker && datepickers.datepicker( 'widget' ).is( ':visible' ) ) {
444 datepickers.datepicker( 'refresh' ).datepicker( 'hide' );
445 }
446 },
447 'select2-removed': function() {
448 predefined.val( '' ).trigger( 'change' );
449 },
450 check_options: function() {
451 if ( '' !== to.val() && '' !== from.val() ) {
452 var option = predefined
453 .find( 'option' )
454 .filter( '[data-to="' + to.val() + '"]' )
455 .filter( '[data-from="' + from.val() + '"]' );
456 if ( 0 !== option.length ) {
457 predefined.val( option.attr( 'value' ) ).trigger( 'change', [ true ] );
458 } else {
459 predefined.val( 'custom' ).trigger( 'change', [ true ] );
460 }
461 } else if ( '' === to.val() && '' === from.val() ) {
462 predefined.val( '' ).trigger( 'change', [ true ] );
463 } else {
464 predefined.val( 'custom' ).trigger( 'change', [ true ] );
465 }
466 },
467 }
468 );
469
470 from.on(
471 'change', function() {
472 if ( '' !== from.val() ) {
473 from_remove.show();
474 to.datepicker( 'option', 'minDate', from.val() );
475 } else {
476 from_remove.hide();
477 }
478
479 if ( true === arguments[ arguments.length - 1 ] ) {
480 return false;
481 }
482
483 predefined.trigger( 'check_options' );
484 }
485 );
486
487 to.on(
488 'change', function() {
489 if ( '' !== to.val() ) {
490 to_remove.show();
491 from.datepicker( 'option', 'maxDate', to.val() );
492 } else {
493 to_remove.hide();
494 }
495
496 if ( true === arguments[ arguments.length - 1 ] ) {
497 return false;
498 }
499
500 predefined.trigger( 'check_options' );
501 }
502 );
503
504 // Trigger change on load
505 predefined.trigger( 'change' );
506
507 $( '' ).add( from_remove ).add( to_remove ).on(
508 'click', function() {
509 $( this ).next( 'input' ).val( '' ).trigger( 'change' );
510 }
511 );
512 }
513 );
514 },
515
516 save_interval: function( $btn ) {
517 var $wrapper = this.wrapper;
518 $btn.click(
519 function() {
520 var data = {
521 key: $wrapper.find( 'select.field-predefined' ).find( ':selected' ).val(),
522 start: $wrapper.find( '.date-inputs .field-from' ).val(),
523 end: $wrapper.find( '.date-inputs .field-to' ).val(),
524 };
525
526 // Add params to URL
527 $( this ).attr( 'href', $( this ).attr( 'href' ) + '&' + $.param( data ) );
528 }
529 );
530 },
531 };
532
533 $( document ).ready(
534 function() {
535 intervals.init( $( '.date-interval' ) );
536
537 // Disable option groups whose children are all disabled
538 $( 'select[name="context"] .level-1' ).each(
539 function() {
540 var all_disabled = true;
541
542 $( this ).nextUntil( '.level-1' ).each(
543 function() {
544 if ( $( this ).is( ':not(:disabled)' ) ) {
545 all_disabled = false;
546 return false;
547 }
548 }
549 );
550
551 if ( true === all_disabled ) {
552 $( this ).prop( 'disabled', true );
553 }
554 }
555 );
556 }
557 );
558 }
559 );
560
561 jQuery.extend(
562 {
563 streamGetQueryVars: function( str ) {
564 return ( str || document.location.search ).replace( /(^\?)/, '' ).split( '&' ).map( function( n ) {
565 return n = n.split( '=' ), this[n[0]] = n[1], this;
566 }.bind( {} ) )[0];
567 },
568 }
569 );
570