Skip to content

Commit 8b6111d

Browse files
sukhwinder33445nilmerg
authored andcommitted
ContinueWith: Introduce factory methods create() and createDisabled()
- Disable button if `createDisabled()` is called or no filter is applied - Apply `baseTarget` to the link instead of the class itself
1 parent dc642b9 commit 8b6111d

File tree

1 file changed

+104
-10
lines changed

1 file changed

+104
-10
lines changed

src/Widget/ContinueWith.php

Lines changed: 104 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
use ipl\Html\BaseHtmlElement;
77
use ipl\Html\HtmlElement;
88
use ipl\Stdlib\Filter;
9-
use ipl\Web\Common\BaseTarget;
10-
use ipl\Web\Filter\QueryString;
9+
use ipl\Stdlib\Filter\Rule;
1110
use ipl\Web\Url;
1211

1312
class ContinueWith extends BaseHtmlElement
1413
{
15-
use BaseTarget;
16-
1714
protected $tag = 'span';
1815

1916
protected $defaultAttributes = ['class' => 'continue-with'];
@@ -27,10 +24,98 @@ class ContinueWith extends BaseHtmlElement
2724
/** @var string */
2825
protected $title;
2926

30-
public function __construct(Url $url, $filter)
27+
/** @var string The title to use when the filter is empty */
28+
protected string $emptyFilterTitle;
29+
30+
/** @var ?string The reason why the widget is disabled */
31+
protected ?string $disableReason = null;
32+
33+
/** @var ?string The `data-base-target` attribute */
34+
protected ?string $baseTarget = null;
35+
36+
/**
37+
* @deprecated Use {@see ContinueWith::create()} and {@see ContinueWith::createDisabled()} instead
38+
*/
39+
public function __construct(?Url $url = null, $filter = null)
3140
{
3241
$this->url = $url;
3342
$this->filter = $filter;
43+
44+
$this->emptyFilterTitle = '';
45+
}
46+
47+
/**
48+
* Create a `ContinueWith` widget
49+
*
50+
* @param Url $url The url to use
51+
* @param Rule|callable $filter The filter to apply
52+
* @param string $title The title of the widget
53+
* @param string $emptyFilterTitle The title to use when the filter is empty
54+
*
55+
* @return static
56+
*/
57+
public static function create(
58+
Url $url,
59+
Filter\Rule|callable $filter,
60+
string $title,
61+
string $emptyFilterTitle
62+
): static {
63+
return (new static($url, $filter))
64+
->setTitle($title)
65+
->setEmptyFilterTitle($emptyFilterTitle);
66+
}
67+
68+
/**
69+
* Create a disabled `ContinueWith` widget
70+
*
71+
* @param string $reason The reason why the widget is disabled
72+
*
73+
* @return static
74+
*/
75+
public static function createDisabled(string $reason): static
76+
{
77+
$instance = new static();
78+
$instance->disableReason = $reason;
79+
80+
return $instance;
81+
}
82+
83+
/**
84+
* Get the `data-base-target` attribute
85+
*
86+
* @return ?string
87+
*/
88+
public function getBaseTarget(): ?string
89+
{
90+
return $this->baseTarget;
91+
}
92+
93+
/**
94+
* Set the `data-base-target` attribute
95+
*
96+
* @param string $target
97+
*
98+
* @return $this
99+
*/
100+
public function setBaseTarget(string $target): static
101+
{
102+
$this->baseTarget = $target;
103+
104+
return $this;
105+
}
106+
107+
/**
108+
* Set the title to use when the filter is empty
109+
*
110+
* @param string $emptyFilterTitle
111+
*
112+
* @return $this
113+
*/
114+
public function setEmptyFilterTitle(string $emptyFilterTitle): static
115+
{
116+
$this->emptyFilterTitle = $emptyFilterTitle;
117+
118+
return $this;
34119
}
35120

36121
/**
@@ -54,24 +139,33 @@ public function assemble()
54139
$filter = $filter(); /** @var Filter\Rule $filter */
55140
}
56141

57-
$baseFilter = $this->url->getFilter();
142+
$baseFilter = $this->url?->getFilter();
58143
if ($baseFilter && ((! $baseFilter instanceof Filter\Chain) || ! $baseFilter->isEmpty())) {
59144
$filter = Filter::all($baseFilter, $filter);
60145
}
61146

62-
if ($filter instanceof Filter\Chain && $filter->isEmpty()) {
147+
if ($this->disableReason || ($filter instanceof Filter\Chain && $filter->isEmpty())) {
63148
$this->addHtml(new HtmlElement(
64149
'span',
65-
Attributes::create(['class' => ['control-button', 'disabled']]),
150+
Attributes::create([
151+
'class' => ['control-button', 'disabled'],
152+
'title' => $this->disableReason ?? $this->emptyFilterTitle,
153+
]),
66154
new Icon('share')
67155
));
68156
} else {
69-
$this->addHtml(new ActionLink(
157+
$link = new ActionLink(
70158
null,
71159
$this->url->setFilter($filter),
72160
'share',
73161
['class' => 'control-button', 'title' => $this->title]
74-
));
162+
);
163+
164+
if ($this->getBaseTarget()) {
165+
$link->setBaseTarget($this->getBaseTarget());
166+
}
167+
168+
$this->addHtml($link);
75169
}
76170
}
77171
}

0 commit comments

Comments
 (0)