| 1 |
/* globals jQuery, streamAlerts, inlineEditPost */ |
| 2 |
jQuery( |
| 3 |
function( $ ) { |
| 4 |
'use strict'; |
| 5 |
var $post_row, |
| 6 |
$edit_row; |
| 7 |
var setupSelectTwo = function setupSelectTwo( id ) { |
| 8 |
var $target = $( id ); |
| 9 |
$target.find( '.select2-select.connector_or_context' ).each( |
| 10 |
function( k, el ) { |
| 11 |
$( el ).select2( |
| 12 |
{ |
| 13 |
allowClear: true, |
| 14 |
placeholder: streamAlerts.anyContext, |
| 15 |
templateResult: function( item ) { |
| 16 |
if ( 'undefined' === typeof item.id ) { |
| 17 |
return item.text; |
| 18 |
} |
| 19 |
if ( -1 === item.id.indexOf( '-' ) ) { |
| 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 ); |
| 26 |
|
| 27 |
if ( null === params.term || '' === $.trim( params.term ) ) { |
| 28 |
return match; |
| 29 |
} |
| 30 |
|
| 31 |
var term = params.term.toLowerCase(); |
| 32 |
|
| 33 |
match.id = match.id.replace( 'blogs', 'sites' ); |
| 34 |
if ( match.id.toLowerCase().indexOf( term ) >= 0 ) { |
| 35 |
return match; |
| 36 |
} |
| 37 |
|
| 38 |
if ( match.children ) { |
| 39 |
for ( var i = match.children.length - 1; i >= 0; i-- ) { |
| 40 |
var child = match.children[i]; |
| 41 |
|
| 42 |
// Remove term from results if it doesn't match. |
| 43 |
if ( -1 === child.id.toLowerCase().indexOf( term ) ) { |
| 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 |
).change( |
| 57 |
function() { |
| 58 |
var value = $( this ).val(); |
| 59 |
if ( value ) { |
| 60 |
var parts = value.split( '-' ); |
| 61 |
$( this ).siblings( '.connector' ).val( parts[0] ); |
| 62 |
$( this ).siblings( '.context' ).val( parts[1] ); |
| 63 |
} |
| 64 |
} |
| 65 |
); |
| 66 |
|
| 67 |
var parts = [ |
| 68 |
$( el ).siblings( '.connector' ).val(), |
| 69 |
$( el ).siblings( '.context' ).val(), |
| 70 |
]; |
| 71 |
if ( '' === parts[1] ) { |
| 72 |
parts.splice( 1, 1 ); |
| 73 |
} |
| 74 |
$( el ).val( parts.join( '-' ) ).trigger( 'change' ); |
| 75 |
} |
| 76 |
); |
| 77 |
|
| 78 |
$target.find( 'select.select2-select:not(.connector_or_context)' ).each( |
| 79 |
function() { |
| 80 |
var element_id_split = $( this ).attr( 'id' ).split( '_' ); |
| 81 |
var select_name = element_id_split[element_id_split.length - 1].charAt( 0 ).toUpperCase() + |
| 82 |
element_id_split[element_id_split.length - 1].slice( 1 ); |
| 83 |
$( this ).select2( |
| 84 |
{ |
| 85 |
allowClear: true, |
| 86 |
placeholder: streamAlerts.any + ' ' + select_name, |
| 87 |
} |
| 88 |
); |
| 89 |
} |
| 90 |
); |
| 91 |
}; |
| 92 |
var $alertSettingSelect = $( '#wp_stream_alert_type' ); |
| 93 |
|
| 94 |
var loadAlertSettings = function( alert_type ) { |
| 95 |
var data = { |
| 96 |
action: 'load_alerts_settings', |
| 97 |
alert_type: alert_type, |
| 98 |
}; |
| 99 |
|
| 100 |
var $alert_edit_row = $( '#wp_stream_alert_type' ).closest( 'tr' ); |
| 101 |
var row_id = $alert_edit_row.attr( 'id' ); |
| 102 |
data.post_id = row_id.split( '-' )[1]; |
| 103 |
$.post( |
| 104 |
window.ajaxurl, data, function( response ) { |
| 105 |
var $alert_type_settings = $( '#wp_stream_alert_type_form' ); |
| 106 |
var alert_type_value = $( '#wp_stream_alert_type' ).val(); |
| 107 |
if ( 'none' === alert_type_value ) { |
| 108 |
$alert_type_settings.hide(); |
| 109 |
return; |
| 110 |
} |
| 111 |
$alert_type_settings.html( response.data.html ); |
| 112 |
$alert_type_settings.show(); |
| 113 |
} |
| 114 |
); |
| 115 |
}; |
| 116 |
|
| 117 |
$( '#the-list' ).on( |
| 118 |
'change', '#wp_stream_trigger_connector_or_context', function() { |
| 119 |
if ( 'wp_stream_trigger_connector_or_context' === $( this ).attr( 'id' ) ) { |
| 120 |
var connector = $( this ).val(); |
| 121 |
if ( connector && 0 < connector.indexOf( '-' ) ) { |
| 122 |
var connector_split = connector.split( '-' ); |
| 123 |
connector = connector_split[0]; |
| 124 |
} |
| 125 |
getActions( connector ); |
| 126 |
} |
| 127 |
} |
| 128 |
); |
| 129 |
|
| 130 |
var getActions = function( connector ) { |
| 131 |
var trigger_action = $( '#wp_stream_trigger_action' ); |
| 132 |
trigger_action.empty(); |
| 133 |
trigger_action.prop( 'disabled', true ); |
| 134 |
|
| 135 |
var placeholder = $( '<option/>', { value: '', text: '' } ); |
| 136 |
trigger_action.append( placeholder ); |
| 137 |
|
| 138 |
var data = { |
| 139 |
action: 'get_actions', |
| 140 |
connector: connector, |
| 141 |
}; |
| 142 |
|
| 143 |
$.post( |
| 144 |
window.ajaxurl, data, function( response ) { |
| 145 |
var success = response.success, |
| 146 |
actions = response.data; |
| 147 |
if ( ! success ) { |
| 148 |
return; |
| 149 |
} |
| 150 |
for ( var key in actions ) { |
| 151 |
if ( actions.hasOwnProperty( key ) ) { |
| 152 |
var value = actions[key]; |
| 153 |
var option = $( '<option/>', { value: key, text: value } ); |
| 154 |
trigger_action.append( option ); |
| 155 |
} |
| 156 |
} |
| 157 |
trigger_action.prop( 'disabled', false ); |
| 158 |
$( document ).trigger( 'alert-actions-updated' ); |
| 159 |
} |
| 160 |
); |
| 161 |
}; |
| 162 |
|
| 163 |
$alertSettingSelect.change( |
| 164 |
function() { |
| 165 |
loadAlertSettings( $( this ).val() ); |
| 166 |
} |
| 167 |
); |
| 168 |
|
| 169 |
$( '#wpbody-content' ).on( |
| 170 |
'click', 'a.page-title-action', function( e ) { |
| 171 |
e.preventDefault(); |
| 172 |
$( '#add-new-alert' ).remove(); |
| 173 |
if ( $( '.inline-edit-wp_stream_alerts' ).length > 0 ) { |
| 174 |
$( '.inline-edit-wp_stream_alerts .inline-edit-save button.button-secondary.cancel' ).trigger( 'click' ); |
| 175 |
} |
| 176 |
var alert_form_html = ''; |
| 177 |
var data = { |
| 178 |
action: 'get_new_alert_triggers_notifications', |
| 179 |
}; |
| 180 |
$.post( |
| 181 |
window.ajaxurl, data, function( response ) { |
| 182 |
if ( true === response.success ) { |
| 183 |
alert_form_html = response.data.html; |
| 184 |
$( 'tbody#the-list' ).prepend( '<tr id="add-new-alert" class="inline-edit-row inline-edit-row-page inline-edit-page quick-edit-row quick-edit-row-page inline-edit-page inline-editor" style=""><td colspan="4" class="colspanchange">' + alert_form_html + '<p class="submit inline-edit-save"> <button type="button" class="button-secondary cancel alignleft">Cancel</button> <input type="hidden" id="_inline_edit" name="_inline_edit" value="3550d271fe"> <button type="button" class="button-primary save alignright">Save</button> <span class="spinner"></span><span class="error" style="display:none"></span> <br class="clear"></p></td></tr>' ); |
| 185 |
var add_new_alert = $( '#add-new-alert' ); |
| 186 |
var current_bg_color = add_new_alert.css( 'background-color' ); |
| 187 |
|
| 188 |
// Color taken from /wp-admin/css/forms.css |
| 189 |
// #pass-strength-result.strong |
| 190 |
add_new_alert.css( 'background-color', '#C1E1B9' ); |
| 191 |
setTimeout( |
| 192 |
function() { |
| 193 |
add_new_alert.css( 'background-color', current_bg_color ); |
| 194 |
}, 250 |
| 195 |
); |
| 196 |
|
| 197 |
$( '#wp_stream_alert_type' ).change( |
| 198 |
function() { |
| 199 |
loadAlertSettings( $( this ).val() ); |
| 200 |
} |
| 201 |
); |
| 202 |
add_new_alert.on( |
| 203 |
'click', '.button-secondary.cancel', function() { |
| 204 |
$( '#add-new-alert' ).remove(); |
| 205 |
} |
| 206 |
); |
| 207 |
add_new_alert.on( 'click', '.button-primary.save', save_new_alert ); |
| 208 |
|
| 209 |
setupSelectTwo( '#add-new-alert' ); |
| 210 |
} |
| 211 |
} |
| 212 |
); |
| 213 |
} |
| 214 |
); |
| 215 |
var save_new_alert = function save_new_alert( e ) { |
| 216 |
e.preventDefault(); |
| 217 |
$( '#add-new-alert' ).find( 'p.submit.inline-edit-save span.spinner' ).css( 'visibility', 'visible' ); |
| 218 |
var data = { |
| 219 |
action: 'save_new_alert', |
| 220 |
wp_stream_alerts_nonce: $( '#wp_stream_alerts_nonce' ).val(), |
| 221 |
wp_stream_trigger_author: $( '#wp_stream_trigger_author' ).val(), |
| 222 |
wp_stream_trigger_context: $( '#wp_stream_trigger_connector_or_context' ).val(), |
| 223 |
wp_stream_trigger_action: $( '#wp_stream_trigger_action' ).val(), |
| 224 |
wp_stream_alert_type: $( '#wp_stream_alert_type' ).val(), |
| 225 |
wp_stream_alert_status: $( '#wp_stream_alert_status' ).val(), |
| 226 |
}; |
| 227 |
$( '#wp_stream_alert_type_form' ).find( ':input' ).each( |
| 228 |
function() { |
| 229 |
var alert_type_data_id = $( this ).attr( 'id' ); |
| 230 |
if ( $( this ).val() ) { |
| 231 |
data[alert_type_data_id] = $( this ).val(); |
| 232 |
} |
| 233 |
} |
| 234 |
); |
| 235 |
|
| 236 |
$.post( |
| 237 |
window.ajaxurl, data, function( response ) { |
| 238 |
if ( true === response.success ) { |
| 239 |
$( '#add-new-alert' ).find( 'p.submit.inline-edit-save span.spinner' ).css( 'visibility', 'hidden' ); |
| 240 |
location.reload(); |
| 241 |
} |
| 242 |
} |
| 243 |
); |
| 244 |
}; |
| 245 |
|
| 246 |
// we create a copy of the WP inline edit post function |
| 247 |
var $wp_inline_edit = inlineEditPost.edit; |
| 248 |
|
| 249 |
// and then we overwrite the function with our own code |
| 250 |
inlineEditPost.edit = function( id ) { |
| 251 |
// "call" the original WP edit function |
| 252 |
// we don't want to leave WordPress hanging |
| 253 |
$wp_inline_edit.apply( this, arguments ); |
| 254 |
|
| 255 |
// now we take care of our business |
| 256 |
|
| 257 |
// get the post ID |
| 258 |
var post_id = 0; |
| 259 |
if ( typeof ( id ) === 'object' ) { |
| 260 |
post_id = parseInt( this.getId( id ), 10 ); |
| 261 |
} |
| 262 |
|
| 263 |
if ( post_id > 0 ) { |
| 264 |
// define the edit row |
| 265 |
$edit_row = $( '#edit-' + post_id ); |
| 266 |
$post_row = $( '#post-' + post_id ); |
| 267 |
|
| 268 |
// get the data |
| 269 |
var alert_trigger_connector = $post_row.find( 'input[name="wp_stream_trigger_connector"]' ).val(); |
| 270 |
var alert_trigger_context = $post_row.find( 'input[name="wp_stream_trigger_context"]' ).val(); |
| 271 |
var alert_trigger_connector_context = alert_trigger_connector + '-' + alert_trigger_context; |
| 272 |
var alert_trigger_action = $post_row.find( 'input[name="wp_stream_trigger_action"]' ).val(); |
| 273 |
var alert_status = $post_row.find( 'input[name="wp_stream_alert_status"]' ).val(); |
| 274 |
|
| 275 |
// populate the data |
| 276 |
$edit_row.find( 'input[name="wp_stream_trigger_connector"]' ).attr( 'value', alert_trigger_connector ); |
| 277 |
$edit_row.find( 'input[name="wp_stream_trigger_context"]' ).attr( 'value', alert_trigger_context ); |
| 278 |
$edit_row.find( 'select[name="wp_stream_trigger_connector_or_context"] option[value="' + alert_trigger_connector_context + '"]' ).attr( 'selected', 'selected' ); |
| 279 |
$( document ).one( |
| 280 |
'alert-actions-updated', function() { |
| 281 |
$edit_row.find( 'input[name="wp_stream_trigger_action"]' ).attr( 'value', alert_trigger_action ); |
| 282 |
$edit_row.find( 'select[name="wp_stream_trigger_action"] option[value="' + alert_trigger_action + '"]' ).attr( 'selected', 'selected' ).trigger( 'change' ); |
| 283 |
} |
| 284 |
); |
| 285 |
$edit_row.find( 'select[name="wp_stream_alert_status"] option[value="' + alert_status + '"]' ).attr( 'selected', 'selected' ); |
| 286 |
setupSelectTwo( '#edit-' + post_id ); |
| 287 |
|
| 288 |
// Alert type handling |
| 289 |
$( '#wp_stream_alert_type_form' ).hide(); |
| 290 |
var alert_type = $post_row.find( 'input[name="wp_stream_alert_type"]' ).val(); |
| 291 |
$edit_row.find( 'select[name="wp_stream_alert_type"] option[value="' + alert_type + '"]' ).attr( 'selected', 'selected' ).trigger( 'change' ); |
| 292 |
} |
| 293 |
}; |
| 294 |
if ( window.location.hash ) { |
| 295 |
var $target_post_row = $( window.location.hash ); |
| 296 |
if ( $target_post_row.length ) { |
| 297 |
var scroll_to_position = $target_post_row.offset().top - $( '#wpadminbar' ).height(); |
| 298 |
$( 'html, body' ).animate( |
| 299 |
{ |
| 300 |
scrollTop: scroll_to_position, |
| 301 |
}, 1000 |
| 302 |
); |
| 303 |
$target_post_row.find( '.row-actions a.editinline' ).trigger( 'click' ); |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
); |
| 308 |
|