1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13:
14: class TbApi extends CApplicationComponent
15: {
16:
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: 33:
34: public static $counter = 0;
35:
36: 37: 38:
39: public $forceCopyAssets = false;
40:
41: private $_assetsUrl;
42:
43: 44: 45: 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: 58: 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:
67: $cs = Yii::app()->getClientScript();
68: $cs->registerMetaTag('width=device-width, initial-scale=1.0', 'viewport');
69: $cs->registerCssFile($url);
70: }
71:
72: 73: 74: 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:
83: $cs = Yii::app()->getClientScript();
84: $cs->registerCssFile($url);
85: }
86:
87: 88: 89:
90: public function registerAllCss()
91: {
92: $this->registerCoreCss();
93: $this->registerResponsiveCss();
94: $this->registerYiistrapCss();
95: }
96:
97: 98: 99: 100: 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:
109: $cs = Yii::app()->getClientScript();
110: $cs->registerCoreScript('jquery');
111: $cs->registerScriptFile($url, $position);
112: }
113:
114: 115: 116:
117: public function registerTooltipAndPopover()
118: {
119: $this->registerPopover();
120: $this->registerTooltip();
121: }
122:
123: 124: 125:
126: public function registerAllScripts()
127: {
128: $this->registerCoreScripts();
129: $this->registerTooltipAndPopover();
130: }
131:
132: 133: 134:
135: public function register()
136: {
137: $this->registerAllCss();
138: $this->registerAllScripts();
139: }
140:
141: 142: 143: 144: 145: 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: 157: 158: 159: 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: 171: 172: 173: 174: 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: 186: 187: 188: 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: 210: 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: