v1.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace zin;
  3. require_once dirname(__DIR__) . DS . 'content' . DS . 'v1.php';
  4. class datalist extends wg
  5. {
  6. /**
  7. * @var mixed[]
  8. */
  9. protected static $defineProps = array
  10. (
  11. 'items' => '?array',
  12. 'labelWidth' => '?int=68'
  13. );
  14. public static function getPageCSS()
  15. {
  16. return <<<CSS
  17. .datalist-item {display: flex; gap: 8px; padding: 6px 0;}
  18. .datalist-item-label {width: var(--datalist-label-width); color: var(--color-gray-500); flex: none; display: flex; align-items: center; justify-content: flex-end; white-space: nowrap; overflow: hidden; text-overflow: clip;}
  19. .datalist-item-content {flex: 1; display: flex; gap: 8px; align-items: center;}
  20. CSS;
  21. }
  22. public function onBuildItem($item)
  23. {
  24. if($item === null) return null;
  25. if($item instanceof setting) $item = $item->toArray();
  26. if($item instanceof item)
  27. {
  28. $item = array_merge($item->props->toArray(), array('children' => $item->children()));
  29. }
  30. if($item instanceof node) return $item;
  31. $class = isset($item['class']) ? $item['class'] : null;
  32. $label = isset($item['label']) ? $item['label'] : null;
  33. $children = isset($item['children']) ? $item['children'] : null;
  34. $content = isset($item['content']) ? $item['content'] : null;
  35. $labelClass = isset($item['labelClass']) ? $item['labelClass'] : null;
  36. $contentClass = isset($item['contentClass']) ? $item['contentClass'] : null;
  37. unset($item['class']);
  38. unset($item['label']);
  39. unset($item['children']);
  40. unset($item['content']);
  41. unset($item['labelClass']);
  42. unset($item['contentClass']);
  43. $content = isset($item['control']) ? new content(set($item), set::content($content)) : $content;
  44. return div
  45. (
  46. setClass('datalist-item', $class),
  47. div
  48. (
  49. setClass('datalist-item-label', $labelClass),
  50. set::title($label),
  51. span(setClass('text'), $label)
  52. ),
  53. div
  54. (
  55. setClass('datalist-item-content', $contentClass),
  56. $content,
  57. $children
  58. )
  59. );
  60. }
  61. protected function buildItems()
  62. {
  63. $items = $this->prop('items');
  64. $itemsView = array();
  65. if(is_array($itemsView))
  66. {
  67. foreach ($items as $key => $item)
  68. {
  69. if($item === '-') $item = array('control' => 'divider');
  70. elseif(is_string($item)) $item = array('children' => $item);
  71. if(is_array($item) && is_string($key)) $item['label'] = $key;
  72. $itemsView[] = $this->onBuildItem($item);
  73. }
  74. }
  75. return $itemsView;
  76. }
  77. protected function build()
  78. {
  79. global $app;
  80. $currentLang = $app->getClientLang();
  81. return div
  82. (
  83. setClass('datalist break-all overflow-hidden text-clip'),
  84. setStyle('--datalist-label-width', $this->prop('labelWidth') . 'px'),
  85. set($this->getRestProps()),
  86. strpos($currentLang, 'zh-') !== false ? null : on::init()->call('$.autoLabelWidth', ['container' => jsRaw('$element'), 'item' => '.datalist-item', 'label' => '.datalist-item-label', 'text' => '.text', 'cssVar' => '--datalist-label-width']),
  87. $this->buildItems(),
  88. $this->children()
  89. );
  90. }
  91. }