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

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

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