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

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