datatable.directory codebase https://datatable.directory/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
datatable.directory/app/View/CollapsibleTextBoxWidget.php

81 lines
2.0 KiB

<?php
namespace App\View;
/**
* Collapsible text box widget
*/
class CollapsibleTextBoxWidget
{
private $maxHeight;
private $collapsing;
private $text;
private $srPrefix;
private $srEscape;
/**
* Add a prefix inside the text box shown only to screen readers, Lynx etc.
*
* @param string $content
* @param bool $escape - use html escape, default true
* @return $this
*/
public function srPrefix($content, $escape=true)
{
$this->srEscape = $escape;
$this->srPrefix = $content;
return $this;
}
/**
* @param string $text - text to show
* @param int $thresholdLength - mb_character length that triggers the collapsible behavior; choose experimentally
* @param string $maxHeight - max height CSS value (e.g. 8em)
*/
public function __construct($text, $thresholdLength, $maxHeight)
{
$this->text = $text;
$this->collapsing = mb_strlen($text) > $thresholdLength;
$this->maxHeight = $maxHeight;
}
private function processText($t)
{
$out = e($t);
$out = str_replace("\r\n", "\n", $out);
$out = '<p class="last-p-mb-0"> '.str_replace("\n\n",
" </p>". '<p class="last-p-mb-0"> ', $out).' </p>';
$out = nl2br($out);
$out = preg_replace('|(https?://[^\s]+)|i', '<a href="\1">\1</a>', $out);
return $out;
}
public function render()
{
$content = $this->processText($this->text);
$prefix = '';
if ($this->srPrefix) {
$prefix = '<span class="sr-only">'.
($this->srEscape ? e($this->srPrefix) : $this->srPrefix).
'&nbsp;</span>';
}
if ($this->collapsing) {
return '<div class="block-collapse" style="max-height:'.$this->maxHeight.'">' . $prefix . $content . '</div>';
} else {
return $prefix.'<div>' . $prefix . $content . '</div>';
}
}
public function __toString()
{
return (string) $this->render();
}
}