-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathControl.php
More file actions
169 lines (132 loc) · 3.84 KB
/
Control.php
File metadata and controls
169 lines (132 loc) · 3.84 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
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
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Application\UI;
use Nette;
/**
* Control is renderable Presenter component.
*
* @property-read ITemplate|Nette\Bridges\ApplicationLatte\DefaultTemplate|\stdClass $template
*/
abstract class Control extends Component implements IRenderable
{
/** @var bool */
public $snippetMode;
/** @var ITemplateFactory */
private $templateFactory;
/** @var ITemplate */
private $template;
/** @var array */
private $invalidSnippets = [];
/********************* template factory ****************d*g**/
final public function setTemplateFactory(ITemplateFactory $templateFactory)
{
$this->templateFactory = $templateFactory;
return $this;
}
final public function getTemplate(): ITemplate
{
if ($this->template === null) {
$this->template = $this->createTemplate();
}
return $this->template;
}
/**
* @param string $class
*/
protected function createTemplate(/*string $class = null*/): ITemplate
{
$class = func_num_args() ? func_get_arg(0) : $this->formatTemplateClass(); // back compatibility
$templateFactory = $this->templateFactory ?: $this->getPresenter()->getTemplateFactory();
return $templateFactory->createTemplate($this, $class);
}
public function formatTemplateClass(): ?string
{
$class = preg_replace('#Presenter$|Control$#', 'Template', static::class);
if ($class === static::class || !class_exists($class)) {
return null;
} elseif (!is_a($class, ITemplate::class, true)) {
trigger_error(sprintf(
'%s: class %s was found but does not implement the %s, so it will not be used for the template.',
static::class, $class, ITemplate::class
), E_USER_NOTICE);
return null;
} else {
return $class;
}
}
/**
* Descendant can override this method to customize template compile-time filters.
*/
public function templatePrepareFilters(ITemplate $template): void
{
}
/**
* Saves the message to template, that can be displayed after redirect.
*/
public function flashMessage($message, string $type = 'info'): \stdClass
{
$id = $this->getParameterId('flash');
$messages = $this->getPresenter()->getFlashSession()->$id;
$messages[] = $flash = (object) [
'message' => $message,
'type' => $type,
];
$this->getTemplate()->flashes = $messages;
$this->getPresenter()->getFlashSession()->$id = $messages;
return $flash;
}
/********************* rendering ****************d*g**/
/**
* Forces control or its snippet to repaint.
*/
public function redrawControl(string $snippet = null, bool $redraw = true): void
{
if ($redraw) {
$this->invalidSnippets[$snippet === null ? "\0" : $snippet] = true;
} elseif ($snippet === null) {
$this->invalidSnippets = [];
} else {
$this->invalidSnippets[$snippet] = false;
}
}
/**
* Is required to repaint the control or its snippet?
*/
public function isControlInvalid(string $snippet = null): bool
{
if ($snippet === null) {
if (count($this->invalidSnippets) > 0) {
return true;
} else {
$queue = [$this];
do {
foreach (array_shift($queue)->getComponents() as $component) {
if ($component instanceof IRenderable) {
if ($component->isControlInvalid()) {
// $this->invalidSnippets['__child'] = true; // as cache
return true;
}
} elseif ($component instanceof Nette\ComponentModel\IContainer) {
$queue[] = $component;
}
}
} while ($queue);
return false;
}
} else {
return $this->invalidSnippets[$snippet] ?? isset($this->invalidSnippets["\0"]);
}
}
/**
* Returns snippet HTML ID.
*/
public function getSnippetId(string $name): string
{
// HTML 4 ID & NAME: [A-Za-z][A-Za-z0-9:_.-]*
return 'snippet-' . $this->getUniqueId() . '-' . $name;
}
}