N°2847 - Button: Add factory, color constants and refactor existing code to use the factory

This commit is contained in:
Molkobain
2020-08-26 15:58:18 +02:00
parent d5f44ffd7b
commit 61ec7f8053
7 changed files with 471 additions and 141 deletions

View File

@@ -17,8 +17,7 @@
* You should have received a copy of the GNU Affero General Public License
*/
namespace Combodo\iTop\Application\UI\Component\Button\Button;
namespace Combodo\iTop\Application\UI\Component\Button;
use Combodo\iTop\Application\UI\UIBlock;
@@ -27,7 +26,7 @@ use Combodo\iTop\Application\UI\UIBlock;
* Class Button
*
* @author Stephen Abello <stephen.abello@combodo.com>
* @package Combodo\iTop\Application\UI\Component\Button\Button
* @package Combodo\iTop\Application\UI\Component\Button
* @since 2.8.0
*/
class Button extends UIBlock
@@ -37,19 +36,55 @@ class Button extends UIBlock
const HTML_TEMPLATE_REL_PATH = 'components/button/layout';
const JS_TEMPLATE_REL_PATH = 'components/button/layout';
// Specific constants
/** @var string ENUM_TYPE_BUTTON */
const ENUM_TYPE_BUTTON = 'button';
/** @var string ENUM_TYPE_SUBMIT */
const ENUM_TYPE_SUBMIT = 'submit';
/** @var string ENUM_TYPE_RESET */
const ENUM_TYPE_RESET = 'reset';
/** @var string DEFAULT_TYPE */
const DEFAULT_TYPE = self::ENUM_TYPE_BUTTON;
/** @var string ENUM_ACTION_TYPE_REGULAR */
const ENUM_ACTION_TYPE_REGULAR = 'regular';
/** @var string ENUM_ACTION_TYPE_ALTERNATIVE */
const ENUM_ACTION_TYPE_ALTERNATIVE = 'alternative';
/** @var string DEFAULT_ACTION_TYPE */
const DEFAULT_ACTION_TYPE = self::ENUM_ACTION_TYPE_REGULAR;
/** @var string ENUM_COLOR_NEUTRAL */
const ENUM_COLOR_NEUTRAL = 'neutral';
/** @var string ENUM_COLOR_VALIDATION */
const ENUM_COLOR_VALIDATION = 'green';
/** @var string ENUM_COLOR_DESTRUCTIVE */
const ENUM_COLOR_DESTRUCTIVE = 'red';
/** @var string ENUM_COLOR_PRIMARY */
const ENUM_COLOR_PRIMARY = 'primary';
/** @var string ENUM_COLOR_SECONDARY */
const ENUM_COLOR_SECONDARY = 'secondary';
/** @var string ENUM_COLOR_GREEN */
const ENUM_COLOR_GREEN = 'green';
/** @var string ENUM_COLOR_RED */
const ENUM_COLOR_RED = 'red';
/** @var string ENUM_COLOR_CYAN */
const ENUM_COLOR_CYAN = 'cyan';
/** @var string DEFAULT_COLOR */
const DEFAULT_COLOR = self::ENUM_COLOR_NEUTRAL;
/** @var string $sLabel */
protected $sLabel;
/** @var string $sType */
/** @var string $sType The HTML type of the button (eg. 'submit', 'button', ...) */
protected $sType;
/** @var string $sName */
/** @var string $sName The HTML name of the button, used by forms */
protected $sName;
/** @var string $sValue */
/** @var string $sValue The HTML value of the button, used by forms */
protected $sValue;
/** @var string $sTooltip */
protected $sTooltip;
/** @var string $sIconClass */
protected $sIconClass;
/** @var string $sActionType */
/** @var string $sActionType The type of action, a 'regular' action or a 'misc' action */
protected $sActionType;
/** @var string $sColor */
protected $sColor;
@@ -61,8 +96,8 @@ class Button extends UIBlock
/**
* Button constructor.
*
* @param string $sId
* @param string $sLabel
* @param string|null $sId
* @param string $sName
* @param string $sValue
* @param string $sType
@@ -73,8 +108,10 @@ class Button extends UIBlock
* @param string $sJsCode
* @param string $sOnClickJsCode
*/
public function __construct($sId, $sLabel, $sName, $sValue, $sType = '', $sTooltip = '', $sIconClass = '', $sActionType = 'regular', $sColor = 'secondary', $sJsCode = '', $sOnClickJsCode = '')
{
public function __construct(
$sLabel, $sId = null, $sName = '', $sValue = '', $sType = self::DEFAULT_TYPE, string $sTooltip = '', $sIconClass = '',
$sActionType = self::DEFAULT_ACTION_TYPE, $sColor = self::DEFAULT_COLOR, $sJsCode = '', $sOnClickJsCode = ''
) {
$this->sLabel = $sLabel;
$this->sName = $sName;
$this->sValue = $sValue;
@@ -97,7 +134,6 @@ class Button extends UIBlock
return $this->sLabel;
}
/**
/**
* @param string $sLabel
*
@@ -220,6 +256,8 @@ class Button extends UIBlock
public function SetActionType(string $sActionType)
{
$this->sActionType = $sActionType;
return $this;
}
/**

View File

@@ -0,0 +1,240 @@
<?php
/**
* Copyright (C) 2013-2020 Combodo SARL
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*/
namespace Combodo\iTop\Application\UI\Component\Button;
use Combodo\iTop\Application\UI\Component\Button\Button;
/**
* Class ButtonFactory
*
* @internal
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
* @package Combodo\iTop\Application\UI\Component\Button
* @since 2.8.0
*/
class ButtonFactory
{
/**
* Make a basis Button component for any purpose
*
* @param string $sLabel
* @param string $sName See Button::$sName
*
* @return \Combodo\iTop\Application\UI\Component\Button\Button
*/
public static function MakeNeutral(string $sLabel, string $sName): Button
{
$oButton = new Button($sLabel);
$oButton->SetActionType(Button::ENUM_ACTION_TYPE_REGULAR)
->SetColor(Button::ENUM_COLOR_NEUTRAL)
->SetName($sName);
return $oButton;
}
/**
* Make a Button component for a primary action, should be used to tell the user this is the main choice
*
* @param string $sLabel
* @param string|null $sName See Button::$sName
* @param string|null $sValue See Button::$sValue
* @param bool $bIsSubmit See Button::$sType
*
* @return \Combodo\iTop\Application\UI\Component\Button\Button
*/
public static function MakeForPrimaryAction(string $sLabel, string $sName = null, string $sValue = null, bool $bIsSubmit = false
): Button {
return static::MakeForAction($sLabel, Button::ENUM_COLOR_PRIMARY, Button::ENUM_ACTION_TYPE_REGULAR, $sValue, $sName, $bIsSubmit);
}
/**
* Make a Button component for a secondary action, should be used to tell the user this is an second hand choice
*
* @param string $sLabel
* @param string|null $sName See Button::$sName
* @param string|null $sValue See Button::$sValue
* @param bool $bIsSubmit See Button::$sType
*
* @return \Combodo\iTop\Application\UI\Component\Button\Button
*/
public static function MakeForSecondaryAction(string $sLabel, string $sName = null, string $sValue = null, bool $bIsSubmit = false
): Button {
return static::MakeForAction($sLabel, Button::ENUM_COLOR_SECONDARY, Button::ENUM_ACTION_TYPE_REGULAR, $sValue, $sName, $bIsSubmit);
}
/**
* Make a Button component for a validation action, should be used to tell the user he/she going to save / validate / confirm his/her
* choices
*
* @param string $sLabel
* @param string|null $sName See Button::$sName
* @param string|null $sValue See Button::$sValue
* @param bool $bIsSubmit See Button::$sType
*
* @return \Combodo\iTop\Application\UI\Component\Button\Button
*/
public static function MakeForValidationAction(string $sLabel, string $sName = null, string $sValue = null, bool $bIsSubmit = false
): Button {
return static::MakeForAction($sLabel, Button::ENUM_COLOR_VALIDATION, Button::ENUM_ACTION_TYPE_REGULAR, $sValue, $sName, $bIsSubmit);
}
/**
* Make a Button component for a destructive action, should be used to tell the user he/she going to make something that cannot be
* undone easily (deleting an object) or break something (link between objects)
*
* @param string $sLabel
* @param string|null $sName See Button::$sName
* @param string|null $sValue See Button::$sValue
* @param bool $bIsSubmit See Button::$sType
*
* @return \Combodo\iTop\Application\UI\Component\Button\Button
*/
public static function MakeForDestructiveAction(string $sLabel, string $sName = null, string $sValue = null, bool $bIsSubmit = false
): Button {
return static::MakeForAction($sLabel, Button::ENUM_COLOR_DESTRUCTIVE, Button::ENUM_ACTION_TYPE_REGULAR, $sValue, $sName,
$bIsSubmit);
}
/**
* Make a basis Button component for any purpose
*
* @param string $sLabel
* @param string $sName See Button::$sName
*
* @return \Combodo\iTop\Application\UI\Component\Button\Button
*/
public static function MakeAlternativeNeutral(string $sLabel, string $sName): Button
{
$oButton = new Button($sLabel);
$oButton->SetActionType(Button::ENUM_ACTION_TYPE_ALTERNATIVE)
->SetColor(Button::ENUM_COLOR_NEUTRAL)
->SetName($sName);
return $oButton;
}
/**
* Make a Button component for an alternative primary action, should be used to avoid the user to consider this action as the first
* choice
*
* @param string $sLabel
* @param string|null $sName See Button::$sName
* @param string|null $sValue See Button::$sValue
* @param bool $bIsSubmit See Button::$sType
*
* @return \Combodo\iTop\Application\UI\Component\Button\Button
*/
public static function MakeForAlternativePrimaryAction(
string $sLabel, string $sName = null, string $sValue = null, bool $bIsSubmit = false
): Button {
return static::MakeForAction($sLabel, Button::ENUM_COLOR_PRIMARY, Button::ENUM_ACTION_TYPE_ALTERNATIVE, $sValue, $sName,
$bIsSubmit);
}
/**
* Make a Button component for an alternative secondary action, should be used to avoid the user to focus on this
*
* @param string $sLabel
* @param string|null $sName See Button::$sName
* @param string|null $sValue See Button::$sValue
* @param bool $bIsSubmit See Button::$sType
*
* @return \Combodo\iTop\Application\UI\Component\Button\Button
*/
public static function MakeForAlternativeSecondaryAction(
string $sLabel, string $sName = null, string $sValue = null, bool $bIsSubmit = false
): Button {
return static::MakeForAction($sLabel, Button::ENUM_COLOR_SECONDARY, Button::ENUM_ACTION_TYPE_ALTERNATIVE, $sValue, $sName,
$bIsSubmit);
}
/**
* Make a Button component for a validation action, should be used to avoid the user to focus on this
*
* @param string $sLabel
* @param string|null $sName See Button::$sName
* @param string|null $sValue See Button::$sValue
* @param bool $bIsSubmit See Button::$sType
*
* @return \Combodo\iTop\Application\UI\Component\Button\Button
*/
public static function MakeForAlternativeValidationAction(
string $sLabel, string $sName = null, string $sValue = null, bool $bIsSubmit = false
): Button {
return static::MakeForAction($sLabel, Button::ENUM_COLOR_VALIDATION, Button::ENUM_ACTION_TYPE_ALTERNATIVE, $sValue, $sName,
$bIsSubmit);
}
/**
* Make a Button component for a destructive action, should be used to avoid the user to focus on this
*
* @param string $sLabel
* @param string|null $sName See Button::$sName
* @param string|null $sValue See Button::$sValue
* @param bool $bIsSubmit See Button::$sType
*
* @return \Combodo\iTop\Application\UI\Component\Button\Button
*/
public static function MakeForAlternativeDestructiveAction(
string $sLabel, string $sName = null, string $sValue = null, bool $bIsSubmit = false
): Button {
return static::MakeForAction($sLabel, Button::ENUM_COLOR_DESTRUCTIVE, Button::ENUM_ACTION_TYPE_ALTERNATIVE, $sValue, $sName,
$bIsSubmit);
}
/**
* Internal helper
*
* @param string $sLabel
* @param string $sColor See Button::$sColor
* @param string $sActionType See Button::$sActionType
* @param string|null $sValue See Button::$sValue
* @param string|null $sName See Button::$sValue
* @param bool $bIsSubmit
*
* @return \Combodo\iTop\Application\UI\Component\Button\Button
* @interal
*/
protected static function MakeForAction(
string $sLabel, string $sColor, string $sActionType, string $sValue = null, string $sName = null, bool $bIsSubmit = false
): Button {
$oButton = new Button($sLabel);
$oButton->SetActionType($sActionType)
->SetColor($sColor);
if (empty($sValue) === false)
{
$oButton->SetValue($sValue);
}
if (empty($sName) === false)
{
$oButton->SetName($sName);
}
// Set as submit button if necessary
if ($bIsSubmit === true)
{
$oButton->SetType(Button::ENUM_TYPE_SUBMIT);
}
return $oButton;
}
}