Skip to content

Commit a3c930b

Browse files
author
arutyunyan
committed
release 8.0.0
1 parent 4ee6098 commit a3c930b

File tree

5 files changed

+60
-54
lines changed

5 files changed

+60
-54
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change log
22

3+
## v8.0.0 - August 11, 2022
4+
5+
- Fronted side refactor
6+
7+
---
8+
39
## v7.3.26 - July 29, 2022
410

511
- bug fixes

src/dom/Dom.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ class Dom
1919
*/
2020
public function addVariable(Variable $variable)
2121
{
22-
$this->variables[$variable->getName()] = $variable;
22+
if (isset($this->variables[$variable->getName()])) {
23+
foreach ($variable->getPath() as $path) {
24+
$this->variables[$variable->getName()]->addPath($path);
25+
}
26+
}
27+
else {
28+
$this->variables[$variable->getName()] = $variable;
29+
}
2330
}
2431
/**
2532
* @return array

src/dom/Variable.php

+7
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,11 @@ public function addPath(Path $path)
5151
{
5252
$this->paths[$path->getPath()] = $path;
5353
}
54+
/**
55+
*
56+
*/
57+
public function getPath()
58+
{
59+
return $this->paths;
60+
}
5461
}

src/syntax/SuQL.php

+33-50
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace suql\syntax;
44

55
use sagittaracc\Html;
6+
use suql\dom\Dom;
7+
use suql\dom\Path;
8+
use suql\dom\Variable;
69
use suql\syntax\field\Field;
710
use suql\syntax\parser\Tsml;
811

@@ -77,6 +80,7 @@ public static function template($file, SuQLParser $parser = null)
7780
$html = '';
7881
$js = '';
7982
$jsConfig = [];
83+
$dom = new Dom();
8084

