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

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