yii-bootstrap / 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});");
        }
    }
}
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.