xref: /plugin/autotooltip/helper.php (revision 07c401fc837ee0b6ef0a426e8e3936460a819126)
16bfd3f23SEli Fenton<?php
26bfd3f23SEli Fentonif(!defined('DOKU_INC')) die();
36bfd3f23SEli Fenton
46bfd3f23SEli Fenton/**
56bfd3f23SEli Fenton * Auto-Tooltip DokuWiki plugin
66bfd3f23SEli Fenton *
76bfd3f23SEli Fenton * @license    MIT
86bfd3f23SEli Fenton * @author     Eli Fenton
96bfd3f23SEli Fenton */
106bfd3f23SEli Fentonclass helper_plugin_autotooltip extends DokuWiki_Admin_Plugin {
116bfd3f23SEli Fenton	private $localRenderer;
126bfd3f23SEli Fenton
136bfd3f23SEli Fenton	public function __construct() {
146bfd3f23SEli Fenton		$this->localRenderer = new Doku_Renderer_xhtml;
156bfd3f23SEli Fenton	}
166bfd3f23SEli Fenton
176bfd3f23SEli Fenton
186bfd3f23SEli Fenton	/**
196bfd3f23SEli Fenton	 * Return a simple tooltip.
206bfd3f23SEli Fenton	 *
21*07c401fcSEli Fenton	 * @param string $content - The on-page content. May contain newlines.
22*07c401fcSEli Fenton	 * @param string $tooltip - Tooltip content. May contain newlines.
236bfd3f23SEli Fenton	 * @param string $classes - CSS classes to add to this tooltip.
246bfd3f23SEli Fenton	 * @return string
256bfd3f23SEli Fenton	 */
266bfd3f23SEli Fenton	function forText($content, $tooltip, $classes = '') {
276bfd3f23SEli Fenton		if (!$classes) {
286bfd3f23SEli Fenton			$classes = 'plugin-autotooltip__default';
296bfd3f23SEli Fenton		}
306bfd3f23SEli Fenton
316bfd3f23SEli Fenton		$textclass = strstr($content, '<a ') !== FALSE ? '' : 'plugin-autotooltip__simple';
326bfd3f23SEli Fenton
33*07c401fcSEli Fenton		return '<span class="plugin-autotooltip_linked ' . $textclass . '" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()">' .
346bfd3f23SEli Fenton			$content .
35*07c401fcSEli Fenton			'<span class="plugin-autotooltip-hidden-classes">' . $classes . '</span>' .
36*07c401fcSEli Fenton			'<span class="plugin-autotooltip-hidden-tip">' . $this->_formatTT($tooltip) . '</span>' .
37*07c401fcSEli Fenton		'</span>';
386bfd3f23SEli Fenton	}
396bfd3f23SEli Fenton
406bfd3f23SEli Fenton
416bfd3f23SEli Fenton	/**
426bfd3f23SEli Fenton	 * Render a tooltip, with the title and abstract of a page.
436bfd3f23SEli Fenton	 *
446bfd3f23SEli Fenton	 * @param string $id - A page id.
45*07c401fcSEli Fenton	 * @param string $content - The on-page content. May contain newlines.
466bfd3f23SEli Fenton	 * @param string $classes - CSS classes to add to this tooltip.
476bfd3f23SEli Fenton	 * @return string
486bfd3f23SEli Fenton	 */
496bfd3f23SEli Fenton	function forWikilink($id, $content, $classes = '') {
506bfd3f23SEli Fenton		if (!$classes) {
516bfd3f23SEli Fenton			$classes = 'plugin-autotooltip__default';
526bfd3f23SEli Fenton		}
536bfd3f23SEli Fenton
546bfd3f23SEli Fenton		$title = p_get_metadata($id, 'title');
556bfd3f23SEli Fenton		$abstract = p_get_metadata($id, 'description abstract');
566bfd3f23SEli Fenton		try {
576bfd3f23SEli Fenton			// By default, the abstract starts with the title. Remove it so it's not displayed twice, but still fetch
586bfd3f23SEli Fenton			// both pieces of metadata, in case another plugin rewrote the abstract.
596bfd3f23SEli Fenton			$abstract = preg_replace('/^' . preg_quote($title) . '(\r?\n)+/', '', $abstract);
606bfd3f23SEli Fenton		} catch(\Exception $e) {
616bfd3f23SEli Fenton			// Ignore.
626bfd3f23SEli Fenton		}
636bfd3f23SEli Fenton
64812ebc9aSEli Fenton		$link = $this->localRenderer->internallink($id, $content ?: $title, null, true);
656bfd3f23SEli Fenton
666bfd3f23SEli Fenton		if (page_exists($id)) {
676bfd3f23SEli Fenton			// Remove the title attribute, since we have a better tooltip.
686bfd3f23SEli Fenton			$link = preg_replace('/title="[^"]*"/', '', $link);
696bfd3f23SEli Fenton
70*07c401fcSEli Fenton			return '<span class="plugin-autotooltip_linked" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()">' .
716bfd3f23SEli Fenton				$link .
72*07c401fcSEli Fenton				'<span class="plugin-autotooltip-hidden-classes">plugin-autotooltip_big ' . $classes . '</span>' .
73*07c401fcSEli Fenton				'<span class="plugin-autotooltip-hidden-tip">' .
74*07c401fcSEli Fenton				'  <span class="plugin-autotooltip-title">' . $title . '</span>' .
75*07c401fcSEli Fenton				($abstract ? '  <br><br><span class="plugin-autotooltip_abstract">' . $this->_formatTT($abstract) . '</span>' : '') .
76*07c401fcSEli Fenton				'</span>' .
77*07c401fcSEli Fenton				'</span>';
786bfd3f23SEli Fenton		}
796bfd3f23SEli Fenton		else {
806bfd3f23SEli Fenton			return $link;
816bfd3f23SEli Fenton		}
826bfd3f23SEli Fenton	}
836bfd3f23SEli Fenton
846bfd3f23SEli Fenton
856bfd3f23SEli Fenton	/**
866bfd3f23SEli Fenton	 * Format tooltip text.
876bfd3f23SEli Fenton	 *
886bfd3f23SEli Fenton	 * @param string $tt - Tooltip text.
896bfd3f23SEli Fenton	 * @return string
906bfd3f23SEli Fenton	 */
916bfd3f23SEli Fenton	private function _formatTT($tt) {
926bfd3f23SEli Fenton		return preg_replace('/\r?\n/', '<br>', $tt);
936bfd3f23SEli Fenton	}
946bfd3f23SEli Fenton}
95