PluginProbe
Stream – Activity Log & Audit Trail / 3.0.2
Stream – Activity Log & Audit Trail v3.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 3.0.2, at ui/js/settings.js

393 lines 10.7 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 { text: 'Roles', children: roles },
142 { text: 'Users', children: [] }
143 ]
144 };
145
146 if ( true !== response.success || undefined === response.data || true !== response.data.status ) {
147 return answer;
148 }
149
150 $.each( response.data.users, function( k, user ) {
151 if ( $.contains( roles, user.id ) ) {
152 user.disabled = true;
153 }
154 });
155
156 answer.results[ 1 ].children = response.data.users;
157
158 // Notice we return the value of more so Select2 knows if more results can be loaded
159 return answer;
160 }
161 },
162 initSelection: function( item, callback ) {
163 callback( { id: item.data( 'selected-id' ), text: item.data( 'selected-text' ) } );
164 },
165 formatResult: function( object, container ) {
166 var result = object.text;
167
168 if ( 'undefined' !== typeof object.icon && object.icon ) {
169 result = '<img src="' + object.icon + '" class="wp-stream-select2-icon">' + result;
170
171 // Add more info to the container
172 container.attr( 'title', object.tooltip );
173 }
174
175 // Add more info to the container
176 if ( 'undefined' !== typeof object.tooltip ) {
177 container.attr( 'title', object.tooltip );
178 } else if ( 'undefined' !== typeof object.user_count ) {
179 container.attr( 'title', object.user_count );
180 }
181
182 return result;
183 },
184 formatSelection: function( object ) {
185 if ( $.isNumeric( object.id ) && object.text.indexOf( 'icon-users' ) < 0 ) {
186 object.text += '<i class="icon16 icon-users"></i>';
187 }
188
189 return object.text;
190 },
191 allowClear: true,
192 placeholder: $input_user.data( 'placeholder' )
193 }).on( 'change', function() {
194 var value = $( this ).select2( 'data' );
195
196 $( this ).data( 'selected-id', value.id );
197 $( this ).data( 'selected-text', value.text );
198 });
199 });
200
201 $( 'ul.select2-choices, ul.select2-choices li, input.select2-input', '.stream-exclude-list tr:not(.hidden) .ip_address' ).on( 'mousedown click focus', function() {
202 var $container = $( this ).closest( '.select2-container' ),
203 $input = $container.find( 'input.select2-input' ),
204 value = $container.select2( 'data' );
205
206 if ( value.length >= 1 ) {
207 $input.blur();
208 return false;
209 }
210 });
211
212 $( '.stream-exclude-list tr:not(.hidden) input[type=hidden].select2-select.context' ).on( 'change', function( val ) {
213 var $connector = $( this ).prevAll( ':input.connector' );
214
215 if ( undefined !== val.added && undefined !== val.added.parent ) {
216 $connector.val( val.added.parent );
217 } else {
218 $connector.val( $( this ).val() );
219 $( this ).val( '' );
220 }
221 });
222
223 $( '.stream-exclude-list tr:not(.hidden) .exclude_rules_remove_rule_row' ).on( 'click', function() {
224 var $thisRow = $( this ).closest( 'tr' );
225
226 $thisRow.remove();
227
228 recalculate_rules_found();
229 recalculate_rules_selected();
230 });
231
232 };
233
234 initSettingsSelect2();
235
236 var keepRecordsIndefinitely = $( '#wp_stream\\[general_keep_records_indefinitely\\]' ),
237 keepRecordsFor = $( '#wp_stream_general_records_ttl' ),
238 keepRecordsForRow = keepRecordsFor.closest( 'tr' );
239
240 function toggleKeepRecordsFor() {
241 if ( keepRecordsIndefinitely.is( ':checked' ) ) {
242 keepRecordsFor.prop( 'disabled', true );
243 keepRecordsForRow.contents().fadeTo( 250, 0.25 );
244 } else {
245 keepRecordsFor.prop( 'disabled', false );
246 keepRecordsForRow.contents().fadeTo( 250, 1.0 );
247 }
248 }
249
250 keepRecordsIndefinitely.on( 'change', function() {
251 toggleKeepRecordsFor();
252 });
253
254 toggleKeepRecordsFor();
255
256 $( '#exclude_rules_new_rule' ).on( 'click', function() {
257 var $excludeList = $( 'table.stream-exclude-list' );
258
259 $( '.select2-select', $excludeList ).each( function() {
260 $( this ).select2( 'destroy' );
261 });
262
263 var $lastRow = $( 'tr', $excludeList ).last(),
264 $newRow = $lastRow.clone();
265
266 $newRow.removeAttr( 'class' );
267 $( '.stream-exclude-list tbody :input' ).off();
268 $( ':input', $newRow ).off().val( '' );
269
270 $lastRow.after( $newRow );
271
272 initSettingsSelect2();
273
274 recalculate_rules_found();
275 recalculate_rules_selected();
276 });
277
278 $( '#exclude_rules_remove_rules' ).on( 'click', function() {
279 var $excludeList = $( 'table.stream-exclude-list' ),
280 selectedRows = $( 'tbody input.cb-select:checked', $excludeList ).closest( 'tr' );
281
282 if ( ( $( 'tbody tr', $excludeList ).length - selectedRows.length ) >= 2 ) {
283 selectedRows.remove();
284 } else {
285 $( ':input', selectedRows ).val( '' );
286 $( selectedRows ).not( ':first' ).remove();
287 $( '.select2-select', selectedRows ).select2( 'val', '' );
288 }
289
290 $excludeList.find( 'input.cb-select' ).prop( 'checked', false );
291
292 recalculate_rules_found();
293 recalculate_rules_selected();
294 });
295
296 $( '.stream-exclude-list' ).closest( 'form' ).submit( function() {
297 $( '.stream-exclude-list tbody tr', this ).each( function() {
298 if ( 0 === $( this ).find( ':input[value][value!=""]' ).length ) {
299 // Don't send inputs in this row
300 $( this ).find( ':input[value]' ).removeAttr( 'name' );
301 }
302 });
303 });
304
305 $( '.stream-exclude-list' ).closest( 'td' ).prev( 'th' ).hide();
306
307 $( 'table.stream-exclude-list' ).on( 'click', 'input.cb-select', function() {
308 recalculate_rules_selected();
309 });
310
311 function recalculate_rules_selected() {
312 var $selectedRows = $( 'table.stream-exclude-list tbody tr:not( .hidden ) input.cb-select:checked' ),
313 $deleteButton = $( '#exclude_rules_remove_rules' );
314
315 if ( 0 === $selectedRows.length ) {
316 $deleteButton.prop( 'disabled', true );
317 } else {
318 $deleteButton.prop( 'disabled', false );
319 }
320 }
321
322 function recalculate_rules_found() {
323 var $allRows = $( 'table.stream-exclude-list tbody tr:not( .hidden )' ),
324 $noRulesFound = $( 'table.stream-exclude-list tbody tr.no-items' ),
325 $selectAll = $( '.check-column.manage-column input.cb-select' ),
326 $deleteButton = $( '#exclude_rules_remove_rules' );
327
328 if ( 0 === $allRows.length ) {
329 $noRulesFound.show();
330 $selectAll.prop( 'disabled', true );
331 $deleteButton.prop( 'disabled', true );
332 } else {
333 $noRulesFound.hide();
334 $selectAll.prop( 'disabled', false );
335 }
336
337 wp_stream_regenerate_alt_rows( $allRows );
338 }
339
340 $( document ).ready( function() {
341 recalculate_rules_found();
342 recalculate_rules_selected();
343 });
344
345 // Confirmation on some important actions
346 $( '#wp_stream_general_reset_site_settings' ).click( function( e ) {
347 if ( ! confirm( wp_stream.i18n.confirm_defaults ) ) {
348 e.preventDefault();
349 }
350 });
351
352 // Settings page tabs
353 var $tabs = $( '.nav-tab-wrapper' ),
354 $panels = $( '.nav-tab-content table.form-table' ),
355 $activeTab = $tabs.find( '.nav-tab-active' ),
356 defaultIndex = $activeTab.length > 0 ? $tabs.find( 'a' ).index( $activeTab ) : 0,
357 hashIndex = window.location.hash.match( /^#(\d+)$/ ),
358 currentHash = ( null !== hashIndex ? hashIndex[ 1 ] : defaultIndex ),
359 syncFormAction = function( index ) {
360 var $optionsForm = $( 'input[name="option_page"][value^="wp_stream"]' ).closest( 'form' );
361 if ( $optionsForm.length === 0 ) {
362 return;
363 }
364 var currentAction = $optionsForm.attr( 'action' );
365
366 $optionsForm.prop( 'action', currentAction.replace( /(^[^#]*).*$/, '$1#' + index ) );
367 };
368
369 $tabs.on( 'click', 'a', function() {
370 var index = $tabs.find( 'a' ).index( $( this ) ),
371 hashIndex = window.location.hash.match( /^#(\d+)$/ );
372
373 $panels.hide().eq( index ).show();
374
375 $tabs
376 .find( 'a' )
377 .removeClass( 'nav-tab-active' )
378 .filter( $( this ) )
379 .addClass( 'nav-tab-active' );
380
381 if ( '' === window.location.hash || null !== hashIndex ) {
382 window.location.hash = index;
383 }
384
385 syncFormAction( index );
386
387 return false;
388 });
389
390 $tabs.children().eq( currentHash ).trigger( 'click' );
391
392 });
393