MainSidebarMenu.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace app\widgets;
  3. use yii\helpers\ArrayHelper;
  4. use yii\helpers\Html;
  5. use yii\helpers\Url;
  6. /**
  7. * Menu displays a multi-level sidebar menu according to AdminLTE 3 design.
  8. *
  9. * The main property of Menu is [[items]], which specifies the possible items in the menu.
  10. * A menu item can contain sub-items which specify the sub-menu under that menu item.
  11. *
  12. * Menu checks the current route and request parameters to toggle certain menu items
  13. * with active state.
  14. *
  15. * The rendered HTML complies with the AdminLTE 3 design for the main sidebar component.
  16. * See [AdminLTE 3 Docs](https://adminlte.io/docs/3.0/components/main-sidebar.html).
  17. *
  18. * @package backend\components\widgets
  19. * @author Eugine Terentev <eugine@terentev.net>
  20. * @author Victor Gonzalez <victor@vgr.cl>
  21. */
  22. class MainSidebarMenu extends \yii\widgets\Menu
  23. {
  24. /**
  25. * @var string the template used to render the body of a menu which is a link.
  26. *
  27. * In this template, the token `{url}` will be replaced with the corresponding link URL;
  28. * while `{label}` will be replaced with the link text.
  29. *
  30. * Additionally, to comply with AdminLTE 3 design, the following tags were added:
  31. *
  32. * - `{icon}`: an icon shown before a label.
  33. * - `{dropdown-caret}`: the dropdown caret icon.
  34. * - `{badge}`: a badge shown between the label and the `{dropdown-caret}`.
  35. *
  36. * This property will be overridden by the `template` option set in individual menu items via [[items]].
  37. */
  38. public $linkTemplate = '<a href="{url}" class="nav-link">{icon}&nbsp;<p>{label}{dropdown-caret}{badge}</p></a>';
  39. /**
  40. * @var string the template used to render the body of a menu which is a link.
  41. *
  42. * In this template, the token `{url}` will be replaced with the corresponding link URL;
  43. * while `{label}` will be replaced with the link text.
  44. *
  45. * Additionally, to comply with AdminLTE 3 design, the following tags were added:
  46. *
  47. * - `{icon}`: replaced with an icon shown before a label.
  48. * - `{dropdown-caret}`: replaced with the dropdown caret icon.
  49. * - `{badge}`: a badge shown between the label and the `{dropdown-caret}`.
  50. *
  51. * This property will be overridden by the `template` option set in individual menu items via [[items]],
  52. * but this template should not be changed to preserve AdminLte 3 compatibility.
  53. */
  54. public $linkTemplateActive = '<a href="{url}" class="nav-link active">{icon}&nbsp;<p>{label}{dropdown-caret}{badge}</p></a>';
  55. /**
  56. * @var string the template used to render the body of a menu which is NOT a link.
  57. * In this template, the token `{label}` will be replaced with the label of the menu item,
  58. * while `{icon}` will be repplaced by an icon image
  59. * This property will be overridden by the `template` option set in individual menu items via [[items]].
  60. */
  61. public $labelTemplate = "{icon}\n{label}\n{badge}";
  62. /**
  63. * @var string the template used to render a list of sub-menus.
  64. * In this template, the token `{items}` will be replaced with the rendered sub-menu items.
  65. */
  66. public $submenuTemplate = '<ul class="nav nav-treeview">{items}</ul>';
  67. /**
  68. * @var string the badge element HTML tag.
  69. */
  70. public $badgeTag = 'span';
  71. /**
  72. * @var string the badge element base CSS classes.
  73. */
  74. public $badgeClass = 'badge right';
  75. /**
  76. * @var string the badge element background CSS class.
  77. */
  78. public $badgeBgClass;
  79. /**
  80. * @var string the dropdown caret icon.
  81. * @see $linkTemplate, $linkTemplateActive
  82. */
  83. public $dropdownCaret = '<i class="fas fa-angle-left right"></i>';
  84. /**
  85. * @var array array list of the HTML attributes for the menu's container tag..
  86. */
  87. public $options;
  88. /**
  89. * @var array list of HTML attributes shared by all menu [[items]]. If any individual menu item
  90. * specifies its `options`, it will be merged with this property before being used to generate the HTML
  91. * attributes for the menu item tag. The following special options are recognized:
  92. *
  93. * - tag: string, defaults to "li", the tag name of the item container tags.
  94. * Set to false to disable container tag.
  95. * See also [[\yii\helpers\Html::tag()]].
  96. *
  97. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  98. */
  99. public $itemOptions = ['class' => ['nav-item']];
  100. /**
  101. * @var bool whether to activate parent menu items when one of the corresponding child menu items is active.
  102. * The activated parent menu items will also have its CSS classes appended with [[activeCssClass]].
  103. */
  104. public $activateParents = true;
  105. /**
  106. * @var string the CSS class to be appended to the active menu item.
  107. */
  108. public $activeCssClass = 'menu-open';
  109. /**
  110. * @inheritdoc
  111. */
  112. protected function renderItem($item)
  113. {
  114. // check if item is active and set special template
  115. $linkTemplate = $this->linkTemplate;
  116. if ($item['active']) {
  117. $linkTemplate = $this->linkTemplateActive;
  118. }
  119. // set badge if any
  120. $item['badgeOptions'] = isset($item['badgeOptions']) ? $item['badgeOptions'] : [];
  121. if (!ArrayHelper::getValue($item, 'badgeOptions.class')) {
  122. $bg = isset($item['badgeBgClass']) ? $item['badgeBgClass'] : $this->badgeBgClass;
  123. $item['badgeOptions']['class'] = $this->badgeClass . ' ' . $bg;
  124. }
  125. // add dropdown caret if item has childrens
  126. if (isset($item['items']) && !isset($item['dropdown-caret'])) {
  127. $item['dropdown-caret'] = $this->dropdownCaret;
  128. }
  129. if (isset($item['url'])) {
  130. // item uses an url, so uses the `$linkTemplate` variable
  131. $template = ArrayHelper::getValue($item, 'template', $linkTemplate);
  132. return strtr($template, [
  133. '{badge}' => isset($item['badge']) ? Html::tag('span', $item['badge'], $item['badgeOptions']) : '',
  134. '{icon}' => isset($item['icon']) ? $item['icon'] : '',
  135. '{dropdown-caret}' => isset($item['dropdown-caret']) ? $item['dropdown-caret'] : '',
  136. '{url}' => Url::to($item['url']),
  137. '{label}' => $item['label'],
  138. ]);
  139. } else {
  140. // just a plain text item
  141. $template = ArrayHelper::getValue($item, 'template', $this->labelTemplate);
  142. return strtr($template, [
  143. '{badge}' => isset($item['badge'])
  144. ? Html::tag('small', $item['badge'], $item['badgeOptions'])
  145. : '',
  146. '{icon}' => isset($item['icon']) ? $item['icon'] : '',
  147. '{dropdown-caret}' => isset($item['dropdown-caret']) ? $item['dropdown-caret'] : '',
  148. '{label}' => $item['label'],
  149. ]);
  150. }
  151. }
  152. }