Tab.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace Encore\Admin\Widgets;
  3. use Illuminate\Contracts\Support\Renderable;
  4. class Tab extends Widget implements Renderable
  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 array
  14. */
  15. protected $data = [
  16. 'id' => '',
  17. 'title' => '',
  18. 'tabs' => [],
  19. 'dropDown' => [],
  20. 'active' => 0,
  21. ];
  22. public function __construct()
  23. {
  24. $this->class('nav-tabs-custom');
  25. }
  26. /**
  27. * Add a tab and its contents.
  28. *
  29. * @param string $title
  30. * @param string|Renderable $content
  31. * @param bool $active
  32. *
  33. * @return $this
  34. */
  35. public function add($title, $content, $active = false)
  36. {
  37. $this->data['tabs'][] = [
  38. 'id' => mt_rand(),
  39. 'title' => $title,
  40. 'content' => $content,
  41. 'type' => static::TYPE_CONTENT,
  42. ];
  43. if ($active) {
  44. $this->data['active'] = count($this->data['tabs']) - 1;
  45. }
  46. return $this;
  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 customAdd($title, $content, $active=1,$href='')
  58. {
  59. $this->data['tabs'][] = [
  60. 'id' => mt_rand(),
  61. 'title' => $title,
  62. 'content' => $content,
  63. 'type' => static::TYPE_LINK,
  64. 'href' =>$href
  65. ];
  66. if ($active) {
  67. $this->data['active'] = $active;
  68. }
  69. return $this;
  70. }
  71. /**
  72. * Add a link on tab.
  73. *
  74. * @param string $title
  75. * @param string $href
  76. * @param bool $active
  77. *
  78. * @return $this
  79. */
  80. public function addLink($title, $href, $active = false)
  81. {
  82. $this->data['tabs'][] = [
  83. 'id' => mt_rand(),
  84. 'title' => $title,
  85. 'href' => $href,
  86. 'type' => static::TYPE_LINK,
  87. ];
  88. if ($active) {
  89. $this->data['active'] = count($this->data['tabs']) - 1;
  90. }
  91. return $this;
  92. }
  93. /**
  94. * Set title.
  95. *
  96. * @param string $title
  97. */
  98. public function title($title = '')
  99. {
  100. $this->data['title'] = $title;
  101. }
  102. /**
  103. * Set drop-down items.
  104. *
  105. * @param array $links
  106. *
  107. * @return $this
  108. */
  109. public function dropDown(array $links)
  110. {
  111. if (is_array($links[0])) {
  112. foreach ($links as $link) {
  113. call_user_func([$this, 'dropDown'], $link);
  114. }
  115. return $this;
  116. }
  117. $this->data['dropDown'][] = [
  118. 'name' => $links[0],
  119. 'href' => $links[1],
  120. ];
  121. return $this;
  122. }
  123. /**
  124. * Render Tab.
  125. *
  126. * @return string
  127. */
  128. public function render()
  129. {
  130. $data = array_merge(
  131. $this->data,
  132. ['attributes' => $this->formatAttributes()]
  133. );
  134. return view($this->view, $data)->render();
  135. }
  136. }