Add inline result editing

This commit is contained in:
Brian Miyaji
2014-11-23 13:32:19 +11:00
parent fcf398420e
commit 7319549abc
4 changed files with 118 additions and 6 deletions

View File

@@ -60,8 +60,8 @@
content: "\f504"; content: "\f504";
} }
.fixed .column-sp_team .result, .fixed .column-sp_team .sp-result,
.sp-calendar-table .result { .sp-calendar-table .sp-result {
background: #888; background: #888;
color: #fff; color: #fff;
font-size: 11px; font-size: 11px;
@@ -77,11 +77,16 @@
cursor: pointer; cursor: pointer;
} }
.fixed .column-sp_team .result:hover, .fixed .column-sp_team .sp-result:hover,
.sp-calendar-table .result:hover { .sp-calendar-table .sp-result:hover {
background: #2ea2cc; background: #2ea2cc;
} }
.fixed .column-sp_team .sp-edit-result {
font-size: 11px;
line-height: 1.4em;
}
.fixed .column-sp_format, .fixed .column-sp_format,
.fixed .column-sp_icon, .fixed .column-sp_icon,
.fixed .column-sp_number { .fixed .column-sp_number {

View File

@@ -547,6 +547,60 @@ jQuery(document).ready(function($){
}); });
$(".sp-date-selector select").trigger("change"); $(".sp-date-selector select").trigger("change");
// Edit inline results
$("#the-list").on("click", ".sp-result, .sp-edit-results", function(){
team = $(this).data("team");
$column = $(this).closest(".column-sp_team");
$column.find(".sp-result, .sp-row-actions").hide();
$column.find(".sp-edit-result, .sp-inline-edit-save").show();
if ( team != undefined ) {
$column.find(".sp-edit-result[data-team='"+team+"']").select();
}
return false;
});
// Cancel inline results
$("#the-list").on("click", ".sp-inline-edit-save .cancel", function(){
$column = $(this).closest(".column-sp_team");
$column.find(".sp-edit-result, .sp-inline-edit-save").hide();
$column.find(".sp-result, .sp-row-actions").show();
return false;
});
// Save inline results
$("#the-list").on("click", ".sp-inline-edit-save .save", function(){
$column = $(this).closest(".column-sp_team");
results = [];
$column.find(".sp-edit-result").each(function() {
team = {};
team.id = $(this).data("team");
team.key = $(this).data("key");
team.result = $(this).val();
results.push( team );
});
$.post( ajaxurl, {
action: "sp-save-inline-results",
post_id: $column.find("input[name='sp_post_id']").val(),
results: results,
nonce: $("#sp-inline-nonce").val()
}, function(response) {
$column.find(".sp-edit-result").each(function() {
$column.find(".sp-result[data-team='"+$(this).data("team")+"']").html($(this).val());
});
$column.find(".sp-edit-result, .sp-inline-edit-save").hide();
$column.find(".sp-result, .sp-row-actions").show();
return false;
});
});
// Override inline form submission
$("#the-list").on("keypress", ".sp-edit-result", function(e) {
if ( e.which == 13 ) {
$(this).closest(".column-sp_team").find(".sp-inline-edit-save .save").trigger("click");
return false;
}
});
// Fitvids // Fitvids
$(".sp-fitvids").fitVids(); $(".sp-fitvids").fitVids();
}); });

View File

