PageRenderTime 89ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/cms/html/bootstrap.php

https://bitbucket.org/eternaware/joomus
PHP | 546 lines | 230 code | 69 blank | 247 comment | 31 complexity | 0b18c1da9249dabda7ab9d65923aa773 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Libraries
  4. * @subpackage HTML
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Utility class for Bootstrap elements.
  12. *
  13. * @package Joomla.Libraries
  14. * @subpackage HTML
  15. * @since 3.0
  16. */
  17. abstract class JHtmlBootstrap
  18. {
  19. /**
  20. * @var array Array containing information for loaded files
  21. * @since 3.0
  22. */
  23. protected static $loaded = array();
  24. /**
  25. * Add javascript support for Bootstrap alerts
  26. *
  27. * @param string $selector Common class for the alerts
  28. * @return void
  29. *
  30. * @since 3.0
  31. */
  32. public static function alert($selector = 'alert')
  33. {
  34. // Only load once
  35. if (isset(self::$loaded[__METHOD__][$selector]))
  36. {
  37. return;
  38. }
  39. // Include Bootstrap framework
  40. self::framework();
  41. // Attach the alerts to the document
  42. JFactory::getDocument()->addScriptDeclaration(
  43. "(function($){
  44. $('.$selector').alert();
  45. })(jQuery);"
  46. );
  47. self::$loaded[__METHOD__][$selector] = true;
  48. return;
  49. }
  50. /**
  51. * Add javascript support for Bootstrap carousels
  52. *
  53. * @param string $selector Common class for the carousels.
  54. * @param array $params An array of options for the modal.
  55. * Options for the modal can be:
  56. * - interval number The amount of time to delay between automatically cycling an item.
  57. * If false, carousel will not automatically cycle.
  58. * - pause string Pauses the cycling of the carousel on mouseenter and resumes the cycling
  59. * of the carousel on mouseleave.
  60. *
  61. * @return void
  62. *
  63. * @since 3.0
  64. */
  65. public static function carousel($selector = 'carousel', $params = array())
  66. {
  67. $sig = md5(serialize(array($selector, $params)));
  68. if (!isset(self::$loaded[__METHOD__][$sig]))
  69. {
  70. // Include Bootstrap framework
  71. self::framework();
  72. // Setup options object
  73. $opt['interval'] = (isset($params['interval']) && ($params['interval'])) ? (int) $params['interval'] : 5000;
  74. $opt['pause'] = (isset($params['pause']) && ($params['pause'])) ? $params['pause'] : 'hover';
  75. $options = self::_getJSObject($opt);
  76. // Attach the carousel to document
  77. JFactory::getDocument()->addScriptDeclaration(
  78. "(function($){
  79. $('.$selector').carousel($options);
  80. })(jQuery);"
  81. );
  82. // Set static array
  83. self::$loaded[__METHOD__][$sig] = true;
  84. }
  85. return;
  86. }
  87. /**
  88. * Add javascript support for Bootstrap dropdowns
  89. *
  90. * @param string $selector Common class for the dropdowns
  91. * @return void
  92. *
  93. * @since 3.0
  94. */
  95. public static function dropdown($selector = 'dropdown-toggle')
  96. {
  97. // Only load once
  98. if (isset(self::$loaded[__METHOD__][$selector]))
  99. {
  100. return;
  101. }
  102. // Include Bootstrap framework
  103. self::framework();
  104. // Attach the dropdown to the document
  105. JFactory::getDocument()->addScriptDeclaration(
  106. "(function($){
  107. $('.$selector').dropdown();
  108. })(jQuery);"
  109. );
  110. self::$loaded[__METHOD__][$selector] = true;
  111. return;
  112. }
  113. /**
  114. * Method to load the Bootstrap JavaScript framework into the document head
  115. *
  116. * If debugging mode is on an uncompressed version of Bootstrap is included for easier debugging.
  117. *
  118. * @param mixed $debug Is debugging mode on? [optional]
  119. *
  120. * @return void
  121. *
  122. * @since 3.0
  123. */
  124. public static function framework($debug = null)
  125. {
  126. // Only load once
  127. if (!empty(self::$loaded[__METHOD__]))
  128. {
  129. return;
  130. }
  131. // Load jQuery
  132. JHtml::_('jquery.framework');
  133. // If no debugging value is set, use the configuration setting
  134. if ($debug === null)
  135. {
  136. $config = JFactory::getConfig();
  137. $debug = (boolean) $config->get('debug');
  138. }
  139. JHtml::_('script', 'jui/bootstrap.min.js', false, true, false, false, $debug);
  140. self::$loaded[__METHOD__] = true;
  141. return;
  142. }
  143. /**
  144. * Add javascript support for Bootstrap modals
  145. *
  146. * @param string $selector The ID selector for the modal.
  147. * @param array $params An array of options for the modal.
  148. * Options for the modal can be:
  149. * - backdrop boolean Includes a modal-backdrop element.
  150. * - keyboard boolean Closes the modal when escape key is pressed.
  151. * - show boolean Shows the modal when initialized.
  152. * - remote string An optional remote URL to load
  153. *
  154. * @return void
  155. *
  156. * @since 3.0
  157. */
  158. public static function modal($selector = 'modal', $params = array())
  159. {
  160. $sig = md5(serialize(array($selector, $params)));
  161. if (!isset(self::$loaded[__METHOD__][$sig]))
  162. {
  163. // Include Bootstrap framework
  164. self::framework();
  165. // Setup options object
  166. $opt['backdrop'] = (isset($params['backdrop']) && ($params['backdrop'])) ? (boolean) $params['backdrop'] : true;
  167. $opt['keyboard'] = (isset($params['keyboard']) && ($params['keyboard'])) ? (boolean) $params['keyboard'] : true;
  168. $opt['show'] = (isset($params['show']) && ($params['show'])) ? (boolean) $params['show'] : true;
  169. $opt['remote'] = (isset($params['remote']) && ($params['remote'])) ? (boolean) $params['remote'] : '';
  170. $options = JHtml::getJSObject($opt);
  171. // Attach the modal to document
  172. JFactory::getDocument()->addScriptDeclaration(
  173. "(function($){
  174. $('#$selector').modal($options);
  175. })(jQuery);"
  176. );
  177. // Set static array
  178. self::$loaded[__METHOD__][$sig] = true;
  179. }
  180. return;
  181. }
  182. /**
  183. * Add javascript support for Bootstrap popovers
  184. *
  185. * Use element's Title as popover content
  186. *
  187. * @param string $selector Selector for the tooltip
  188. * @param array $params An array of options for the tooltip.
  189. * Options for the tooltip can be:
  190. * animation boolean apply a css fade transition to the tooltip
  191. * html boolean Insert HTML into the tooltip. If false, jQuery's text method will be used to insert
  192. * content into the dom.
  193. * placement string|function how to position the tooltip - top | bottom | left | right
  194. * selector string If a selector is provided, tooltip objects will be delegated to the specified targets.
  195. * title string|function default title value if `title` tag isn't present
  196. * trigger string how tooltip is triggered - hover | focus | manual
  197. * content string|function default content value if `data-content` attribute isn't present
  198. * delay number|object delay showing and hiding the tooltip (ms) - does not apply to manual trigger type
  199. * If a number is supplied, delay is applied to both hide/show
  200. * Object structure is: delay: { show: 500, hide: 100 }
  201. *
  202. * @return void
  203. *
  204. * @since 3.0
  205. */
  206. public static function popover($selector = '[rel=popover]', $params = array())
  207. {
  208. // Only load once
  209. $sig = md5(serialize(array($selector, $params)));
  210. if (isset(self::$loaded[__METHOD__][$sig]))
  211. {
  212. return;
  213. }
  214. // Include Bootstrap framework
  215. self::framework();
  216. $opt['animation'] = isset($params['animation']) ? $params['animation'] : true;
  217. $opt['html'] = isset($params['html']) ? $params['html'] : true;
  218. $opt['placement'] = isset($params['placement']) ? $params['placement'] : 'top';
  219. $opt['selector'] = isset($params['selector']) ? $params['selector'] : false;
  220. $opt['title'] = isset($params['title']) ? $params['title'] : '';
  221. $opt['trigger'] = isset($params['trigger']) ? $params['trigger'] : 'hover';
  222. $opt['content'] = isset($params['content']) ? $params['content'] : '';
  223. $opt['delay'] = isset($params['delay']) ? $params['delay'] : 0;
  224. $options = JHtml::getJSObject($opt);
  225. // Attach the popover to the document
  226. JFactory::getDocument()->addScriptDeclaration(
  227. "(function($){
  228. $('#$selector').popover($options);
  229. })(jQuery);"
  230. );
  231. self::$loaded[__METHOD__][$sig] = true;
  232. return;
  233. }
  234. /**
  235. * Add javascript support for Bootstrap ScrollSpy
  236. *
  237. * @param string $selector The ID selector for the ScrollSpy element.
  238. * @param array $params An array of options for the ScrollSpy.
  239. * Options for the modal can be:
  240. * - offset number Pixels to offset from top when calculating position of scroll.
  241. *
  242. * @return void
  243. *
  244. * @since 3.0
  245. */
  246. public static function scrollspy($selector = 'navbar', $params = array())
  247. {
  248. $sig = md5(serialize(array($selector, $params)));
  249. if (!isset(self::$loaded[__METHOD__][$sig]))
  250. {
  251. // Include Bootstrap framework
  252. self::framework();
  253. // Setup options object
  254. $opt['offset'] = (isset($params['offset']) && ($params['offset'])) ? (int) $params['offset'] : 10;
  255. $options = JHtml::getJSObject($opt);
  256. // Attach ScrollSpy to document
  257. JFactory::getDocument()->addScriptDeclaration(
  258. "(function($){
  259. $('#$selector').scrollspy($options);
  260. })(jQuery);"
  261. );
  262. // Set static array
  263. self::$loaded[__METHOD__][$sig] = true;
  264. }
  265. return;
  266. }
  267. /**
  268. * Add javascript support for Bootstrap tooltips
  269. *
  270. * Add a title attribute to any element in the form
  271. * title="title::text"
  272. *
  273. * @param string $selector The ID selector for the tooltip.
  274. * @param array $params An array of options for the tooltip.
  275. * Options for the tooltip can be:
  276. * - animation boolean Apply a CSS fade transition to the tooltip
  277. * - html boolean Insert HTML into the tooltip. If false, jQuery's text method will be used to insert
  278. * content into the dom.
  279. * - placement string|function How to position the tooltip - top | bottom | left | right
  280. * - selector string If a selector is provided, tooltip objects will be delegated to the specified targets.
  281. * - title string|function Default title value if `title` tag isn't present
  282. * - trigger string How tooltip is triggered - hover | focus | manual
  283. * - delay number Delay showing and hiding the tooltip (ms) - does not apply to manual trigger type
  284. * If a number is supplied, delay is applied to both hide/show
  285. * Object structure is: delay: { show: 500, hide: 100 }
  286. *
  287. * @return void
  288. *
  289. * @since 3.0
  290. */
  291. public static function tooltip($selector = 'content', $params = array())
  292. {
  293. $sig = md5(serialize(array($selector, $params)));
  294. if (!isset(self::$loaded[__METHOD__][$sig]))
  295. {
  296. // Include Bootstrap framework
  297. self::framework();
  298. // Setup options object
  299. $opt['animation'] = (isset($params['animation']) && ($params['animation'])) ? (boolean) $params['animation'] : true;
  300. $opt['html'] = (isset($params['html']) && ($params['html'])) ? (boolean) $params['html'] : true;
  301. $opt['placement'] = (isset($params['placement']) && ($params['placement'])) ? (string) $params['placement'] : 'top';
  302. $opt['selector'] = (isset($params['selector']) && ($params['selector'])) ? (string) $params['selector'] : false;
  303. $opt['title'] = (isset($params['title']) && ($params['title'])) ? (string) $params['title'] : '';
  304. $opt['trigger'] = (isset($params['trigger']) && ($params['trigger'])) ? (string) $params['trigger'] : 'hover';
  305. $opt['delay'] = (isset($params['delay']) && ($params['delay'])) ? (int) $params['delay'] : 0;
  306. $options = JHtml::getJSObject($opt);
  307. // Attach tooltips to document
  308. JFactory::getDocument()->addScriptDeclaration(
  309. "(function($){
  310. $('#$selector').tooltip($options);
  311. })(jQuery);"
  312. );
  313. // Set static array
  314. self::$loaded[__METHOD__][$sig] = true;
  315. }
  316. return;
  317. }
  318. /**
  319. * Add javascript support for Bootstrap accordians and insert the accordian
  320. *
  321. * @param string $selector The ID selector for the tooltip.
  322. * @param array $params An array of options for the tooltip.
  323. * Options for the tooltip can be:
  324. * - parent selector If selector then all collapsible elements under the specified parent will be closed when this
  325. * collapsible item is shown. (similar to traditional accordion behavior)
  326. * - toggle boolean Toggles the collapsible element on invocation
  327. * - active string Sets the active slide during load
  328. *
  329. * @return string HTML for the accordian
  330. *
  331. * @since 3.0
  332. */
  333. public static function startAccordion($selector = 'myAccordian', $params = array())
  334. {
  335. $sig = md5(serialize(array($selector, $params)));
  336. if (!isset(self::$loaded[__METHOD__][$sig]))
  337. {
  338. // Include Bootstrap framework
  339. self::framework();
  340. // Setup options object
  341. $opt['parent'] = (isset($params['parent']) && ($params['parent'])) ? (boolean) $params['parent'] : false;
  342. $opt['toggle'] = (isset($params['toggle']) && ($params['toggle'])) ? (boolean) $params['toggle'] : true;
  343. $opt['active'] = (isset($params['active']) && ($params['active'])) ? (string) $params['active'] : '';
  344. $options = JHtml::getJSObject($opt);
  345. // Attach accordion to document
  346. JFactory::getDocument()->addScriptDeclaration(
  347. "(function($){
  348. $('#$selector').collapse($options);
  349. })(jQuery);"
  350. );
  351. // Set static array
  352. self::$loaded[__METHOD__][$sig] = true;
  353. self::$loaded[__METHOD__]['active'] = $opt['active'];
  354. }
  355. return '<div id="' . $selector . '" class="accordion">';
  356. }
  357. /**
  358. * Close the current accordion
  359. *
  360. * @return string HTML to close the accordian
  361. *
  362. * @since 3.0
  363. */
  364. public static function endAccordion()
  365. {
  366. return '</div></div>';
  367. }
  368. /**
  369. * Begins the display of a new accordion slide.
  370. *
  371. * @param string $selector Identifier of the accordion group.
  372. * @param string $text Text to display.
  373. * @param string $id Identifier of the slide.
  374. *
  375. * @return string HTML to add the slide
  376. *
  377. * @since 3.0
  378. */
  379. public static function addSlide($selector, $text, $id)
  380. {
  381. $in = (self::$loaded['JHtmlBootstrap::startAccordion']['active'] == $id) ? ' in' : '';
  382. $html = '<div class="accordion-group">'
  383. . '<div class="accordion-heading">'
  384. . '<strong><a href="#' . $id . '" data-parent="#' . $selector . '" data-toggle="collapse" class="accordion-toggle">'
  385. . $text
  386. . '</a></strong>'
  387. . '</div>'
  388. . '<div class="accordion-body collapse' . $in . '" id="' . $id . '">'
  389. . '<div class="accordion-inner">';
  390. return $html;
  391. }
  392. /**
  393. * Close the current slide
  394. *
  395. * @return string HTML to close the slide
  396. *
  397. * @since 3.0
  398. */
  399. public static function endSlide()
  400. {
  401. return '</div></div></div>';
  402. }
  403. /**
  404. * Creates a tab pane
  405. *
  406. * @param string $selector The pane identifier.
  407. * @param array $params The parameters for the pane
  408. *
  409. * @return string
  410. *
  411. * @since 3.0
  412. */
  413. public static function startPane($selector = 'myTab', $params = array())
  414. {
  415. $sig = md5(serialize(array($selector, $params)));
  416. if (!isset(self::$loaded[__METHOD__][$sig]))
  417. {
  418. // Include Bootstrap framework
  419. self::framework();
  420. // Setup options object
  421. $opt['active'] = (isset($params['active']) && ($params['active'])) ? (string) $params['active'] : '';
  422. $options = JHtml::getJSObject($opt);
  423. // Attach tooltips to document
  424. JFactory::getDocument()->addScriptDeclaration(
  425. "(function($){
  426. $('#$selector a').click(function (e) {
  427. e.preventDefault();
  428. $(this).tab('show');
  429. });
  430. })(jQuery);"
  431. );
  432. // Set static array
  433. self::$loaded[__METHOD__][$sig] = true;
  434. self::$loaded[__METHOD__][$selector]['active'] = $opt['active'];
  435. }
  436. return '<div class="tab-content" id="' . $selector . 'Content">';
  437. }
  438. /**
  439. * Close the current tab pane
  440. *
  441. * @return string HTML to close the pane
  442. *
  443. * @since 3.0
  444. */
  445. public static function endPane()
  446. {
  447. return '</div>';
  448. }
  449. /**
  450. * Begins the display of a new tab content panel.
  451. *
  452. * @param string $selector Identifier of the panel.
  453. * @param string $id The ID of the div element
  454. *
  455. * @return string HTML to start a new panel
  456. *
  457. * @since 3.0
  458. */
  459. public static function addPanel($selector, $id)
  460. {
  461. $active = (self::$loaded['JHtmlBootstrap::startPane'][$selector]['active'] == $id) ? ' active' : '';
  462. return '<div id="' . $id . '" class="tab-pane' . $active . '">';
  463. }
  464. /**
  465. * Close the current tab content panel
  466. *
  467. * @return string HTML to close the pane
  468. *
  469. * @since 3.0
  470. */
  471. public static function endPanel()
  472. {
  473. return '</div>';
  474. }
  475. }