PageRenderTime 74ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/widgets/TbNavbar.php

https://github.com/jacmoe/yiistrap-1
PHP | 128 lines | 73 code | 6 blank | 49 comment | 9 complexity | 91b07d554ff4228b8b73dd2d0dfb14fb MD5 | raw file
  1. <?php
  2. /**
  3. * TbNavbar class file.
  4. * @author Christoffer Niska <christoffer.niska@gmail.com>
  5. * @copyright Copyright &copy; Christoffer Niska 2013-
  6. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  7. * @package bootstrap.widgets
  8. */
  9. Yii::import('bootstrap.helpers.TbHtml');
  10. /**
  11. * Bootstrap navbar widget.
  12. * @see http://twitter.github.com/bootstrap/components.html#navbar
  13. */
  14. class TbNavbar extends CWidget
  15. {
  16. /**
  17. * @var string the navbar color.
  18. */
  19. public $color;
  20. /**
  21. * @var string the brand label text.
  22. */
  23. public $brandLabel;
  24. /**
  25. * @var mixed the brand url.
  26. */
  27. public $brandUrl;
  28. /**
  29. * @var array the HTML attributes for the brand link.
  30. */
  31. public $brandOptions = array();
  32. /**
  33. * @var string nanvbar display type.
  34. */
  35. public $display = TbHtml::NAVBAR_FIXED_TOP;
  36. /**
  37. * @var boolean whether the navbar spans over the whole page.
  38. */
  39. public $fluid = false;
  40. /**
  41. * @var boolean whether to enable collapsing of the navbar on narrow screens.
  42. */
  43. public $collapse = false;
  44. /**
  45. * @var array additional HTML attributes for the collapse widget.
  46. */
  47. public $collapseOptions = array();
  48. /**
  49. * @var array list of navbar item.
  50. */
  51. public $items = array();
  52. /**
  53. * @var array the HTML attributes for the navbar.
  54. */
  55. public $htmlOptions = array();
  56. /**
  57. * Initializes the widget.
  58. */
  59. public function init()
  60. {
  61. if ($this->brandLabel !== false)
  62. {
  63. if (!isset($this->brandLabel))
  64. $this->brandLabel = CHtml::encode(Yii::app()->name);
  65. if (!isset($this->brandUrl))
  66. $this->brandUrl = Yii::app()->homeUrl;
  67. }
  68. if (isset($this->color))
  69. $this->htmlOptions = TbHtml::defaultOption('color', $this->color, $this->htmlOptions);
  70. if (isset($this->display) && $this->display !== TbHtml::NAVBAR_INLINE)
  71. $this->htmlOptions = TbHtml::defaultOption('display', $this->display, $this->htmlOptions);
  72. }
  73. /**
  74. * Runs the widget.
  75. */
  76. public function run()
  77. {
  78. $brand = $this->brandLabel !== false
  79. ? TbHtml::navbarBrandLink($this->brandLabel, $this->brandUrl, $this->brandOptions)
  80. : '';
  81. ob_start();
  82. foreach ($this->items as $item)
  83. {
  84. if (is_string($item))
  85. echo $item;
  86. else
  87. {
  88. $widgetClassName = TbHtml::popOption('class', $item);
  89. if ($widgetClassName !== null)
  90. $this->controller->widget($widgetClassName, $item);
  91. }
  92. }
  93. $items = ob_get_clean();
  94. ob_start();
  95. if ($this->collapse !== false)
  96. {
  97. $this->collapseOptions = TbHtml::addClassName('nav-collapse', $this->collapseOptions);
  98. // todo: fix collapse, currently it cannot be clicked when within a navbar
  99. ob_start();
  100. /* @var TbCollapse $collapseWidget */
  101. $collapseWidget = $this->controller->widget('bootstrap.widgets.TbCollapse', array(
  102. 'toggle' => false, // navbars are collapsed by default
  103. 'content' => $items,
  104. 'htmlOptions' => $this->collapseOptions,
  105. ));
  106. $collapseContent = ob_get_clean();
  107. echo TbHtml::collapseIcon('#' . $collapseWidget->getId());
  108. echo $brand . $collapseContent;
  109. }
  110. else
  111. echo $brand . $items;
  112. $containerContent = ob_get_clean();
  113. $containerOptions = TbHtml::popOption('containerOptions', $this->htmlOptions, array());
  114. $containerOptions = TbHtml::addClassName($this->fluid ? 'container-fluid' : 'container', $containerOptions);
  115. ob_start();
  116. echo CHtml::openTag('div', $containerOptions);
  117. echo $containerContent;
  118. echo '</div>';
  119. $content = ob_get_clean();
  120. echo TbHtml::navbar($content, $this->htmlOptions);
  121. }
  122. }