From 53b4dae538014497eaeb2e9bf13dc591eae15e37 Mon Sep 17 00:00:00 2001 From: sunhao Date: Mon, 11 Mar 2024 14:59:38 +0800 Subject: [PATCH] * zin: change render to renderPage, use render to set widget for difference targets. --- framework/base/control.class.php | 2 +- lib/zin/core/context.class.php | 22 ++++++++++++++++++++++ lib/zin/core/render.func.php | 28 +++++++++++++++++----------- module/api/ui/index.html.php | 2 +- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/framework/base/control.class.php b/framework/base/control.class.php index 405f3940ff..5855d70461 100644 --- a/framework/base/control.class.php +++ b/framework/base/control.class.php @@ -1032,7 +1032,7 @@ class baseControl ob_start(); include $viewFile; - if(!$context->rendered) \zin\render(); + if(!$context->rendered) \zin\renderPage(); $content = ob_get_clean(); ob_start(); diff --git a/lib/zin/core/context.class.php b/lib/zin/core/context.class.php index b63e4d5d3e..53fa1ee659 100644 --- a/lib/zin/core/context.class.php +++ b/lib/zin/core/context.class.php @@ -64,6 +64,8 @@ class context extends \zin\utils\dataset public array $eventBindings = array(); + public array $renderWgMap = array('page' => 'page', 'modal' => 'modalDialog', 'fragment' => 'fragment'); + public function __construct(string $name) { parent::__construct(); @@ -480,6 +482,26 @@ class context extends \zin\utils\dataset $this->onRenderCallbacks[] = $callback; } + public function setRenderWgMap(string|array $mapOrName, ?string $wgName) + { + if(is_array($mapOrName)) + { + $this->renderWgMap = array_merge($this->renderWgMap, $mapOrName); + } + else + { + $this->renderWgMap[$mapOrName] = $wgName; + } + } + + public function getRenderWgName() + { + if(isset($this->renderWgMap['all'])) return $this->renderWgMap['all']; + if(isAjaxRequest('modal')) return $this->renderWgMap['modal']; + if(isAjaxRequest() && !isAjaxRequest('zin')) return $this->renderWgMap['fragment']; + return 'page'; + } + public static array $stack = array(); public static function js(string ...$code) diff --git a/lib/zin/core/render.func.php b/lib/zin/core/render.func.php index b9820ca4b0..e65b2bea5e 100644 --- a/lib/zin/core/render.func.php +++ b/lib/zin/core/render.func.php @@ -14,28 +14,34 @@ namespace zin; require_once __DIR__ . DS . 'zin.class.php'; +/** + * 设置渲染目标所使用的部件名称。 + * Set render widget name for target. + * + * @param string $wgName + * @param string $target + * @return void + */ +function render(string $wgName = '', string $target = 'all') +{ + if(!$wgName) return; + context()->setRenderWgMap($target, $wgName); +} + /** * 将视图页面声明的所有内容通过一个部件进行渲染,并输出 HTML。 * Render page content with a widget to HTML. * * @access public - * @param string $wgName * @param array $options * @return void */ -function render(string $wgName = '', array $options = array()) +function renderPage(array $options = array()) { /* 获取全局渲染部件实例和指令。 Get global render widgets and directives. */ - $context = context(); + $context = context(); $globalItems = $context->getGlobalRenderList(); - - /* 决定部件名称,如果是 Ajax 请求则进行特殊处理。 Decide widget name, if is ajax request, then do special process. */ - if(empty($wgName)) - { - $wgName = 'page'; - if(isAjaxRequest('modal')) $wgName = 'modalDialog'; - else if(isAjaxRequest() && !isAjaxRequest('zin')) $wgName = 'fragment'; - } + $wgName = $context->getRenderWgName(); /* 判断是否渲染为完整页面。 Check if render in full page. */ $isFullPage = str_starts_with($wgName, 'page'); diff --git a/module/api/ui/index.html.php b/module/api/ui/index.html.php index 7f3ba1d85c..c8db65507c 100644 --- a/module/api/ui/index.html.php +++ b/module/api/ui/index.html.php @@ -45,7 +45,7 @@ if(empty($libTree)) ) ) ); - render(); + renderPage(); return; }