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

503 lines 14.1 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 [type=search]' ).off( 'mousedown' );
128 });
129
130 // Admin page tabs
131 var $tabs = $( '.nav-tab-wrapper' ),
132 $panels = $( '.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 dayOffset = '0';
316
317 // check if the site date is different from the local date, and set a day offset
318 if ( localTime.getDate() !== siteTime.getDate() || localTime.getMonth() !== siteTime.getMonth() ) {
319 if ( localTime.getTime() < siteTime.getTime() ) {
320 dayOffset = '+1d';
321 } else {
322 dayOffset = '-1d';
323 }
324 }
325
326 datepickers.datepicker({
327 dateFormat: 'yy/mm/dd',
328 maxDate: dayOffset,
329 defaultDate: siteTime,
330 beforeShow: function() {
331 $( this ).prop( 'disabled', true );
332 },
333 onClose: function() {
334 $( this ).prop( 'disabled', false );
335 }
336 });
337
338 datepickers.datepicker( 'widget' ).addClass( 'stream-datepicker' );
339 }
340
341 predefined.select2({
342 'allowClear': true
343 });
344
345 if ( '' !== from.val() ) {
346 from_remove.show();
347 }
348
349 if ( '' !== to.val() ) {
350 to_remove.show();
351 }
352
353 predefined.on({
354 'change': function () {
355 var value = $( this ).val(),
356 option = predefined.find( '[value="' + value + '"]' ),
357 to_val = option.data( 'to' ),
358 from_val = option.data( 'from' );
359
360 if ( 'custom' === value ) {
361 dateinputs.show();
362 return false;
363 } else {
364 dateinputs.hide();
365 datepickers.datepicker( 'hide' );
366 }
367
368 from.val( from_val ).trigger( 'change', [ true ] );
369 to.val( to_val ).trigger( 'change', [ true ] );
370
371 if ( jQuery.datepicker && datepickers.datepicker( 'widget' ).is( ':visible' ) ) {
372 datepickers.datepicker( 'refresh' ).datepicker( 'hide' );
373 }
374 },
375 'select2-removed': function() {
376 predefined.val( '' ).trigger( 'change' );
377 },
378 'check_options': function () {
379 if ( '' !== to.val() && '' !== from.val() ) {
380 var option = predefined
381 .find( 'option' )
382 .filter( '[data-to="' + to.val() + '"]' )
383 .filter( '[data-from="' + from.val() + '"]' );
384 if ( 0 !== option.length ) {
385 predefined.val( option.attr( 'value' ) ).trigger( 'change', [ true ] );
386 } else {
387 predefined.val( 'custom' ).trigger( 'change', [ true ] );
388 }
389 } else if ( '' === to.val() && '' === from.val() ) {
390 predefined.val( '' ).trigger( 'change', [ true ] );
391 } else {
392 predefined.val( 'custom' ).trigger( 'change', [ true ] );
393 }
394 }
395 });
396
397 from.on( 'change', function() {
398 if ( '' !== from.val() ) {
399 from_remove.show();
400 to.datepicker( 'option', 'minDate', from.val() );
401 } else {
402 from_remove.hide();
403 }
404
405 if ( true === arguments[ arguments.length - 1 ] ) {
406 return false;
407 }
408
409 predefined.trigger( 'check_options' );
410 });
411
412 to.on( 'change', function() {
413 if ( '' !== to.val() ) {
414 to_remove.show();
415 from.datepicker( 'option', 'maxDate', to.val() );
416 } else {
417 to_remove.hide();
418 }
419
420 if ( true === arguments[ arguments.length - 1 ] ) {
421 return false;
422 }
423
424 predefined.trigger( 'check_options' );
425 });
426
427 // Trigger change on load
428 predefined.trigger( 'change' );
429
430 $( '' ).add( from_remove ).add( to_remove ).on( 'click', function() {
431 $( this ).next( 'input' ).val( '' ).trigger( 'change' );
432 });
433 });
434 },
435
436 save_interval: function( $btn ) {
437 var $wrapper = this.wrapper;
438 $btn.click( function() {
439 var data = {
440 key: $wrapper.find( 'select.field-predefined' ).find( ':selected' ).val(),
441 start: $wrapper.find( '.date-inputs .field-from' ).val(),
442 end: $wrapper.find( '.date-inputs .field-to' ).val()
443 };
444
445 // Add params to URL
446 $( this ).attr( 'href', $( this ).attr( 'href' ) + '&' + $.param( data ) );
447 });
448 }
449 };
450
451 $( '.wp-stream-feeds-key #stream_user_feed_key_generate' ).click( function( e ) {
452 e.preventDefault();
453
454 var user = $( '#user_id' ).val(),
455 nonce = $( '.wp-stream-feeds-key #wp_stream_generate_key_nonce' ).val();
456
457 $.ajax({
458 type: 'POST',
459 url: ajaxurl,
460 data: { action: 'wp_stream_feed_key_generate', nonce: nonce, user: user },
461 dataType: 'json',
462 beforeSend: function() {
463 $( '.wp-stream-feeds-key .spinner' ).show().css( { 'display': 'inline-block' } );
464 },
465 success: function( response ) {
466 $( '.wp-stream-feeds-key .spinner' ).hide();
467 if ( true === response.success || undefined !== response.data ) {
468 $( '.wp-stream-feeds-key #stream_user_feed_key' ).val( response.data.feed_key );
469 $( '.wp-stream-feeds-links a.rss-feed' ).attr( 'href', response.data.xml_feed );
470 $( '.wp-stream-feeds-links a.json-feed' ).attr( 'href', response.data.json_feed );
471 }
472 }
473 });
474 });
475
476 $( document ).ready( function() {
477 intervals.init( $( '.date-interval' ) );
478
479 // Disable option groups whose children are all disabled
480 $( 'select[name="context"] .level-1' ).each( function() {
481 var all_disabled = true;
482
483 $( this ).nextUntil( '.level-1' ).each( function() {
484 if ( $( this ).is( ':not(:disabled)' ) ) {
485 all_disabled = false;
486 return false;
487 }
488 });
489
490 if ( true === all_disabled ) {
491 $( this ).prop( 'disabled', true );
492 }
493 });
494 });
495
496 });
497
498 jQuery.extend({
499 streamGetQueryVars: function( str ) {
500 return ( str || document.location.search ).replace( /(^\?)/, '' ).split( '&' ).map( function( n ) { return n = n.split( '=' ), this[n[0]] = n[1], this; }.bind( {} ) )[0];
501 }
502 });
503