From 66fa922d5b44597b3ade72a3ab96be216b0798bb Mon Sep 17 00:00:00 2001 From: sunhao Date: Wed, 6 Mar 2024 20:50:12 +0800 Subject: [PATCH] * zin: refactor storyList and linkedStoryList. --- lib/zin/wg/linkedstorylist/v1.php | 64 ++++++++++++++++++++++--------- lib/zin/wg/storylist/v1.php | 40 +++++++++++-------- 2 files changed, 71 insertions(+), 33 deletions(-) diff --git a/lib/zin/wg/linkedstorylist/v1.php b/lib/zin/wg/linkedstorylist/v1.php index 16ce944d65..64ac07aed6 100644 --- a/lib/zin/wg/linkedstorylist/v1.php +++ b/lib/zin/wg/linkedstorylist/v1.php @@ -10,11 +10,18 @@ class linkedStoryList extends storyList protected static array $defineProps = array ( 'story' => '?object', // 被关联的需求。 + 'relationType' => '?string', // 关联类型。 'unlinkBtn' => '?array|bool', // 取消关联按钮。 'unlinkStoryTip' => '?string', // 取消关联提示信息。 + 'unlinkUrl' => '?string', // 取消关联需求链接 'newLinkBtn' => '?array|bool' // 底部新关联按钮。 ); + protected static array $defaultProps = array + ( + 'name' => 'linked-story-list' + ); + public static function getPageJS(): ?string { return <<<'JS' @@ -31,31 +38,45 @@ class linkedStoryList extends storyList protected ?object $story = null; - protected function created() + protected ?string $unlinkUrl = null; + + protected null|array|bool $unlinkBtn = null; + + protected ?string $relationType = null; + + protected function beforeBuild() { $story = $this->prop('story'); if(!$story) $story = data('story'); if(!$story) return; - $this->story = $story; + $relationType = $this->prop('relationType'); + if(!$relationType) $relationType = $story->type == 'story' ? 'requirement' : 'story'; - $canLinkStory = hasPriv($story->type, 'linkStory'); - if(!$this->hasProp('unlinkBtn')) $this->setProp('unlinkBtn', $canLinkStory); - if(!$this->hasProp('newLinkBtn')) $this->setProp('newLinkBtn', $canLinkStory); - if(!$this->hasProp('storyType')) $this->setProp('storyType', $story->type == 'story' ? 'requirement' : 'story'); - if(!$this->hasProp('unlinkStoryTip')) - { - global $lang; - $this->setProp('unlinkStoryTip', $story->type == 'story' ? str_replace($lang->SRCommon, $lang->URCommon, $lang->story->unlinkStory) : $lang->story->unlinkStory); - } + $viewUrl = $this->prop('viewUrl'); + if($viewUrl === null) $viewUrl = hasPriv($relationType, 'view', null, "storyType=$relationType"); + if($viewUrl === true) $viewUrl = createLink('story', 'view', "id={id}&version=0¶m=0&storyType=$relationType"); + + $unlinkUrl = $this->prop('unlinkUrl'); + if($unlinkUrl === null) $unlinkUrl = createLink('story', 'linkStory', "storyID={$story->id}&type=remove&linkedID={id}&browseType=&queryID=0&storyType=$story->type"); + + $canLink = hasPriv($story->type, 'linkStory'); + $unlinkBtn = $this->prop('unlinkBtn'); + if($unlinkBtn === null) $unlinkBtn = $canLink; + + $this->story = $story; + $this->relationType = $relationType; + $this->viewUrl = $viewUrl; + $this->unlinkUrl = $unlinkUrl; + $this->unlinkBtn = $unlinkBtn; } - protected function getItem(object $story, bool $canView, string $storyType = 'story'): array + protected function getItem(object $story): array { global $lang; - $item = parent::getItem($story, $canView, $storyType); - $unlinkBtn = $this->prop('unlinkBtn'); + $item = parent::getItem($story); + $unlinkBtn = $this->unlinkBtn; if($unlinkBtn) { @@ -66,7 +87,7 @@ class linkedStoryList extends storyList 'icon' => 'unlink', 'data-id' => $story->id, 'data-on' => 'click', - 'data-url' => createLink('story', 'linkStory', "storyID=$story->id&type=remove&linkedID={$story->id}&browseType=&queryID=0&storyType=$storyType"), + 'data-url' => str_replace('{id}', "$story->id", $this->unlinkUrl), 'data-params' => 'event', 'data-call' => 'unlinkStory', 'hint' => $lang->story->unlinkStory @@ -84,13 +105,20 @@ class linkedStoryList extends storyList { $list = parent::build(); - if($this->prop('unlinkBtn')) + if($this->unlinkBtn) { - $list->add(setClass('linked-story-list group')); - $list->add(setData('unlinkStoryTip', $this->prop('unlinkStoryTip'))); + $unlinkStoryTip = $this->prop('unlinkStoryTip'); + if($unlinkStoryTip === null && $this->story) + { + global $lang; + $unlinkStoryTip = $this->story->type == 'story' ? str_replace($lang->SRCommon, $lang->URCommon, $lang->story->unlinkStory) : $lang->story->unlinkStory; + } + + $list->add(setData('unlinkStoryTip', $unlinkStoryTip)); } $newLinkBtn = $this->prop('newLinkBtn'); + if($newLinkBtn === null) $newLinkBtn = $this->unlinkBtn; if($newLinkBtn) { global $lang; diff --git a/lib/zin/wg/storylist/v1.php b/lib/zin/wg/storylist/v1.php index 4743abc5ea..3b6f07b835 100644 --- a/lib/zin/wg/storylist/v1.php +++ b/lib/zin/wg/storylist/v1.php @@ -9,12 +9,15 @@ class storyList extends wg { protected static array $defineProps = array ( - 'items' => 'array', // 需求对象列表。 - 'storyType' => '?string', // 需求类型。 - 'onRenderItem' => '?callable' // 渲染需求对象的回调函数。 + 'name' => '?string="story-list"', // 列表名称。 + 'items' => 'array', // 需求对象列表。 + 'viewUrl' => '?string|bool=true', // 查看链接模版,使用 `{id}` 代替需求 ID,如果设置为 false 不展示链接,如果设置为 true 则使用默认链接。 + 'onRenderItem' => '?callable' // 渲染需求对象的回调函数。 ); - protected function getItem(object $story, bool $canView, string $storyType = 'story'): array + protected string $viewUrl = ''; + + protected function getItem(object $story): array { $item = array ( @@ -25,14 +28,14 @@ class storyList extends wg 'textClass' => 'flex-none', 'actionsClass' => 'absolute top-0 right-0', 'hint' => $story->title, - 'title' => span(setClass('text-clip '), $story->title), + 'title' => span(setClass('text-clip'), $story->title), 'leading' => idLabel::create($story->id) ); - if($canView) + if($this->viewUrl) { $item['titleAttrs'] = array('data-toggle' => 'modal', 'data-size' => 'lg'); - $item['url'] = createLink('story', 'view', "id={$story->id}&version=0¶m=0&storyType=$storyType"); + $item['url'] = str_replace('{id}', "$story->id", $this->viewUrl); } return $item; @@ -40,27 +43,34 @@ class storyList extends wg protected function getItems() { - $stories = $this->prop('items', array()); - $storyType = $this->prop('storyType', 'story'); - $items = array(); - $canView = hasPriv($storyType, 'view', null, "storyType=$storyType"); + $stories = $this->prop('items', array()); + $items = array(); foreach($stories as $story) { - $items[] = $this->getItem($story, $canView, $storyType); + $items[] = $this->getItem($story); } return $items; } + protected function beforeBuild() + { + $viewUrl = $this->prop('viewUrl'); + if($viewUrl === null) $viewUrl = hasPriv('story', 'view'); + if($viewUrl === true) $viewUrl = createLink('story', 'view', 'id={id}'); + $this->viewUrl = $viewUrl; + } + protected function build() { - $items = $this->getItems(); + $this->beforeBuild(); return new simpleList ( - setClass('story-list'), - set::items($items), + setClass($this->prop('name')), + set::items($this->getItems()), + set::onRenderItem($this->prop('onRenderItem')), set($this->getRestProps()), $this->children() );