@@ -19,6 +19,7 @@ class SP_Admin_AJAX {
*/ */
public function __construct() { public function __construct() {
add_action( 'wp_ajax_sp-save-primary-result', array( $this, 'save_primary_result' ), 1 ); add_action( 'wp_ajax_sp-save-primary-result', array( $this, 'save_primary_result' ), 1 );
add_action( 'wp_ajax_sp-save-inline-results', array( $this, 'save_inline_results' ) );
} }
/** /**
@@ -34,6 +35,40 @@ class SP_Admin_AJAX {
update_option( 'sportspress_primary_result', $primary_result ); update_option( 'sportspress_primary_result', $primary_result );
wp_send_json_success(); wp_send_json_success();
} }
/**
* Save event results inline.
*
* @since 1.5
*/
function save_inline_results() {
check_ajax_referer( 'sp-save-inline-results', 'nonce' );
$post_id = sp_array_value( $_POST, 'post_id' );
$results = sp_array_value( $_POST, 'results' );
if ( ! $post_id || ! is_array( $results ) ) {
// Return error
wp_send_json_error();
}
// Get current results meta
$meta = get_post_meta( $post_id, 'sp_results', true );
foreach ( $results as $result ) {
$id = sp_array_value( $result, 'id' );
$key = sp_array_value( $result, 'key' );
if ( ! $id || ! $key ) continue;
$meta[ $id ][ $key ] = sp_array_value( $result, 'result' );
}
// Update results
update_post_meta( $post_id, 'sp_results', $meta );
// Return success
wp_send_json_success();
}
} }
return new SP_Admin_AJAX(); return new SP_Admin_AJAX();

View File

@@ -127,6 +127,7 @@ class SP_Admin_CPT_Event extends SP_Admin_CPT {
else: else:
$results = get_post_meta( $post_id, 'sp_results', true ); $results = get_post_meta( $post_id, 'sp_results', true );
$main_result = get_option( 'sportspress_primary_result', null ); $main_result = get_option( 'sportspress_primary_result', null );
echo '<input type="hidden" name="sp_post_id" value="' . $post_id . '">';
foreach( $teams as $team_id ): foreach( $teams as $team_id ):
if ( ! $team_id ) continue; if ( ! $team_id ) continue;
$team = get_post( $team_id ); $team = get_post( $team_id );
@@ -140,20 +141,37 @@ class SP_Admin_CPT_Event extends SP_Admin_CPT {
if ( is_array( $team_results ) ): if ( is_array( $team_results ) ):
end( $team_results ); end( $team_results );
$team_result = prev( $team_results ); $team_result = prev( $team_results );
$main_result = key( $team_results );
else: else:
$team_result = null; $team_result = null;
endif; endif;
endif; endif;
if ( $team_result != null ): if ( is_array( $team_results ) ):
unset( $team_results['outcome'] ); unset( $team_results['outcome'] );
$team_results = implode( ' | ', $team_results ); $team_results = implode( ' | ', $team_results );
echo '<a class="result tips" title="' . $team_results . '" href="' . get_edit_post_link( $post_id ) . '">' . $team_result . '</a> ';
endif; endif;
if ( $team_result == null ) $team_result = '-';
echo '<a class="sp-result tips" title="' . $team_results . '" data-team="' . $team_id . '" href="#">' . $team_result . '</a>';
echo '<input type="text" class="sp-edit-result hidden small-text" data-team="' . $team_id . '" data-key="' . $main_result . '" value="' . $team_result . '"> ';
echo $team->post_title; echo $team->post_title;
echo '<br>'; echo '<br>';
endif; endif;
endforeach; endforeach;
?>
<div class="row-actions sp-row-actions"><span class="inline hide-if-no-js"><a href="#" class="sp-edit-results"><?php _e( 'Edit Results', 'sportspress' ); ?></a></span></div>
<p class="inline-edit-save sp-inline-edit-save hidden">
<a href="#inline-edit" class="button-secondary cancel alignleft"><?php _e( 'Cancel' ); ?></a>
<?php wp_nonce_field( 'sp-save-inline-results', 'sp-inline-nonce', false ); ?>
<a href="#inline-edit" class="button-primary save alignright"><?php _e( 'Update' ); ?></a>
<span class="spinner"></span>
<input type="hidden" name="post_view" value="<?php echo esc_attr( $m ); ?>" />
<input type="hidden" name="screen" value="<?php echo esc_attr( $screen->id ); ?>" />
<span class="error" style="display:none"></span>
<br class="clear" />
</p>
<?php
endif; endif;
break; break;
case 'sp_league': case 'sp_league':