Overview

Packages

  • bootstrap
    • behaviors
    • components
    • form
    • gii
    • helpers
    • widgets

Classes

  • TbWidget
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * TbWidget class file.
  4:  * @author Christoffer Niska <christoffer.niska@gmail.com>
  5:  * @copyright Copyright &copy; Christoffer Niska 2013-
  6:  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  7:  * @package bootstrap.behaviors
  8:  */
  9: 
 10: Yii::import('bootstrap.helpers.TbHtml');
 11: Yii::import('bootstrap.components.TbApi');
 12: 
 13: /**
 14:  * Bootstrap widget behavior.
 15:  * @property $owner CWidget
 16:  */
 17: class TbWidget extends CBehavior
 18: {
 19:     private $_api;
 20:     private $_assetsUrl;
 21:     private $_clientScript;
 22: 
 23:     /**
 24:      * Returns the widget id and copies it to HTML attributes or vice versa.
 25:      * @param string $id the widget id.
 26:      * @return string the widget id.
 27:      */
 28:     public function resolveId($id = null)
 29:     {
 30:         if ($id === null) {
 31:             $id = $this->owner->getId();
 32:         }
 33:         if (isset($this->owner->htmlOptions['id'])) {
 34:             $id = $this->owner->htmlOptions['id'];
 35:         } else {
 36:             $this->owner->htmlOptions['id'] = $id;
 37:         }
 38:         return $id;
 39:     }
 40: 
 41:     /**
 42:      * Copies the id to the widget HTML attributes or vise versa.
 43:      * @deprecated by TbWidget::resolveId
 44:      */
 45:     public function copyId()
 46:     {
 47:         // todo: remove this when it's safe to do so.
 48:         if (!isset($this->owner->htmlOptions['id'])) {
 49:             $this->owner->htmlOptions['id'] = $this->owner->id;
 50:         } else {
 51:             $this->owner->id = $this->owner->htmlOptions['id'];
 52:         }
 53:     }
 54: 
 55:     /**
 56:      * Publishes an asset path.
 57:      * @param string $path the assets path.
 58:      * @param boolean $forceCopy whether we should copy the asset files even if they are already published before.
 59:      * @return string the url.
 60:      * @throws CException if the asset manager cannot be located.
 61:      */
 62:     public function publishAssets($path, $forceCopy = false)
 63:     {
 64:         if (!Yii::app()->hasComponent('assetManager')) {
 65:             throw new CException('Failed to locate the asset manager component.');
 66:         }
 67:         /* @var CAssetManager $assetManager */
 68:         $assetManager = Yii::app()->getComponent('assetManager');
 69:         $assetsUrl = $assetManager->publish($path, false, -1, $forceCopy);
 70:         return $this->_assetsUrl = $assetsUrl;
 71:     }
 72: 
 73:     /**
 74:      * Registers a CSS file.
 75:      * @param string $url URL of the CSS file.
 76:      * @param string $media media that the CSS file should be applied to.
 77:      */
 78:     public function registerCssFile($url, $media = '')
 79:     {
 80:         if (isset($this->_assetsUrl)) {
 81:             $url = $this->_assetsUrl . '/' . ltrim($url, '/');
 82:         }
 83:         $this->getClientScript()->registerCssFile($url, $media);
 84:     }
 85: 
 86:     /**
 87:      * Registers a JavaScript file.
 88:      * @param string $url URL of the javascript file.
 89:      * @param integer $position the position of the JavaScript code.
 90:      */
 91:     public function registerScriptFile($url, $position = null)
 92:     {
 93:         if (isset($this->_assetsUrl)) {
 94:             $url = $this->_assetsUrl . '/' . ltrim($url, '/');
 95:         }
 96:         $this->getClientScript()->registerScriptFile($url, $position);
 97:     }
 98: 
 99:     /**
100:      * Returns the name of the correct script file to use.
101:      * @param string $filename the base file name.
102:      * @param boolean $minified whether to include the minified version (defaults to false).
103:      * @return string the full filename.
104:      */
105:     public function resolveScriptVersion($filename, $minified = false)
106:     {
107:         list($name, $extension) = str_split($filename, strrpos($filename, '.') + 1);
108:         return !$minified ? $name . $extension : $name . 'min.' . $extension;
109:     }
110: 
111:     /**
112:      * Registers the given plugin with the API.
113:      * @param string $name the plugin name.
114:      * @param string $selector the CSS selector.
115:      * @param array $options the JavaScript options for the plugin.
116:      * @param int $position the position of the JavaScript code.
117:      * @return boolean whether the plugin was registered.
118:      */
119:     public function registerPlugin($name, $selector, $options = array(), $position = CClientScript::POS_END)
120:     {
121:         if (($api = $this->getApi()) !== null) {
122:             $api->registerPlugin($name, $selector, $options, $position);
123:             return true;
124:         }
125:         return false;
126:     }
127: 
128:     /**
129:      * Registers plugin events with the API.
130:      * @param string $selector the CSS selector.
131:      * @param string[] $events the JavaScript event configuration (name=>handler).
132:      * @param int $position the position of the JavaScript code.
133:      * @return boolean whether the events were registered.
134:      */
135:     public function registerEvents($selector, $events, $position = CClientScript::POS_END)
136:     {
137:         if (($api = $this->getApi()) !== null) {
138:             $api->registerEvents($selector, $events, $position);
139:             return true;
140:         }
141:         return false;
142:     }
143: 
144:     /**
145:      * Returns the API instance.
146:      * @return TbApi the api.
147:      */
148:     protected function getApi()
149:     {
150:         if (isset($this->_api)) {
151:             return $this->_api;
152:         } else {
153:             return $this->_api = Yii::app()->getComponent('bootstrap');
154:         }
155:     }
156: 
157:     /**
158:      * Returns the client script component.
159:      * @return CClientScript the component.
160:      */
161:     protected function getClientScript()
162:     {
163:         if (isset($this->_clientScript)) {
164:             return $this->_clientScript;
165:         } else {
166:             if (!Yii::app()->hasComponent('clientScript')) {
167:                 return false;
168:             }
169:             return $this->_clientScript = Yii::app()->getComponent('clientScript');
170:         }
171:     }
172: }
Yiistrap API documentation generated by ApiGen 2.8.0