v1.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace zin;
  3. require_once dirname(__DIR__) . DS . 'icon' . DS . 'v1.php';
  4. require_once dirname(__DIR__) . DS . 'checkbox' . DS . 'v1.php';
  5. require_once dirname(__DIR__) . DS . 'avatar' . DS . 'v1.php';
  6. require_once dirname(__DIR__) . DS . 'toolbar' . DS . 'v1.php';
  7. class listItem extends wg
  8. {
  9. /**
  10. * @var bool
  11. */
  12. public $isLink = false;
  13. /**
  14. * @var bool
  15. */
  16. public $multiline = false;
  17. /**
  18. * @var mixed[]
  19. */
  20. protected static $defineProps = array
  21. (
  22. 'type' => '?string', // divider, heading, item
  23. 'tagName' => '?string="div"',
  24. 'innerTag' => '?string',
  25. 'innerClass' => '?string|array',
  26. 'innerAttrs' => '?array',
  27. 'multiline' => '?bool',
  28. 'icon' => '?string|array',
  29. 'toggleIcon' => '?string|array',
  30. 'selected' => '?bool',
  31. 'disabled' => '?bool',
  32. 'checked' => '?bool|array',
  33. 'active' => '?bool',
  34. 'divider' => '?bool',
  35. 'avatar' => '?array',
  36. 'leading' => '?mixed',
  37. 'leadingClass' => '?string|array',
  38. 'url' => '?string',
  39. 'target' => '?string',
  40. 'text' => '?string',
  41. 'textClass' => '?string|array',
  42. 'title' => '?string',
  43. 'titleClass' => '?string|array',
  44. 'titleAttrs' => '?array',
  45. 'subtitle' => '?string',
  46. 'subtitleClass' => '?string|array',
  47. 'hint' => '?string',
  48. 'trailing' => '?mixed',
  49. 'trailingIcon' => '?string|array',
  50. 'actions' => '?array',
  51. 'actionsClass' => '?array|string',
  52. 'content' => '?mixed',
  53. 'contentClass' => '?string|array',
  54. 'contentAttrs' => '?array',
  55. );
  56. protected function buildLeading()
  57. {
  58. list($icon, $avatar, $toggleIcon, $leading, $leadingClass, $checked, $checkbox) = $this->prop(array('icon', 'avatar', 'toggleIcon', 'leading', 'leadingClass', 'checked', 'checkbox'));
  59. $views = array();
  60. if($toggleIcon) $views[] = icon::create($toggleIcon);
  61. if($checked !== null) $views[] = checkbox::create($checked, array('class' => 'item-checkbox'), $checkbox ? set($checkbox) : null);
  62. if($icon) $views[] = icon::create($icon, array('class' => 'item-icon'));
  63. if($avatar) $views[] = new avatar(setClass('item-avatar'), set($avatar));
  64. if($leading) $views[] = $leading;
  65. if($this->multiline && $views)
  66. {
  67. $views = div
  68. (
  69. setClass('item-leading', $leadingClass),
  70. $views
  71. );
  72. }
  73. return $views;
  74. }
  75. protected function buildContent()
  76. {
  77. list($text, $title, $textClass, $titleClass, $titleAttrs, $subtitle, $subtitleClass, $url, $target, $content, $contentClass, $contentAttrs) = $this->prop(array('text', 'title', 'textClass','titleClass','titleAttrs','subtitle','subtitleClass','url','target','content','contentClass','contentAttrs'));
  78. if($title === null)
  79. {
  80. $title = $text;
  81. $text = null;
  82. }
  83. $titleTag = (!$this->isLink && $url) ? 'a' : 'div';
  84. return div
  85. (
  86. setClass('item-content', $contentClass),
  87. set($contentAttrs),
  88. $title ? h::$titleTag
  89. (
  90. setClass('item-title', $titleClass),
  91. set($titleAttrs),
  92. $titleTag === 'a' ? set(array('href' => $url, 'target' => $target)) : null,
  93. $title
  94. ) : null,
  95. $subtitle ? div
  96. (
  97. setClass('item-subtitle', $subtitleClass),
  98. $subtitle
  99. ) : null,
  100. $text ? div
  101. (
  102. setClass('item-text text', $textClass),
  103. $text
  104. ) : null,
  105. $content
  106. );
  107. }
  108. protected function buildTrailing()
  109. {
  110. list($trailing, $trailingClass, $trailingIcon, $actions, $actionsClass) = $this->prop(array('trailing', 'trailingClass', 'trailingIcon', 'actions', 'actionsClass'));
  111. $views = array();
  112. if($trailingIcon) $views[] = icon::create($trailingIcon, array('class' => 'item-trailing-icon'));
  113. if($actions) $views[] = toolbar::create($actions, set::size('sm'), $actionsClass ? setClass($actionsClass) : null);
  114. if($trailing) $views[] = $trailing;
  115. if($this->multiline && $views)
  116. {
  117. $views = div
  118. (
  119. setClass('item-trailing', $trailingClass),
  120. $views
  121. );
  122. }
  123. return $views;
  124. }
  125. protected function buildInner()
  126. {
  127. list($innerTag, $innerClass, $innerAttrs, $active, $disabled, $selected, $divider, $checked, $hint, $url, $target) = $this->prop(array('innerTag', 'innerClass', 'innerAttrs', 'active', 'disabled', 'selected', 'divider', 'checked', 'hint', 'url', 'target'));
  128. $isLink = $this->isLink;
  129. if(!$innerTag) $innerTag = $isLink ? 'a' : 'div';
  130. return h::$innerTag
  131. (
  132. setClass('listitem', array('active' => $active, 'disabled' => $disabled, 'selected' => $selected, 'has-divider' => $divider, 'checked' => $checked, 'multiline' => $this->multiline), $innerClass),
  133. set::title($hint),
  134. $isLink ? set(array('href' => $url ? $url : 'javascript:;', 'target' => $target)) : null,
  135. set($innerAttrs),
  136. $this->buildLeading(),
  137. $this->buildContent(),
  138. $this->buildTrailing()
  139. );
  140. }
  141. protected function build()
  142. {
  143. list($tagName, $innerTag, $url, $multiline, $title, $subtitle) = $this->prop(array('tagName', 'innerTag', 'url', 'multiline', 'title', 'subtitle'));
  144. $this->isLink = $url && (!$innerTag || $innerTag === 'a');
  145. $this->multiline = is_null($multiline) ? ($subtitle && $title) : $multiline;
  146. return h::$tagName
  147. (
  148. setClass('item'),
  149. set($this->getRestProps()),
  150. $this->buildInner()
  151. );
  152. }
  153. }