8185
if (is_null($parser)) {
8286
$parser = new Tsml;
@@ -87,8 +91,8 @@ public static function template($file, SuQLParser $parser = null)
8791
foreach ($template as $root => $view) {
8892
list($rootElement, $namespace) = explode('@', $root);
8993
$view['id'] = $namespace;
90-
$html = self::parseTemplate($namespace, $rootElement, $view, $jsConfig);
91-
$js = self::generateJs($namespace, $jsConfig);
94+
$html = self::parseTemplate($namespace, $rootElement, $view, $dom);
95+
$js = self::generateJs($namespace, $dom);
9296
}
9397

9498
return $html . $js;
@@ -98,13 +102,13 @@ public static function template($file, SuQLParser $parser = null)
98102
* @param string $namespace заданная пользователем namespace блока
99103
* @param string $parent html tag родителя
100104
* @param array $children вложенные элементы в parent
101-
* @param array $jsConfig генерируемые по ходу связи между используемыми переменными шаблона и участками их в DOM
105+
* @param \suql\dom\Dom $dom генерируемые по ходу связи между используемыми переменными шаблона и участками их в DOM
102106
* @return string html
103107
*/
104-
private static function parseTemplate($namespace, $parent, $children, &$jsConfig)
108+
private static function parseTemplate($namespace, $parent, $children, $dom)
105109
{
106-
$content = self::getContent($namespace, $children, $jsConfig);
107-
$attributes = self::getAttributes($namespace, $children, $jsConfig);
110+
$content = self::getContent($namespace, $children, $dom);
111+
$attributes = self::getAttributes($namespace, $children, $dom);
108112
return
109113
is_null($parent)
110114
? $content
@@ -125,21 +129,25 @@ private static function attachClassToElement(&$dom)
125129
* Получает все атрибуты
126130
* @param string $namespace
127131
* @param array $children вложенные элементы (атрибуты и контент вместе)
128-
* @param array $jsConfig генерируемые по ходу связи между используемыми переменными шаблона и участками их в DOM
132+
* @param \suql\dom\Dom $dom генерируемые по ходу связи между используемыми переменными шаблона и участками их в DOM
129133
* @return array массив атрибутов (сырых и конвертированных из спец атрибутов sg)
130134
*/
131-
private static function getAttributes($namespace, $children, &$jsConfig)
135+
private static function getAttributes($namespace, $children, $dom)
132136
{
133137
$list = [];
134138
$sgAttributes = [
135139
'sg-click' => function ($namespace, $value) {
136140
return ['onclick' => "$namespace.$value"];
137141
},
138-
'sg-model' => function ($namespace, $value) use ($children, &$jsConfig) {
142+
'sg-model' => function ($namespace, $varName) use ($children, $dom) {
139143
$class = self::attachClassToElement($children);
140-
self::addJsVariable($jsConfig, $value, "$namespace>$class");
144+
$format = 'value';
145+
$var = new Variable($varName);
146+
$path = new Path("$namespace>$class", $format);
147+
$var->addPath($path);
148+
$dom->addVariable($var);
141149
return [
142-
'onkeyup' => "assign($namespace.$value, this.value)",
150+
'onkeyup' => "assign($namespace.$varName, this.value)",
143151
'class' => $class,
144152
];
145153
},
@@ -162,79 +170,54 @@ private static function getAttributes($namespace, $children, &$jsConfig)
162170
* Генерирует html контент
163171
* @param string $namespace основной namespace
164172
* @param array $children все дочерние элементы из которых генерируем
165-
* @param array $jsConfig будущая конфигурация для генерации js
173+
* @param \suql\dom\Dom $dom будущая конфигурация для генерации js
166174
* @return string html
167175
*/
168-
private static function getContent($namespace, &$children, &$jsConfig)
176+
private static function getContent($namespace, &$children, $dom)
169177
{
170178
$html = '';
171179

172180
foreach ($children as $key => $value) {
173181
// Template variable
174182
if (preg_match('/\{\{(\w+)\}\}/', $key, $matches)) {
175183
$class = self::attachClassToElement($children);
176-
$template = !empty($value) ? self::parseTemplate($namespace, null, $value, $jsConfig) : null;
177-
$variable = $matches[1];
178-
self::addJsVariable($jsConfig, $variable, "$namespace>$class", $template);
184+
$template = !empty($value) ? self::parseTemplate($namespace, null, $value, $dom) : null;
185+
$format = is_null($template) ? 'raw' : 'html';
186+
$varName = $matches[1];
187+
$var = new Variable($varName);
188+
$path = new Path("$namespace>$class", $format, $template);
189+
$var->addPath($path);
190+
$dom->addVariable($var);
179191
$html = '';
180192
}
181193
// Template function
182194
else if (preg_match('/\{\{\w+\(\)\}\}/', $key)) {
183195
$class = self::attachClassToElement($children);
184-
$template = !empty($value) ? self::parseTemplate($namespace, null, $value, $jsConfig) : null;
196+
$template = !empty($value) ? self::parseTemplate($namespace, null, $value, $dom) : null;
185197
}
186198
else {
187199
if (is_array($value)) {
188200
if (empty($value)) {
189201
$html .= $key;
190202
}
191203
else {
192-
$html .= self::parseTemplate($namespace, $key, $value, $jsConfig);
204+
$html .= self::parseTemplate($namespace, $key, $value, $dom);
193205
}
194206
}
195207
}
196208
}
197209

198210
return $html;
199211
}
200-
/**
201-
* Добавляет js template variable
202-
* @param array $jsConfig будущая конфигурация для генерации js
203-
* @param string $variable
204-
* @param string $path
205-
* @param string $template
206-
*/
207-
private static function addJsVariable(&$jsConfig, $variable, $path = null, $template = null)
208-
{
209-
if (!isset($jsConfig[$variable])) {
210-
$jsConfig[$variable] = [
211-
'value' => null,
212-
'paths' => [],
213-
];
214-
}
215-
216-
if (!is_null($path)) {
217-
$jsConfig[$variable]['paths'][$path] = [
218-
'format' => 'raw',
219-
];
220-
}
221-
222-
if (!is_null($template)) {
223-
$jsConfig[$variable]['paths'][$path] = [
224-
'format' => 'html',
225-
'template' => $template,
226-
];
227-
}
228-
}
229212
/**
230213
* Генерация дополнительного js
231214
* @param string $namespace основной namespace
232-
* @param array $jsConfig конфигурация для js
215+
* @param \suql\dom\Dom $dom конфигурация для js
233216
* @return string script tag
234217
*/
235-
private static function generateJs($namespace, $jsConfig)
218+
private static function generateJs($namespace, $dom)
236219
{
237220
$list = [];
238-
return Html::tag('script', ['type' => 'text/javascript'], "window.$namespace = " . json_encode($jsConfig));
221+
return Html::tag('script', ['type' => 'text/javascript'], "window.$namespace = " . json_encode($dom->getVariables()));
239222
}
240223
}

tests/suql/TemplateTest.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public function testSgClick(): void
2020
'"value":null,'.
2121
'"paths":{'.
2222
'"app>count":{'.
23-
'"format":"raw"'.
23+
'"format":"raw",'.
24+
'"template":null'.
2425
'}'.
2526
'}'.
2627
'}'.
@@ -43,10 +44,12 @@ public function testSgModel(): void
4344
'"value":null,'.
4445
'"paths":{'.
4546
'"app>input":{'.
46-
'"format":"raw"'.
47+
'"format":"raw",'.
48+
'"template":null'.
4749
'},'.
4850
'"app>my-input":{'.
49-
'"format":"raw"'.
51+
'"format":"value",'.
52+
'"template":null'.
5053
'}'.
5154
'}'.
5255
'}'.

0 commit comments

Comments
 (0)