Add highlighting option to league tables

This commit is contained in:
Brian Miyaji
2014-08-15 22:25:14 +10:00
parent 7defc8d773
commit 47a2aca3f7
3 changed files with 66 additions and 8 deletions

View File

@@ -25,6 +25,16 @@ jQuery(document).ready(function($){
// Activate auto key placeholder
$("#poststuff #title").keyup();
// Radio input toggle
$(".sp-radio-toggle").click(function() {
if($(this).data("sp-checked")) {
$(this).attr("checked", false );
$(this).data("sp-checked", false );
} else {
$(this).data("sp-checked", true );
}
});
// Table switcher
$(".sp-table-panel").siblings(".sp-table-bar").find("a").click(function() {
$(this).closest("li").find("a").addClass("current").closest("li").siblings().find("a").removeClass("current").closest(".sp-table-bar").siblings($(this).attr("href")).show().siblings(".sp-table-panel").hide();

View File

@@ -22,13 +22,15 @@ class SP_Meta_Box_Table_Data {
$table = new SP_League_Table( $post );
list( $columns, $usecolumns, $data, $placeholders, $merged ) = $table->data( true );
$adjustments = $table->adjustments;
self::table( $columns, $usecolumns, $data, $placeholders, $adjustments );
$highlight = get_post_meta( $table->ID, 'sp_highlight', true );
self::table( $columns, $usecolumns, $data, $placeholders, $adjustments, $highlight );
}
/**
* Save meta box data
*/
public static function save( $post_id, $post ) {
update_post_meta( $post_id, 'sp_highlight', sp_array_value( $_POST, 'sp_highlight', array() ) );
update_post_meta( $post_id, 'sp_columns', sp_array_value( $_POST, 'sp_columns', array() ) );
update_post_meta( $post_id, 'sp_adjustments', sp_array_value( $_POST, 'sp_adjustments', array() ) );
update_post_meta( $post_id, 'sp_teams', sp_array_value( $_POST, 'sp_teams', array() ) );
@@ -37,11 +39,12 @@ class SP_Meta_Box_Table_Data {
/**
* Admin edit table
*/
public static function table( $columns = array(), $usecolumns = null, $data = array(), $placeholders = array(), $adjustments = array() ) {
public static function table( $columns = array(), $usecolumns = null, $data = array(), $placeholders = array(), $adjustments = array(), $highlight = null ) {
if ( is_array( $usecolumns ) )
$usecolumns = array_filter( $usecolumns );
$show_team_logo = get_option( 'sportspress_table_show_logos', 'no' ) == 'yes' ? true : false;
?>
<input type="hidden" name="sp_highlight" value="0">
<ul class="subsubsub sp-table-bar">
<li><a href="#sp-table-values" class="current"><?php _e( 'Values', 'sportspress' ); ?></a></li> |
<li><a href="#sp-table-adjustments" class=""><?php _e( 'Adjustments', 'sportspress' ); ?></a></li>
@@ -50,6 +53,7 @@ class SP_Meta_Box_Table_Data {
<table class="widefat sp-data-table sp-league-table">
<thead>
<tr>
<th class="radio"><span class="dashicons sp-icon-shield tips" title="<?php _e( 'Highlight', 'sportspress' ); ?>"></span></th>
<th><?php _e( 'Team', 'sportspress' ); ?></th>
<?php foreach ( $columns as $key => $label ): ?>
<th><label for="sp_columns_<?php echo $key; ?>">
@@ -72,6 +76,7 @@ class SP_Meta_Box_Table_Data {
$default_name = get_the_title( $team_id );
?>
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td><input type="radio" class="sp-radio-toggle" name="sp_highlight" value="<?php echo $team_id; ?>" <?php checked( $highlight, $team_id ); ?>></td>
<td>
<?php if ( $show_team_logo ) echo get_the_post_thumbnail( $team_id, 'sportspress-fit-mini' ); ?>
<span class="sp-default-value">

View File

@@ -21,10 +21,13 @@ $defaults = array(
'responsive' => get_option( 'sportspress_enable_responsive_tables', 'yes' ) == 'yes' ? true : false,
'paginated' => get_option( 'sportspress_table_paginated', 'yes' ) == 'yes' ? true : false,
'rows' => get_option( 'sportspress_table_rows', 10 ),
'highlight' => get_post_meta( get_the_ID(), 'sp_highlight', true ),
);
extract( $defaults, EXTR_SKIP );
if ( empty( $highlight ) ) $highlight = null;
$output = '<h4 class="sp-table-caption">' . get_the_title( $id ) . '</h4>';
$output .= '<div class="sp-table-wrapper' . ( $scrollable ? ' sp-scrollable-table-wrapper' : '' ) . '">';
@@ -57,28 +60,67 @@ endforeach;
$output .= '</tr>' . '</thead>' . '<tbody>';
$i = 0;
$start = 0;
if ( intval( $number ) > 0 )
if ( intval( $number ) > 0 ):
$limit = $number;
foreach( $data as $team_id => $row ):
// Trim table to center around highlighted team
if ( $highlight && sizeof( $data ) > $limit && array_key_exists( $highlight, $data ) ):
// Number of teams in the table
$size = sizeof( $data );
// Position of highlighted team in the table
$key = array_search( $highlight, array_keys( $data ) );
// Get starting position
$start = $key - ceil( $limit / 2 ) + 1;
if ( $start < 0 ) $start = 0;
// Trim table using starting position
$trimmed = array_slice( $data, $start, $limit, true );
// Move starting position if we are too far down the table
if ( sizeof( $trimmed ) < $limit && sizeof( $trimmed ) < $size ):
$offset = $limit - sizeof( $trimmed );
$start -= $offset;
if ( $start < 0 ) $start = 0;
$trimmed = array_slice( $data, $start, $limit, true );
endif;
// Replace data
$data = $trimmed;
endif;
endif;
// Loop through the teams
foreach ( $data as $team_id => $row ):
if ( isset( $limit ) && $i >= $limit ) continue;
$name = sp_array_value( $row, 'name', null );
if ( ! $name ) continue;
$output .= '<tr class="' . ( $i % 2 == 0 ? 'odd' : 'even' ) . '">';
// Generate tags for highlighted team
$before = $after = $class = '';
if ( $highlight == $team_id ):
$before = '<strong>';
$after = '</strong>';
$class = ' highlighted';
endif;
$output .= '<tr class="' . ( $i % 2 == 0 ? 'odd' : 'even' ) . $class . '">';
// Rank
$output .= '<td class="data-rank">' . ( $i + 1 ) . '</td>';
$output .= '<td class="data-rank">' . $before . ( $start + 1 ) . $after . '</td>';
$name_class = '';
if ( $show_team_logo ):
if ( has_post_thumbnail( $team_id ) ):
$logo = get_the_post_thumbnail( $team_id, 'sportspress-fit-icon' );
$name = '<span class="team-logo">' . $logo . '</span>' . $name;
$name = '<span class="team-logo">' . $logo . '</span>' . $before . $name . $after;
$name_class .= ' has-logo';
endif;
endif;
@@ -94,12 +136,13 @@ foreach( $data as $team_id => $row ):
if ( $key == 'name' )
continue;
if ( ! is_array( $columns ) || in_array( $key, $columns ) )
$output .= '<td class="data-' . $key . '">' . sp_array_value( $row, $key, '&mdash;' ) . '</td>';
$output .= '<td class="data-' . $key . '">' . $before . sp_array_value( $row, $key, '&mdash;' ) . $after . '</td>';
endforeach;
$output .= '</tr>';
$i++;
$start++;
endforeach;