PageRenderTime 38ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/Shcoder/funny
PHP | 319 lines | 141 code | 28 blank | 150 comment | 7 complexity | 7a2d538967d495e45402478f86c988f9 MD5 | raw file
Possible License(s): LGPL-2.1
  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 2.0.2
  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 $forceCopyAssets = false;
  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. */
  91. protected function registerJS($position = CClientScript::POS_HEAD)
  92. {
  93. /** @var CClientScript $cs */
  94. $cs = Yii::app()->getClientScript();
  95. $cs->registerCoreScript('jquery');
  96. $filename = YII_DEBUG ? 'bootstrap.js' : 'bootstrap.min.js';
  97. $cs->registerScriptFile($this->getAssetsUrl().'/js/'.$filename, $position);
  98. }
  99. /**
  100. * Registers all Bootstrap CSS and JavaScript.
  101. * @since 2.1.0
  102. */
  103. public function register()
  104. {
  105. $this->registerAllCss();
  106. $this->registerCoreScripts();
  107. }
  108. /**
  109. * Registers the Bootstrap affix plugin.
  110. * @param string $selector the CSS selector
  111. * @param array $options the plugin options
  112. * @see http://twitter.github.com/bootstrap/javascript.html#affix
  113. * @since 2.0.0
  114. */
  115. public function registerAffix($selector = null, $options = array())
  116. {
  117. $this->registerPlugin(self::PLUGIN_AFFIX, $selector, $options);
  118. }
  119. /**
  120. * Registers the Bootstrap alert plugin.
  121. * @param string $selector the CSS selector
  122. * @param array $options the plugin options
  123. * @see http://twitter.github.com/bootstrap/javascript.html#alerts
  124. * @since 0.9.8
  125. */
  126. public function registerAlert($selector = null, $options = array())
  127. {
  128. $this->registerPlugin(self::PLUGIN_ALERT, $selector, $options);
  129. }
  130. /**
  131. * Registers the Bootstrap buttons plugin.
  132. * @param string $selector the CSS selector
  133. * @param array $options the plugin options
  134. * @see http://twitter.github.com/bootstrap/javascript.html#buttons
  135. * @since 0.9.8
  136. */
  137. public function registerButton($selector = null, $options = array())
  138. {
  139. $this->registerPlugin(self::PLUGIN_BUTTON, $selector, $options);
  140. }
  141. /**
  142. * Registers the Bootstrap carousel plugin.
  143. * @param string $selector the CSS selector
  144. * @param array $options the plugin options
  145. * @see http://twitter.github.com/bootstrap/javascript.html#carousel
  146. * @since 0.9.8
  147. */
  148. public function registerCarousel($selector = null, $options = array())
  149. {
  150. $this->registerPlugin(self::PLUGIN_CAROUSEL, $selector, $options);
  151. }
  152. /**
  153. * Registers the Bootstrap collapse plugin.
  154. * @param string $selector the CSS selector
  155. * @param array $options the plugin options
  156. * @see http://twitter.github.com/bootstrap/javascript.html#collapse
  157. * @since 0.9.8
  158. */
  159. public function registerCollapse($selector = null, $options = array())
  160. {
  161. $this->registerPlugin(self::PLUGIN_COLLAPSE, $selector, $options);
  162. }
  163. /**
  164. * Registers the Bootstrap dropdowns plugin.
  165. * @param string $selector the CSS selector
  166. * @param array $options the plugin options
  167. * @see http://twitter.github.com/bootstrap/javascript.html#dropdowns
  168. * @since 0.9.8
  169. */
  170. public function registerDropdown($selector = null, $options = array())
  171. {
  172. $this->registerPlugin(self::PLUGIN_DROPDOWN, $selector, $options);
  173. }
  174. /**
  175. * Registers the Bootstrap modal plugin.
  176. * @param string $selector the CSS selector
  177. * @param array $options the plugin options
  178. * @see http://twitter.github.com/bootstrap/javascript.html#modal
  179. * @since 0.9.8
  180. */
  181. public function registerModal($selector = null, $options = array())
  182. {
  183. $this->registerPlugin(self::PLUGIN_MODAL, $selector, $options);
  184. }
  185. /**
  186. * Registers the Bootstrap scrollspy plugin.
  187. * @param string $selector the CSS selector
  188. * @param array $options the plugin options
  189. * @see http://twitter.github.com/bootstrap/javascript.html#scrollspy
  190. * @since 0.9.8
  191. */
  192. public function registerScrollSpy($selector = null, $options = array())
  193. {
  194. $this->registerPlugin(self::PLUGIN_SCROLLSPY, $selector, $options);
  195. }
  196. /**
  197. * Registers the Bootstrap popover plugin.
  198. * @param string $selector the CSS selector
  199. * @param array $options the plugin options
  200. * @see http://twitter.github.com/bootstrap/javascript.html#popover
  201. * @since 0.9.8
  202. */
  203. public function registerPopover($selector = null, $options = array())
  204. {
  205. $this->registerTooltip(); // Popover requires the tooltip plugin
  206. if (!isset($options['selector']))
  207. $options['selector'] = $selector !== null ? $selector : 'a[rel=popover]';
  208. $this->registerPlugin(self::PLUGIN_POPOVER, 'body', $options);
  209. }
  210. /**
  211. * Registers the Bootstrap tabs plugin.
  212. * @param string $selector the CSS selector
  213. * @param array $options the plugin options
  214. * @see http://twitter.github.com/bootstrap/javascript.html#tabs
  215. * @since 0.9.8
  216. */
  217. public function registerTabs($selector = null, $options = array())
  218. {
  219. $this->registerPlugin(self::PLUGIN_TAB, $selector, $options);
  220. }
  221. /**
  222. * Registers the Bootstrap tooltip plugin.
  223. * @param string $selector the CSS selector
  224. * @param array $options the plugin options
  225. * @see http://twitter.github.com/bootstrap/javascript.html#tooltip
  226. * @since 0.9.8
  227. */
  228. public function registerTooltip($selector = null, $options = array())
  229. {
  230. if (!isset($options['selector']))
  231. $options['selector'] = $selector !== null ? $selector : 'a[rel=tooltip]';
  232. $this->registerPlugin(self::PLUGIN_TOOLTIP, 'body', $options);
  233. }
  234. /**
  235. * Registers the Bootstrap typeahead plugin.
  236. * @param string $selector the CSS selector
  237. * @param array $options the plugin options
  238. * @see http://twitter.github.com/bootstrap/javascript.html#typeahead
  239. * @since 0.9.8
  240. */
  241. public function registerTypeahead($selector = null, $options = array())
  242. {
  243. $this->registerPlugin(self::PLUGIN_TYPEAHEAD, $selector, $options);
  244. }
  245. /**
  246. * Registers a Bootstrap JavaScript plugin.
  247. * @param string $name the name of the plugin
  248. * @param string $selector the CSS selector
  249. * @param array $options the plugin options
  250. * @param string $defaultSelector the default CSS selector
  251. * @since 0.9.8
  252. */
  253. protected function registerPlugin($name, $selector = null, $options = array())
  254. {
  255. // Initialization from extension configuration.
  256. $config = isset($this->plugins[$name]) ? $this->plugins[$name] : array();
  257. if ($selector === null && isset($config['selector']))
  258. $selector = $config['selector'];
  259. if (isset($config['options']))
  260. $options = !empty($options) ? CMap::mergeArray($options, $config['options']) : $config['options'];
  261. if ($selector !== null)
  262. {
  263. $key = __CLASS__.'.'.md5($name.$selector.serialize($options));
  264. $options = !empty($options) ? CJavaScript::encode($options) : '';
  265. Yii::app()->clientScript->registerScript($key, "jQuery('{$selector}').{$name}({$options});");
  266. }
  267. }
  268. /**
  269. * Returns the URL to the published assets folder.
  270. * @return string the URL
  271. */
  272. protected function getAssetsUrl()
  273. {
  274. if (isset($this->_assetsUrl))
  275. return $this->_assetsUrl;
  276. else
  277. {
  278. $assetsPath = Yii::getPathOfAlias('bootstrap.assets');
  279. $assetsUrl = Yii::app()->assetManager->publish($assetsPath, true, -1, $this->forceCopyAssets);
  280. return $this->_assetsUrl = $assetsUrl;
  281. }
  282. }
  283. /**
  284. * Returns the extension version number.
  285. * @return string the version
  286. */
  287. public function getVersion()
  288. {
  289. return '2.0.2';
  290. }
  291. }