| @@ -1,401 +1,461 @@ | ||
| 1 | -/* globals ajaxurl, wp_stream_regenerate_alt_rows */ | |
| 2 | -jQuery( function( $ ) { | |
| 3 | - var initSettingsSelect2 = function() { | |
| 4 | - var $input_user; | |
| 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' ); | |
| 5 | 6 | |
| 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 ); | |
| 7 | + var initSettingsSelect2 = function( $rowsWithSelect2 ) { | |
| 8 | + var $input_user; | |
| 21 | 9 | |
| 22 | - if ( params.term == null || $.trim( params.term ) === '') { | |
| 23 | - return match; | |
| 24 | - } | |
| 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 ); | |
| 25 | 26 | |
| 26 | - var term = params.term.toLowerCase(); | |
| 27 | + if ( null === params.term || $.trim( params.term ) === '' ) { | |
| 28 | + return match; | |
| 29 | + } | |
| 27 | 30 | |
| 28 | - match.id = match.id.replace( 'blogs', 'sites' ); | |
| 29 | - if ( match.id.toLowerCase().indexOf( term ) >= 0 ) { | |
| 30 | - return match; | |
| 31 | - } | |
| 31 | + var term = params.term.toLowerCase(); | |
| 32 | 32 | |
| 33 | - if ( match.children ) { | |
| 33 | + match.id = match.id.replace( 'blogs', 'sites' ); | |
| 34 | + if ( match.id.toLowerCase().indexOf( term ) >= 0 ) { | |
| 35 | + return match; | |
| 36 | + } | |
| 34 | 37 | |
| 35 | - for ( var i = match.children.length - 1; i >= 0; i--) { | |
| 36 | - var child = match.children[i]; | |
| 38 | + if ( match.children ) { | |
| 39 | + for ( var i = match.children.length - 1; i >= 0; i-- ) { | |
| 40 | + var child = match.children[i]; | |
| 37 | 41 | |
| 38 | - // Remove term from results if it doesn't match. | |
| 39 | - if ( child.id.toLowerCase().indexOf( term ) === -1 ) { | |
| 40 | - match.children.splice( i, 1 ); | |
| 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]; | |
| 41 | 63 | } |
| 64 | + getActions( row, connector ); | |
| 42 | 65 | } |
| 66 | + ); | |
| 67 | + } | |
| 68 | + ); | |
| 43 | 69 | |
| 44 | - if ( match.children.length > 0 ) { | |
| 45 | - return match; | |
| 70 | + $( 'select.select2-select.action', $rowsWithSelect2 ).each( | |
| 71 | + function( k, el ) { | |
| 72 | + $( el ).select2( | |
| 73 | + { | |
| 74 | + allowClear: true, | |
| 46 | 75 | } |
| 47 | - } | |
| 48 | - | |
| 49 | - return null; | |
| 76 | + ); | |
| 50 | 77 | } |
| 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 | - }); | |
| 78 | + ); | |
| 61 | 79 | |
| 62 | - $( '.stream-exclude-list tr:not(.hidden) select.select2-select.action' ).each( function( k, el ) { | |
| 63 | - $( el ).select2({ | |
| 64 | - allowClear: true | |
| 65 | - }); | |
| 66 | - }); | |
| 80 | + $( 'select.select2-select.author_or_role', $rowsWithSelect2 ).each( | |
| 81 | + function( k, el ) { | |
| 82 | + $input_user = $( el ); | |
| 67 | 83 | |
| 68 | - $( '.stream-exclude-list tr:not(.hidden) select.select2-select.author_or_role' ).each( function( k, el ) { | |
| 69 | - $input_user = $( el ); | |
| 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 | + }; | |
| 70 | 108 | |
| 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 | - }; | |
| 109 | + if ( true !== response.success || undefined === response.data || true !== response.data.status ) { | |
| 110 | + return answer; | |
| 111 | + } | |
| 94 | 112 | |
| 95 | - if ( true !== response.success || undefined === response.data || true !== response.data.status ) { | |
| 96 | - return answer; | |
| 97 | - } | |
| 113 | + if ( undefined === response.data.users || undefined === response.data.roles ) { | |
| 114 | + return answer; | |
| 115 | + } | |
| 98 | 116 | |
| 99 | - if ( undefined === response.data.users || undefined === response.data.roles ) { | |
| 100 | - return answer; | |
| 101 | - } | |
| 117 | + var roles = []; | |
| 102 | 118 | |
| 103 | - var roles = []; | |
| 119 | + $.each( | |
| 120 | + response.data.roles, function( id, text ) { | |
| 121 | + roles.push( | |
| 122 | + { | |
| 123 | + id: id, | |
| 124 | + text: text, | |
| 125 | + } | |
| 126 | + ); | |
| 127 | + } | |
| 128 | + ); | |
| 104 | 129 | |
| 105 | - $.each( response.data.roles, function( id, text ) { | |
| 106 | - roles.push({ | |
| 107 | - 'id' : id, | |
| 108 | - 'text' : text | |
| 109 | - }); | |
| 110 | - }); | |
| 130 | + answer.results[ 1 ].children = roles; | |
| 131 | + answer.results[ 2 ].children = response.data.users; | |
| 111 | 132 | |
| 112 | - answer.results[ 1 ].children = roles; | |
| 113 | - answer.results[ 2 ].children = response.data.users; | |
| 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 ); | |
| 114 | 139 | |
| 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 ); | |
| 140 | + if ( 'undefined' !== typeof object.icon && object.icon ) { | |
| 141 | + $result.prepend( $( '<img src="' + object.icon + '" class="wp-stream-select2-icon">' ) ); | |
| 121 | 142 | |
| 122 | - if ( 'undefined' !== typeof object.icon && object.icon ) { | |
| 123 | - $result.prepend( $( '<img src="' + object.icon + '" class="wp-stream-select2-icon">' ) ); | |
| 143 | + // Add more info to the container | |
| 144 | + $result.attr( 'title', object.tooltip ); | |
| 145 | + } | |
| 124 | 146 | |
| 125 | - // Add more info to the container | |
| 126 | - $result.attr( 'title', object.tooltip ); | |
| 127 | - } | |
| 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 | + } | |
| 128 | 153 | |
| 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 | - } | |
| 154 | + return $result; | |
| 155 | + }, | |
| 156 | + templateSelection: function( object ) { | |
| 157 | + var $result = $( '<div>' ).text( object.text ); | |
| 135 | 158 | |
| 136 | - return $result; | |
| 137 | - }, | |
| 138 | - templateSelection: function( object ) { | |
| 139 | - var $result = $( '<div>' ).text( object.text ); | |
| 159 | + if ( $.isNumeric( object.id ) && object.text.indexOf( 'icon-users' ) < 0 ) { | |
| 160 | + $result.append( $( '<i class="icon16 icon-users"></i>' ) ); | |
| 161 | + } | |
| 140 | 162 | |
| 141 | - if ( $.isNumeric( object.id ) && object.text.indexOf( 'icon-users' ) < 0 ) { | |
| 142 | - $result.append( $( '<i class="icon16 icon-users"></i>' ) ); | |
| 143 | - } | |
| 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' ); | |
| 144 | 171 | |
| 145 | - return $result; | |
| 146 | - }, | |
| 147 | - allowClear: true, | |
| 148 | - placeholder: $input_user.data( 'placeholder' ) | |
| 149 | - }).on( 'change', function() { | |
| 150 | - var value = $( this ).select2( 'data' ); | |
| 172 | + $( this ).data( 'selected-id', value.id ); | |
| 173 | + $( this ).data( 'selected-text', value.text ); | |
| 174 | + } | |
| 175 | + ); | |
| 176 | + } | |
| 177 | + ); | |
| 151 | 178 | |
| 152 | - $( this ).data( 'selected-id', value.id ); | |
| 153 | - $( this ).data( 'selected-text', value.text ); | |
| 154 | - }); | |
| 155 | - }); | |
| 179 | + $( 'select.select2-select.ip_address', $rowsWithSelect2 ).each( | |
| 180 | + function( k, el ) { | |
| 181 | + var $input_ip = $( el ), | |
| 182 | + searchTerm = ''; | |
| 156 | 183 | |
| 157 | - $( '.stream-exclude-list tr:not(.hidden) select.select2-select.ip_address' ).each( function( k, el ) { | |
| 158 | - var $input_ip = $( el ), | |
| 159 | - searchTerm = ''; | |
| 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 = []; | |
| 160 | 203 | |
| 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 = []; | |
| 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 | + } | |
| 179 | 216 | |
| 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 | - } | |
| 217 | + if ( undefined === searchTerm ) { | |
| 218 | + return answer; | |
| 219 | + } | |
| 188 | 220 | |
| 189 | - if ( undefined === searchTerm ) { | |
| 190 | - return answer; | |
| 191 | - } | |
| 221 | + ip_chunks = searchTerm.match( /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ ); | |
| 192 | 222 | |
| 193 | - ip_chunks = searchTerm.match( /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ ); | |
| 223 | + if ( null === ip_chunks ) { | |
| 224 | + return answer; | |
| 225 | + } | |
| 194 | 226 | |
| 195 | - if ( null === ip_chunks ) { | |
| 196 | - return answer; | |
| 197 | - } | |
| 227 | + // remove whole match | |
| 228 | + ip_chunks.shift(); | |
| 198 | 229 | |
| 199 | - // remove whole match | |
| 200 | - ip_chunks.shift(); | |
| 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 | + ); | |
| 201 | 237 | |
| 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 | - ); | |
| 238 | + if ( ip_chunks.length >= 4 ) { | |
| 239 | + answer.results.push( | |
| 240 | + { | |
| 241 | + id: searchTerm, | |
| 242 | + text: searchTerm, | |
| 243 | + } | |
| 244 | + ); | |
| 245 | + } | |
| 209 | 246 | |
| 210 | - if ( ip_chunks.length >= 4 ) { | |
| 211 | - answer.results.push({ | |
| 212 | - id: searchTerm, | |
| 213 | - text: searchTerm | |
| 214 | - }); | |
| 247 | + return answer; | |
| 248 | + }, | |
| 249 | + }, | |
| 250 | + allowClear: false, | |
| 251 | + multiple: true, | |
| 252 | + maximumSelectionSize: 1, | |
| 253 | + placeholder: $input_ip.data( 'placeholder' ), | |
| 254 | + tags: true, | |
| 215 | 255 | } |
| 256 | + ); | |
| 257 | + } | |
| 258 | + ).on( | |
| 259 | + 'change', function() { | |
| 260 | + $( this ).prev( '.select2-container' ).find( 'input.select2-input' ).blur(); | |
| 261 | + } | |
| 262 | + ); | |
| 216 | 263 | |
| 217 | - return answer; | |
| 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; | |
| 218 | 273 | } |
| 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 | - }); | |
| 274 | + } | |
| 275 | + ); | |
| 229 | 276 | |
| 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' ); | |
| 277 | + $( '.exclude_rules_remove_rule_row', $rowsWithSelect2 ).on( | |
| 278 | + 'click', function( e ) { | |
| 279 | + var $thisRow = $( this ).closest( 'tr' ); | |
| 234 | 280 | |
| 235 | - if ( value.length >= 1 ) { | |
| 236 | - $input.blur(); | |
| 237 | - return false; | |
| 238 | - } | |
| 239 | - }); | |
| 281 | + $thisRow.remove(); | |
| 240 | 282 | |
| 241 | - $( '.stream-exclude-list tr:not(.hidden) .exclude_rules_remove_rule_row' ).on( 'click', function() { | |
| 242 | - var $thisRow = $( this ).closest( 'tr' ); | |
| 283 | + recalculate_rules_found(); | |
| 284 | + recalculate_rules_selected(); | |
| 243 | 285 | |
| 244 | - $thisRow.remove(); | |
| 286 | + e.preventDefault(); | |
| 287 | + } | |
| 288 | + ); | |
| 289 | + }; | |
| 245 | 290 | |
| 246 | - recalculate_rules_found(); | |
| 247 | - recalculate_rules_selected(); | |
| 248 | - }); | |
| 249 | - }; | |
| 291 | + initSettingsSelect2( $excludeRows ); | |
| 250 | 292 | |
| 251 | - initSettingsSelect2(); | |
| 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 | + ); | |
| 252 | 299 | |
| 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 | - }); | |
| 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 | + ); | |
| 257 | 312 | |
| 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 | - }); | |
| 313 | + $( '#exclude_rules_new_rule' ).on( | |
| 314 | + 'click', function() { | |
| 315 | + var $newRow = $placeholderRow.clone(); | |
| 268 | 316 | |
| 269 | - $( '#exclude_rules_new_rule' ).on( 'click', function() { | |
| 270 | - var $excludeList = $( 'table.stream-exclude-list' ); | |
| 317 | + $newRow.removeAttr( 'class' ); | |
| 318 | + $newRow.insertBefore( $placeholderRow ); | |
| 271 | 319 | |
| 272 | - $( 'tr:not(.hidden) select.select2-select', $excludeList ).each( function() { | |
| 273 | - $( this ).select2( 'destroy' ); | |
| 274 | - }); | |
| 320 | + initSettingsSelect2( $newRow ); | |
| 321 | + recalculate_rules_found(); | |
| 322 | + recalculate_rules_selected(); | |
| 323 | + } | |
| 324 | + ); | |
| 275 | 325 | |
| 276 | - var $lastRow = $( 'tr', $excludeList ).last(), | |
| 277 | - $newRow = $lastRow.clone(); | |
| 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' ); | |
| 278 | 330 | |
| 279 | - $newRow.removeAttr( 'class' ); | |
| 280 | - $( '.stream-exclude-list tbody :input' ).off(); | |
| 281 | - $( ':input', $newRow ).off().val( '' ); | |
| 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 | + } | |
| 282 | 338 | |
| 283 | - $lastRow.after( $newRow ); | |
| 339 | + $excludeList.find( 'input.cb-select' ).prop( 'checked', false ); | |
| 284 | 340 | |
| 285 | - initSettingsSelect2(); | |
| 341 | + recalculate_rules_found(); | |
| 342 | + recalculate_rules_selected(); | |
| 343 | + } | |
| 344 | + ); | |
| 286 | 345 | |
| 287 | - recalculate_rules_found(); | |
| 288 | - recalculate_rules_selected(); | |
| 289 | - }); | |
| 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[1] ); | |
| 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(); | |
| 290 | 364 | |
| 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' ); | |
| 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 | + } | |
| 294 | 369 | |
| 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 | - } | |
| 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 | + ); | |
| 302 | 380 | |
| 303 | - $excludeList.find( 'input.cb-select' ).prop( 'checked', false ); | |
| 381 | + $( '.stream-exclude-list' ).closest( 'td' ).prev( 'th' ).hide(); | |
| 304 | 382 | |
| 305 | - recalculate_rules_found(); | |
| 306 | - recalculate_rules_selected(); | |
| 307 | - }); | |
| 383 | + $( 'table.stream-exclude-list' ).on( | |
| 384 | + 'click', 'input.cb-select', function() { | |
| 385 | + recalculate_rules_selected(); | |
| 386 | + } | |
| 387 | + ); | |
| 308 | 388 | |
| 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 | - }); | |
| 389 | + function getActions( row, connector ) { | |
| 390 | + var trigger_action = $( '.select2-select.action', row ), | |
| 391 | + action_value = trigger_action.val(); | |
| 327 | 392 | |
| 328 | - $( '.stream-exclude-list' ).closest( 'td' ).prev( 'th' ).hide(); | |
| 393 | + trigger_action.empty(); | |
| 394 | + trigger_action.prop( 'disabled', true ); | |
| 329 | 395 | |
| 330 | - $( 'table.stream-exclude-list' ).on( 'click', 'input.cb-select', function() { | |
| 331 | - recalculate_rules_selected(); | |
| 332 | - }); | |
| 396 | + var placeholder = $( '<option/>', { value: '', text: '' } ); | |
| 397 | + trigger_action.append( placeholder ); | |
| 333 | 398 | |
| 334 | - function getActions( row, connector ) { | |
| 335 | - var trigger_action = $( '.select2-select.action', row ), | |
| 336 | - action_value = trigger_action.val(); | |
| 399 | + var data = { | |
| 400 | + action: 'get_actions', | |
| 401 | + connector: connector, | |
| 402 | + }; | |
| 337 | 403 | |
| 338 | - trigger_action.empty(); | |
| 339 | - trigger_action.prop( 'disabled', true ); | |
| 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 | + } | |
| 340 | 424 | |
| 341 | - var placeholder = $( '<option/>', {value: '', text: ''} ); | |
| 342 | - trigger_action.append( placeholder ); | |
| 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' ); | |
| 343 | 428 | |
| 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; | |
| 429 | + if ( 0 === $selectedRows.length ) { | |
| 430 | + $deleteButton.prop( 'disabled', true ); | |
| 431 | + } else { | |
| 432 | + $deleteButton.prop( 'disabled', false ); | |
| 354 | 433 | } |
| 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 | - } | |
| 434 | + } | |
| 367 | 435 | |
| 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' ); | |
| 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' ); | |
| 371 | 441 | |
| 372 | - if ( 0 === $selectedRows.length ) { | |
| 373 | - $deleteButton.prop( 'disabled', true ); | |
| 374 | - } else { | |
| 375 | - $deleteButton.prop( 'disabled', false ); | |
| 376 | - } | |
| 377 | - } | |
| 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 | + } | |
| 378 | 450 | |
| 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 ); | |
| 451 | + wp_stream_regenerate_alt_rows( $allRows ); | |
| 392 | 452 | } |
| 393 | 453 | |
| 394 | - wp_stream_regenerate_alt_rows( $allRows ); | |
| 454 | + $( document ).ready( | |
| 455 | + function() { | |
| 456 | + recalculate_rules_found(); | |
| 457 | + recalculate_rules_selected(); | |
| 458 | + } | |
| 459 | + ); | |
| 395 | 460 | } |
| 396 | - | |
| 397 | - $( document ).ready( function() { | |
| 398 | - recalculate_rules_found(); | |
| 399 | - recalculate_rules_selected(); | |
| 400 | - }); | |
| 401 | -}); | |
| 461 | +); | |