From 56027ccbf584869a2d07f6d9d75d0e7fe0c31291 Mon Sep 17 00:00:00 2001 From: Hao Sun Date: Wed, 18 Jan 2023 11:48:33 +0800 Subject: [PATCH] * zin: optimize core lib --- module/program/ui/browse.html.php | 12 ++++++---- zin/core/builder.class.php | 25 ++++++++++--------- zin/core/ele.class.php | 40 +++++++++++++------------------ zin/core/wg.class.php | 4 ++-- zin/helper.php | 3 ++- zin/wg/page/v1.php | 6 ++--- 6 files changed, 44 insertions(+), 46 deletions(-) diff --git a/module/program/ui/browse.html.php b/module/program/ui/browse.html.php index b4167e3ef5..d95375695e 100644 --- a/module/program/ui/browse.html.php +++ b/module/program/ui/browse.html.php @@ -1,12 +1,14 @@ primary(), - div( + button('BUTTON'), + btn('Primary')->primary()->rounded(), + div + ( h2('Headings2'), h3('Headings3'), p('lorem', h5::strong('bold')) ) -)->title('哈哈哈')->x(); +)->title($title)->x(); diff --git a/zin/core/builder.class.php b/zin/core/builder.class.php index 66dcb063bc..5403c7d502 100644 --- a/zin/core/builder.class.php +++ b/zin/core/builder.class.php @@ -32,8 +32,8 @@ class builder public function __construct($tag = '') { - $this->tag = $tag; - $this->selfClosing = in_array($tag, array('area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr')); + $this->tag = $tag; + $this->selfClosing = in_array($tag, array('area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr')); } public function before($content) @@ -60,6 +60,7 @@ class builder { if(is_array($content)) $this->children = array_merge($this->children, $content); else $this->children[] = $content; + return $this; } @@ -67,6 +68,7 @@ class builder { if(is_array($content)) $this->children = array_merge($content, $this->children); else array_unshift($this->children, $content); + return $this; } @@ -74,7 +76,8 @@ class builder { if($reset) $this->propsStr = ''; - $this->propsStr .= ' ' . strval($props); + $propsStr = trim(strval($props)); + if(!empty($propsStr)) $this->propsStr .= ' ' . $propsStr; return $this; } @@ -124,6 +127,8 @@ class builder if(!$this->selfClosing && $this->inTag && !empty($this->tag)) $html[] = "<$this->tag" . "$this->propsStr>"; + if(!empty($this->prefix)) $html = array_merge($html, $this->prefix); + if(!empty($this->cssImports)) { foreach($this->cssImports as $href) @@ -142,19 +147,17 @@ class builder if(!empty($cssCode)) $html[] = ""; } - if(!empty($this->prefix)) $html = array_merge($html, $this->prefix); - if($this->selfClosing) { if(!empty($this->tag)) $html[] = "<$this->tag" . "$this->propsStr />"; } else { - if(!empty($this->tag) && !$this->inTag) $html[] = "<$this->tag" . "$this->propsStr>"; - - if(!empty($this->children)) $html = array_merge($html, $this->children); - - if(!empty($this->tag) && !$this->inTag) $html[] = "tag>"; + $innerHtml = ''; + if(!empty($this->tag) && !$this->inTag) $innerHtml .= "<$this->tag" . "$this->propsStr>"; + if(!empty($this->children)) $innerHtml .= implode("\n", $this->children); + if(!empty($this->tag) && !$this->inTag) $innerHtml .= "tag>"; + if(!empty($innerHtml)) $html[] = $innerHtml; } if(!empty($this->suffix)) $html = array_merge($html, $this->suffix); @@ -187,7 +190,7 @@ class builder if(!$this->selfClosing && $this->inTag && !empty($this->tag)) $html[] = "tag>"; - return implode("\n", $html); + return implode('', $html); } /** diff --git a/zin/core/ele.class.php b/zin/core/ele.class.php index 6187b2ea1e..3cc3642857 100755 --- a/zin/core/ele.class.php +++ b/zin/core/ele.class.php @@ -22,6 +22,8 @@ class ele protected static $selfClosing = NULL; + protected static $asBlock = NULL; + public $props; public $tagName; @@ -244,6 +246,12 @@ class ele foreach($children as $child) { + if(is_array($child)) + { + $this->add($child, $prepend, $strAsHtml, $reset); + continue; + } + if($child instanceof ele) $child->parent = $this; else if($strAsHtml && is_string($child)) $child = array('html' => $child); @@ -274,30 +282,6 @@ class ele return $this->add(func_get_args(), true, true); } - /** - * Append current widget to the given parent - * - * @param object $parent - * @return wg Return self for chain calls. - */ - public function appendTo($parent, $strAsHtml = false) - { - $parent->append($this, $strAsHtml); - return $this; - } - - /** - * Prepend current widget to the given parent - * - * @param object $parent - * @return wg Return self for chain calls. - */ - public function prependTo($parent, $strAsHtml = false) - { - $parent->prepend($this, $strAsHtml); - return $this; - } - /** * Get or set inner text * @@ -542,6 +526,14 @@ class ele return array($tagName, $props, $children); } + static protected function isBlockTag($tag = '') + { + $isAsBlock = static::$asBlock; + if(is_bool($isAsBlock)) return $isAsBlock; + + return !in_array($tag, array('a', 'span', 'strong', 'small', 'i', 'em', 'code')); + } + /** * @return builder */ diff --git a/zin/core/wg.class.php b/zin/core/wg.class.php index 928b6565d3..3ee92d0fd0 100755 --- a/zin/core/wg.class.php +++ b/zin/core/wg.class.php @@ -49,7 +49,7 @@ class wg extends ele return $builder; } - public function appendToSlot() + public function appendTo() { $args = func_get_args(); $slot = array_shift($args); @@ -61,7 +61,7 @@ class wg extends ele return $this; } - public function prependToSlot() + public function prependTo() { $args = func_get_args(); $slot = array_shift($args); diff --git a/zin/helper.php b/zin/helper.php index 9bcdfbd68e..8809c3b031 100755 --- a/zin/helper.php +++ b/zin/helper.php @@ -34,5 +34,6 @@ function createWg($name, $args) $wgVer = getWgVer($name); include_once $app->getBasePath() . 'zin' . DS . 'wg' . DS . $name . DS . "v$wgVer.php"; - return new $name($args); + + return class_exists($name) ? (new $name($args)) : $name($args); } diff --git a/zin/wg/page/v1.php b/zin/wg/page/v1.php index 5dc858500f..40673767a2 100644 --- a/zin/wg/page/v1.php +++ b/zin/wg/page/v1.php @@ -18,7 +18,7 @@ class page extends wg $this->setDefaultProps(array('lang' => $clientLang)); - if($this->props('zui') !== false && isset($config->zin->zuiPath)) + if($this->prop('zui') && isset($config->zin->zuiPath)) { $this->importJs($config->zin->zuiPath . 'zui.zentao.umd.cjs') ->importCss($config->zin->zuiPath . 'zui.zentao.css'); @@ -59,8 +59,8 @@ class page extends wg global $lang; $headBuilder = wg::createBuilder('head') - ->append($this->prop('metas')) - ->append('' . htmlspecialchars($this->props->get('title', '')) . " - $lang->zentaoPMS") + ->before($this->prop('metas')) + ->before('' . htmlspecialchars($this->props->get('title', '')) . " - $lang->zentaoPMS") ->css($this->cssList) ->importCss($this->cssImports) ->renderInTag();