PageRenderTime 68ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/bootstrap/components/Bootstrap.php

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