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 / exclude.js

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

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