Clean Up ToggleCompositeField

This commit is contained in:
Christopher Pitt 2015-04-27 15:08:56 +12:00
parent 72ee96cd65
commit c59eb1bb70

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* Allows visibility of a group of fields to be toggled. * Allows visibility of a group of fields to be toggled.
* *
@ -6,17 +7,21 @@
* @subpackage fields-structural * @subpackage fields-structural
*/ */
class ToggleCompositeField extends CompositeField { class ToggleCompositeField extends CompositeField {
/** /**
* @var bool * @var bool
*/ */
protected $startClosed = true; protected $startClosed = true;
/** /**
* @var $int * @var int
*/ */
protected $headingLevel = 3; protected $headingLevel = 3;
/**
* @param string $name
* @param string $title
* @param array|FieldList $children
*/
public function __construct($name, $title, $children) { public function __construct($name, $title, $children) {
$this->name = $name; $this->name = $name;
$this->title = $title; $this->title = $title;
@ -24,30 +29,46 @@ class ToggleCompositeField extends CompositeField {
parent::__construct($children); parent::__construct($children);
} }
/**
* @param array $properties
*
* @return HTMLText
*/
public function FieldHolder($properties = array()) { public function FieldHolder($properties = array()) {
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js'); Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-ui/jquery-ui.js'); Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-ui/jquery-ui.js');
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js'); Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
Requirements::javascript(FRAMEWORK_DIR . '/javascript/ToggleCompositeField.js'); Requirements::javascript(FRAMEWORK_DIR . '/javascript/ToggleCompositeField.js');
Requirements::css(FRAMEWORK_DIR . '/thirdparty/jquery-ui-themes/smoothness/jquery.ui.css'); Requirements::css(FRAMEWORK_DIR . '/thirdparty/jquery-ui-themes/smoothness/jquery.ui.css');
$obj = $properties ? $this->customise($properties) : $this; $context = $this;
return $obj->renderWith($this->getTemplates());
if(count($properties)) {
$context = $this->customise($properties);
}
return $context->renderWith($this->getTemplates());
} }
/**
* {@inheritdoc}
*/
public function getAttributes() { public function getAttributes() {
$attributes = array(
'id' => $this->id(),
'class' => $this->extraClass(),
);
if($this->getStartClosed()) { if($this->getStartClosed()) {
$class = 'ss-toggle ss-toggle-start-closed'; $attributes['class'] .= ' ss-toggle ss-toggle-start-closed';
} else { } else {
$class = 'ss-toggle'; $attributes['class'] .= ' ss-toggle';
} }
return array_merge( return array_merge(
$this->attributes, $this->attributes,
array( $attributes
'id' => $this->id(),
'class' => $class . ' ' . $this->extraClass()
)
); );
} }
@ -59,13 +80,15 @@ class ToggleCompositeField extends CompositeField {
} }
/** /**
* Controls whether the field is open or closed by default. By default the * Controls whether the field is open or closed by default. By default the field is closed.
* field is closed.
* *
* @param bool $bool * @param bool $startClosed
*
* @return $this
*/ */
public function setStartClosed($bool) { public function setStartClosed($startClosed) {
$this->startClosed = (bool) $bool; $this->startClosed = (bool) $startClosed;
return $this; return $this;
} }
@ -77,12 +100,13 @@ class ToggleCompositeField extends CompositeField {
} }
/** /**
* @param int $level * @param int $headingLevel
*
* @return $this
*/ */
public function setHeadingLevel($level) { public function setHeadingLevel($headingLevel) {
$this->headingLevel = $level; $this->headingLevel = $headingLevel;
return $this; return $this;
} }
} }