From 53bfbcdee60b1d09cbceff75d42d136ec038fdb2 Mon Sep 17 00:00:00 2001 From: sunhao Date: Fri, 8 Mar 2024 09:08:29 +0800 Subject: [PATCH] + zin: add entityList widget. --- lib/zin/func.php | 1 + lib/zin/wg/entitylist/v1.php | 93 ++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 lib/zin/wg/entitylist/v1.php diff --git a/lib/zin/func.php b/lib/zin/func.php index 73551ca6df..0a615ad670 100644 --- a/lib/zin/func.php +++ b/lib/zin/func.php @@ -37,6 +37,7 @@ function content(): content {return createWg('content', func_get_args());} function idLabel(): idLabel {return createWg('idLabel', func_get_args());} function listItem(): listItem {return createWg('listitem', func_get_args());} function simpleList(): simpleList {return createWg('simplelist', func_get_args());} +function entityList(): entityList {return createWg('entityList', func_get_args());} function breadcrumb(): breadcrumb {return createWg('breadcrumb', func_get_args());} function datalist(): datalist {return createWg('datalist', func_get_args());} function control(): control {return createWg('control', func_get_args());} diff --git a/lib/zin/wg/entitylist/v1.php b/lib/zin/wg/entitylist/v1.php new file mode 100644 index 0000000000..99e4673b5e --- /dev/null +++ b/lib/zin/wg/entitylist/v1.php @@ -0,0 +1,93 @@ + '?string', // 列表名称。 + 'items' => 'array', // 对象对象列表。 + 'type' => '?string', // 对象类型。 + 'viewUrl' => '?string|bool=true', // 查看链接模版,使用 `{id}` 代替对象 ID,如果设置为 false 不展示链接,如果设置为 true 则使用默认链接。 + 'onRenderItem' => '?callable' // 渲染对象对象的回调函数。 + ); + + protected string $viewUrl = ''; + + protected function created() + { + if(!$this->hasProp('name')) + { + $type = $this->prop('type'); + $this->setProp('name', $type ? "$type-list" : 'entity-list'); + } + } + + protected function getItem(object $entity): array + { + $item = array + ( + 'innerClass' => 'px-0 relative group', + 'leading' => array(), + 'innerTag' => 'div', + 'titleClass' => 'flex gap-2 items-center flex-auto min-w-0', + 'textClass' => 'flex-none', + 'actionsClass' => 'absolute top-0 right-0', + 'hint' => $entity->title, + 'title' => span(setClass('text-clip'), $entity->title), + 'leading' => idLabel::create($entity->id) + ); + + if($this->viewUrl) + { + $item['titleAttrs'] = array('data-toggle' => 'modal', 'data-size' => 'lg'); + $item['url'] = str_replace('{id}', "$entity->id", $this->viewUrl); + } + + return $item; + } + + protected function getItems() + { + $items = $this->prop('items', array()); + $list = array(); + + foreach($items as $key => $entity) + { + if(is_string($entity)) $entity = (object)array('id' => $key, 'title' => $entity); + $list[] = $this->getItem($entity); + } + + return $list; + } + + protected function beforeBuild() + { + $viewUrl = $this->prop('viewUrl'); + $type = $this->prop('type'); + if($type) + { + if($viewUrl === null) $viewUrl = hasPriv($type, 'view'); + if($viewUrl === true) $viewUrl = createLink($type, 'view', 'id={id}'); + } + $this->viewUrl = $viewUrl; + } + + protected function build() + { + $this->beforeBuild(); + + return new simpleList + ( + setClass($this->prop('name')), + set::items($this->getItems()), + set::onRenderItem($this->prop('onRenderItem')), + set($this->getRestProps()), + $this->children() + ); + } +}