PluginProbe
Stream – Activity Log & Audit Trail / 2.0.5
Stream – Activity Log & Audit Trail v2.0.5
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 2.0.5, at ui/js/admin.js

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