Overview

Packages

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

Classes

  • TbApi
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * TbApi 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.components
  8:  * @version 1.2.0
  9:  */
 10: 
 11: /**
 12:  * Bootstrap API component.
 13:  */
 14: class TbApi extends CApplicationComponent
 15: {
 16:     // Bootstrap plugins
 17:     const PLUGIN_AFFIX = 'affix';
 18:     const PLUGIN_ALERT = 'alert';
 19:     const PLUGIN_BUTTON = 'button';
 20:     const PLUGIN_CAROUSEL = 'carousel';
 21:     const PLUGIN_COLLAPSE = 'collapse';
 22:     const PLUGIN_DROPDOWN = 'dropdown';
 23:     const PLUGIN_MODAL = 'modal';
 24:     const PLUGIN_POPOVER = 'popover';
 25:     const PLUGIN_SCROLLSPY = 'scrollspy';
 26:     const PLUGIN_TAB = 'tab';
 27:     const PLUGIN_TOOLTIP = 'tooltip';
 28:     const PLUGIN_TRANSITION = 'transition';
 29:     const PLUGIN_TYPEAHEAD = 'typeahead';
 30: 
 31:     /**
 32:      * @var int static counter, used for determining script identifiers
 33:      */
 34:     public static $counter = 0;
 35: 
 36:     /**
 37:      * @var bool whether we should copy the asset file or directory even if it is already published before.
 38:      */
 39:     public $forceCopyAssets = false;
 40: 
 41:     private $_assetsUrl;
 42: 
 43:     /**
 44:      * Registers the Bootstrap CSS.
 45:      * @param string $url the URL to the CSS file to register.
 46:      */
 47:     public function registerCoreCss($url = null)
 48:     {
 49:         if ($url === null) {
 50:             $fileName = YII_DEBUG ? 'bootstrap.css' : 'bootstrap.min.css';
 51:             $url = $this->getAssetsUrl() . '/css/' . $fileName;
 52:         }
 53:         Yii::app()->clientScript->registerCssFile($url);
 54:     }
 55: 
 56:     /**
 57:      * Registers the responsive Bootstrap CSS.
 58:      * @param string $url the URL to the CSS file to register.
 59:      */
 60:     public function registerResponsiveCss($url = null)
 61:     {
 62:         if ($url === null) {
 63:             $fileName = YII_DEBUG ? 'bootstrap-responsive.css' : 'bootstrap-responsive.min.css';
 64:             $url = $this->getAssetsUrl() . '/css/' . $fileName;
 65:         }
 66:         /** @var CClientScript $cs */
 67:         $cs = Yii::app()->getClientScript();
 68:         $cs->registerMetaTag('width=device-width, initial-scale=1.0', 'viewport');
 69:         $cs->registerCssFile($url);
 70:     }
 71: 
 72:     /**
 73:      * Registers the Yiistrap CSS.
 74:      * @param string $url the URL to the CSS file to register.
 75:      */
 76:     public function registerYiistrapCss($url = null)
 77:     {
 78:         if ($url === null) {
 79:             $fileName = YII_DEBUG ? 'yiistrap.css' : 'yiistrap.min.css';
 80:             $url = $this->getAssetsUrl() . '/css/' . $fileName;
 81:         }
 82:         /** @var CClientScript $cs */
 83:         $cs = Yii::app()->getClientScript();
 84:         $cs->registerCssFile($url);
 85:     }
 86: 
 87:     /**
 88:      * Registers all Bootstrap CSS files.
 89:      */
 90:     public function registerAllCss()
 91:     {
 92:         $this->registerCoreCss();
 93:         $this->registerResponsiveCss();
 94:         $this->registerYiistrapCss();
 95:     }
 96: 
 97:     /**
 98:      * Registers jQuery and Bootstrap JavaScript.
 99:      * @param string $url the URL to the JavaScript file to register.
100:      * @param int $position the position of the JavaScript code.
101:      */
102:     public function registerCoreScripts($url = null, $position = CClientScript::POS_END)
103:     {
104:         if ($url === null) {
105:             $fileName = YII_DEBUG ? 'bootstrap.js' : 'bootstrap.min.js';
106:             $url = $this->getAssetsUrl() . '/js/' . $fileName;
107:         }
108:         /** @var CClientScript $cs */
109:         $cs = Yii::app()->getClientScript();
110:         $cs->registerCoreScript('jquery');
111:         $cs->registerScriptFile($url, $position);
112:     }
113: 
114:     /**
115:      * Registers the Tooltip and Popover plugins.
116:      */
117:     public function registerTooltipAndPopover()
118:     {
119:         $this->registerPopover();
120:         $this->registerTooltip();
121:     }
122: 
123:     /**
124:      * Registers all Bootstrap JavaScript files.
125:      */
126:     public function registerAllScripts()
127:     {
128:         $this->registerCoreScripts();
129:         $this->registerTooltipAndPopover();
130:     }
131: 
132:     /**
133:      * Registers all assets.
134:      */
135:     public function register()
136:     {
137:         $this->registerAllCss();
138:         $this->registerAllScripts();
139:     }
140: 
141:     /**
142:      * Registers the Bootstrap Popover plugin.
143:      * @param string $selector the CSS selector.
144:      * @param array $options the JavaScript options for the plugin.
145:      * @see http://twitter.github.com/bootstrap/javascript.html#popover
146:      */
147:     public function registerPopover($selector = 'body', $options = array())
148:     {
149:         if (!isset($options['selector'])) {
150:             $options['selector'] = 'a[rel=popover]';
151:         }
152:         $this->registerPlugin(self::PLUGIN_POPOVER, $selector, $options);
153:     }
154: 
155:     /**
156:      * Registers the Bootstrap Tooltip plugin.
157:      * @param string $selector the CSS selector.
158:      * @param array $options the JavaScript options for the plugin.
159:      * @see http://twitter.github.com/bootstrap/javascript.html#tooltip
160:      */
161:     public function registerTooltip($selector = 'body', $options = array())
162:     {
163:         if (!isset($options['selector'])) {
164:             $options['selector'] = 'a[rel=tooltip]';
165:         }
166:         $this->registerPlugin(self::PLUGIN_TOOLTIP, $selector, $options);
167:     }
168: 
169:     /**
170:      * Registers a specific Bootstrap plugin using the given selector and options.
171:      * @param string $name the plugin name.
172:      * @param string $selector the CSS selector.
173:      * @param array $options the JavaScript options for the plugin.
174:      * @param int $position the position of the JavaScript code.
175:      */
176:     public function registerPlugin($name, $selector, $options = array(), $position = CClientScript::POS_END)
177:     {
178:         $options = !empty($options) ? CJavaScript::encode($options) : '';
179:         $script = "jQuery('{$selector}').{$name}({$options});";
180:         $id = __CLASS__ . '#Plugin' . self::$counter++;
181:         Yii::app()->clientScript->registerScript($id, $script, $position);
182:     }
183: 
184:     /**
185:      * Registers events using the given selector.
186:      * @param string $selector the CSS selector.
187:      * @param string[] $events the JavaScript event configuration (name=>handler).
188:      * @param int $position the position of the JavaScript code.
189:      */
190:     public function registerEvents($selector, $events, $position = CClientScript::POS_END)
191:     {
192:         if (empty($events)) {
193:             return;
194:         }
195: 
196:         $script = '';
197:         foreach ($events as $name => $handler) {
198:             $handler = ($handler instanceof CJavaScriptExpression)
199:                 ? $handler
200:                 : new CJavaScriptExpression($handler);
201: 
202:             $script .= "jQuery('{$selector}').on('{$name}', {$handler});";
203:         }
204:         $id = __CLASS__ . '#Events' . self::$counter++;
205:         Yii::app()->clientScript->registerScript($id, $script, $position);
206:     }
207: 
208:     /**
209:      * Returns the url to the published assets folder.
210:      * @return string the url.
211:      */
212:     protected function getAssetsUrl()
213:     {
214:         if (isset($this->_assetsUrl)) {
215:             return $this->_assetsUrl;
216:         } else {
217:             $assetsPath = Yii::getPathOfAlias('bootstrap.assets');
218:             $assetsUrl = Yii::app()->assetManager->publish($assetsPath, false, -1, $this->forceCopyAssets);
219:             return $this->_assetsUrl = $assetsUrl;
220:         }
221:     }
222: 
223: }
224: 
Yiistrap API documentation generated by ApiGen 2.8.0