ETooltip.php 2.56 KB
Newer Older
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
<?php
class ETooltip extends CWidget
{
    /**
     * Tooltip options
     * 
     * @var array
     **/
    public $tooltip;
    /**
     * The selector that contains the tooltip.
     * 
     * @var sting
     **/
    public $selector;
    /**
     * The image for the tooltip background
     * 
     * @var string
     **/
    public $image = "black_arrow.png";


    private $options;
    private $init;
    private $cssFile = "tooltip.css.php";
    private $jsFiles = array("jquery.tools.min.js");
    
    private $cssPath = "css";
    private $jsPath = "js";

    private $css;
    private $js;

    private function registerScripts()
    {
        $cs = Yii::app()->clientScript;
        //Publish only one pic
        $imagesPath = dirname(__FILE__).DIRECTORY_SEPARATOR."images".DIRECTORY_SEPARATOR;
        $image= Yii::app()->getAssetManager()->publish($imagesPath.$this->image);
        
        if($this->css===null) {
            $cssScript  = require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.$this->cssPath.DIRECTORY_SEPARATOR.$this->cssFile);
            $this->css = $cs->registerCss($this->id."_css", $cssScript, "screen");
        }
        if($this->js===null) {
            $jsPath = dirname(__FILE__).DIRECTORY_SEPARATOR.$this->jsPath.DIRECTORY_SEPARATOR;

            if(!$cs->isScriptRegistered('jquery')) {
                $cs->registerCoreScript('jquery');
            }
            $jsAssetPath = Yii::app()->getAssetManager()->publish($jsPath);
            foreach($this->jsFiles as $file)
            {
                $cs->registerScriptFile($jsAssetPath.DIRECTORY_SEPARATOR.$file, CClientScript::POS_BEGIN);
            }
        }
    }
    public function init()
    {    
        $this->registerScripts();
        $this->options = require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'tooltip.options.php');
        $this->initTooltip();

        
        parent::init();
    }
    public function run()
    {
        if(isset($this->selector)) {
            $options = (empty($this->init)) ? '{}' : $this->init;
            $script = "var jsn = eval(".CJSON::encode($options)."); $('".$this->selector."').tooltip(jsn);";
            Yii::app()->clientScript->registerScript($this->id, $script, CClientScript::POS_READY);
        }
    }
    private function initTooltip()
    {
        $initialize = array();
        if(is_array($this->tooltip)) {
            foreach($this->tooltip as $option => $value ){
                if(in_array($option, $this->options))
                    $initialize[$option] = $value;
            }
        }
        $this->init = $initialize;
    }    
}