Renderer.php
3.13 KB
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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Rendering;
use YahnisElsts\AdminMenuEditor\Customizable\Controls\Control;
use YahnisElsts\AdminMenuEditor\Customizable\Controls\ControlGroup;
use YahnisElsts\AdminMenuEditor\Customizable\Controls\Section;
use YahnisElsts\AdminMenuEditor\Customizable\Controls\Tooltip;
abstract class Renderer {
/**
* @param \YahnisElsts\AdminMenuEditor\Customizable\Controls\InterfaceStructure $structure
* @return void
*/
public function renderStructure($structure) {
foreach ($structure->getAsSections() as $section) {
if ( $section->shouldRender() ) {
$this->renderSection($section);
}
}
}
/**
* @param Section $section
* @return void
*/
abstract public function renderSection($section);
protected function renderSectionChildren(Section $section) {
foreach ($section->getChildren() as $child) {
if ( !$child->shouldRender() ) {
continue;
}
if ( $child instanceof Section ) {
$this->renderChildSection($child);
} else if ( $child instanceof ControlGroup ) {
$this->renderControlGroup($child);
} else if ( $child instanceof Control ) {
$this->renderUngroupedControl($child);
} else {
throw new \RuntimeException(
'Unexpected child type: ' .
(is_object($child) ? get_class($child) : gettype($child))
);
}
}
}
/**
* @param Section $section
* @return void
*/
protected function renderChildSection($section) {
$this->renderSection($section);
}
/**
* @param ControlGroup $group
* @return void
*/
abstract protected function renderControlGroup($group);
protected function renderGroupChildren(ControlGroup $group, $parentContext = null) {
foreach ($group->getChildren() as $child) {
if ( !$child->shouldRender() ) {
continue;
}
if ( $child instanceof Control ) {
$this->renderControl($child, $parentContext);
} else if ( $child instanceof ControlGroup ) {
$this->renderChildControlGroup($child);
} else {
throw new \RuntimeException(
'Unexpected child type: ' .
(is_object($child) ? get_class($child) : gettype($child))
);
}
}
}
protected function renderChildControlGroup(ControlGroup $group) {
$this->renderControlGroup($group);
}
/**
* @param Control $control
*/
protected function renderUngroupedControl($control) {
$params = [];
$controlId = $control->getId();
if ( $controlId ) {
$params['id'] = 'ame_control_group-' . $controlId;
}
$tempGroup = new ControlGroup($control->getAutoGroupTitle(), [$control], $params);
$this->renderControlGroup($tempGroup);
}
/**
* @param Control $control
* @param null|mixed $parentContext Arbitrary context data from the parent control.
*/
public function renderControl($control, $parentContext = null) {
$control->renderContent($this);
}
abstract public function renderTooltipTrigger(Tooltip $tooltip);
/**
* @param string $containerSelector The CSS selector for the element that contains
* the rendered controls. Typically, this is the form element.
* @return void
*/
public function enqueueDependencies($containerSelector = '') {
//No dependencies by default.
}
}