Skip to content

Commit

Permalink
Use enum value to determine CSS class name
Browse files Browse the repository at this point in the history
As per atrol's suggestion in issue #20068

This simpler approach allows more efficient code and removes need for an
extra API function.
  • Loading branch information
dregad committed Oct 24, 2015
1 parent 94aa005 commit 72ab933
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 42 deletions.
46 changes: 13 additions & 33 deletions core/html_api.php
Expand Up @@ -1431,12 +1431,9 @@ function html_status_legend( $p_display_position, $p_restrict_by_filter = false
$t_status_enum_string = config_get( 'status_enum_string' );
foreach( $t_status_array as $t_status => $t_name ) {
$t_val = isset( $t_status_names[$t_status] ) ? $t_status_names[$t_status] : $t_status_array[$t_status];
$t_status_css = html_get_css_identifier(
MantisEnum::getLabel( $t_status_enum_string, $t_status ),
'color'
);

echo '<td class="small-caption ' . $t_status_css . '">' . $t_val . '</td>';
echo '<td class="small-caption ' . html_get_status_css_class( $t_status ) . '">'
. $t_val . '</td>';
}

echo '</tr>';
Expand Down Expand Up @@ -1474,10 +1471,11 @@ function html_status_percentage_legend() {
$t_percent = ( isset( $t_status_percents[$t_status] ) ? $t_status_percents[$t_status] : 0 );

if( $t_percent > 0 ) {
$t_status_css = html_get_css_identifier(
MantisEnum::getLabel( $t_status_enum_string, $t_status )
);
echo '<td class="small-caption-center ' . $t_status_css . '-color ' . $t_status_css . '-percentage">' . $t_percent . '%</td>';
$t_class = html_get_status_css_class( $t_status );
echo '<td class="small-caption-center '
. $t_class . ' '
. str_replace( 'color', 'percentage', $t_class ) . '">'
. $t_percent . '%</td>';
}
}

Expand Down Expand Up @@ -1911,28 +1909,10 @@ function html_buttons_view_bug_page( $p_bug_id ) {
* Build CSS including project or even user-specific colors ?
*/
function html_get_status_css_class( $p_status, $p_user = null, $p_project = null ) {
$t_identifier = MantisEnum::getLabel(
config_get( 'status_enum_string', null, $p_user, $p_project ),
$p_status
);
return html_get_css_identifier( $t_identifier, 'color' );
}

/**
* Get a CSS-friendly identifier for the given string
* Replace all invalid characters by a dash, remove multiple consecutive dashes
* and append the given suffix.
* This is useful e.g. for dynamic css class names used for status colors.
* @param string $p_string Identifier to convert
* @param string $p_string Suffix to append at end of string
* @return string
*/
function html_get_css_identifier( $p_string, $p_suffix = null ) {
$t_string = string_attribute( strtolower ( $p_string ) );
$t_string = preg_replace( '/[^a-z0-9_-]/', '-', $t_string );
if( $p_suffix ) {
$t_string .= '-' . $p_suffix;
}
$t_string = preg_replace( '/-+/', '-', trim( $t_string, '-' ) );
return $t_string;
$t_status_enum = config_get( 'status_enum_string', null, $p_user, $p_project );
if( MantisEnum::hasValue( $t_status_enum, $p_status ) ) {
return 'status-' . $p_status . '-color';
} else {
return '';
}
}
19 changes: 10 additions & 9 deletions css/status_config.php
Expand Up @@ -75,13 +75,14 @@
$t_color_width = ( $t_color_count > 0 ? ( round( 100/$t_color_count ) ) : 0 );
$t_status_percents = auth_is_user_authenticated() ? get_percentage_by_status() : array();

foreach( $t_statuses AS $t_id=>$t_label ) {
if( array_key_exists( $t_label, $t_colors ) ) {
echo '.' . html_get_css_identifier( $t_label, 'color' )
. " { background-color: {$t_colors[$t_label]}; width: $t_color_width%; }\n";
}
if( array_key_exists( $t_id, $t_status_percents ) ) {
echo '.' . html_get_css_identifier( $t_label, 'percentage' )
. " { width: {$t_status_percents[$t_id]}%; }\n";
}
foreach( $t_statuses as $t_id => $t_label ) {
$t_css_class = html_get_status_css_class( $t_id );

# Status color class
echo '.' . $t_css_class
. " { background-color: {$t_colors[$t_label]}; width: $t_color_width%; }\n";

# Status percentage width class
echo '.' . str_replace( 'color', 'percentage', $t_css_class )
. " { width: {$t_status_percents[$t_id]}%; }\n";
}

0 comments on commit 72ab933

Please sign in to comment.