* @license GNU General Public License version 2 or later; see LICENSE.txt */ defined('JPATH_PLATFORM') or die; /** * Utility class for Bootstrap elements. * * @since 3.0 */ abstract class JHtmlBootstrap { /** * @var array Array containing information for loaded files * @since 3.0 */ protected static $loaded = array(); /** * Add javascript support for the Bootstrap affix plugin * * @param string $selector Unique selector for the element to be affixed. * @param array $params An array of options. * Options for the affix plugin can be: * - offset number|function|object Pixels to offset from screen when calculating position of scroll. * If a single number is provided, the offset will be applied in both top * and left directions. To listen for a single direction, or multiple * unique offsets, just provide an object offset: { x: 10 }. * Use a function when you need to dynamically provide an offset * (useful for some responsive designs). * * @return void * * @since 3.1 * * @deprecated 4.0 Bootstrap 4.0 dropped this so will Joomla. */ public static function affix($selector = 'affix', $params = array()) { $sig = md5(serialize(array($selector, $params))); if (!isset(static::$loaded[__METHOD__][$sig])) { // Include Bootstrap framework JHtml::_('bootstrap.framework'); // Setup options object $opt['offset'] = isset($params['offset']) ? $params['offset'] : 10; $options = JHtml::getJSObject($opt); // Attach affix to document JFactory::getDocument()->addScriptDeclaration( 'jQuery(function($){ $(' . json_encode('#' . $selector) . ').affix(' . $options . '); });' ); // Set static array static::$loaded[__METHOD__][$sig] = true; } return; } /** * Add javascript support for Bootstrap alerts * * @param string $selector Common class for the alerts * * @return void * * @since 3.0 */ public static function alert($selector = 'alert') { // Only load once if (isset(static::$loaded[__METHOD__][$selector])) { return; } // Include Bootstrap framework JHtml::_('bootstrap.framework'); // Attach the alerts to the document JFactory::getDocument()->addScriptDeclaration( 'jQuery(function($){ $(' . json_encode('.' . $selector) . ').alert(); });' ); static::$loaded[__METHOD__][$selector] = true; return; } /** * Add javascript support for Bootstrap buttons * * @param string $selector Common class for the buttons * * @return void * * @since 3.1 */ public static function button($selector = 'button') { // Only load once if (isset(static::$loaded[__METHOD__][$selector])) { return; } // Include Bootstrap framework JHtml::_('bootstrap.framework'); // Attach the button to the document JFactory::getDocument()->addScriptDeclaration( 'jQuery(function($){ $(' . json_encode('.' . $selector) . ').button(); });' ); static::$loaded[__METHOD__][$selector] = true; return; } /** * Add javascript support for Bootstrap carousels * * @param string $selector Common class for the carousels. * @param array $params An array of options for the carousel. * Options for the carousel can be: * - interval number The amount of time to delay between automatically cycling an item. * If false, carousel will not automatically cycle. * - pause string Pauses the cycling of the carousel on mouseenter and resumes the cycling * of the carousel on mouseleave. * * @return void * * @since 3.0 */ public static function carousel($selector = 'carousel', $params = array()) { $sig = md5(serialize(array($selector, $params))); if (!isset(static::$loaded[__METHOD__][$sig])) { // Include Bootstrap framework JHtml::_('bootstrap.framework'); // Setup options object $opt['interval'] = isset($params['interval']) ? (int) $params['interval'] : 5000; $opt['pause'] = isset($params['pause']) ? $params['pause'] : 'hover'; $options = JHtml::getJSObject($opt); // Attach the carousel to document JFactory::getDocument()->addScriptDeclaration( 'jQuery(function($){ $(' . json_encode('.' . $selector) . ').carousel(' . $options . '); });' ); // Set static array static::$loaded[__METHOD__][$sig] = true; } return; } /** * Add javascript support for Bootstrap dropdowns * * @param string $selector Common class for the dropdowns * * @return void * * @since 3.0 */ public static function dropdown($selector = 'dropdown-toggle') { // Only load once if (isset(static::$loaded[__METHOD__][$selector])) { return; } // Include Bootstrap framework JHtml::_('bootstrap.framework'); // Attach the dropdown to the document JFactory::getDocument()->addScriptDeclaration( 'jQuery(function($){ $(' . json_encode('.' . $selector) . ').dropdown(); });' ); static::$loaded[__METHOD__][$selector] = true; return; } /** * Method to load the Bootstrap JavaScript framework into the document head * * If debugging mode is on an uncompressed version of Bootstrap is included for easier debugging. * * @param mixed $debug Is debugging mode on? [optional] * * @return void * * @since 3.0 */ public static function framework($debug = null) { // Only load once if (!empty(static::$loaded[__METHOD__])) { return; } // Load jQuery JHtml::_('jquery.framework'); // If no debugging value is set, use the configuration setting if ($debug === null) { $debug = JDEBUG; } JHtml::_('script', 'jui/bootstrap.min.js', array('version' => 'auto', 'relative' => true, 'detectDebug' => $debug)); static::$loaded[__METHOD__] = true; return; } /** * Add javascript support for Bootstrap modals * * @param string $selector The ID selector for the modal. * @param array $params An array of options for the modal. * Options for the modal can be: * - backdrop boolean Includes a modal-backdrop element. * - keyboard boolean Closes the modal when escape key is pressed. * - show boolean Shows the modal when initialized. * - remote string An optional remote URL to load * * @return void * * @since 3.0 * @deprecated 4.0 This method was used by the old renderModal() implementation. * Since the new implementation it is unneeded and the broken JS it was injecting could create issues * As a case, please see: https://github.com/joomla/joomla-cms/pull/6918 */ public static function modal($selector = 'modal', $params = array()) { $sig = md5(serialize(array($selector, $params))); if (!isset(static::$loaded[__METHOD__][$sig])) { // Include Bootstrap framework JHtml::_('bootstrap.framework'); // Setup options object $opt['backdrop'] = isset($params['backdrop']) ? (boolean) $params['backdrop'] : true; $opt['keyboard'] = isset($params['keyboard']) ? (boolean) $params['keyboard'] : true; $opt['show'] = isset($params['show']) ? (boolean) $params['show'] : false; $opt['remote'] = isset($params['remote']) ? $params['remote'] : ''; $options = JHtml::getJSObject($opt); // Attach the modal to document JFactory::getDocument()->addScriptDeclaration( 'jQuery(function($){ $(' . json_encode('#' . $selector) . ').modal(' . $options . '); });' ); // Set static array static::$loaded[__METHOD__][$sig] = true; } return; } /** * Method to render a Bootstrap modal * * @param string $selector The ID selector for the modal. * @param array $params An array of options for the modal. * Options for the modal can be: * - title string The modal title * - backdrop mixed A boolean select if a modal-backdrop element should be included (default = true) * The string 'static' includes a backdrop which doesn't close the modal on click. * - keyboard boolean Closes the modal when escape key is pressed (default = true) * - closeButton boolean Display modal close button (default = true) * - animation boolean Fade in from the top of the page (default = true) * - footer string Optional markup for the modal footer * - url string URL of a resource to be inserted as an `