| @@ -1,0 +1,303 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikWP - Libraries | |
| 4 | + * @subpackage adapter.html | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. | |
| 7 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Utility class for Bootstrap elements. | |
| 16 | + * | |
| 17 | + * @since 10.1.16 | |
| 18 | + */ | |
| 19 | +abstract class JHtmlBootstrap | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * Array containing information for loaded files. | |
| 23 | + * | |
| 24 | + * @var array | |
| 25 | + */ | |
| 26 | + protected static $loaded = array(); | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Add javascript support for Bootstrap modals. | |
| 30 | + * | |
| 31 | + * @param string $selector The ID selector for the modal. | |
| 32 | + * @param array $params An array of options for the modal. | |
| 33 | + * Options for the modal can be: | |
| 34 | + * - backdrop boolean Includes a modal-backdrop element. | |
| 35 | + * - keyboard boolean Closes the modal when escape key is pressed. | |
| 36 | + * - show boolean Shows the modal when initialized. | |
| 37 | + * - remote string An optional remote URL to load. | |
| 38 | + * | |
| 39 | + * @return void | |
| 40 | + */ | |
| 41 | + public static function modal($selector = 'modal', $params = array()) | |
| 42 | + { | |
| 43 | + // do nothing here | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * Method to render a Bootstrap modal. | |
| 48 | + * | |
| 49 | + * @param string $selector The ID selector for the modal. | |
| 50 | + * @param array $params An array of options for the modal. | |
| 51 | + * Options for the modal can be: | |
| 52 | + * - title string The modal title. | |
| 53 | + * - backdrop mixed A boolean select if a modal-backdrop element should be included (default = true). | |
| 54 | + * The string 'static' includes a backdrop which doesn't close the modal on click. | |
| 55 | + * - keyboard boolean Closes the modal when escape key is pressed (default = true). | |
| 56 | + * - closeButton boolean Display modal close button (default = true). | |
| 57 | + * - animation boolean Fade in from the top of the page (default = true). | |
| 58 | + * - footer string Optional markup for the modal footer. | |
| 59 | + * - url string URL of a resource to be inserted as an `<iframe>` inside the modal body. | |
| 60 | + * - height string height of the `<iframe>` containing the remote resource. | |
| 61 | + * - width string width of the `<iframe>` containing the remote resource. | |
| 62 | + * @param string $body Markup for the modal body. Appended after the `<iframe>` if the URL option is set. | |
| 63 | + * | |
| 64 | + * @return string HTML markup for a modal. | |
| 65 | + */ | |
| 66 | + public static function renderModal($selector = 'modal', $params = array(), $body = '') | |
| 67 | + { | |
| 68 | + if (is_array($params)) | |
| 69 | + { | |
| 70 | + $width = isset($params['width']) ? abs($params['width']) : 96; | |
| 71 | + $height = isset($params['height']) ? abs($params['height']) : 90; | |
| 72 | + $left = isset($params['left']) ? abs($params['left']) : $width / 2; | |
| 73 | + | |
| 74 | + $style = "width:$width%;height:$height%;margin-left:-$left%;"; | |
| 75 | + | |
| 76 | + if (isset($params['top'])) | |
| 77 | + { | |
| 78 | + if ($params['top'] === true) | |
| 79 | + { | |
| 80 | + $top = (100 - $height) / 2; | |
| 81 | + } | |
| 82 | + else | |
| 83 | + { | |
| 84 | + $top = $params['top']; | |
| 85 | + } | |
| 86 | + | |
| 87 | + $style .= "top:$top%;"; | |
| 88 | + } | |
| 89 | + } | |
| 90 | + else if (is_string($params)) | |
| 91 | + { | |
| 92 | + // we probably received a style string, use it directly | |
| 93 | + $style = $params; | |
| 94 | + | |
| 95 | + // then, reset the params array | |
| 96 | + $params = array(); | |
| 97 | + } | |
| 98 | + else | |
| 99 | + { | |
| 100 | + // use some default styles | |
| 101 | + $style = "width:96%;height:90%;margin-left:-48%;top:5%;"; | |
| 102 | + | |
| 103 | + // cast params to array in case of non-scalar argument | |
| 104 | + if (!is_scalar($params)) | |
| 105 | + { | |
| 106 | + $params = (array) $params; | |
| 107 | + } | |
| 108 | + else | |
| 109 | + { | |
| 110 | + $params = array(); | |
| 111 | + } | |
| 112 | + } | |
| 113 | + | |
| 114 | + // remove initial "jmodal-" if set from ID | |
| 115 | + $params['id'] = preg_replace('/^jmodal-/', '', $selector); | |
| 116 | + $params['body'] = $body; | |
| 117 | + $params['style'] = $style; | |
| 118 | + | |
| 119 | + // render modal layout | |
| 120 | + return JHtml::fetch('layoutfile', 'html.plugins.modal')->render($params); | |
| 121 | + } | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * Adds javascript support for Bootstrap popovers. | |
| 125 | + * | |
| 126 | + * @param string $selector Selector for the popover. | |
| 127 | + * @param array $options An array of options for the popover. | |
| 128 | + * Options for the popover can be: | |
| 129 | + * - animation boolean apply a css fade transition to the popover | |
| 130 | + * - html boolean Insert HTML into the popover. If false, jQuery's text method will be used to insert | |
| 131 | + * content into the dom. | |
| 132 | + * - placement string|function how to position the popover - top | bottom | left | right | |
| 133 | + * - selector string If a selector is provided, popover objects will be delegated to the specified targets. | |
| 134 | + * - trigger string how popover is triggered - hover | focus | manual | |
| 135 | + * - title string|function default title value if `title` tag isn't present | |
| 136 | + * - content string|function default content value if `data-content` attribute isn't present | |
| 137 | + * - delay number|object delay showing and hiding the popover (ms) - does not apply to manual trigger type | |
| 138 | + * If a number is supplied, delay is applied to both hide/show | |
| 139 | + * Object structure is: delay: { show: 500, hide: 100 } | |
| 140 | + * - container string|boolean Appends the popover to a specific element: { container: 'body' } | |
| 141 | + * | |
| 142 | + * @return void | |
| 143 | + */ | |
| 144 | + public static function popover($selector = '.wpPopover', $params = array()) | |
| 145 | + { | |
| 146 | + $sign = serialize(array($selector, $params)); | |
| 147 | + | |
| 148 | + // only load once | |
| 149 | + if (isset(static::$loaded[__METHOD__][$sign])) | |
| 150 | + { | |
| 151 | + return; | |
| 152 | + } | |
| 153 | + | |
| 154 | + /** | |
| 155 | + * Always disable HTML sanitizing to allow any kind of tags. | |
| 156 | + * | |
| 157 | + * @since 10.1.27 | |
| 158 | + */ | |
| 159 | + $params['sanitize'] = false; | |
| 160 | + | |
| 161 | + /** | |
| 162 | + * In case the "container" attribute is not set, | |
| 163 | + * always place the popover within the body. | |
| 164 | + * | |
| 165 | + * @since 10.1.28 | |
| 166 | + */ | |
| 167 | + if (!isset($params['container'])) | |
| 168 | + { | |
| 169 | + $params['container'] = 'body'; | |
| 170 | + } | |
| 171 | + | |
| 172 | + $data = $params ? json_encode($params) : '{}'; | |
| 173 | + JFactory::getDocument()->addScriptDeclaration( | |
| 174 | +<<<JAVASCRIPT | |
| 175 | +jQuery(function() { | |
| 176 | + jQuery('$selector').popover($data); | |
| 177 | +}); | |
| 178 | +JAVASCRIPT | |
| 179 | + ); | |
| 180 | + | |
| 181 | + // set static array | |
| 182 | + static::$loaded[__METHOD__][$sign] = true; | |
| 183 | + } | |
| 184 | + | |
| 185 | + /** | |
| 186 | + * Add javascript support for Bootstrap tooltips. | |
| 187 | + * | |
| 188 | + * Add a title attribute to any element in the form: | |
| 189 | + * title="title::text" | |
| 190 | + * | |
| 191 | + * @param string $selector The ID selector for the tooltip. | |
| 192 | + * @param array $params An array of options for the tooltip. | |
| 193 | + * Options for the tooltip can be: | |
| 194 | + * - animation boolean Apply a CSS fade transition to the tooltip | |
| 195 | + * - html boolean Insert HTML into the tooltip. If false, jQuery's text method will be used to insert | |
| 196 | + * content into the dom. | |
| 197 | + * - placement string|function How to position the tooltip - top | bottom | left | right | |
| 198 | + * - selector string If a selector is provided, tooltip objects will be delegated to the specified targets. | |
| 199 | + * - title string|function Default title value if `title` tag isn't present | |
| 200 | + * - trigger string How tooltip is triggered - hover | focus | manual | |
| 201 | + * - delay integer Delay showing and hiding the tooltip (ms) - does not apply to manual trigger type | |
| 202 | + * If a number is supplied, delay is applied to both hide/show | |
| 203 | + * Object structure is: delay: { show: 500, hide: 100 } | |
| 204 | + * - container string|boolean Appends the popover to a specific element: { container: 'body' } | |
| 205 | + * | |
| 206 | + * @return void | |
| 207 | + */ | |
| 208 | + public static function tooltip($selector = '.hasTooltip', $params = array()) | |
| 209 | + { | |
| 210 | + $sign = serialize(array($selector, $params)); | |
| 211 | + | |
| 212 | + // only load once | |
| 213 | + if (isset(static::$loaded[__METHOD__][$sign])) | |
| 214 | + { | |
| 215 | + return; | |
| 216 | + } | |
| 217 | + | |
| 218 | + /** | |
| 219 | + * If the container has not been specified, | |
| 220 | + * always append tooltips at the end of the body. | |
| 221 | + * | |
| 222 | + * @since 10.1.30 | |
| 223 | + */ | |
| 224 | + if (!isset($params['container'])) | |
| 225 | + { | |
| 226 | + $params['container'] = 'body'; | |
| 227 | + } | |
| 228 | + | |
| 229 | + $data = $params ? json_encode($params) : '{}'; | |
| 230 | + JFactory::getDocument()->addScriptDeclaration( | |
| 231 | +<<<JAVASCRIPT | |
| 232 | +jQuery(function() { | |
| 233 | + jQuery('$selector').tooltip($data); | |
| 234 | +}); | |
| 235 | +JAVASCRIPT | |
| 236 | + ); | |
| 237 | + | |
| 238 | + // set static array | |
| 239 | + static::$loaded[__METHOD__][$sign] = true; | |
| 240 | + } | |
| 241 | + | |
| 242 | + /** | |
| 243 | + * Creates a tab pane. | |
| 244 | + * | |
| 245 | + * @param string $selector The pane identifier. | |
| 246 | + * @param array $params The parameters for the pane. | |
| 247 | + * | |
| 248 | + * @return string | |
| 249 | + */ | |
| 250 | + public static function startTabSet($selector = 'myTab', $params = array()) | |
| 251 | + { | |
| 252 | + $sign = md5(serialize(array($selector, $params))); | |
| 253 | + | |
| 254 | + if (!isset(static::$loaded[__METHOD__][$sign])) | |
| 255 | + { | |
| 256 | + $opt = array(); | |
| 257 | + // setup options object | |
| 258 | + $opt['active'] = (isset($params['active']) && $params['active']) ? (string) $params['active'] : ''; | |
| 259 | + | |
| 260 | + // Set static array | |
| 261 | + static::$loaded[__METHOD__][$sign] = true; | |
| 262 | + static::$loaded[__METHOD__][$selector] = $opt; | |
| 263 | + } | |
| 264 | + | |
| 265 | + return JHtml::fetch('layoutfile', 'html.bootstrap.starttabset')->render(array('selector' => $selector)); | |
| 266 | + } | |
| 267 | + | |
| 268 | + /** | |
| 269 | + * Closes the current tab pane. | |
| 270 | + * | |
| 271 | + * @return string HTML to close the pane. | |
| 272 | + */ | |
| 273 | + public static function endTabSet() | |
| 274 | + { | |
| 275 | + return JHtml::fetch('layoutfile', 'html.bootstrap.endtabset')->render(); | |
| 276 | + } | |
| 277 | + | |
| 278 | + /** | |
| 279 | + * Begins the display of a new tab content panel. | |
| 280 | + * | |
| 281 | + * @param string $selector Identifier of the panel. | |
| 282 | + * @param string $id The ID of the div element. | |
| 283 | + * @param string $title The title text for the new UL tab. | |
| 284 | + * | |
| 285 | + * @return string HTML to start a new panel. | |
| 286 | + */ | |
| 287 | + public static function addTab($selector, $id, $title) | |
| 288 | + { | |
| 289 | + $active = (static::$loaded['JHtmlBootstrap::startTabSet'][$selector]['active'] == $id) ? ' active' : ''; | |
| 290 | + | |
| 291 | + return JHtml::fetch('layoutfile', 'html.bootstrap.addtab')->render(array('id' => $id, 'active' => $active, 'selector' => $selector, 'title' => $title)); | |
| 292 | + } | |
| 293 | + | |
| 294 | + /** | |
| 295 | + * Closes the current tab content panel. | |
| 296 | + * | |
| 297 | + * @return string HTML to close the pane. | |
| 298 | + */ | |
| 299 | + public static function endTab() | |
| 300 | + { | |
| 301 | + return JHtml::fetch('layoutfile', 'html.bootstrap.endtab')->render(); | |
| 302 | + } | |
| 303 | +} | |