v1.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace zin;
  3. class breadcrumb extends wg
  4. {
  5. /**
  6. * @var mixed[]
  7. */
  8. protected static $defineProps = array
  9. (
  10. 'items' => '?array',
  11. 'divider' => '?string',
  12. 'labelWidth' => '?int=76'
  13. );
  14. public static function getPageCSS()
  15. {
  16. return <<<CSS
  17. .breadcrumb.flex-wrap{padding-inline-start:1.25rem;box-sizing:border-box}
  18. .breadcrumb.flex-wrap>li:first-child{margin-inline-start:-1.25rem}
  19. CSS;
  20. }
  21. public function onBuildItem($item)
  22. {
  23. if($item === null) return null;
  24. if($item instanceof item)
  25. {
  26. $item = array_merge($item->props->toArray(), array('children' => $item->children()));
  27. }
  28. if($item instanceof node) return $item;
  29. $active = isset($item['active']) && $item['active'];
  30. $url = isset($item['url']) ? $item['url'] : null;
  31. $text = isset($item['text']) ? $item['text'] : null;
  32. $children = isset($item['children']) ? $item['children'] : null;
  33. $liProps = isset($item['liProps']) ? $item['liProps'] : null;
  34. $icon = isset($item['icon']) ? $item['icon'] : null;
  35. unset($item['active']);
  36. unset($item['url']);
  37. unset($item['text']);
  38. unset($item['children']);
  39. unset($item['icon']);
  40. if(is_string($icon)) $icon = icon($icon);
  41. elseif(is_array($icon)) $icon = icon(set($icon));
  42. $tag = $url ? 'a' : 'span';
  43. return h::li
  44. (
  45. $liProps ? set($liProps) : null,
  46. $active ? setClass('active') : null,
  47. h::$tag
  48. (
  49. set::href($url),
  50. set($item),
  51. $icon,
  52. $text,
  53. $children
  54. )
  55. );
  56. }
  57. protected function buildItems()
  58. {
  59. $items = $this->prop('items');
  60. $itemsView = array();
  61. if(is_array($itemsView))
  62. {
  63. foreach ($items as $key => $item)
  64. {
  65. if(is_string($item)) $item = array('text' => $item);
  66. if(is_array($item) && is_string($key)) $item['text'] = $key;
  67. $itemsView[] = $this->onBuildItem($item);
  68. }
  69. }
  70. return $itemsView;
  71. }
  72. protected function build()
  73. {
  74. return h::ol
  75. (
  76. setClass('breadcrumb flex-wrap min-w-0 flex-1'),
  77. setStyle('--breadcrumb-divider', $this->prop('divider')),
  78. set($this->getRestProps()),
  79. $this->buildItems(),
  80. $this->children()
  81. );
  82. }
  83. }