N°3542 Separate JS buttons from URL buttons

This commit is contained in:
Stephen Abello
2021-03-18 15:38:04 +01:00
parent 82a5ab5aef
commit 8471bdeffe
13 changed files with 428 additions and 157 deletions

View File

@@ -37,15 +37,6 @@ class Button extends UIBlock
public const DEFAULT_JS_TEMPLATE_REL_PATH = 'base/components/button/layout';
// Specific constants
/** @var string ENUM_TYPE_BUTTON */
public const ENUM_TYPE_BUTTON = 'button';
/** @var string ENUM_TYPE_SUBMIT */
public const ENUM_TYPE_SUBMIT = 'submit';
/** @var string ENUM_TYPE_RESET */
public const ENUM_TYPE_RESET = 'reset';
/** @var string DEFAULT_TYPE */
public const DEFAULT_TYPE = self::ENUM_TYPE_BUTTON;
/** @var string ENUM_ACTION_TYPE_REGULAR */
public const ENUM_ACTION_TYPE_REGULAR = 'regular';
/** @var string ENUM_ACTION_TYPE_ALTERNATIVE */
@@ -74,12 +65,6 @@ class Button extends UIBlock
/** @var string $sLabel */
protected $sLabel;
/** @var string $sType The HTML type of the button (eg. 'submit', 'button', ...) */
protected $sType;
/** @var string $sName The HTML name of the button, used by forms */
protected $sName;
/** @var string $sValue The HTML value of the button, used by forms */
protected $sValue;
/** @var string $sTooltip */
protected $sTooltip;
/** @var string $sIconClass */
@@ -88,8 +73,6 @@ class Button extends UIBlock
protected $sActionType;
/** @var string $sColor */
protected $sColor;
/** @var bool $bIsDisabled */
protected $bIsDisabled;
/** @var string $sJsCode */
protected $sJsCode;
/** @var string $sOnClickJsCode */
@@ -100,9 +83,6 @@ class Button extends UIBlock
*
* @param string $sLabel
* @param string|null $sId
* @param string $sName
* @param string $sValue
* @param string $sType
* @param string $sTooltip
* @param string $sIconClass
* @param string $sActionType
@@ -111,24 +91,19 @@ class Button extends UIBlock
* @param string $sOnClickJsCode
*/
public function __construct(
string $sLabel, string $sId = null, string $sName = '', string $sValue = '', string $sType = self::DEFAULT_TYPE,
string $sTooltip = '', string $sIconClass = '',
string $sLabel, string $sId = null, string $sTooltip = '', string $sIconClass = '',
string $sActionType = self::DEFAULT_ACTION_TYPE, string $sColor = self::DEFAULT_COLOR, string $sJsCode = '',
string $sOnClickJsCode = ''
) {
parent::__construct($sId);
$this->sLabel = $sLabel;
$this->sName = $sName;
$this->sValue = $sValue;
$this->sType = $sType;
$this->sTooltip = $sTooltip;
$this->sIconClass = $sIconClass;
$this->sActionType = $sActionType;
$this->sColor = $sColor;
$this->sJsCode = $sJsCode;
$this->sOnClickJsCode = $sOnClickJsCode;
$this->bIsDisabled = false;
$this->aDataAttributes = ['role' => 'ibo-button'];
}
@@ -151,62 +126,6 @@ class Button extends UIBlock
return $this;
}
/**
* @return string
*/
public function GetType()
{
return $this->sType;
}
/**
* @param string $sType
*
* @return $this
*/
public function SetType(string $sType)
{
$this->sType = $sType;
return $this;
}
/**
* @return string
*/
public function GetName()
{
return $this->sName;
}
/**
* @param string $sName
*
* @return $this
*/
public function SetName(string $sName)
{
$this->sName = $sName;
return $this;
}
/**
* @return string
*/
public function GetValue()
{
return $this->sValue;
}
/**
* @param string $sValue
*
* @return $this
*/
public function SetValue(string $sValue)
{
$this->sValue = $sValue;
return $this;
}
/**
* @return string
@@ -285,25 +204,6 @@ class Button extends UIBlock
return $this;
}
/**
* @return bool
*/
public function IsDisabled()
{
return $this->bIsDisabled;
}
/**
* @param bool $bIsDisabled
*
* @return $this
*/
public function SetIsDisabled(bool $bIsDisabled)
{
$this->bIsDisabled = $bIsDisabled;
return $this;
}
/**
* @return string
*/

