Christoffer Niska avatar Christoffer Niska committed 9675c6a

added collapse and scrollspy widgets and renamed tabbable to tabs

Comments (0)

Files changed (4)

widgets/TbCollapse.php

+<?php
+/**
+ * TbCollapse class file.
+ * @author Christoffer Niska <ChristofferNiska@gmail.com>
+ * @copyright Copyright &copy; Christoffer Niska 2012-
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @package bootstrap.widgets
+ * @since 1.0.0
+ */
+
+/**
+ * Bootstrap collapse widget.
+ * @see http://twitter.github.com/bootstrap/javascript.html#collapse
+ */
+class TbCollapse extends CWidget
+{
+    /**
+     * @var string the name of the collapse element. Defaults to 'a'.
+     */
+    public $tagName = 'div';
+    /**
+     * @var boolean the CSS selector for element to collapse. Default to 'false'.
+     */
+    public $parent = false;
+    /**
+     * @var boolean indicates whether to toggle the collapsible element on invocation.
+     */
+    public $toggle = true;
+    /**
+     * @var array the options for the Bootstrap Javascript plugin.
+     */
+    public $options = array();
+    /**
+     * @var string[] the Javascript event handlers.
+     */
+    public $events = array();
+    /**
+    * @var array the HTML attributes for the widget container.
+    */
+    public $htmlOptions = array();
+
+    /**
+     * Initializes the widget.
+     */
+    public function init()
+    {
+        if (!isset($this->htmlOptions['id']))
+            $this->htmlOptions['id'] = $this->getId();
+
+        if (isset($this->parent) && !isset($this->options['parent']))
+            $this->options['parent'] = $this->parent;
+
+        if (isset($this->toggle) && !isset($this->options['toggle']))
+            $this->options['toggle'] = $this->toggle;
+
+        echo CHtml::tag($this->tagName, $this->htmlOptions);
+    }
+
+    /**
+     * Runs the widget.
+     */
+    public function run()
+    {
+        $id = $this->htmlOptions['id'];
+
+        echo CHtml::closeTag($this->tagName);
+
+        /** @var CClientScript $cs */
+        $cs = Yii::app()->getClientScript();
+        $options = !empty($this->options) ? CJavaScript::encode($this->options) : '';
+        $cs->registerScript(__CLASS__.'#'.$id, "jQuery('#{$id}').collapse({$options});");
+
+        foreach ($this->events as $name => $handler)
+        {
+            $handler = CJavaScript::encode($handler);
+            $cs->registerScript(__CLASS__.'#'.$id.'_'.$name, "jQuery('#{$id}').on('{$name}', {$handler});");
+        }
+    }
+}
+

widgets/TbScrollSpy.php

+<?php
+/**
+ * TbScrollSpy class file.
+ * @author Christoffer Niska <ChristofferNiska@gmail.com>
+ * @copyright Copyright &copy; Christoffer Niska 2012-
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @package bootstrap.widgets
+ * @since 1.0.0
+ */
+
+/**
+ * Bootstrap scrollspy widget.
+ * @see http://twitter.github.com/bootstrap/javascript.html#scrollspy
+ */
+class TbScrollSpy extends CWidget
+{
+    /**
+     * @var string the CSS selector for the scrollspy element. Defaults to 'body'.
+     */
+    public $selector = 'body';
+    /**
+     * @var string the CSS selector for the spying element.
+     */
+    public $target;
+    /**
+     * @var integer the scroll offset (in pixels).
+     */
+    public $offset;
+    /**
+     * @var array string[] the Javascript event handlers.
+     */
+    public $events = array();
+
+    /**
+     * Runs the widget.
+     */
+    public function run()
+    {
+        $script = "jQuery('{$this->selector}').attr('data-spy', 'scroll');";
+
+        if (isset($this->target))
+            $script .= "jQuery('{$this->selector}').attr('data-target', '{$this->target}');";
+
+        if (isset($this->offset))
+            $script .= "jQuery('{$this->selector}').attr('data-offset', '{$this->offset}');";
+
+        /** @var CClientScript $cs */
+        $cs = Yii::app()->getClientScript();
+        $cs->registerScript(__CLASS__.'#'.$this->selector, $script, CClientScript::POS_BEGIN);
+
+        foreach ($this->events as $name => $handler)
+        {
+            $handler = CJavaScript::encode($handler);
+            $cs->registerScript(__CLASS__.'#'.$this->selector.'_'.$name, "jQuery('{$this->selector}').on('{$name}', {$handler});");
+        }
+    }
+}
+

