xref: /template/strap/ComboStrap/Tooltip.php (revision 4cadd4f8c541149bdda95f080e38a6d4e3a640ca)
1<?php
2
3
4namespace ComboStrap;
5
6
7use syntax_plugin_combo_button;
8use syntax_plugin_combo_link;
9use syntax_plugin_combo_tooltip;
10
11class Tooltip
12{
13
14
15    public const TOOLTIP_ATTRIBUTE = "tooltip";
16    public const CALLSTACK = "callstack";
17    public const POSITION_ATTRIBUTE = "position";
18
19    public static function processTooltip(TagAttributes &$tagAttributes)
20    {
21
22        $tooltip = $tagAttributes->getValueAndRemove(self::TOOLTIP_ATTRIBUTE);
23        if ($tooltip === null) {
24            return;
25        }
26
27        /**
28         * Tooltip
29         */
30        $dataAttributeNamespace = Bootstrap::getDataNamespace();
31
32
33        /**
34         * Old tooltip syntax
35         */
36        $title = $tooltip[syntax_plugin_combo_tooltip::TEXT_ATTRIBUTE];
37        if ($title === null) {
38
39            $callStack = $tooltip[Tooltip::CALLSTACK];
40            if ($callStack !== null) {
41                try {
42                    $title = PluginUtility::renderInstructionsToXhtml($callStack);
43                } catch (ExceptionCombo $e) {
44                    $title = LogUtility::wrapInRedForHtml("Error while rendering the tooltip. Error: {$e->getMessage()}");
45                }
46
47                /**
48                 * New Syntax
49                 * (The new syntax add the attributes to the previous element
50                 */
51                $tagAttributes->addOutputAttributeValue("data{$dataAttributeNamespace}-html", "true");
52
53            }
54        }
55
56
57
58
59        if (empty($title)) {
60            $title = LogUtility::wrapInRedForHtml("The tooltip is empty");
61        }
62        $tagAttributes->addOutputAttributeValue("title", $title);
63
64        /**
65         * Snippet
66         */
67        Tooltip::addToolTipSnippetIfNeeded();
68        $tagAttributes->addOutputAttributeValue("data{$dataAttributeNamespace}-toggle", "tooltip");
69
70        /**
71         * Position
72         */
73        $position = $tooltip[Tooltip::POSITION_ATTRIBUTE];
74        if ($position === null) {
75            $position = "top";
76        }
77        $tagAttributes->addOutputAttributeValue("data{$dataAttributeNamespace}-placement", "${position}");
78
79
80        /**
81         * Keyboard user and assistive technology users
82         * If not button or link (ie span), add tabindex to make the element focusable
83         * in order to see the tooltip
84         * Not sure, if this is a good idea
85         *
86         * Arbitrary HTML elements (such as <span>s) can be made focusable by adding the tabindex="0" attribute
87         */
88        $logicalTag = $tagAttributes->getLogicalTag();
89        if (!in_array($logicalTag, [syntax_plugin_combo_link::TAG, syntax_plugin_combo_button::TAG])) {
90            $tagAttributes->addOutputAttributeValue("tabindex", "0");
91        }
92
93
94    }
95
96    /**
97     * tooltip is used also in page protection
98     */
99    public
100    static function addToolTipSnippetIfNeeded()
101    {
102        PluginUtility::getSnippetManager()->attachInternalJavascriptForSlot(syntax_plugin_combo_tooltip::TAG);
103        PluginUtility::getSnippetManager()->attachCssInternalStyleSheetForSlot(syntax_plugin_combo_tooltip::TAG);
104    }
105}
106