1 <?php
2 3 4 5 6 7
8 namespace foundation\widgets;
9
10 use foundation\helpers\ArrayHelper;
11 use foundation\helpers\Html;
12 use foundation\enum\Enum;
13
14 15 16 17 18 19 20 21
22 class Tooltip extends base\Widget
23 {
24 25 26
27 public $tip;
28 29 30
31 public $text;
32 33 34
35 public $position = Enum::TOOLTIP_TOP;
36 37 38 39
40 public $pluginOptions = array();
41
42
43 44 45
46 public function init()
47 {
48 $this->assets = array(
49 'js' => YII_DEBUG ? 'foundation/foundation.tooltips.js' : 'foundation.min.js'
50 );
51
52 Html::addCssClass($this->htmlOptions, Enum::TOOLTIP);
53 Html::addCssClass($this->htmlOptions, $this->position);
54 ArrayHelper::addValue('title', $this->tip, $this->htmlOptions);
55 ArrayHelper::addValue('data-tooltip', 'data-tooltip', $this->htmlOptions);
56 if ($this->tip === null || $this->text === null) {
57 throw new InvalidConfigException('"tip" and "text" cannot be null.');
58 }
59 $this->registerClientScript();
60
61 parent::init();
62 }
63
64 65 66
67 public function run()
68 {
69 echo $this->renderTooltip();
70 }
71
72 73 74 75
76 public function renderTooltip()
77 {
78 return \CHtml::tag('span', $this->htmlOptions, $this->text);
79 }
80
81 82 83
84 public function registerClientScript()
85 {
86 if (!empty($this->pluginOptions)) {
87 $options = \CJavaScript::encode($this->pluginOptions);
88 \Yii::app()->clientScript
89 ->registerScript('Tooltip#' . $this->getId(), "$(document).foundation('tooltip', {$options});");
90 }
91 }
92 }