N°3882 - Header dynamic: Pills now shows the real color from the DM

This commit is contained in:
Molkobain
2021-09-23 09:25:16 +02:00
parent 197864ff83
commit 1cb100b010
13 changed files with 139 additions and 74 deletions

View File

@@ -24,8 +24,8 @@ class Pill extends UIContentBlock
public const BLOCK_CODE = 'ibo-pill';
public const DEFAULT_HTML_TEMPLATE_REL_PATH = 'base/components/pill/layout';
/** @var string */
protected $sColor;
/** @var null|string CSS class that will be used on the block */
protected $sCSSColorClass;
/** @var null|string URL to go to if the pill is clicked */
protected $sUrl;
/** @var null|string Text to display as a tooltip */
@@ -34,30 +34,48 @@ class Pill extends UIContentBlock
/**
* Pill constructor.
*
* @param string $sColor
* @param string|null $sSemanticColor Semantic color code such as "success", "failure", "active", ... {@see css/backoffice/components/_pill.scss}
*/
public function __construct(string $sColor)
public function __construct(?string $sSemanticColor = null)
{
parent::__construct();
$this->SetColor($sColor);
if (is_null($sSemanticColor) === false) {
$this->SetSemanticColor($sSemanticColor);
}
}
/**
* @see static::$sCSSColorClass
* @return string
*/
public function GetColor(): ?string
public function GetCSSColorClass(): ?string
{
return $this->sColor;
return $this->sCSSColorClass;
}
/**
* @param string $sColor
* @param string $sCSSColorClass
*
* @see static::$sCSSColorClass
* @return $this
*/
public function SetCSSColorClass(string $sCSSColorClass)
{
$this->sCSSColorClass = $sCSSColorClass;
return $this;
}
/**
* @param string $sSemanticColor Semantic color code such as "success", "failure", "active", ... {@see css/backoffice/components/_pill.scss}
*
* @see static::$sCSSColorClass
* @return Pill
*/
public function SetColor(string $sColor): Pill
public function SetSemanticColor(string $sSemanticColor)
{
$this->sColor = $sColor;
$this->sCSSColorClass = 'ibo-is-'.$sSemanticColor;
return $this;
}

View File

@@ -9,6 +9,7 @@ namespace Combodo\iTop\Application\UI\Base\Component\Pill;
use Combodo\iTop\Application\UI\Helper\UIHelper;
use MetaModel;
/**
* Class PillFactory
@@ -25,12 +26,21 @@ class PillFactory
* @param string $sStateCode
*
* @return \Combodo\iTop\Application\UI\Base\Component\Pill\Pill
* @throws \CoreException
*/
public static function MakeForState(string $sClass, string $sStateCode)
{
$sColor = UIHelper::GetColorFromStatus($sClass, $sStateCode);
$oPill = new Pill();
return new Pill($sColor);
// First we try to apply style defined in the DM if any, otherwise we fallback on the default colors
$oStyle = MetaModel::GetEnumStyle($sClass, MetaModel::GetStateAttributeCode($sClass), $sStateCode);
if ($oStyle !== null) {
$oPill->SetCSSColorClass($oStyle->GetStyleClass());
} else {
$oPill->SetSemanticColor(UIHelper::GetColorFromStatusCode($sStateCode));
}
return $oPill;
}
}

View File

@@ -8,10 +8,6 @@
namespace Combodo\iTop\Application\UI\Helper;
use MetaModel;
// TODO 3.0.0: Delete this class as it is only a temporary helper while code is being reworked.
/**
* Class UIHelper
*
@@ -22,71 +18,62 @@ use MetaModel;
*/
class UIHelper
{
public static function GetColorFromStatus(string $sClass, ?string $sStateCode): string
/**
* @param string $sStateCode Code of the state value
* @param bool $bAllowFallbackIfNoMatch If set to true, a fallback semantic color code will be returned in case of no matching mappping. Otherwise it will return null to indicate there was no match.
*
* @return string|null A semantic status color code (eg. success, pending, failure, neutral, ...) depending on the value's code. Usefull to try to find a semantic match when a class has no style defined on its state attribute.
*/
public static function GetColorFromStatusCode(string $sStateCode, bool $bAllowFallbackIfNoMatch = true): ?string
{
// Example on how to get the color for the current status of a class
// $sStatusColor = 'neutral';
// $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
// if (strlen($sStateAttCode) == 0) {
// return $sStatusColor;
// }
//
// $oStyle = MetaModel::GetEnumStyle($sClass, $sStateAttCode, $sStateCode);
// if ($oStyle) {
// $sStatusColor = $oStyle->GetMainColor();
// }
// return $sStatusColor;
$sStatusColor = null;
$sRootClass = MetaModel::GetRootClass($sClass);
switch ($sRootClass) {
case 'Ticket':
// TODO 3.0.0 : Dehardcode this
switch ($sStateCode) {
case 'new':
$sStatusColor = 'new';
break;
switch ($sStateCode) {
case 'active':
$sStatusColor = 'active';
break;
case 'waiting_for_approval':
case 'pending':
$sStatusColor = 'waiting';
break;
case 'inactive':
$sStatusColor = 'inactive';
break;
case 'escalated_tto':
case 'escalated_ttr':
case 'rejected':
$sStatusColor = 'failure';
break;
case 'new':
$sStatusColor = 'new';
break;
case 'resolved':
$sStatusColor = 'success';
break;
case 'waiting_for_approval':
case 'pending':
$sStatusColor = 'waiting';
break;
case 'closed':
$sStatusColor = 'frozen';
break;
case 'escalated_tto':
case 'escalated_ttr':
case 'rejected':
$sStatusColor = 'failure';
break;
case 'approved':
case 'assigned':
case 'dispatched':
case 'redispatched':
default:
$sStatusColor = 'neutral';
break;
case 'resolved':
$sStatusColor = 'success';
break;
case 'closed':
$sStatusColor = 'frozen';
break;
case 'approved':
case 'assigned':
case 'dispatched':
case 'redispatched':
$sStatusColor = 'neutral';
break;
default:
if ($bAllowFallbackIfNoMatch) {
$sStatusColor = 'neutral';
}
break;
default:
switch ($sStateCode) {
case 'active':
$sStatusColor = 'active';
break;
case 'inactive':
$sStatusColor = 'inactive';
break;
default:
$sStatusColor = 'neutral';
break;
}
}
return $sStatusColor;
}
}