PluginProbe
Stream – Activity Log & Audit Trail / 3.9.2
Stream – Activity Log & Audit Trail v3.9.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 / exclude.js

exclude.js in Stream – Activity Log & Audit Trail 3.9.2, at ui/js/exclude.js

462 lines 12.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* globals jQuery, ajaxurl, wp_stream_regenerate_alt_rows */
2 jQuery(
3 function( $ ) {
4 var $excludeRows = $( '.stream-exclude-list tbody tr:not(.hidden)' );
5 var $placeholderRow = $( '.stream-exclude-list tr.helper' );
6
7 var initSettingsSelect2 = function( $rowsWithSelect2 ) {
8 var $input_user;
9
10 $( 'select.select2-select.connector_or_context', $rowsWithSelect2 ).each(
11 function( k, el ) {
12 $( el ).select2(
13 {
14 allowClear: true,
15 templateResult: function( item ) {
16 if ( typeof item.id === 'undefined' ) {
17 return item.text;
18 }
19 if ( item.id.indexOf( '-' ) === -1 ) {
20 return $( '<span class="parent">' + item.text + '</span>' );
21 }
22 return $( '<span class="child">' + item.text + '</span>' );
23 },
24 matcher: function( params, data ) {
25 var match = $.extend( true, {}, data );
26
27 if ( null === params.term || $.trim( params.term ) === '' ) {
28 return match;
29 }
30
31 var term = params.term.toLowerCase();
32
33 match.id = match.id.replace( 'blogs', 'sites' );
34 if ( match.id.toLowerCase().indexOf( term ) >= 0 ) {
35 return match;
36 }
37
38 if ( match.children ) {
39 for ( var i = match.children.length - 1; i >= 0; i-- ) {
40 var child = match.children[i];
41
42 // Remove term from results if it doesn't match.
43 if ( child.id.toLowerCase().indexOf( term ) === -1 ) {
44 match.children.splice( i, 1 );
45 }
46 }
47
48 if ( match.children.length > 0 ) {
49 return match;
50 }
51 }
52
53 return null;
54 },
55 }
56 ).on(
57 'change', function() {
58 var row = $( this ).closest( 'tr' ),
59 connector = $( this ).val();
60 if ( connector && 0 < connector.indexOf( '-' ) ) {
61 var connector_split = connector.split( '-' );
62 connector = connector_split[0];
63 }
64 getActions( row, connector );
65 }
66 );
67 }
68 );
69
70 $( 'select.select2-select.action', $rowsWithSelect2 ).each(
71 function( k, el ) {
72 $( el ).select2(
73 {
74 allowClear: true,
75 }
76 );
77 }
78 );
79
80 $( 'select.select2-select.author_or_role', $rowsWithSelect2 ).each(
81 function( k, el ) {
82 $input_user = $( el );
83
84 $input_user.select2(
85 {
86 ajax: {
87 type: 'POST',
88 url: ajaxurl,
89 dataType: 'json',
90 quietMillis: 500,
91 data: function( term, page ) {
92 return {
93 find: term,
94 limit: 10,
95 pager: page,
96 action: 'stream_get_users',
97 nonce: $input_user.data( 'nonce' ),
98 };
99 },
100 processResults: function( response ) {
101 var answer = {
102 results: [
103 { text: '', id: '' },
104 { text: 'Roles', children: [] },
105 { text: 'Users', children: [] },
106 ],
107 };
108
109 if ( true !== response.success || undefined === response.data || true !== response.data.status ) {
110 return answer;
111 }
112
113 if ( undefined === response.data.users || undefined === response.data.roles ) {
114 return answer;
115 }
116
117 var roles = [];
118
119 $.each(
120 response.data.roles, function( id, text ) {
121 roles.push(
122 {
123 id: id,
124 text: text,
125 }
126 );
127 }
128 );
129
130 answer.results[ 1 ].children = roles;
131 answer.results[ 2 ].children = response.data.users;
132
133 // Return the value of more so Select2 knows if more results can be loaded
134 return answer;
135 },
136 },
137 templateResult: function( object ) {
138 var $result = $( '<div>' ).text( object.text );
139
140 if ( 'undefined' !== typeof object.icon && object.icon ) {
141 $result.prepend( $( '<img src="' + object.icon + '" class="wp-stream-select2-icon">' ) );
142
143 // Add more info to the container
144 $result.attr( 'title', object.tooltip );
145 }
146
147 // Add more info to the container
148 if ( 'undefined' !== typeof object.tooltip ) {
149 $result.attr( 'title', object.tooltip );
150 } else if ( 'undefined' !== typeof object.user_count ) {
151 $result.attr( 'title', object.user_count );
152 }
153
154 return $result;
155 },
156 templateSelection: function( object ) {
157 var $result = $( '<div>' ).text( object.text );
158
159 if ( $.isNumeric( object.id ) && object.text.indexOf( 'icon-users' ) < 0 ) {
160 $result.append( $( '<i class="icon16 icon-users"></i>' ) );
161 }
162
163 return $result;
164 },
165 allowClear: true,
166 placeholder: $input_user.data( 'placeholder' ),
167 }
168 ).on(
169 'change', function() {
170 var value = $( this ).select2( 'data' );
171
172 $( this ).data( 'selected-id', value.id );
173 $( this ).data( 'selected-text', value.text );
174 }
175 );
176 }
177 );
178
179 $( 'select.select2-select.ip_address', $rowsWithSelect2 ).each(
180 function( k, el ) {
181 var $input_ip = $( el ),
182 searchTerm = '';
183
184 $input_ip.select2(
185 {
186 ajax: {
187 type: 'POST',
188 url: ajaxurl,
189 dataType: 'json',
190 quietMillis: 500,
191 data: function( term ) {
192 searchTerm = term.term;
193 return {
194 find: term,
195 limit: 10,
196 action: 'stream_get_ips',
197 nonce: $input_ip.data( 'nonce' ),
198 };
199 },
200 processResults: function( response ) {
201 var answer = { results: [] },
202 ip_chunks = [];
203
204 if ( true === response.success && undefined !== response.data ) {
205 $.each(
206 response.data, function( key, ip ) {
207 answer.results.push(
208 {
209 id: ip,
210 text: ip,
211 }
212 );
213 }
214 );
215 }
216
217 if ( undefined === searchTerm ) {
218 return answer;
219 }
220
221 ip_chunks = searchTerm.match( /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ );
222
223 if ( null === ip_chunks ) {
224 return answer;
225 }
226
227 // remove whole match
228 ip_chunks.shift();
229
230 ip_chunks = $.grep(
231 ip_chunks,
232 function( chunk ) {
233 var numeric = parseInt( chunk, 10 );
234 return numeric <= 255 && numeric.toString() === chunk;
235 }
236 );
237
238 if ( ip_chunks.length >= 4 ) {
239 answer.results.push(
240 {
241 id: searchTerm,
242 text: searchTerm,
243 }
244 );
245 }
246
247 return answer;
248 },
249 },
250 allowClear: false,
251 multiple: true,
252 maximumSelectionSize: 1,
253 placeholder: $input_ip.data( 'placeholder' ),
254 tags: true,
255 }
256 );
257 }
258 ).on(
259 'change', function() {
260 $( this ).prev( '.select2-container' ).find( 'input.select2-input' ).blur();
261 }
262 );
263
264 $( 'ul.select2-choices, ul.select2-choices li, input.select2-input', '.stream-exclude-list tr:not(.hidden) .ip_address' ).on(
265 'mousedown click focus', function() {
266 var $container = $( this ).closest( '.select2-container' ),
267 $input = $container.find( 'input.select2-input' ),
268 value = $container.select2( 'data' );
269
270 if ( value.length >= 1 ) {
271 $input.blur();
272 return false;
273 }
274 }
275 );
276
277 $( '.exclude_rules_remove_rule_row', $rowsWithSelect2 ).on(
278 'click', function( e ) {
279 var $thisRow = $( this ).closest( 'tr' );
280
281 $thisRow.remove();
282
283 recalculate_rules_found();
284 recalculate_rules_selected();
285
286 e.preventDefault();
287 }
288 );
289 };
290
291 initSettingsSelect2( $excludeRows );
292
293 $( 'select.select2-select.author_or_role', $excludeRows ).each(
294 function() {
295 var $option = $( '<option selected>' + $( this ).data( 'selected-text' ) + '</option>' ).val( $( this ).data( 'selected-id' ) );
296 $( this ).append( $option ).trigger( 'change' );
297 }
298 );
299
300 $( 'select.select2-select.connector_or_context', $excludeRows ).each(
301 function() {
302 var parts = [
303 $( this ).siblings( '.connector' ).val(),
304 $( this ).siblings( '.context' ).val(),
305 ];
306 if ( parts[1] === '' ) {
307 parts.splice( 1, 1 );
308 }
309 $( this ).val( parts.join( '-' ) ).trigger( 'change' );
310 }
311 );
312
313 $( '#exclude_rules_new_rule' ).on(
314 'click', function() {
315 var $newRow = $placeholderRow.clone();
316
317 $newRow.removeAttr( 'class' );
318 $newRow.insertBefore( $placeholderRow );
319
320 initSettingsSelect2( $newRow );
321 recalculate_rules_found();
322 recalculate_rules_selected();
323 }
324 );
325
326 $( '#exclude_rules_remove_rules' ).on(
327 'click', function() {
328 var $excludeList = $( 'table.stream-exclude-list' ),
329 selectedRows = $( 'tbody input.cb-select:checked', $excludeList ).closest( 'tr' );
330
331 if ( ( $( 'tbody tr', $excludeList ).length - selectedRows.length ) >= 2 ) {
332 selectedRows.remove();
333 } else {
334 $( ':input', selectedRows ).val( '' );
335 $( selectedRows ).not( ':first' ).remove();
336 $( '.select2-select', selectedRows ).select2( 'val', '' );
337 }
338
339 $excludeList.find( 'input.cb-select' ).prop( 'checked', false );
340
341 recalculate_rules_found();
342 recalculate_rules_selected();
343 }
344 );
345
346 $( '.stream-exclude-list' ).closest( 'form' ).submit(
347 function() {
348 $( '.stream-exclude-list tbody tr.hidden', this ).each(
349 function() {
350 $( this ).find( ':input' ).removeAttr( 'name' );
351 }
352 );
353 $( '.stream-exclude-list tbody tr:not(.hidden) select.select2-select.connector_or_context', this ).each(
354 function() {
355 var parts = $( this ).val().split( '-' );
356 $( this ).siblings( '.connector' ).val( parts[0] );
357 $( this ).siblings( '.context' ).val( parts.slice( 1 ).join( '-' ) );
358 $( this ).removeAttr( 'name' );
359 }
360 );
361 $( '.stream-exclude-list tbody tr:not(.hidden) select.select2-select.ip_address', this ).each(
362 function() {
363 var firstSelected = $( 'option:selected', this ).first();
364
365 // Ugly hack to ensure we always pass an empty value or the order of rows gets messed up.
366 if ( ! firstSelected.length ) {
367 $( this ).append( '<option selected="selected"></option>' );
368 }
369
370 $( 'option:selected:not(:first)', this ).each(
371 function() {
372 firstSelected.attr( 'value', firstSelected.attr( 'value' ) + ',' + $( this ).attr( 'value' ) );
373 $( this ).removeAttr( 'selected' );
374 }
375 );
376 }
377 );
378 }
379 );
380
381 $( '.stream-exclude-list' ).closest( 'td' ).prev( 'th' ).hide();
382
383 $( 'table.stream-exclude-list' ).on(
384 'click', 'input.cb-select', function() {
385 recalculate_rules_selected();
386 }
387 );
388
389 function getActions( row, connector ) {
390 var trigger_action = $( '.select2-select.action', row ),
391 action_value = trigger_action.val();
392
393 trigger_action.empty();
394 trigger_action.prop( 'disabled', true );
395
396 var placeholder = $( '<option/>', { value: '', text: '' } );
397 trigger_action.append( placeholder );
398
399 var data = {
400 action: 'get_actions',
401 connector: connector,
402 };
403
404 $.post(
405 window.ajaxurl, data, function( response ) {
406 var success = response.success,
407 actions = response.data;
408 if ( ! success ) {
409 return;
410 }
411 for ( var key in actions ) {
412 if ( actions.hasOwnProperty( key ) ) {
413 var value = actions[key];
414 var option = $( '<option/>', { value: key, text: value } );
415 trigger_action.append( option );
416 }
417 }
418 trigger_action.val( action_value );
419 trigger_action.prop( 'disabled', false );
420 $( document ).trigger( 'alert-actions-updated' );
421 }
422 );
423 }
424
425 function recalculate_rules_selected() {
426 var $selectedRows = $( 'table.stream-exclude-list tbody tr:not( .hidden ) input.cb-select:checked' ),
427 $deleteButton = $( '#exclude_rules_remove_rules' );
428
429 if ( 0 === $selectedRows.length ) {
430 $deleteButton.prop( 'disabled', true );
431 } else {
432 $deleteButton.prop( 'disabled', false );
433 }
434 }
435
436 function recalculate_rules_found() {
437 var $allRows = $( 'table.stream-exclude-list tbody tr:not( .hidden )' ),
438 $noRulesFound = $( 'table.stream-exclude-list tbody tr.no-items' ),
439 $selectAll = $( '.check-column.manage-column input.cb-select' ),
440 $deleteButton = $( '#exclude_rules_remove_rules' );
441
442 if ( 0 === $allRows.length ) {
443 $noRulesFound.show();
444 $selectAll.prop( 'disabled', true );
445 $deleteButton.prop( 'disabled', true );
446 } else {
447 $noRulesFound.hide();
448 $selectAll.prop( 'disabled', false );
449 }
450
451 wp_stream_regenerate_alt_rows( $allRows );
452 }
453
454 $( document ).ready(
455 function() {
456 recalculate_rules_found();
457 recalculate_rules_selected();
458 }
459 );
460 }
461 );
462