* zin: add more node modify commands.

This commit is contained in:
sunhao
2024-02-26 17:09:49 +08:00
parent 1391f2d71e
commit 3af45d4475
3 changed files with 85 additions and 13 deletions
+45 -3
View File
@@ -26,12 +26,54 @@ class command
public static function removeClass(node $node, array $args)
{
$node->props->class->remove(...$args);
}
public static function remove(node $node)
public static function prop(node $node, array $args)
{
$node->removed = true;
$node->setProp(...$args);
}
public static function toggleClass(node $node, array $args)
{
$node->props->class->toggle(...$args);
}
public static function html(node $node, array $args)
{
$node->empty();
$node->add(html(...$args));
}
public static function append(node $node, array $args)
{
$node->add($args);
}
public static function text(node $node, array $args)
{
$node->empty();
$node->add(text(...$args));
}
public static function prepend(node $node, array $args)
{
$node->add($args, 'children', true);
}
public static function before(node $node, array $args)
{
$node->add($args, 'before');
}
public static function after(node $node, array $args)
{
$node->add($args, 'after');
}
public static function replaceWith(node $node, array $args)
{
$node->replaceWith(...$args);
}
/**
+3
View File
@@ -272,6 +272,8 @@ class context extends \zin\utils\dataset
public function render(node $node, null|object|string|array $selectors = null, string $renderType = 'html', null|string|array $dataCommands = null, bool $renderInner = false): string|array|object
{
$this->disableGlobalRender();
$renderer = new render($node, $selectors, $renderType, $dataCommands, $renderInner);
$this->rendered = true;
$this->renderer = $renderer;
@@ -326,6 +328,7 @@ class context extends \zin\utils\dataset
call_user_func($callback, $data);
}
$this->enabledGlobalRender();
return $data->output;
}
+37 -10
View File
@@ -49,6 +49,8 @@ class node implements \JsonSerializable
public bool $removed = false;
public ?array $replacedWith = null;
public ?stdClass $buildData = null;
public function __construct(mixed ...$args)
@@ -201,7 +203,7 @@ class node implements \JsonSerializable
return $this->props->pick(array_keys(static::definedPropsList()));
}
public function add($item, string $blockName = 'children')
public function add($item, string $blockName = 'children', bool $prepend = false)
{
if($item === null || is_bool($item)) return;
@@ -212,10 +214,10 @@ class node implements \JsonSerializable
}
if(isDirective($item)) $this->directive($item, $blockName);
else $this->addToBlock($blockName, $item);
else $this->addToBlock($blockName, $item, $prepend);
}
public function addToBlock(string $name, mixed $child)
public function addToBlock(string $name, mixed $child, bool $prepend = false)
{
if($child === null || is_bool($child)) return;
@@ -223,7 +225,7 @@ class node implements \JsonSerializable
{
foreach($child as $blockChild)
{
$this->addToBlock($name, $blockChild);
$this->addToBlock($name, $blockChild, $prepend);
}
return;
}
@@ -245,8 +247,15 @@ class node implements \JsonSerializable
if($result === false) return;
if($result !== null && $result !== true) $child = $result;
if(isset($this->blocks[$name])) $this->blocks[$name][] = $child;
else $this->blocks[$name] = array($child);
if(isset($this->blocks[$name]))
{
if($prepend) array_unshift($this->blocks[$name], $child);
else $this->blocks[$name][] = $child;
}
else
{
$this->blocks[$name] = array($child);
}
}
public function directive(iDirective $directive, string $blockName = 'children')
@@ -260,6 +269,17 @@ class node implements \JsonSerializable
return $this->addToBlock('children', $child);
}
public function remove()
{
$this->removed = true;
}
public function empty(?string $blockName = null)
{
if($blockName) unset($this->blocks[$blockName]);
else $this->blocks = array();
}
public function render(): string
{
if($this->removed) return '';
@@ -291,10 +311,10 @@ class node implements \JsonSerializable
elseif(!is_array($build)) $build = array($build);
$cache = new stdClass();
$cache->before = prebuild($this->buildBefore());
$cache->children = prebuild($this->children());
$cache->build = prebuild($build);
$cache->after = prebuild($this->buildAfter());
$cache->before = $this->buildBefore();
$cache->children = $this->children();
$cache->build = $build;
$cache->after = $this->buildAfter();
$context->handleBuildNode($cache, $this);
@@ -306,10 +326,17 @@ class node implements \JsonSerializable
public function buildAll(): array
{
if($this->replacedWith !== null) return $this->replacedWith;
$buildData = $this->prebuild();
return array_merge($buildData->before, $buildData->build, $buildData->after);
}
public function replaceWith(mixed ...$args)
{
$this->replacedWith = $args;
}
public function children(): array
{
return $this->block('children');