View File

@@ -0,0 +1,161 @@
<?php
/**
* Copyright (C) 2013-2021 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\Base\Component\Button;
/**
* Class Button
*
* @author Stephen Abello <stephen.abello@combodo.com>
* @package Combodo\iTop\Application\UI\Base\Component\Button
* @since 3.0.0
*/
class ButtonJS extends Button
{
// Overloaded constants
public const DEFAULT_HTML_TEMPLATE_REL_PATH = 'base/components/button/buttonjs';
// Specific constants
/** @var string ENUM_TYPE_BUTTON */
public const ENUM_TYPE_BUTTON = 'button';
/** @var string ENUM_TYPE_SUBMIT */
public const ENUM_TYPE_SUBMIT = 'submit';
/** @var string ENUM_TYPE_RESET */
public const ENUM_TYPE_RESET = 'reset';
/** @var string DEFAULT_TYPE */
public const DEFAULT_TYPE = self::ENUM_TYPE_BUTTON;
/** @var string $sType The HTML type of the button (eg. 'submit', 'button', ...) */
protected $sType;
/** @var string $sName The HTML name of the button, used by forms */
protected $sName;
/** @var string $sValue The HTML value of the button, used by forms */
protected $sValue;
/** @var bool $bIsDisabled */
protected $bIsDisabled;
/**
* ButtonJS constructor.
*
* @param string $sLabel
* @param string|null $sId
* @param string $sName
* @param string $sValue
* @param string $sType
* @param string $sTooltip
* @param string $sIconClass
* @param string $sActionType
* @param string $sColor
* @param string $sJsCode
* @param string $sOnClickJsCode
*/
public function __construct(
string $sLabel, string $sId = null, string $sName = '', string $sValue = '', string $sType = self::DEFAULT_TYPE,
string $sTooltip = '', string $sIconClass = '',
string $sActionType = self::DEFAULT_ACTION_TYPE, string $sColor = self::DEFAULT_COLOR, string $sJsCode = '',
string $sOnClickJsCode = ''
) {
parent::__construct( $sLabel,$sId, $sTooltip, $sIconClass,
$sActionType, $sColor, $sJsCode, $sOnClickJsCode);
$this->sName = $sName;
$this->sValue = $sValue;
$this->sType = $sType;
$this->bIsDisabled = false;
}
/**
* @return string
*/
public function GetType()
{
return $this->sType;
}
/**
* @param string $sType
*
* @return $this
*/
public function SetType(string $sType)
{
$this->sType = $sType;
return $this;
}
/**
* @return string
*/
public function GetName()
{
return $this->sName;
}
/**
* @param string $sName
*
* @return $this
*/
public function SetName(string $sName)
{
$this->sName = $sName;
return $this;
}
/**
* @return string
*/
public function GetValue()
{
return $this->sValue;
}
/**
* @param string $sValue
*
* @return $this
*/
public function SetValue(string $sValue)
{
$this->sValue = $sValue;
return $this;
}
/**
* @return bool
*/
public function IsDisabled()
{
return $this->bIsDisabled;
}
/**
* @param bool $bIsDisabled
*
* @return $this
*/
public function SetIsDisabled(bool $bIsDisabled)
{
$this->bIsDisabled = $bIsDisabled;
return $this;
}
}

View File

