/* globals jQuery, streamAlerts, inlineEditPost */ jQuery( function( $ ) { 'use strict'; var $post_row, $edit_row; var setupSelectTwo = function setupSelectTwo( id ) { var $target = $( id ); $target.find( '.select2-select.connector_or_context' ).each( function( k, el ) { $( el ).select2( { allowClear: true, placeholder: streamAlerts.anyContext, templateResult: function( item ) { if ( 'undefined' === typeof item.id ) { return item.text; } if ( -1 === item.id.indexOf( '-' ) ) { return $( '' + item.text + '' ); } return $( '' + item.text + '' ); }, matcher: function( params, data ) { var match = $.extend( true, {}, data ); if ( null === params.term || '' === $.trim( params.term ) ) { return match; } var term = params.term.toLowerCase(); match.id = match.id.replace( 'blogs', 'sites' ); if ( match.id.toLowerCase().indexOf( term ) >= 0 ) { return match; } if ( match.children ) { for ( var i = match.children.length - 1; i >= 0; i-- ) { var child = match.children[i]; // Remove term from results if it doesn't match. if ( -1 === child.id.toLowerCase().indexOf( term ) ) { match.children.splice( i, 1 ); } } if ( match.children.length > 0 ) { return match; } } return null; }, } ).change( function() { var value = $( this ).val(); if ( value ) { var parts = value.split( '-' ); $( this ).siblings( '.connector' ).val( parts[0] ); $( this ).siblings( '.context' ).val( parts[1] ); } } ); var parts = [ $( el ).siblings( '.connector' ).val(), $( el ).siblings( '.context' ).val(), ]; if ( '' === parts[1] ) { parts.splice( 1, 1 ); } $( el ).val( parts.join( '-' ) ).trigger( 'change' ); } ); $target.find( 'select.select2-select:not(.connector_or_context)' ).each( function() { var element_id_split = $( this ).attr( 'id' ).split( '_' ); var select_name = element_id_split[element_id_split.length - 1].charAt( 0 ).toUpperCase() + element_id_split[element_id_split.length - 1].slice( 1 ); $( this ).select2( { allowClear: true, placeholder: streamAlerts.any + ' ' + select_name, } ); } ); }; var $alertSettingSelect = $( '#wp_stream_alert_type' ); var loadAlertSettings = function( alert_type ) { var data = { action: 'load_alerts_settings', alert_type: alert_type, }; var $alert_edit_row = $( '#wp_stream_alert_type' ).closest( 'tr' ); var row_id = $alert_edit_row.attr( 'id' ); data.post_id = row_id.split( '-' )[1]; $.post( window.ajaxurl, data, function( response ) { var $alert_type_settings = $( '#wp_stream_alert_type_form' ); var alert_type_value = $( '#wp_stream_alert_type' ).val(); if ( 'none' === alert_type_value ) { $alert_type_settings.hide(); return; } $alert_type_settings.html( response.data.html ); $alert_type_settings.show(); } ); }; $( '#the-list' ).on( 'change', '#wp_stream_trigger_connector_or_context', function() { if ( 'wp_stream_trigger_connector_or_context' === $( this ).attr( 'id' ) ) { var connector = $( this ).val(); if ( connector && 0 < connector.indexOf( '-' ) ) { var connector_split = connector.split( '-' ); connector = connector_split[0]; } getActions( connector ); } } ); var getActions = function( connector ) { var trigger_action = $( '#wp_stream_trigger_action' ); trigger_action.empty(); trigger_action.prop( 'disabled', true ); var placeholder = $( '', { value: '', text: '' } ); trigger_action.append( placeholder ); var data = { action: 'get_actions', connector: connector, }; $.post( window.ajaxurl, data, function( response ) { var success = response.success, actions = response.data; if ( ! success ) { return; } for ( var key in actions ) { if ( actions.hasOwnProperty( key ) ) { var value = actions[key]; var option = $( '', { value: key, text: value } ); trigger_action.append( option ); } } trigger_action.prop( 'disabled', false ); $( document ).trigger( 'alert-actions-updated' ); } ); }; $alertSettingSelect.change( function() { loadAlertSettings( $( this ).val() ); } ); $( '#wpbody-content' ).on( 'click', 'a.page-title-action', function( e ) { e.preventDefault(); $( '#add-new-alert' ).remove(); if ( $( '.inline-edit-wp_stream_alerts' ).length > 0 ) { $( '.inline-edit-wp_stream_alerts .inline-edit-save button.button-secondary.cancel' ).trigger( 'click' ); } var alert_form_html = ''; var data = { action: 'get_new_alert_triggers_notifications', }; $.post( window.ajaxurl, data, function( response ) { if ( true === response.success ) { alert_form_html = response.data.html; $( 'tbody#the-list' ).prepend( '