widgets/TbTabbable.php

-<?php
-/**
- * BootTabbable class file.
- * @author Christoffer Niska <ChristofferNiska@gmail.com>
- * @copyright Copyright &copy; Christoffer Niska 2011-
- * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
- * @package bootstrap.widgets
- */
-
-Yii::import('bootstrap.widgets.TbMenu');
-
-/**
- * Bootstrap JavaScript tabs widget.
- * @since 0.9.8
- */
-class TbTabbable extends CWidget
-{
-	// Tab placements.
-	const PLACEMENT_ABOVE = 'above';
-	const PLACEMENT_BELOW = 'below';
-	const PLACEMENT_LEFT = 'left';
-	const PLACEMENT_RIGHT = 'right';
-
-	/**
-	 * @var string the type of tabs to display. Defaults to 'tabs'.
-	 * Valid values are 'tabs' and 'pills'.
-	 * Please not that JavaScript pills are not fully supported in Bootstrap!
-	 */
-    public $type = TbMenu::TYPE_TABS;
-	/**
-	 * @var string the placement of the tabs.
-	 * Valid values are 'above', 'below', 'left' and 'right'.
-	 */
-	public $placement;
-	/**
-	 * @var array the tab configuration.
-	 */
-    public $tabs = array();
-	/**
-	 * @var boolean whether to encode item labels.
-	 */
-	public $encodeLabel = true;
-	/**
-	 * @var string[] the JavaScript event handlers.
-	 */
-	public $events = array();
-	/**
-	 * @var array the HTML attributes for the widget container.
-	 */
-	public $htmlOptions = array();
-
-    /**
-     * Initializes the widget.
-     */
-    public function init()
-    {
-		if (!isset($this->htmlOptions['id']))
-			$this->htmlOptions['id'] = $this->getId();
-
-        $classes = array();
-
-        $validPlacements = array(self::PLACEMENT_ABOVE, self::PLACEMENT_BELOW, self::PLACEMENT_LEFT, self::PLACEMENT_RIGHT);
-
-        if (isset($this->placement) && in_array($this->placement, $validPlacements))
-            $classes[] = 'tabs-'.$this->placement;
-
-        if (!empty($classes))
-		{
-			$classes = implode(' ', $classes);
-			if (isset($this->htmlOptions['class']))
-				$this->htmlOptions['class'] .= ' '.$classes;
-			else
-				$this->htmlOptions['class'] = $classes;
-		}
-    }
-
-    /**
-     * Run this widget.
-     */
-    public function run()
-    {
-	    $id = $this->id;
-	    $content = array();
-	    $items = $this->normalizeTabs($this->tabs, $content);
-
-		ob_start();
-		$this->controller->widget('bootstrap.widgets.TbMenu', array(
-			'type'=>$this->type,
-			'encodeLabel'=>$this->encodeLabel,
-			'items'=>$items,
-		));
-		$tabs = ob_get_clean();
-
-		ob_start();
-		echo '<div class="tab-content">';
-		echo implode('', $content);
-		echo '</div>';
-		$content = ob_get_clean();
-
-		echo CHtml::openTag('div', $this->htmlOptions);
-		echo $this->placement === self::PLACEMENT_BELOW ? $content.$tabs : $tabs.$content;
-		echo '</div>';
-
-	    /** @var CClientScript $cs */
-	    $cs = Yii::app()->getClientScript();
-	    $cs->registerScript(__CLASS__.'#'.$id, "jQuery('#{$id}').tab('show');");
-
-	    foreach ($this->events as $name => $handler)
-        {
-            $handler = CJavaScript::encode($handler);
-            $cs->registerScript(__CLASS__.'#'.$id.'_'.$name, "jQuery('#{$id}').on('".$name."', {$handler});");
-        }
-    }
-
-	/**
-	 * Normalizes the tab configuration.
-	 * @param array $tabs the tab configuration
-	 * @param array $panes a reference to the panes array
-	 * @param integer $i the current index
-	 * @return array the items
-	 */
-	protected function normalizeTabs($tabs, &$panes, &$i = 0)
-	{
-		$id = $this->getId();
-		$items = array();
-
-	    foreach ($tabs as $tab)
-	    {
-			$item = $tab;
-
-		    if (isset($item['visible']) && !$item['visible'])
-                continue;
-
-			if (!isset($item['itemOptions']))
-				$item['itemOptions'] = array();
-
-			$item['linkOptions']['data-toggle'] = 'tab';
-
-		    if (isset($tab['items']))
-				$item['items'] = $this->normalizeTabs($item['items'], $panes, $i);
-			else
-			{
-				if (!isset($item['id']))
-					$item['id'] = $id.'_tab_'.($i + 1);
-
-				$item['url'] = '#'.$item['id'];
-
-				if (!isset($item['content']))
-					$item['content'] = '';
-
-				$content = $item['content'];
-				unset($item['content']);
-
-				if (!isset($item['paneOptions']))
-					$item['paneOptions'] = array();
-
-				$paneOptions = $item['paneOptions'];
-				unset($item['paneOptions']);
-
-				$paneOptions['id'] = $item['id'];
-
-				$classes = array('tab-pane fade');
-
-				if (isset($item['active']) && $item['active'])
-					$classes[] = 'active in';
-
-				$classes = implode(' ', $classes);
-				if (isset($paneOptions['class']))
-					$paneOptions['class'] .= ' '.$classes;
-				else
-					$paneOptions['class'] = $classes;
-
-				$panes[] = CHtml::tag('div', $paneOptions, $content);
-
-				$i++; // increment the tab-index
-			}
-
-			$items[] = $item;
-	    }
-
-		return $items;
-	}
-}

