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

settings.js in Stream – Activity Log & Audit Trail 2.0.2, at ui/js/settings.js

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