mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
35fb0cd0c5
FEATURE Refactored MoreLessField->ToggleField FEATURE Refactored TogglePanel->ToggleCompositeField FEATURE Degrading gracefully (javascript), using behaviour+classes+prototype, partially i18ned, improved markup git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@43660 467b73ca-7a2a-4603-9d3b-597d59a354a9
99 lines
2.7 KiB
PHP
Executable File
99 lines
2.7 KiB
PHP
Executable File
<?php
|
|
|
|
/**
|
|
* ReadonlyField with added toggle-capabilities - will preview the first sentence of the contained text-value,
|
|
* and show the full content by a javascript-switch.
|
|
*
|
|
* Caution: Strips HTML-encoding for the preview.
|
|
*/
|
|
|
|
class ToggleField extends ReadonlyField {
|
|
|
|
/**
|
|
* @var $labelMore string Text shown as a link to see the full content of the field
|
|
*/
|
|
public $labelMore;
|
|
|
|
/**
|
|
* @var $labelLess string Text shown as a link to see the partial view of the field content
|
|
*/
|
|
public $labelLess;
|
|
|
|
/**
|
|
* @var $truncateMethod string (FirstSentence|FirstParagraph) @see {Text}
|
|
*/
|
|
public $truncateMethod = 'FirstSentence';
|
|
|
|
/**
|
|
* @var $truncateChars int Number of chars to preview (optional).
|
|
* Truncating will be applied with $truncateMethod by default.
|
|
*/
|
|
public $truncateChars;
|
|
|
|
/**
|
|
* @param name The field name
|
|
* @param title The field title
|
|
* @param value The current value
|
|
*/
|
|
function __construct($name, $title = "", $value = "") {
|
|
$this->labelMore = _t('ToggleField.MORE', 'more');
|
|
$this->labelLess = _t('ToggleField.LESS', 'less');
|
|
|
|
$this->startClosed(true);
|
|
|
|
parent::__construct($name, $title, $value);
|
|
}
|
|
|
|
function Field() {
|
|
$content = '';
|
|
|
|
Requirements::javascript("jsparty/prototype.js");
|
|
Requirements::javascript("jsparty/behaviour.js");
|
|
Requirements::javascript("jsparty/prototype_improvements.js");
|
|
Requirements::javascript("sapphire/javascript/ToggleField.js");
|
|
|
|
if($this->startClosed) $this->addExtraClass('startClosed');
|
|
|
|
$valforInput = $this->value ? Convert::raw2att($this->value) : "";
|
|
$rawInput = Convert::html2raw($valforInput);
|
|
|
|
if($this->charNum) $reducedVal = substr($rawInput,0,$this->charNum);
|
|
else $reducedVal = DBField::create('Text',$rawInput)->{$this->truncateMethod}();
|
|
|
|
// only create togglefield if the truncated content is shorter
|
|
if(strlen($reducedVal) < strlen($rawInput)) {
|
|
$content = <<<HTML
|
|
<div class="readonly typography contentLess" style="display: none">
|
|
$reducedVal
|
|
<a href="#" class="triggerMore">$this->labelMore</a>
|
|
</div>
|
|
<div class="readonly typography contentMore">
|
|
$this->value
|
|
<a href="#" class="triggerLess">$this->labelLess</a>
|
|
</div>
|
|
<br />
|
|
<input type="hidden" name="$this->name" value="$valforInput" />
|
|
HTML;
|
|
} else {
|
|
$this->dontEscape = true;
|
|
$content = parent::Field();
|
|
}
|
|
|
|
return $content;
|
|
}
|
|
|
|
/**
|
|
* Determines if the field should render open or closed by default.
|
|
*
|
|
* @param boolean
|
|
*/
|
|
public function startClosed($bool) {
|
|
($bool) ? $this->addExtraClass('startClosed') : $this->removeExtraClass('startClosed');
|
|
}
|
|
|
|
function Type() {
|
|
return "toggleField";
|
|
}
|
|
}
|
|
|
|
?>
|