/
Links.php
141 lines (121 loc) · 2.7 KB
/
Links.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* This file is part of the Mesour DataGrid (http://grid.mesour.com)
*
* Copyright (c) 2015-2016 Matouš Němec (http://mesour.com)
*
* For full licence and copyright please view the file licence.md in root of this project
*/
namespace Mesour\DataGrid\Extensions\Selection;
use Mesour;
/**
* @author Matouš Němec <matous.nemec@mesour.com>
*/
class Links implements Mesour\Components\Utils\IString
{
use Mesour\SmartObject;
/**
* @var Mesour\Components\Control\IControl|Mesour\Components\Localization\Translatable
*/
private $parent;
/**
* @var Mesour\UI\DropDown
*/
private $dropDown;
/**
* @var Link[]
*/
private $links = [];
public function __construct(ISelection $parent)
{
$this->parent = $parent;
$this->dropDown = new Mesour\UI\DropDown('links', $parent);
$this->dropDown
->setAttribute('class', 'dropdown selection-dropdown');
$this->dropDown
->getMainButton()
->setText('Selected')
->setDisabled(true);
}
public function setParent(ISelection $parent)
{
$this->parent = $parent;
$this->dropDown = clone $this->dropDown;
$parent->removeComponent('links');
$parent->addComponent($this->dropDown, 'links');
}
/**
* @param string $name
* @return Link
*/
public function addLink($name)
{
$button = $this->dropDown->addButton($this->parent->getTranslator()->translate($name));
$link = new Link($name, $button, $this->parent->getTranslator());
$this->links[$link->getFixedName()] = $link;
return $link;
}
/**
* @param array $attributes
* @return $this
*/
public function addDivider(array $attributes = [])
{
$this->dropDown->addDivider($attributes);
return $this;
}
/**
* @param string $text
* @param array $attributes
* @return $this
*/
public function addHeader($text, array $attributes = [])
{
$this->dropDown->addHeader($text, $attributes);
return $this;
}
/**
* @param string $fixedName
* @return Link
*/
public function getLink($fixedName)
{
return $this->links[$fixedName];
}
/**
* @return Mesour\UI\DropDown
*/
public function getDropDown()
{
return $this->dropDown;
}
public function create($data = [])
{
$this->dropDown->getControlPrototype()
->addAttributes([
'data-mesour-selectiondropdown' => $this->parent->createLinkName(),
]);
foreach ($this->getLinks() as $link) {
$link->setGridSelection($this->parent->createLinkName());
}
$this->dropDown->setOption('data', $data);
return $this->dropDown->create();
}
public function __toString()
{
return (string) $this->create();
}
/**
* @return Link[]
*/
public function getLinks()
{
$out = [];
foreach ($this->links as $link) {
if ($link->isAllowed()) {
$out[] = $link;
}
}
return $out;
}
}