PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/components/Bootstrap.php

https://bitbucket.org/pravinchandar/yii-bootstrap
PHP | 316 lines | 141 code | 28 blank | 147 comment | 9 complexity | 2edf360376d1fa5965c447897ef8fd85 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.1, BSD-2-Clause
  1. <?php
  2. /**
  3. * Bootstrap class file.
  4. * @author Christoffer Niska <ChristofferNiska@gmail.com>
  5. * @copyright Copyright &copy; Christoffer Niska 2011-
  6. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  7. * @version 1.1.0
  8. */
  9. /**
  10. * Bootstrap application component.
  11. */
  12. class Bootstrap extends CApplicationComponent
  13. {
  14. // Bootstrap plugins.
  15. const PLUGIN_AFFIX = 'affix';
  16. const PLUGIN_ALERT = 'alert';
  17. const PLUGIN_BUTTON = 'button';
  18. const PLUGIN_CAROUSEL = 'carousel';
  19. const PLUGIN_COLLAPSE = 'collapse';
  20. const PLUGIN_DROPDOWN = 'dropdown';
  21. const PLUGIN_MODAL = 'modal';
  22. const PLUGIN_POPOVER = 'popover';
  23. const PLUGIN_SCROLLSPY = 'scrollspy';
  24. const PLUGIN_TAB = 'tab';
  25. const PLUGIN_TOOLTIP = 'tooltip';
  26. const PLUGIN_TRANSITION = 'transition';
  27. const PLUGIN_TYPEAHEAD = 'typeahead';
  28. /**
  29. * @var array plugin initial options (name=>options).
  30. * Each array key-value pair represents the initial options for a single plugin class,
  31. * with the array key being the plugin name, and array value being the initial options array.
  32. * @since 0.9.8
  33. */
  34. public $plugins = array();
  35. /**
  36. * @var boolean indicates whether assets should be republished on every request.
  37. */
  38. public $publishAssets = YII_DEBUG;
  39. protected $_assetsUrl;
  40. /**
  41. * Registers the Bootstrap CSS.
  42. */
  43. public function registerCoreCss()
  44. {
  45. $filename = YII_DEBUG ? 'bootstrap.css' : 'bootstrap.min.css';
  46. Yii::app()->clientScript->registerCssFile($this->getAssetsUrl().'/css/'.$filename);
  47. }
  48. /**
  49. * Registers the Bootstrap responsive CSS.
  50. * @since 0.9.8
  51. */
  52. public function registerResponsiveCss()
  53. {
  54. /** @var CClientScript $cs */
  55. $cs = Yii::app()->getClientScript();
  56. $cs->registerMetaTag('width=device-width, initial-scale=1.0', 'viewport');
  57. $filename = YII_DEBUG ? 'bootstrap-responsive.css' : 'bootstrap-responsive.min.css';
  58. $cs->registerCssFile($this->getAssetsUrl().'/css/'.$filename);
  59. }
  60. /**
  61. * Registers the Yii-specific CSS missing from Bootstrap.
  62. * @since 0.9.11
  63. */
  64. public function registerYiiCss()
  65. {
  66. Yii::app()->clientScript->registerCssFile($this->getAssetsUrl().'/css/yii.css');
  67. }
  68. /**
  69. * Registers all Bootstrap CSS.
  70. * @since 2.0.0
  71. */
  72. public function registerAllCss()
  73. {
  74. $this->registerCoreCss();
  75. $this->registerResponsiveCss();
  76. $this->registerYiiCss();
  77. }
  78. /**
  79. * Registers the core JavaScript.
  80. * @since 0.9.8
  81. */
  82. public function registerCoreScripts()
  83. {
  84. $this->registerJS(Yii::app()->clientScript->coreScriptPosition);
  85. $this->registerPopover(); // popover also registers tooltip
  86. }
  87. /**
  88. * Registers the Bootstrap JavaScript.
  89. * @param int $position the position of the JavaScript code.
  90. * @see CClientScript::registerScriptFile
  91. */
  92. protected function registerJS($position = CClientScript::POS_HEAD)
  93. {
  94. /** @var CClientScript $cs */
  95. $cs = Yii::app()->getClientScript();
  96. $cs->registerCoreScript('jquery');
  97. $filename = YII_DEBUG ? 'bootstrap.js' : 'bootstrap.min.js';
  98. $cs->registerScriptFile($this->getAssetsUrl().'/js/'.$filename, $position);
  99. }
  100. /**
  101. * Registers the Bootstrap affix plugin.
  102. * @param string $selector the CSS selector
  103. * @param array $options the plugin options
  104. * @see http://twitter.github.com/bootstrap/javascript.html#affix
  105. * @since 2.0.0
  106. */
  107. public function registerAffix($selector = null, $options = array())
  108. {
  109. $this->registerPlugin(self::PLUGIN_AFFIX, $selector, $options);
  110. }
  111. /**
  112. * Registers the Bootstrap alert plugin.
  113. * @param string $selector the CSS selector
  114. * @param array $options the plugin options
  115. * @see http://twitter.github.com/bootstrap/javascript.html#alerts
  116. * @since 0.9.8
  117. */
  118. public function registerAlert($selector = null, $options = array())
  119. {
  120. $this->registerPlugin(self::PLUGIN_ALERT, $selector, $options);
  121. }
  122. /**
  123. * Registers the Bootstrap buttons plugin.
  124. * @param string $selector the CSS selector
  125. * @param array $options the plugin options
  126. * @see http://twitter.github.com/bootstrap/javascript.html#buttons
  127. * @since 0.9.8
  128. */
  129. public function registerButton($selector = null, $options = array())
  130. {
  131. $this->registerPlugin(self::PLUGIN_BUTTON, $selector, $options);
  132. }
  133. /**
  134. * Registers the Bootstrap carousel plugin.
  135. * @param string $selector the CSS selector
  136. * @param array $options the plugin options
  137. * @see http://twitter.github.com/bootstrap/javascript.html#carousel
  138. * @since 0.9.8
  139. */
  140. public function registerCarousel($selector = null, $options = array())
  141. {
  142. $this->registerPlugin(self::PLUGIN_CAROUSEL, $selector, $options);
  143. }
  144. /**
  145. * Registers the Bootstrap collapse plugin.
  146. * @param string $selector the CSS selector
  147. * @param array $options the plugin options
  148. * @see http://twitter.github.com/bootstrap/javascript.html#collapse
  149. * @since 0.9.8
  150. */
  151. public function registerCollapse($selector = null, $options = array())
  152. {
  153. $this->registerPlugin(self::PLUGIN_COLLAPSE, $selector, $options);
  154. }
  155. /**
  156. * Registers the Bootstrap dropdowns plugin.
  157. * @param string $selector the CSS selector
  158. * @param array $options the plugin options
  159. * @see http://twitter.github.com/bootstrap/javascript.html#dropdowns
  160. * @since 0.9.8
  161. */
  162. public function registerDropdown($selector = null, $options = array())
  163. {
  164. $this->registerPlugin(self::PLUGIN_DROPDOWN, $selector, $options);
  165. }
  166. /**
  167. * Registers the Bootstrap modal plugin.
  168. * @param string $selector the CSS selector
  169. * @param array $options the plugin options
  170. * @see http://twitter.github.com/bootstrap/javascript.html#modal
  171. * @since 0.9.8
  172. */
  173. public function registerModal($selector = null, $options = array())
  174. {
  175. $this->registerPlugin(self::PLUGIN_MODAL, $selector, $options);
  176. }
  177. /**
  178. * Registers the Bootstrap scrollspy plugin.
  179. * @param string $selector the CSS selector
  180. * @param array $options the plugin options
  181. * @see http://twitter.github.com/bootstrap/javascript.html#scrollspy
  182. * @since 0.9.8
  183. */
  184. public function registerScrollSpy($selector = null, $options = array())
  185. {
  186. $this->registerPlugin(self::PLUGIN_SCROLLSPY, $selector, $options);
  187. }
  188. /**
  189. * Registers the Bootstrap popover plugin.
  190. * @param string $selector the CSS selector
  191. * @param array $options the plugin options
  192. * @see http://twitter.github.com/bootstrap/javascript.html#popover
  193. * @since 0.9.8
  194. */
  195. public function registerPopover($selector = null, $options = array())
  196. {
  197. $this->registerTooltip(); // Popover requires the tooltip plugin
  198. if (!isset($options['selector']))
  199. $options['selector'] = $selector !== null ? $selector : 'a[rel=popover]';
  200. $this->registerPlugin(self::PLUGIN_POPOVER, 'body', $options);
  201. }
  202. /**
  203. * Registers the Bootstrap tabs plugin.
  204. * @param string $selector the CSS selector
  205. * @param array $options the plugin options
  206. * @see http://twitter.github.com/bootstrap/javascript.html#tabs
  207. * @since 0.9.8
  208. */
  209. public function registerTabs($selector = null, $options = array())
  210. {
  211. $this->registerPlugin(self::PLUGIN_TAB, $selector, $options);
  212. }
  213. /**
  214. * Registers the Bootstrap tooltip plugin.
  215. * @param string $selector the CSS selector
  216. * @param array $options the plugin options
  217. * @see http://twitter.github.com/bootstrap/javascript.html#tooltip
  218. * @since 0.9.8
  219. */
  220. public function registerTooltip($selector = null, $options = array())
  221. {
  222. if (!isset($options['selector']))
  223. $options['selector'] = $selector !== null ? $selector : 'a[rel=tooltip]';
  224. $this->registerPlugin(self::PLUGIN_TOOLTIP, 'body', $options);
  225. }
  226. /**
  227. * Registers the Bootstrap typeahead plugin.
  228. * @param string $selector the CSS selector
  229. * @param array $options the plugin options
  230. * @see http://twitter.github.com/bootstrap/javascript.html#typeahead
  231. * @since 0.9.8
  232. */
  233. public function registerTypeahead($selector = null, $options = array())
  234. {
  235. $this->registerPlugin(self::PLUGIN_TYPEAHEAD, $selector, $options);
  236. }
  237. /**
  238. * Registers a Bootstrap JavaScript plugin.
  239. * @param string $name the name of the plugin
  240. * @param string $selector the CSS selector
  241. * @param array $options the plugin options
  242. * @param string $defaultSelector the default CSS selector
  243. * @since 0.9.8
  244. */
  245. protected function registerPlugin($name, $selector = null, $options = array(), $defaultSelector = null)
  246. {
  247. if (!isset($selector) && empty($options))
  248. {
  249. // Initialization from extension configuration.
  250. $config = isset($this->plugins[$name]) ? $this->plugins[$name] : array();
  251. if (isset($config['selector']))
  252. $selector = $config['selector'];
  253. if (isset($config['options']))
  254. $options = !empty($options) ? CMap::mergeArray($config['options'], $options) : $config['options'];
  255. if (!isset($selector))
  256. $selector = $defaultSelector;
  257. }
  258. if (isset($selector))
  259. {
  260. $key = __CLASS__.'.'.md5($name.$selector.serialize($options).$defaultSelector);
  261. $options = !empty($options) ? CJavaScript::encode($options) : '';
  262. Yii::app()->clientScript->registerScript($key, "jQuery('{$selector}').{$name}({$options});");
  263. }
  264. }
  265. /**
  266. * Returns the URL to the published assets folder.
  267. * @return string the URL
  268. */
  269. protected function getAssetsUrl()
  270. {
  271. if (isset($this->_assetsUrl))
  272. return $this->_assetsUrl;
  273. else
  274. {
  275. $assetsPath = Yii::getPathOfAlias('bootstrap.assets');
  276. $assetsUrl = Yii::app()->assetManager->publish($assetsPath, true, -1, $this->publishAssets);
  277. return $this->_assetsUrl = $assetsUrl;
  278. }
  279. }
  280. /**
  281. * Returns the extension version number.
  282. * @return string the version
  283. */
  284. public function getVersion()
  285. {
  286. return '2.0.0';
  287. }
  288. }