1<?php 2 3 4use ComboStrap\PluginUtility; 5use ComboStrap\Tag; 6 7if (!defined('DOKU_INC')) die(); 8 9/** 10 * Class syntax_plugin_combo_tooltip 11 * Implementation of a tooltip 12 */ 13class syntax_plugin_combo_tooltip extends DokuWiki_Syntax_Plugin 14{ 15 16 const TAG = "tooltip"; 17 const TEXT_ATTRIBUTE = "text"; 18 const POSITION_ATTRIBUTE = "position"; 19 const SCRIPT_ID = "combo_tooltip"; 20 21 22 public static function addToolTipSnippetIfNeeded(Doku_Renderer_xhtml $renderer) 23 { 24 if (!PluginUtility::htmlSnippetAlreadyAdded($renderer->info, self::TAG)) { 25 $renderer->doc .= "<script id=\"" . self::SCRIPT_ID . "\">" . DOKU_LF 26 . "window.addEventListener('load', function () { jQuery('[data-toggle=\"tooltip\"]').tooltip() })" . DOKU_LF 27 . "</script>" . DOKU_LF; 28 } 29 } 30 31 32 /** 33 * Syntax Type. 34 * 35 * Needs to return one of the mode types defined in $PARSER_MODES in parser.php 36 * @see https://www.dokuwiki.org/devel:syntax_plugins#syntax_types 37 * @see DokuWiki_Syntax_Plugin::getType() 38 */ 39 function getType() 40 { 41 return 'container'; 42 } 43 44 /** 45 * How Dokuwiki will add P element 46 * 47 * * 'normal' - The plugin can be used inside paragraphs (inline) 48 * * 'block' - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs 49 * * 'stack' - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs 50 * 51 * @see DokuWiki_Syntax_Plugin::getPType() 52 * @see https://www.dokuwiki.org/devel:syntax_plugins#ptype 53 */ 54 function getPType() 55 { 56 return 'normal'; 57 } 58 59 /** 60 * @return array 61 * Allow which kind of plugin inside 62 * 63 * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs') 64 * because we manage self the content and we call self the parser 65 * 66 * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php 67 */ 68 function getAllowedTypes() 69 { 70 return array('baseonly', 'container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs'); 71 } 72 73 function getSort() 74 { 75 return 201; 76 } 77 78 79 function connectTo($mode) 80 { 81 82 $pattern = PluginUtility::getContainerTagPattern(self::TAG); 83 $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeForComponent($this->getPluginComponent())); 84 85 } 86 87 function postConnect() 88 { 89 90 $this->Lexer->addExitPattern('</' . self::TAG . '>', PluginUtility::getModeForComponent($this->getPluginComponent())); 91 92 } 93 94 /** 95 * 96 * The handle function goal is to parse the matched syntax through the pattern function 97 * and to return the result for use in the renderer 98 * This result is always cached until the page is modified. 99 * @param string $match 100 * @param int $state 101 * @param int $pos - byte position in the original source file 102 * @param Doku_Handler $handler 103 * @return array|bool 104 * @see DokuWiki_Syntax_Plugin::handle() 105 * 106 */ 107 function handle($match, $state, $pos, Doku_Handler $handler) 108 { 109 110 switch ($state) { 111 112 case DOKU_LEXER_ENTER : 113 $attributes = PluginUtility::getTagAttributes($match); 114 $html = ""; 115 if (isset($attributes[self::TEXT_ATTRIBUTE])) { 116 $position = "top"; 117 if (isset($attributes[self::POSITION_ATTRIBUTE])) { 118 $position = $attributes[self::POSITION_ATTRIBUTE]; 119 } 120 $html = "<span class=\"d-inline-block\" tabindex=\"0\" data-toggle=\"tooltip\" data-placement=\"${position}\" title=\"" . $attributes[self::TEXT_ATTRIBUTE] . "\">" . DOKU_LF; 121 } 122 return array( 123 PluginUtility::STATE => $state, 124 PluginUtility::ATTRIBUTES => $attributes, 125 PluginUtility::PAYLOAD => $html 126 ); 127 128 case DOKU_LEXER_UNMATCHED : 129 return array( 130 PluginUtility::STATE => $state, 131 PluginUtility::PAYLOAD => PluginUtility::escape($match) 132 ); 133 134 case DOKU_LEXER_EXIT : 135 136 $tag = new Tag(self::TAG, array(), $state, $handler->calls); 137 $text = $tag->getOpeningTag()->getAttribute(self::TEXT_ATTRIBUTE); 138 $html = ""; 139 if (!empty($text)) { 140 $html = "</span>"; 141 } 142 return array( 143 PluginUtility::STATE => $state, 144 PluginUtility::PAYLOAD => $html 145 ); 146 147 148 } 149 return array(); 150 151 } 152 153 /** 154 * Render the output 155 * @param string $format 156 * @param Doku_Renderer $renderer 157 * @param array $data - what the function handle() return'ed 158 * @return boolean - rendered correctly? (however, returned value is not used at the moment) 159 * @see DokuWiki_Syntax_Plugin::render() 160 * 161 * 162 */ 163 function render($format, Doku_Renderer $renderer, $data) 164 { 165 if ($format == 'xhtml') { 166 167 /** @var Doku_Renderer_xhtml $renderer */ 168 $state = $data[PluginUtility::STATE]; 169 switch ($state) { 170 171 case DOKU_LEXER_UNMATCHED: 172 case DOKU_LEXER_ENTER : 173 $renderer->doc .= $data[PluginUtility::PAYLOAD]; 174 break; 175 176 case DOKU_LEXER_EXIT: 177 $html = $data[PluginUtility::PAYLOAD]; 178 if (!empty($html)) { 179 180 self::addToolTipSnippetIfNeeded($renderer); 181 } 182 $renderer->doc .= $html; 183 184 } 185 return true; 186 } 187 188 // unsupported $mode 189 return false; 190 } 191 192 193} 194 195