@@ -51,7 +51,7 @@ class ButtonUIBlockFactory extends AbstractUIBlockFactory
* @return \Combodo\iTop\Application\UI\Base\Component\Button\Button
*/
public static function MakeNeutral(string $sLabel, string $sName, ?string $sId = null): Button {
$oButton = new Button($sLabel, $sId);
$oButton = new ButtonJS($sLabel, $sId);
$oButton->SetActionType(Button::ENUM_ACTION_TYPE_REGULAR)
->SetColor(Button::ENUM_COLOR_NEUTRAL)
->SetName($sName);
@@ -284,6 +284,32 @@ class ButtonUIBlockFactory extends AbstractUIBlockFactory
return static::MakeForAction($sLabel, Button::ENUM_COLOR_NEUTRAL, Button::ENUM_ACTION_TYPE_ALTERNATIVE, $sValue, $sName,
$bIsSubmit, $sId);
}
/**
* @param string $sIconClasses
* @param string $sTooltipText
* @param string|null $sName
* @param string|null $sValue
* @param bool $bIsSubmit
* @param string|null $sId
*
* @return \Combodo\iTop\Application\UI\Base\Component\Button\ButtonJS
*/
public static function MakeIconAction(
string $sIconClasses,
string $sTooltipText = '',
string $sName = null,
string $sValue = null,
bool $bIsSubmit = false,
?string $sId = null
) {
$oButton = static::MakeForAction('', Button::ENUM_COLOR_NEUTRAL, Button::ENUM_ACTION_TYPE_ALTERNATIVE, $sValue, $sName,
$bIsSubmit, $sId);
$oButton->SetIconClass($sIconClasses);
$oButton->SetTooltip($sTooltipText);
return $oButton;
}
//----------------------------------------------------------------------------------------------
// Link buttons, mostly used outside forms, to redirect somewhere whilst keeping a button aspect
//----------------------------------------------------------------------------------------------
@@ -294,30 +320,25 @@ class ButtonUIBlockFactory extends AbstractUIBlockFactory
* @param string $sURL
* @param string|null $sLabel
* @param string|null $sIconClasses
* @param string|null $sName See Button::$sName
* @param string|null $sTarget
* @param string|null $sId
*
* @return \Combodo\iTop\Application\UI\Base\Component\Button\Button
*/
public static function MakeLinkNeutral(
string $sURL, ?string $sLabel = null, ?string $sIconClasses = null, ?string $sName = null, ?string $sTarget = null,
string $sURL, ?string $sLabel = '', ?string $sIconClasses = null, ?string $sTarget = null,
?string $sId = null
): Button {
if (empty($sTarget)) {
$sTarget = ButtonURL::DEFAULT_TARGET;
}
$sType = empty($sIconClasses) ? Button::ENUM_ACTION_TYPE_REGULAR : Button::ENUM_ACTION_TYPE_ALTERNATIVE;
$oButton = static::MakeForAction($sLabel, Button::ENUM_COLOR_NEUTRAL, $sType, null, $sName, false, $sId);
$oButton = static::MakeForLink($sLabel, $sURL,Button::ENUM_COLOR_NEUTRAL, $sType, $sTarget, $sId);
if (!empty($sIconClasses)) {
$oButton->SetIconClass($sIconClasses);
}
if (!empty($sURL)) {
if (empty($sTarget)) {
$sTarget = "_self";
}
$oButton->SetOnClickJsCode("window.open('{$sURL}', '{$sTarget}');");
}
return $oButton;
}
@@ -325,27 +346,22 @@ class ButtonUIBlockFactory extends AbstractUIBlockFactory
* @param string $sIconClasses
* @param string $sTooltipText
* @param string|null $sURL
* @param string|null $sName
* @param string|null $sTarget
* @param string|null $sId
*
* @return \Combodo\iTop\Application\UI\Base\Component\Button\Button
* @return \Combodo\iTop\Application\UI\Base\Component\Button\ButtonURL
*/
public static function MakeIconLink(
string $sIconClasses, string $sTooltipText, ?string $sURL = null, ?string $sName = null, ?string $sTarget = null,
string $sIconClasses, string $sTooltipText, ?string $sURL = '', ?string $sTarget = null,
?string $sId = null
) {
$oButton = static::MakeForAction('', Button::ENUM_COLOR_NEUTRAL, Button::ENUM_ACTION_TYPE_ALTERNATIVE, null, $sName, false, $sId);
if (empty($sTarget)) {
$sTarget = ButtonURL::DEFAULT_TARGET;
}
$oButton = static::MakeForLink('', $sURL,Button::ENUM_COLOR_NEUTRAL, Button::ENUM_ACTION_TYPE_ALTERNATIVE, $sTarget, $sId);
$oButton->SetIconClass($sIconClasses);
$oButton->SetTooltip($sTooltipText);
if (!empty($sURL)) {
if (empty($sTarget)) {
$sTarget = "_self";
}
$oButton->SetOnClickJsCode("window.open('{$sURL}', '{$sTarget}');");
}
return $oButton;
}
@@ -363,7 +379,7 @@ class ButtonUIBlockFactory extends AbstractUIBlockFactory
string $sIconClasses, string $sTooltipText, ?string $sURL = null, ?string $sName = null, ?string $sTarget = null,
?string $sId = null
) {
$oButton = static::MakeIconLink($sIconClasses, $sTooltipText, $sURL, $sName, $sTarget, $sId);
$oButton = static::MakeIconLink($sIconClasses, $sTooltipText, $sURL, $sTarget, $sId);
$oButton->SetColor(Button::ENUM_COLOR_DESTRUCTIVE);
$oButton->SetTooltip($sTooltipText);
return $oButton;
@@ -384,7 +400,7 @@ class ButtonUIBlockFactory extends AbstractUIBlockFactory
* @param bool $bIsSubmit
* @param string|null $sId
*
* @return \Combodo\iTop\Application\UI\Base\Component\Button\Button
* @return \Combodo\iTop\Application\UI\Base\Component\Button\ButtonJS
* @internal
*/
protected static function MakeForAction(
@@ -396,7 +412,7 @@ class ButtonUIBlockFactory extends AbstractUIBlockFactory
bool $bIsSubmit = false,
?string $sId = null
): Button {
$oButton = new Button($sLabel, $sId);
$oButton = new ButtonJS($sLabel, $sId);
$oButton->SetActionType($sActionType)
->SetColor($sColor);
@@ -410,9 +426,39 @@ class ButtonUIBlockFactory extends AbstractUIBlockFactory
// Set as submit button if necessary
if ($bIsSubmit === true) {
$oButton->SetType(Button::ENUM_TYPE_SUBMIT);
$oButton->SetType(ButtonJS::ENUM_TYPE_SUBMIT);
}
return $oButton;
}
/**
* Internal helper
*
* @internal
*
* @param string $sLabel
*
* @param string $sURL
* @param string $sColor See Button::$sColor
* @param string $sActionType See Button::$sActionType
* @param string|null $sTarget
* @param string|null $sId
*
* @return \Combodo\iTop\Application\UI\Base\Component\Button\ButtonURL
*/
protected static function MakeForLink(
string $sLabel,
string $sURL,
string $sColor,
string $sActionType,
string $sTarget = null,
?string $sId = null
): Button {
$oButton = new ButtonURL($sLabel, $sURL, $sId, $sTarget);
$oButton->SetActionType($sActionType)
->SetColor($sColor);
return $oButton;
}
}

