-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScrollSpy.php
126 lines (108 loc) · 2.9 KB
/
ScrollSpy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace demogorgorn\uikit;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\base\InvalidConfigException;
/**
* ScrollSpy renders an ScrollSpy component.
*
* For example,
*
* ```php
* echo ScrollSpy::widget([
* 'animation' => ScrollSpy::ANIMATION_SLIDE_RIGHT,
* 'body' => 'Say hello...',
* ]);
* ```
*
* The following example will show the content enclosed between the [[begin()]]
* and [[end()]] calls within the ScrollSpy box:
*
* ```php
* ScrollSpy::begin([
* 'animation' => ScrollSpy::ANIMATION_SLIDE_RIGHT,
* 'repeat' => true,
* 'delay' => 600
* ]);
*
* echo SmoothScroll::widget([
* 'showButton' => true,
* 'body' => 'Say hello...',
* ]);
*
* ScrollSpy::end();
* ```
* @see http://www.getuikit.com/docs/scrollspy.html
* @author Oleg Martemjanov <[email protected]>
* @since 2.0
*
*/
class ScrollSpy extends Widget
{
/**
* @see http://www.getuikit.com/docs/animation.html
*/
const ANIMATION_FADE = "fade";
const ANIMATION_SCALE_UP = "scale-up";
const ANIMATION_SCALE_DOWN = "scale-down";
const ANIMATION_SLIDE_TOP = "slide-top";
const ANIMATION_SLIDE_BOTTOM = "slide-bottom";
const ANIMATION_SLIDE_LEFT = "slide-left";
const ANIMATION_SLIDE_RIGHT = "slide-right";
/**
* @var string the animation effect.
*/
public $animation;
/**
* @var string the body content in the ScrollSpy component. Note that anything between
* the [[begin()]] and [[end()]] calls of the ScrollSpy widget will also be treated
* as the body content, and will be rendered before this.
*/
public $body;
/**
* @var boolean applies the class everytime the element appears in the viewport.
*/
public $repeat = false;
/**
* @var integer a delay in milliseconds to the animation.
*/
public $delay;
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
$this->initOptions();
echo Html::beginTag('div', $this->options) . "\n";
}
/**
* Renders the widget.
*/
public function run()
{
echo "\n" . $this->body;
echo "\n" . Html::endTag('div');
$this->registerAsset();
}
/**
* Initializes the widget options.
* This method sets the default values for various options.
*/
protected function initOptions()
{
if (!$this->animation) {
throw new InvalidConfigException("The 'animation' option is required.");
}
$class = "{cls:'uk-animation-{$this->animation}'";
if ($this->repeat)
$class .= ", repeat:true";
if (isset($this->delay) && is_int($this->delay))
$class .= ", delay:{$this->delay}";
$class .= "}";
$this->options = array_merge([
'data-uk-scrollspy' => $class,
], $this->options);
}
}