widgets/TbTabs.php

+<?php
+/**
+ * TbTabs class file.
+ * @author Christoffer Niska <ChristofferNiska@gmail.com>
+ * @copyright Copyright &copy; Christoffer Niska 2011-
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @package bootstrap.widgets
+ */
+
+Yii::import('bootstrap.widgets.TbMenu');
+
+/**
+ * Bootstrap Javascript tabs widget.
+ * @see http://twitter.github.com/bootstrap/javascript.html#tabs
+ */
+class TbTabs extends CWidget
+{
+	// Tab placements.
+	const PLACEMENT_ABOVE = 'above';
+	const PLACEMENT_BELOW = 'below';
+	const PLACEMENT_LEFT = 'left';
+	const PLACEMENT_RIGHT = 'right';
+
+	/**
+	 * @var string the type of tabs to display. Defaults to 'tabs'. Valid values are 'tabs' and 'pills'.
+	 * Please not that Javascript pills are not fully supported in Bootstrap yet!
+     * @see TbMenu::$type
+	 */
+    public $type = TbMenu::TYPE_TABS;
+	/**
+	 * @var string the placement of the tabs.
+	 * Valid values are 'above', 'below', 'left' and 'right'.
+	 */
+	public $placement;
+	/**
+	 * @var array the tab configuration.
+	 */
+    public $tabs = array();
+	/**
+	 * @var boolean whether to encode item labels.
+	 */
+	public $encodeLabel = true;
+	/**
+	 * @var string[] the Javascript event handlers.
+	 */
+	public $events = array();
+	/**
+	 * @var array the HTML attributes for the widget container.
+	 */
+	public $htmlOptions = array();
+
+    /**
+     * Initializes the widget.
+     */
+    public function init()
+    {
+		if (!isset($this->htmlOptions['id']))
+			$this->htmlOptions['id'] = $this->getId();
+
+        $classes = array();
+
+        $validPlacements = array(self::PLACEMENT_ABOVE, self::PLACEMENT_BELOW, self::PLACEMENT_LEFT, self::PLACEMENT_RIGHT);
+
+        if (isset($this->placement) && in_array($this->placement, $validPlacements))
+            $classes[] = 'tabs-'.$this->placement;
+
+        if (!empty($classes))
+		{
+			$classes = implode(' ', $classes);
+			if (isset($this->htmlOptions['class']))
+				$this->htmlOptions['class'] .= ' '.$classes;
+			else
+				$this->htmlOptions['class'] = $classes;
+		}
+    }
+
+    /**
+     * Run this widget.
+     */
+    public function run()
+    {
+	    $id = $this->id;
+	    $content = array();
+	    $items = $this->normalizeTabs($this->tabs, $content);
+
+		ob_start();
+		$this->controller->widget('bootstrap.widgets.TbMenu', array(
+			'type'=>$this->type,
+			'encodeLabel'=>$this->encodeLabel,
+			'items'=>$items,
+		));
+		$tabs = ob_get_clean();
+
+		ob_start();
+		echo '<div class="tab-content">';
+		echo implode('', $content);
+		echo '</div>';
+		$content = ob_get_clean();
+
+		echo CHtml::openTag('div', $this->htmlOptions);
+		echo $this->placement === self::PLACEMENT_BELOW ? $content.$tabs : $tabs.$content;
+		echo '</div>';
+
+	    /** @var CClientScript $cs */
+	    $cs = Yii::app()->getClientScript();
+	    $cs->registerScript(__CLASS__.'#'.$id, "jQuery('#{$id}').tab('show');");
+
+	    foreach ($this->events as $name => $handler)
+        {
+            $handler = CJavaScript::encode($handler);
+            $cs->registerScript(__CLASS__.'#'.$id.'_'.$name, "jQuery('#{$id}').on('{$name}', {$handler});");
+        }
+    }
+
+	/**
+	 * Normalizes the tab configuration.
+	 * @param array $tabs the tab configuration
+	 * @param array $panes a reference to the panes array
+	 * @param integer $i the current index
+	 * @return array the items
+	 */
+	protected function normalizeTabs($tabs, &$panes, &$i = 0)
+	{
+		$id = $this->getId();
+		$items = array();
+
+	    foreach ($tabs as $tab)
+	    {
+			$item = $tab;
+
+		    if (isset($item['visible']) && $item['visible'] === false)
+                continue;
+
+			if (!isset($item['itemOptions']))
+				$item['itemOptions'] = array();
+
+			$item['linkOptions']['data-toggle'] = 'tab';
+
+		    if (isset($tab['items']))
+				$item['items'] = $this->normalizeTabs($item['items'], $panes, $i);
+			else
+			{
+				if (!isset($item['id']))
+					$item['id'] = $id.'_tab_'.($i + 1);
+
+				$item['url'] = '#'.$item['id'];
+
+				if (!isset($item['content']))
+					$item['content'] = '';
+
+				$content = $item['content'];
+				unset($item['content']);
+
+				if (!isset($item['paneOptions']))
+					$item['paneOptions'] = array();
+
+				$paneOptions = $item['paneOptions'];
+				unset($item['paneOptions']);
+
+				$paneOptions['id'] = $item['id'];
+
+				$classes = array('tab-pane fade');
+
+				if (isset($item['active']) && $item['active'])
+					$classes[] = 'active in';
+
+				$classes = implode(' ', $classes);
+				if (isset($paneOptions['class']))
+					$paneOptions['class'] .= ' '.$classes;
+				else
+					$paneOptions['class'] = $classes;
+
+				$panes[] = CHtml::tag('div', $paneOptions, $content);
+
+				$i++; // increment the tab-index
+			}
+
+			$items[] = $item;
+	    }
+
+		return $items;
+	}
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.