v1.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace zin;
  3. require_once dirname(__DIR__) . DS . 'btn' . DS . 'v1.php';
  4. require_once dirname(__DIR__) . DS . 'backbtn' . DS . 'v1.php';
  5. require_once dirname(__DIR__) . DS . 'actionitem' . DS . 'v1.php';
  6. class toolbar extends wg
  7. {
  8. /**
  9. * @var mixed[]
  10. */
  11. protected static $defineProps = array
  12. (
  13. 'items?: array',
  14. 'btnClass?: string',
  15. 'btnType?: string',
  16. 'size?: string',
  17. 'gap?: int',
  18. 'btnProps?: array',
  19. 'urlFormatter?: array'
  20. );
  21. public function onBuildItem($item)
  22. {
  23. if($item === null) return null;
  24. if(!($item instanceof item))
  25. {
  26. if($item instanceof node) return $item;
  27. $item = item(set($item));
  28. }
  29. $type = $item->prop('type');
  30. if($type === 'divider') return div(setClass('divider toolbar-divider'));
  31. if($type === 'btnGroup') return new btnGroup(inherit($item));
  32. if($type == 'dropdown' || $type == 'checkbox') return new actionItem(inherit($item));
  33. list($btnClass, $btnProps, $btnType, $size) = $this->prop(array('btnClass', 'btnProps', 'btnType', 'size'));
  34. $btn = empty($item->prop('back')) ? '\zin\btn' : '\zin\backBtn';
  35. return new $btn(setClass('toolbar-item', $btnClass), set::type($btnType), set::size($size), is_array($btnProps) ? set($btnProps) : null, inherit($item));
  36. }
  37. protected function buildItems()
  38. {
  39. $items = $this->prop('items');
  40. if(!$items) return null;
  41. $urlFormatter = $this->prop('urlFormatter');
  42. $itemGroups = array();
  43. foreach($items as $item)
  44. {
  45. if($item === '-') $item = array('type' => 'divider');
  46. $group = null;
  47. if(is_array($item) && isset($item['group']))
  48. {
  49. $group = $item['group'];
  50. unset($item['group']);
  51. }
  52. if(is_null($group)) $group = count($itemGroups) ? array_keys($itemGroups)[0] : '';
  53. if($urlFormatter && is_array($item))
  54. {
  55. $url = isset($item['url']) ? $item['url'] : null;
  56. if($url)
  57. {
  58. $url = str_replace(array_keys($urlFormatter), array_values($urlFormatter), $url);
  59. $item['url'] = $url;
  60. }
  61. if(!empty($item['data-url'])) $item['data-url'] = str_replace(array_keys($urlFormatter), array_values($urlFormatter), $item['data-url']);
  62. $itemChildren = isset($item['items']) ? $item['items'] : null;
  63. if(is_array($itemChildren))
  64. {
  65. foreach($itemChildren as $key => &$child)
  66. {
  67. if(is_array($child) && isset($child['url']))
  68. {
  69. $url = $child['url'];
  70. if($url)
  71. {
  72. $url = str_replace(array_keys($urlFormatter), array_values($urlFormatter), $url);
  73. $itemChildren[$key]['url'] = $url;
  74. }
  75. }
  76. if(!empty($itemChildren[$key]['data-url'])) $itemChildren[$key]['data-url'] = str_replace(array_keys($urlFormatter), array_values($urlFormatter), $itemChildren[$key]['data-url']);
  77. }
  78. $item['items'] = $itemChildren;
  79. }
  80. }
  81. if(!isset($itemGroups[$group])) $itemGroups[$group] = array();
  82. $itemGroups[$group][] = $item;
  83. }
  84. $list = array();
  85. foreach($itemGroups as $group => $items)
  86. {
  87. if(count($list)) $list[] = div(setClass('divider toolbar-divider'));
  88. foreach($items as $item)
  89. {
  90. $list[] = $this->onBuildItem($item);
  91. }
  92. }
  93. return $list;
  94. }
  95. protected function build()
  96. {
  97. $gap = $this->prop('gap');
  98. return div
  99. (
  100. setClass('toolbar', $gap ? "gap-$gap" : ''),
  101. set($this->getRestProps()),
  102. $this->buildItems(),
  103. $this->children()
  104. );
  105. }
  106. /**
  107. * @param mixed ...$children
  108. * @return $this
  109. * @param mixed[] $propsOrItems
  110. */
  111. public static function create($propsOrItems, ...$children)
  112. {
  113. $props = array_is_list($propsOrItems) ? array('items' => $propsOrItems) : $propsOrItems;
  114. return new static(set($props), ...$children);
  115. }
  116. }