Tab.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Illuminate\Contracts\Support\Renderable;
  4. class Tab extends Widget
  5. {
  6. const TYPE_CONTENT = 1;
  7. const TYPE_LINK = 2;
  8. /**
  9. * @var string
  10. */
  11. protected $view = 'admin::widgets.tab';
  12. /**
  13. * @var string
  14. */
  15. protected $padding = null;
  16. /**
  17. * @var array
  18. */
  19. protected $data = [
  20. 'id' => '',
  21. 'title' => '',
  22. 'tabs' => [],
  23. 'dropDown' => [],
  24. 'active' => 0,
  25. ];
  26. public function __construct()
  27. {
  28. $this->class('nav-tabs-default');
  29. }
  30. /**
  31. * @return $this
  32. */
  33. public function custom()
  34. {
  35. return $this->style('custom');
  36. }
  37. /**
  38. * Set style.
  39. *
  40. * @param string $style
  41. *
  42. * @return $this
  43. */
  44. public function style($style)
  45. {
  46. return $this->class('nav-tabs-'.$style);
  47. }
  48. /**
  49. * Add a tab and its contents.
  50. *
  51. * @param string $title
  52. * @param string|Renderable $content
  53. * @param bool $active
  54. *
  55. * @return $this
  56. */
  57. public function add($title, $content, $active = false)
  58. {
  59. $this->data['tabs'][] = [
  60. 'id' => mt_rand(),
  61. 'title' => $title,
  62. 'content' => $content instanceof Renderable ? $content->render() : $content,
  63. 'type' => static::TYPE_CONTENT,
  64. ];
  65. if ($active) {
  66. $this->data['active'] = count($this->data['tabs']) - 1;
  67. }
  68. return $this;
  69. }
  70. /**
  71. * Add a link on tab.
  72. *
  73. * @param string $title
  74. * @param string $href
  75. * @param bool $active
  76. *
  77. * @return $this
  78. */
  79. public function addLink($title, $href, $active = false)
  80. {
  81. $this->data['tabs'][] = [
  82. 'id' => mt_rand(),
  83. 'title' => $title,
  84. 'href' => $href,
  85. 'type' => static::TYPE_LINK,
  86. ];
  87. if ($active) {
  88. $this->data['active'] = count($this->data['tabs']) - 1;
  89. }
  90. return $this;
  91. }
  92. /**
  93. * Set tab content padding.
  94. *
  95. * @param string $padding
  96. */
  97. public function padding(string $padding)
  98. {
  99. $this->padding = 'padding:'.$padding;
  100. return $this;
  101. }
  102. public function noPadding()
  103. {
  104. return $this->padding('0');
  105. }
  106. /**
  107. * Set title.
  108. *
  109. * @param string $title
  110. */
  111. public function title($title = '')
  112. {
  113. $this->data['title'] = $title;
  114. return $this;
  115. }
  116. /**
  117. * Set drop-down items.
  118. *
  119. * @param array $links
  120. *
  121. * @return $this
  122. */
  123. public function dropdown(array $links)
  124. {
  125. if (is_array($links[0])) {
  126. foreach ($links as $link) {
  127. call_user_func([$this, 'dropDown'], $link);
  128. }
  129. return $this;
  130. }
  131. $this->data['dropDown'][] = [
  132. 'name' => $links[0],
  133. 'href' => $links[1],
  134. ];
  135. return $this;
  136. }
  137. /**
  138. * Render Tab.
  139. *
  140. * @return string
  141. */
  142. public function render()
  143. {
  144. $data = array_merge(
  145. $this->data,
  146. ['attributes' => $this->formatHtmlAttributes(), 'padding' => $this->padding]
  147. );
  148. return view($this->view, $data)->render();
  149. }
  150. }