v1.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace zin;
  3. require_once dirname(__DIR__) . DS . 'btn' . DS . 'v1.php';
  4. require_once dirname(__DIR__) . DS . 'dropdown' . DS . 'v1.php';
  5. require_once dirname(__DIR__) . DS . 'checkbox' . DS . 'v1.php';
  6. require_once dirname(__DIR__) . DS . 'btngroup' . DS . 'v1.php';
  7. class actionItem extends wg
  8. {
  9. /**
  10. * @var mixed[]
  11. */
  12. protected static $defineProps = array(
  13. 'name:string="action"',
  14. 'type:string="item"',
  15. 'outerTag:string="li"',
  16. 'tagName:string="a"',
  17. 'icon?:string',
  18. 'text?:string',
  19. 'textClass?: string',
  20. 'url?:string',
  21. 'target?:string',
  22. 'active?:bool',
  23. 'disabled?:bool',
  24. 'selected?:bool',
  25. 'trailingIcon?:string',
  26. 'outerProps?:array',
  27. 'outerClass?:string',
  28. 'badge?:string|array|object',
  29. 'props?:array',
  30. 'dropdown?:array',
  31. 'items?:array',
  32. 'caret?:bool|string'
  33. );
  34. protected function buildDividerItem()
  35. {
  36. return setClass('divider');
  37. }
  38. protected function buildHeadingItem()
  39. {
  40. list($icon, $text, $trailingIcon, $textClass) = $this->prop(array('icon', 'text', 'trailingIcon', 'textClass'));
  41. return h::div
  42. (
  43. set($this->props->skip(array_keys(actionItem::definedPropsList()))),
  44. set($this->prop('props')),
  45. $icon ? icon($icon) : null,
  46. empty($text) ? null : span($text, setClass('text', $textClass)),
  47. $this->children(),
  48. $trailingIcon ? icon($trailingIcon) : null
  49. );
  50. }
  51. protected function buildDropdownItem()
  52. {
  53. list($dropdown, $items, $icon, $text, $trailingIcon, $active, $disabled, $badge, $props, $caret, $textClass, $trigger, $menu, $placement) = $this->prop(array('dropdown', 'items', 'icon', 'text', 'trailingIcon', 'active', 'disabled', 'badge', 'props', 'caret', 'textClass', 'trigger', 'menu', 'placement'));
  54. if(is_string($badge))
  55. {
  56. $badge = label($badge);
  57. }
  58. elseif(is_array($badge))
  59. {
  60. $badge = label(set($badge));
  61. }
  62. $dropdown = new dropdown
  63. (
  64. set::items($items),
  65. set::trigger($trigger),
  66. set::menu($menu),
  67. set::placement($placement),
  68. set($dropdown),
  69. h::a(
  70. setClass(array('active' => $active, 'disabled' => $disabled)),
  71. set($this->getRestProps()),
  72. set($props),
  73. $icon ? icon($icon) : null,
  74. span($text, setClass('text', $textClass)),
  75. $badge,
  76. $this->children(),
  77. $trailingIcon ? icon($trailingIcon) : null,
  78. $caret !== false ? h::span(setClass(is_string($caret) ? "caret-$caret" : 'caret')) : null
  79. )
  80. );
  81. return $dropdown;
  82. }
  83. protected function buildBtnItem()
  84. {
  85. return new btn($this->props->skip('tagName,type,name,outerTag,outerProps,props'), set($this->prop('props')),$this->children());
  86. }
  87. protected function buildCheckboxItem()
  88. {
  89. return new checkbox($this->props->skip('tagName,type,name,outerTag,outerProps,props'), set($this->prop('props')),$this->children());
  90. }
  91. protected function buildBtnGroupItem()
  92. {
  93. return new btnGroup($this->props->skip('tagName,type,name,outerTag,outerProps,props'), set($this->prop('props')),$this->children());
  94. }
  95. protected function buildItem()
  96. {
  97. $type = $this->prop('type');
  98. $methodName = "build{$type}Item";
  99. if(method_exists($this, $methodName)) return $this->$methodName();
  100. list($tagName, $icon, $text, $trailingIcon, $url, $target, $active, $disabled, $badge, $textClass, $selected) = $this->prop(array('tagName', 'icon', 'text', 'trailingIcon', 'url', 'target', 'active', 'disabled', 'badge', 'textClass', 'selected'));
  101. if(is_string($badge)) $badge = label($badge);
  102. else if(is_array($badge)) $badge = label(set($badge));
  103. return h::create
  104. (
  105. $tagName,
  106. set($tagName === 'a' ? array('href' => $url, 'target' => $target) : array('data-url' => $url, 'data-target' => $target)),
  107. setClass(array('active' => $active, 'disabled' => $disabled, 'selected' => $selected)),
  108. set($this->getRestProps()),
  109. set($this->prop('props')),
  110. $icon ? icon($icon) : null,
  111. span($text, setClass('text', $textClass)),
  112. $badge,
  113. $this->children(),
  114. $trailingIcon ? icon($trailingIcon) : null
  115. );
  116. }
  117. protected function build()
  118. {
  119. list($name, $type, $outerTag, $outerProps, $outerClass) = $this->prop(array('name', 'type', 'outerTag', 'outerProps', 'outerClass'));
  120. return h::create
  121. (
  122. $outerTag,
  123. setClass(($type !== 'item' && $type !== 'divider' && $type != 'text') ? 'nav-item' : '', "$name-$type", $outerClass),
  124. set($outerProps),
  125. $this->buildItem()
  126. );
  127. }
  128. }