v1.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace zin;
  3. require_once dirname(__DIR__) . DS . 'idlabel' . DS . 'v1.php';
  4. require_once dirname(__DIR__) . DS . 'simplelist' . DS . 'v1.php';
  5. class entityList extends wg
  6. {
  7. /**
  8. * @var mixed[]
  9. */
  10. protected static $defineProps = array
  11. (
  12. 'name' => '?string', // 列表名称。
  13. 'items' => 'array', // 对象对象列表。
  14. 'type' => '?string', // 对象类型。
  15. 'viewUrl' => '?string|bool=true', // 查看链接模版,使用 `{id}` 代替对象 ID,如果设置为 false 不展示链接,如果设置为 true 则使用默认链接。
  16. 'divider' => '?bool', // 是否显示分割线。
  17. 'border' => '?bool', // 是否显示边框。
  18. 'hover' => '?bool=true', // 是否有鼠标悬停效果。
  19. 'compact' => '?bool=true', // 是否显示为紧凑模式。
  20. 'onRenderItem' => '?callable' // 渲染对象对象的回调函数。
  21. );
  22. /**
  23. * @var string
  24. */
  25. protected $viewUrl = '';
  26. /**
  27. * @var bool
  28. */
  29. protected $compact = true;
  30. protected function created()
  31. {
  32. if(!$this->hasProp('name'))
  33. {
  34. $type = $this->prop('type');
  35. $this->setProp('name', $type ? "$type-list" : 'entity-list');
  36. }
  37. }
  38. /**
  39. * @param object $entity
  40. */
  41. protected function getItem($entity)
  42. {
  43. $item = array
  44. (
  45. 'innerClass' => 'px-0 relative group',
  46. 'leading' => array(),
  47. 'innerTag' => 'div',
  48. 'titleClass' => 'flex gap-2 items-center flex-auto min-w-0',
  49. 'textClass' => 'flex-none',
  50. 'actionsClass' => $this->compact ? 'absolute top-0.5 right-0' : null,
  51. 'hint' => $entity->title,
  52. 'title' => span(setClass('text-clip'), $entity->title),
  53. 'leading' => idLabel::create($entity->id)
  54. );
  55. if($this->viewUrl)
  56. {
  57. $item['titleAttrs'] = array('data-toggle' => 'modal', 'data-size' => 'lg');
  58. $item['url'] = str_replace('{id}', "$entity->id", $this->viewUrl);
  59. }
  60. return $item;
  61. }
  62. protected function getItems()
  63. {
  64. $items = $this->prop('items', array());
  65. $list = array();
  66. foreach($items as $key => $entity)
  67. {
  68. if(is_string($entity)) $entity = (object)array('id' => $key, 'title' => $entity);
  69. $list[] = $this->getItem($entity);
  70. }
  71. return $list;
  72. }
  73. protected function beforeBuild()
  74. {
  75. $viewUrl = $this->prop('viewUrl');
  76. $type = $this->prop('type');
  77. if($type)
  78. {
  79. if($viewUrl === null) $viewUrl = hasPriv($type, 'view');
  80. if($viewUrl === true) $viewUrl = createLink($type, 'view', 'id={id}');
  81. }
  82. $this->viewUrl = $viewUrl;
  83. $this->compact = $this->prop('compact');
  84. }
  85. protected function build()
  86. {
  87. $this->beforeBuild();
  88. return new simpleList
  89. (
  90. setClass($this->prop('name')),
  91. set::items($this->getItems()),
  92. set::divider($this->prop('divider')),
  93. set::border($this->prop('border')),
  94. set::hover($this->prop('hover')),
  95. set::compact($this->compact),
  96. set::onRenderItem($this->prop('onRenderItem')),
  97. set($this->getRestProps()),
  98. $this->children()
  99. );
  100. }
  101. }