From 3af45d4475d4ab17aef9648987adc54bf82e2007 Mon Sep 17 00:00:00 2001 From: sunhao Date: Mon, 26 Feb 2024 15:52:46 +0800 Subject: [PATCH] * zin: add more node modify commands. --- lib/zin/core/command.class.php | 48 +++++++++++++++++++++++++++++++--- lib/zin/core/context.class.php | 3 +++ lib/zin/core/node.class.php | 47 ++++++++++++++++++++++++++------- 3 files changed, 85 insertions(+), 13 deletions(-) diff --git a/lib/zin/core/command.class.php b/lib/zin/core/command.class.php index 4fa1ca75f9..62d5db1a84 100644 --- a/lib/zin/core/command.class.php +++ b/lib/zin/core/command.class.php @@ -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); } /** diff --git a/lib/zin/core/context.class.php b/lib/zin/core/context.class.php index a0b9fe3328..e40ffdc0d0 100644 --- a/lib/zin/core/context.class.php +++ b/lib/zin/core/context.class.php @@ -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; } diff --git a/lib/zin/core/node.class.php b/lib/zin/core/node.class.php index c78bcc54f0..27e3ede3cf 100644 --- a/lib/zin/core/node.class.php +++ b/lib/zin/core/node.class.php @@ -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');