View File

@@ -0,0 +1,115 @@
<?php
/**
* Copyright (C) 2013-2021 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\Base\Component\Button;
/**
* Class Button
*
* @author Stephen Abello <stephen.abello@combodo.com>
* @package Combodo\iTop\Application\UI\Base\Component\Button
* @since 3.0.0
*/
class ButtonURL extends Button
{
// Overloaded constants
public const DEFAULT_HTML_TEMPLATE_REL_PATH = 'base/components/button/buttonurl';
// Specific constants
/** @var string ENUM_TARGET_BLANK */
public const ENUM_TARGET_BLANK = '_blank';
/** @var string ENUM_TARGET_SELF */
public const ENUM_TARGET_SELF= '_self';
/** @var string ENUM_TARGET_PARENT */
public const ENUM_TARGET_PARENT= '_parent';
/** @var string ENUM_TARGET_TOP */
public const ENUM_TARGET_TOP= '_top';
/** @var string DEFAULT_TARGET */
public const DEFAULT_TARGET = self::ENUM_TARGET_SELF;
/** @var string $sURL */
protected $sURL;
/** @var string $sTarget */
protected $sTarget;
/**
* ButtonURL constructor.
*
* @param string $sLabel
* @param string $sURL
* @param string|null $sId
* @param string $sTarget
* @param string $sTooltip
* @param string $sIconClass
* @param string $sActionType
* @param string $sColor
* @param string $sJsCode
* @param string $sOnClickJsCode
*/
public function __construct(
string $sLabel, string $sURL, string $sId = null, string $sTarget = self::DEFAULT_TARGET, string $sTooltip = '', string $sIconClass = '',
string $sActionType = self::DEFAULT_ACTION_TYPE, string $sColor = self::DEFAULT_COLOR, string $sJsCode = '',
string $sOnClickJsCode = '')
{
parent::__construct($sLabel, $sId, $sTooltip, $sIconClass,
$sActionType, $sColor, $sJsCode, $sOnClickJsCode);
$this->sURL = $sURL;
$this->sTarget = $sTarget;
}
/**
* @return string
*/
public function GetURL(): string
{
return $this->sURL;
}
/**
* @param string $sURL
*
* @return $this
*/
public function SetURL(string $sURL)
{
$this->sURL = $sURL;
return $this;
}
/**
* @return string
*/
public function GetTarget(): string
{
return $this->sTarget;
}
/**
* @param string $sTarget
*
* @return $this
*/
public function SetTarget(string $sTarget)
{
$this->sTarget = $sTarget;
return $this;
}
}