From 7dfa2deac3601bb45cd73370ba92867c28318fd6 Mon Sep 17 00:00:00 2001 From: Hao Sun Date: Fri, 17 Feb 2023 12:36:48 +0800 Subject: [PATCH] + zin: add zui widget to use js component in zui3 --- zin/core/directive.func.php | 7 +++--- zin/core/h.class.php | 15 +++++++++++-- zin/core/h.func.php | 1 + zin/core/zui.class.php | 43 +++++++++++++++++++++++++++++++++++++ zin/wg/pagebase/v1.php | 14 +++++++++++- 5 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 zin/core/zui.class.php diff --git a/zin/core/directive.func.php b/zin/core/directive.func.php index 69c71913c2..15a732bbe6 100644 --- a/zin/core/directive.func.php +++ b/zin/core/directive.func.php @@ -21,7 +21,7 @@ use stdClass; * @access public * @return object */ -function directive($type, $data) +function directive($type, $data, $options = NULL) { if(!in_array($type, array('prop', 'class', 'style', 'cssVar', 'block', 'html', 'text'))) { @@ -30,8 +30,9 @@ function directive($type, $data) $directive = new stdClass(); $directive->directive = true; - $directive->type = $type; - $directive->data = $data; + $directive->type = $type; + $directive->data = $data; + $directive->options = $options; return $directive; } diff --git a/zin/core/h.class.php b/zin/core/h.class.php index 994883bb81..6f856d8426 100644 --- a/zin/core/h.class.php +++ b/zin/core/h.class.php @@ -144,12 +144,12 @@ class h extends wg public static function css() { - return static::create('style', html(implode('\n', \zin\utils\flat(func_get_args())))); + return static::create('style', html(implode("\n", \zin\utils\flat(func_get_args())))); } public static function js() { - return static::create('script', html('(function(){'. implode('\n', \zin\utils\flat(func_get_args())) . '}())')); + return static::create('script', html('(function(){'. implode("\n", \zin\utils\flat(func_get_args())) . '}())')); } public static function jsVar($name, $value) @@ -165,6 +165,17 @@ class h extends wg return static::js($jsCode); } + public static function jsCall() + { + $args = func_get_args(); + $func = array_shift($args); + foreach($args as $index => $arg) + { + $args[$index] = json_encode($arg, JSON_UNESCAPED_UNICODE); + } + return static::js($func . '(' . implode(',', $args) . ');'); + } + protected static function convertStrToRawHtml($children) { $children = \zin\utils\flat($children); diff --git a/zin/core/h.func.php b/zin/core/h.func.php index abeea94262..f8885e2558 100644 --- a/zin/core/h.func.php +++ b/zin/core/h.func.php @@ -16,6 +16,7 @@ require_once 'item.class.php'; require_once 'wg.func.php'; require_once 'set.class.php'; require_once 'to.class.php'; +require_once 'zui.class.php'; function h() {return call_user_func_array('\zin\h::create', func_get_args());} diff --git a/zin/core/zui.class.php b/zin/core/zui.class.php new file mode 100644 index 0000000000..aabf96d0d6 --- /dev/null +++ b/zin/core/zui.class.php @@ -0,0 +1,43 @@ + + * @package zin + * @version $Id + * @link https://www.zentao.net + */ + +namespace zin; + +require_once 'wg.class.php'; +require_once 'wg.func.php'; + +class zui extends wg +{ + static $defineProps = '_name:string, _to?:string, _tag:string="div", _toProps?: array'; + + protected function build() + { + list($name, $target, $tagName, $targetProps) = $this->prop(array('_name', '_to', '_tag', '_toProps')); + $selector = empty($target) ? "[data-zin-id='$this->gid']" : $target; + $options = $this->props->skip(array_keys(static::getDefinedProps())); + return array + ( + empty($target) ? h + ( + $tagName, + set($targetProps), + set('data-zin-id', $this->gid) + ) : NULL, + $this->children(), + h::jsCall('zui.create', $name, $selector, $options) + ); + } + + public static function __callStatic($name, $args) + { + return new zui(set('_name', $name), $args); + } +} diff --git a/zin/wg/pagebase/v1.php b/zin/wg/pagebase/v1.php index ef427d875a..4070490901 100644 --- a/zin/wg/pagebase/v1.php +++ b/zin/wg/pagebase/v1.php @@ -32,7 +32,19 @@ class pagebase extends wg html($this->prop('metas')), h::title($this->props->get('title', '') . " - $lang->zentaoPMS"), $zui ? h::import(array($config->zin->zuiPath . 'zui.zentao.umd.cjs', $config->zin->zuiPath . 'zui.zentao.css')) : null, - h::js('window.domReady = function(fn){if (document.readyState !== \'loading\') {fn();} else {document.addEventListener(\'DOMContentLoaded\', fn);}}'), + h::js('window.domReady = function(fn){if (document.readyState !== \'loading\') {fn();} else {document.addEventListener(\'DOMContentLoaded\', fn);}};'), + $zui ? h::js + ( + 'zui.create = function(name, element, options){', + 'if(!zui.componentsMap) zui.componentsMap = Object.keys(zui).reduce(function(map, n){', + 'if(n[0] !== n[0].toUpperCase()) return map;', + 'map[n.toLowerCase()] = zui[n];', + 'return map;', + '}, {});', + 'const Component = zui.componentsMap[name.toLowerCase()];', + 'return Component ? new Component(element, options) : null;', + '};' + ) : null, $this->block('head'), ), h::body