Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unlimited user actions support in Card #1977

Merged
merged 9 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions src/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,6 @@ class Card extends View
/** @var string Default executor class. */
public $executor = UserAction\ModalExecutor::class;

/** @var array Columns CSS wide classes */
protected $words = [
'', 'fluid', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve',
'thirteen', 'fourteen', 'fifteen', 'sixteen',
];

/** @var int The number of buttons */
private $btnCount = 0;

protected function init(): void
{
parent::init();
Expand Down Expand Up @@ -153,6 +144,10 @@ public function getButtonContainer()
{
if (!$this->btnContainer) {
$this->btnContainer = $this->addExtraContent(new View(['ui' => 'buttons']));
$this->getButtonContainer()->addClass('wrapping');
if ($this->hasFluidButton) {
$this->getButtonContainer()->addClass('fluid');
}
}

return $this->btnContainer;
Expand Down Expand Up @@ -317,20 +312,11 @@ public function addImage($img)
*/
public function addButton($seed)
{
if ($this->hasFluidButton && $this->btnCount > 0) {
$this->getButtonContainer()->removeClass($this->words[$this->btnCount]);
}

if (!is_object($seed)) {
$seed = Factory::factory([Button::class], $seed);
}

$btn = $this->getButtonContainer()->add($seed);
++$this->btnCount;

if ($this->hasFluidButton && $this->btnCount > 0) {
$this->getButtonContainer()->addClass($this->words[$this->btnCount]);
}

return $btn;
}
Expand Down
16 changes: 8 additions & 8 deletions src/Columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class Columns extends View
/** @var int|false Sum of all column widths added so far. */
protected $calculatedWidth = 0;

/** @var array Allows Grid to calculate widths automatically. */
public $sizes = [
'', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen',
/** @var array<int, string> */
protected $cssWideClasses = [
1 => 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen',
];

/**
Expand All @@ -46,8 +46,8 @@ public function addColumn($defaults = [])

$column = View::addTo($this, $defaults);

if ($size && isset($this->sizes[$size])) {
$column->addClass($this->sizes[$size] . ' wide');
if ($size && isset($this->cssWideClasses[$size])) {
$column->addClass($this->cssWideClasses[$size] . ' wide');
$this->calculatedWidth = false;
} elseif ($this->calculatedWidth !== false) {
++$this->calculatedWidth;
Expand Down Expand Up @@ -78,8 +78,8 @@ protected function renderView(): void
$this->content = null;
}

if (isset($this->sizes[$width])) {
$this->addClass($this->sizes[$width] . ' column');
if (isset($this->cssWideClasses[$width])) {
$this->addClass($this->cssWideClasses[$width] . ' column');
}

parent::renderView();
Expand Down
1 change: 1 addition & 0 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Form extends View
* success() to replace form contents.
*
* WARNING: may be removed in the future as we refactor into using Message class
* and remove the form-success.html template then.
*
* @var string
*/
Expand Down
22 changes: 11 additions & 11 deletions src/Form/Control/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,28 @@ class Input extends Form\Control
*
* Use setInputAttr() to fill this array
*
* @var array
* @var array<string, string>
*/
public $inputAttr = [];
public array $inputAttr = [];

/**
* Set attribute which is added directly to the <input> tag, not the surrounding <div>.
*
* @param string|array $attr Attribute name or hash
* @param string $value Attribute value
* @param string|int|array<string, string|int> $name
* @param ($name is array ? never : string|int) $value
*
* @return $this
*/
public function setInputAttr($attr, $value = null)
public function setInputAttr($name, $value = null)
{
if (is_array($attr)) {
$this->inputAttr = array_merge($this->inputAttr, $attr);

return $this;
if (is_array($name)) {
foreach ($name as $k => $v) {
$this->setInputAttr($k, $v);
}
} else {
$this->inputAttr[$name] = $value;
}

$this->inputAttr[$attr] = $value;

return $this;
}

Expand Down
12 changes: 6 additions & 6 deletions src/GridLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class GridLayout extends View
/** @var int Number of rows */
protected $rows = 1;

/** @var int Number of columns */
/** @var int<1, 16> Number of columns */
protected $columns = 2;

/** @var array columns CSS wide classes */
protected $words = [
'', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve',
'thirteen', 'fourteen', 'fifteen', 'sixteen',
/** @var array<int, string> */
protected $cssWideClasses = [
1 => 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen',
];

/** @var HtmlTemplate */
Expand Down Expand Up @@ -97,6 +97,6 @@ protected function buildTemplate(): void
// $template->rebuildTagsIndex();
}, null, HtmlTemplate::class)();

$this->addClass($this->words[$this->columns] . ' column');
$this->addClass($this->cssWideClasses[$this->columns] . ' column');
}
}
91 changes: 47 additions & 44 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ class View extends AbstractView
*/
public $ui = false;

/** List of classes that needs to be added. */
/** @var array<int, string> List of element CSS classes. */
public array $class = [];

/** List of custom CSS attributes. */
/** @var array<string, string> Map of element CSS styles. */
public array $style = [];

/** List of custom attributes. */
/** @var array<string, string|int> Map of element attributes. */
public array $attr = [];

/**
Expand Down Expand Up @@ -379,22 +379,24 @@ public function set($arg1 = null, $arg2 = null)
* Multiple CSS classes can also be added if passed as space separated
* string or array of class names.
*
* @param string|array $class CSS class name or array of class names
* @param string|array<int, string> $class
*
* @return $this
*/
public function addClass($class)
{
$classArr = explode(' ', is_array($class) ? implode(' ', $class) : $class);
$this->class = array_merge($this->class, $classArr);
if ($class !== []) {
$classArr = explode(' ', is_array($class) ? implode(' ', $class) : $class);
$this->class = array_merge($this->class, $classArr);
}

return $this;
}

/**
* Remove one or several CSS classes from the element.
*
* @param string|array $class CSS class name or array of class names
* @param string|array<int, string> $class
*
* @return $this
*/
Expand All @@ -410,41 +412,44 @@ public function removeClass($class)
* Add inline CSS style to element.
* Multiple CSS styles can also be set if passed as array.
*
* @param string|array $property CSS Property or hash
* @param string $style CSS Style definition
* @param string|array<string, string> $property
* @param ($property is array ? never : string) $value
*
* @return $this
*
* @todo Think about difference between setStyle and addStyle
*/
public function setStyle($property, string $style = null)
public function setStyle($property, string $value = null)
{
$this->style = array_merge(
$this->style,
is_array($property) ? $property : [$property => $style]
);
if (is_array($property)) {
foreach ($property as $k => $v) {
$this->setStyle($k, $v);
}
} else {
$this->style[$property] = $value;
}

return $this;
}

/**
* @param string|array $property CSS Property or hash
* @param string $style CSS Style definition
* @param string|array<string, string> $property
* @param ($property is array ? never : string) $value
*
* @return $this
*
* @see setStyle()
*/
public function addStyle($property, string $style = null)
public function addStyle($property, string $value = null)
{
return $this->setStyle($property, $style);
return $this->setStyle($property, $value);
}

/**
* Remove inline CSS style from element, if it was added with setStyle
* or addStyle.
*
* @param string $property CSS Property to remove
* @param string $property
*
* @return $this
*/
Expand All @@ -458,43 +463,41 @@ public function removeStyle($property)
/**
* Set attribute.
*
* @param string|array $attr Attribute name or hash
* @param string $value Attribute value
* @param string|int|array<string, string|int> $name
* @param ($name is array ? never : string|int) $value
*
* @return $this
*/
public function setAttr($attr, $value = null)
public function setAttr($name, $value = null)
{
if (is_array($attr)) {
$this->attr = array_merge($this->attr, $attr);

return $this;
if (is_array($name)) {
foreach ($name as $k => $v) {
$this->setAttr($k, $v);
}
} else {
$this->attr[$name] = $value;
}

$this->attr[$attr] = $value;

return $this;
}

/**
* Remove attribute.
*
* @param string|array $property Attribute name or hash
* @param string|array<int, string> $name
*
* @return $this
*/
public function removeAttr($property)
public function removeAttr($name)
{
if (is_array($property)) {
foreach ($property as $v) {
unset($this->attr[$v]);
if (is_array($name)) {
foreach ($name as $v) {
$this->removeAttr($v);
}

return $this;
} else {
unset($this->attr[$name]);
}

unset($this->attr[$property]);

return $this;
}

Expand Down Expand Up @@ -569,11 +572,11 @@ public function stickyGet(string $name, string $newValue = null): ?string
*/
protected function renderView(): void
{
if ($this->class) {
if ($this->class !== []) {
$this->template->append('class', implode(' ', $this->class));
}

if ($this->style) {
if ($this->style !== []) {
$styles = [];
foreach ($this->style as $k => $v) {
$styles[] = $k . ': ' . $v . ';';
Expand Down Expand Up @@ -603,12 +606,12 @@ protected function renderView(): void
$this->template->tryDangerouslySetHtml('_element_end_html', '</' . $this->element . '>');
}

if ($this->attr) {
$tmp = [];
foreach ($this->attr as $attr => $val) {
$tmp[] = $attr . '="' . $this->getApp()->encodeHtml((string) $val) . '"';
if ($this->attr !== []) {
$attrs = [];
foreach ($this->attr as $k => $v) {
$attrs[] = $k . '="' . $this->getApp()->encodeHtml((string) $v) . '"';
}
$this->template->dangerouslySetHtml('attributes', implode(' ', $tmp));
$this->template->dangerouslySetHtml('attributes', implode(' ', $attrs));
}
}

Expand Down
4 changes: 2 additions & 2 deletions template/form/control/radio.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<div id="{$_id}" class="{$class} grouped fields" style="{$style}" {$attributes}>
{Radio}{rows}{row}
<div class="field"><div class="ui radio checkbox">
<input id="{$_name}_{$id}" type="radio" name="{$_name}" value="{$id}" {$checked} {$disabled}>
<label for="{$_name}_{$id}">{$name}</label>
<input id="{$_id}_{$id}" type="radio" name="{$_name}" value="{$id}" {$checked} {$disabled}>
<label for="{$_id}_{$id}">{$name}</label>
</div></div>
{/row}{/rows}{/}
<label>{$Content}</label></div>
4 changes: 2 additions & 2 deletions template/form/control/radio.pug
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
| <div id="{$_id}" class="{$class} grouped fields" style="{$style}" {$attributes}>
| {Radio}{rows}{row}
| <div class="field"><div class="ui radio checkbox">
| <input id="{$_name}_{$id}" type="radio" name="{$_name}" value="{$id}" {$checked} {$disabled}>
| <label for="{$_name}_{$id}">{$name}</label>
| <input id="{$_id}_{$id}" type="radio" name="{$_name}" value="{$id}" {$checked} {$disabled}>
| <label for="{$_id}_{$id}">{$name}</label>
| </div></div>
| {/row}{/rows}{/}
label {$Content}
Expand Down
9 changes: 0 additions & 9 deletions template/tree.html

This file was deleted.

Loading