1: <?php
2: /**
3: * TbScrollspy class file.
4: * @author Christoffer Niska <[email protected]>
5: * @copyright Copyright © Christoffer Niska 2013-
6: * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
7: * @package bootstrap.widgets
8: */
9:
10: Yii::import('bootstrap.behaviors.TbWidget');
11:
12: /**
13: * Bootstrap scrollspy widget.
14: * @see http://twitter.github.com/bootstrap/javascript.html#scrollspy
15: */
16: class TbScrollspy extends CWidget
17: {
18: /**
19: * @var string the CSS selector for the scrollspy element.
20: */
21: public $selector = 'body';
22: /**
23: * @var string the CSS selector for the spying element.
24: */
25: public $target;
26: /**
27: * @var integer the scroll offset (in pixels).
28: */
29: public $offset;
30: /**
31: * @var string[] $events the JavaScript event configuration (name=>handler).
32: */
33: public $events = array();
34:
35: /**
36: * Initializes the widget.
37: */
38: public function init()
39: {
40: $this->attachBehavior('TbWidget', new TbWidget);
41: }
42:
43: /**
44: * Runs the widget.
45: */
46: public function run()
47: {
48: // todo: think of a better way of doing this.
49: $script = "jQuery('{$this->selector}').attr('data-spy', 'scroll');";
50: if (isset($this->target)) {
51: $script .= "jQuery('{$this->selector}').attr('data-target', '{$this->target}');";
52: }
53: if (isset($this->offset)) {
54: $script .= "jQuery('{$this->selector}').attr('data-offset', '{$this->offset}');";
55: }
56: Yii::app()->clientScript->registerScript($this->getId(), $script, CClientScript::POS_BEGIN);
57: $this->registerEvents($this->selector, $this->events);
58: }
59: }
60: