1<?php
2
3
4namespace ComboStrap;
5
6
7use syntax_plugin_combo_xmlinlinetag;
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        if (!is_array($tooltip)) {
28            LogUtility::msg("The tooltip value ($tooltip) is not an array.");
29            return;
30        }
31
32        /**
33         * Tooltip
34         */
35        $dataAttributeNamespace = Bootstrap::getDataNamespace();
36
37
38        /**
39         * Old tooltip syntax
40         */
41        $title = $tooltip[syntax_plugin_combo_tooltip::TEXT_ATTRIBUTE] ?? null;
42        if ($title === null) {
43
44            $callStack = $tooltip[Tooltip::CALLSTACK] ?? null;
45            if ($callStack !== null) {
46                try {
47                    $title = PluginUtility::renderInstructionsToXhtml($callStack);
48                } catch (ExceptionCompile $e) {
49                    $title = LogUtility::wrapInRedForHtml("Error while rendering the tooltip. Error: {$e->getMessage()}");
50                }
51
52                /**
53                 * New Syntax
54                 * (The new syntax add the attributes to the previous element
55                 */
56                $tagAttributes->addOutputAttributeValue("data{$dataAttributeNamespace}-html", "true");
57
58            }
59        }
60
61
62        if (empty($title)) {
63            $title = LogUtility::wrapInRedForHtml("The tooltip is empty");
64        }
65        $tagAttributes->addOutputAttributeValue("title", $title);
66
67        /**
68         * Snippet
69         */
70        Tooltip::addToolTipSnippetIfNeeded();
71        $tagAttributes->addOutputAttributeValue("data{$dataAttributeNamespace}-toggle", "tooltip");
72
73        /**
74         * Position
75         */
76        $position = $tooltip[Tooltip::POSITION_ATTRIBUTE] ?? null;
77        if ($position === null) {
78            $position = "top";
79        }
80        $tagAttributes->addOutputAttributeValue("data{$dataAttributeNamespace}-placement", "{$position}");
81
82
83        /**
84         * Keyboard user and assistive technology users
85         * If not button or link (ie span), add tabindex to make the element focusable
86         * in order to see the tooltip
87         * Not sure, if this is a good idea
88         *
89         * Arbitrary HTML elements (such as <span>s) can be made focusable by adding the tabindex="0" attribute
90         */
91        $logicalTag = $tagAttributes->getLogicalTag();
92        if (!in_array($logicalTag, [syntax_plugin_combo_link::TAG, ButtonTag::MARKUP_LONG])) {
93            $tagAttributes->addOutputAttributeValue("tabindex", "0");
94        }
95
96
97    }
98
99    /**
100     * tooltip is used also in page protection
101     */
102    public
103    static function addToolTipSnippetIfNeeded()
104    {
105        $snippetSystem = SnippetSystem::getFromContext();
106        $snippetSystem->attachJavascriptFromComponentId(syntax_plugin_combo_tooltip::TAG);
107        $snippetSystem->attachCssInternalStyleSheet(syntax_plugin_combo_tooltip::TAG);
108    }
109}
110