forked from chalenkoa/Bootstrap.PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DropdownMenu.php
322 lines (247 loc) · 7.23 KB
/
DropdownMenu.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
namespace BootstrapPHP;
use BootstrapPHP\Helpers\View;
use BootstrapPHP\Base\Attributes;
/**
* Класс конструктор выпадающего меню Bootstrap
*
* @see http://twitter.github.io/bootstrap/components.html#dropdowns
* @see http://bootstrap-ru.com/components.php#dropdowns
*/
abstract class DropdownMenuBuilder extends Base\Base
{
const DIVIDER = '---';
const ALIGN_RIGHT = 'pull-right';
const ALIGN_DEFAULT = '';
/** @var DropdownMenuItemBase[] */
protected $_items;
/** @var DropdownMenuItemBase[] */
protected $_items_always_down;
/** @var string выравнивание */
protected $_align;
function __construct()
{
parent::__construct();
$this->_requiredJs(BootstrapPHP::JS_DROPDOWN);
}
/**
* Добавление ссылки
* @param string $text текст ссылки
* @param string|bool $url
* @param string|IconBuilder|bool $icon иконка, используй константы класса Icon TYPE_, или объект Icon
* @param bool $disable true блокирует ссылку
* @param bool $flag_always_down true приклеивает ссыклу вниз меню, если у нескольких элементов будет этот флаг,
* ниже будет тат кто был добавлен позже
* @param Attributes $attributes
* @return $this
*/
public function addLink(
$text, $url = false, $icon = false, $disable = false, $flag_always_down = false, Attributes $attributes = null
)
{
$item = new LinkDropdownMenuItem($this, $text, $url, $attributes);
if ($icon)
{
$item->setIcon($icon);
}
if ($disable)
{
$item->disabled();
}
$this->_addItem($item, $flag_always_down);
return $this;
}
/**
* Добавление подзаголовка
* @param string $text текст подзаголовка
* @param bool $flag_always_down true приклеивает заголовок вниз меню
* @return $this
*/
public function addHeader($text, $flag_always_down = false)
{
$item = new HeaderDropdownMenuItem($text);
$this->_addItem($item, $flag_always_down);
return $this;
}
/**
* Добавление разделителя
* @param bool $flag_always_down true приклеивает разделитель вниз меню
* @return $this
*/
public function addDivider( $flag_always_down = false)
{
$this->addLink(self::DIVIDER, false, false, false, $flag_always_down);
return $this;
}
/**
* Задание выравнивания выпадающего списка относительно родительского элемента
* @param string $align выравнивание, используй константы этого класса ALIGN_
* @return $this
*/
public function setAlign($align = self::ALIGN_DEFAULT)
{
$this->_align = $align;
return $this;
}
/**
* Указываем на то что дальше в цепочке вызовов будут элементы подменю
* @param $text текст элемента списка, при наведении на который будет показываться подменю
* @param bool $flag_always_down true приклеивает подменю вниз меню
* @param Attributes $attributes
* @return DropdownSubmenu
*/
public function startSubmenu($text, $flag_always_down = false, Attributes $attributes = null)
{
$dropdown_submenu = new DropdownSubmenu($this);
$this->_addItem(new SubmenuDropdownMenuItem($text, $dropdown_submenu, $attributes), $flag_always_down);
return $dropdown_submenu;
}
/**
* Метод для IDE, исправляет тип возвращаемого объекта с $this на имя конретного класса
* Использование $this необходимо, так как одни и те же методы возвращают разные объекты в зависимости от
* того находятся они под или после метода начинаюго подменю
* @return DropdownMenuBuilder
*/
public function end()
{
return $this;
}
/**
* @param DropdownMenuItemBase $item
* @param bool $flag_always_down
*/
private function _addItem($item, $flag_always_down = false)
{
if ($flag_always_down)
{
$this->_items_always_down[] = $item;
} else
{
$this->_items[] = $item;
}
}
public function __toString()
{
/** @var \view_dropdown_menu */
return View::render('DropdownMenu', array('dropdown_menu' => $this));
}
}
class DropdownMenu extends DropdownMenuBuilder
{
/**
* Создание объекта выпадающего меню
* @return DropdownMenuBuilder
*/
public static function create()
{
$class = __CLASS__;
return new $class();
}
/**
* @return DropdownMenuItemBase[]
*/
public function getItems()
{
return array_merge
(
(array) $this->_items,
(array) $this->_items_always_down
);
}
public function getAlign()
{
return $this->_align;
}
}
class DropdownSubmenu extends DropdownMenu
{
/** @var DropdownMenu|DropdownMenuBuilder */
private $_parent;
/**
* @param DropdownMenu|DropdownMenuBuilder $parent выпадающий список родитель
*/
function __construct($parent)
{
parent::__construct();
$this->_parent = $parent;
}
/**
* Указатель конца подменю
* @return DropdownMenuBuilder
*/
public function endSubmenu()
{
return $this->_parent;
}
}
abstract class DropdownMenuItemBase
{
private $_text;
private $_icon;
private $_disabled;
private $_attributes;
public function __construct($text, Attributes $attributes = null)
{
$this->_text = $text;
$this->_attributes = $attributes;
}
public function setIcon($icon)
{
$this->_icon = $icon;
return $this;
}
public function disabled()
{
$this->_disabled = true;
return $this;
}
public function isDisabled()
{
return $this->_disabled;
}
public function getIcon()
{
return $this->_icon;
}
public function getText()
{
return $this->_text;
}
public function getAttributes()
{
return $this->_attributes;
}
}
class HeaderDropdownMenuItem extends DropdownMenuItemBase
{
public function __construct($text, Attributes $attributes = null)
{
parent::__construct($text, $attributes);
}
}
class LinkDropdownMenuItem extends DropdownMenuItemBase
{
private $_url;
public function __construct($parent, $text, $url, Attributes $attributes = null)
{
parent::__construct($text, $attributes);
$this->_url = $url;
}
public function getUrl()
{
return $this->_url;
}
}
class SubmenuDropdownMenuItem extends DropdownMenuItemBase
{
private $_dropdown_submenu;
public function __construct($text, DropdownSubmenu $dropdown_submenu, Attributes $attributes = null)
{
parent::__construct($text, $attributes);
$this->_dropdown_submenu = $dropdown_submenu;
}
public function getDropdownSubmenu()
{
return $this->_dropdown_submenu;
}
}