1 <?php
2 3 4 5 6 7 8 9 10
11 Yii::import('bootstrap.helpers.TbArray');
12
13 class WhSparkLines extends CWidget
14 {
15 16 17 18
19 public $tagName = 'div';
20
21 22 23 24
25 public $data = array();
26
27 28 29
30 public $htmlOptions = array();
31
32 33 34
35 public $pluginOptions = array();
36
37 38 39 40
41 public $debugMode = false;
42
43 44 45
46 public function init()
47 {
48 if (empty($this->data)) {
49 throw new CException(Yii::t('zii', '"data" attribute cannot be blank'));
50 }
51
52 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
53
54 $this->htmlOptions['id'] = TbArray::getValue('id', $this->htmlOptions, $this->getId());
55 }
56
57 58 59
60 public function run()
61 {
62 echo CHtml::openTag($this->tagName, $this->htmlOptions);
63 echo CHtml::closeTag($this->tagName);
64 $this->registerClientScript();
65 }
66
67 68 69
70 public function registerClientScript()
71 {
72
73 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
74 $assetsUrl = $this->getAssetsUrl($path);
75
76
77 $cs = Yii::app()->getClientScript();
78
79 $script = $this->debugMode
80 ? 'jquery.sparkline.js'
81 : 'jquery.sparkline.min.js';
82
83 $cs->registerScriptFile($assetsUrl . '/js/' . $script);
84 $cs->registerCssFile($assetsUrl . '/css/jquery.sparkline.css');
85
86
87 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
88
89 $data = CJavaScript::encode($this->data);
90 $options = CJavaScript::encode($this->pluginOptions);
91
92 $cs->registerScript(__CLASS__ . '#' . $selector, "jQuery('{$selector}').sparkline({$data}, {$options});");
93 }
94 }