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.
109 lines
2.4 KiB
109 lines
2.4 KiB
7 years ago
|
<?php
|
||
|
|
||
|
|
||
|
namespace App\View;
|
||
|
|
||
|
/**
|
||
|
* Widget
|
||
|
*
|
||
|
* @property-read string $name
|
||
|
* @property-read string $id
|
||
|
* @property-read string $label
|
||
|
* @property-read string|null $help
|
||
|
* @property-read string|null $value
|
||
|
* @property-read string $attributes
|
||
|
*/
|
||
|
class Widget
|
||
|
{
|
||
|
private $attributes = [],
|
||
|
$id, $name, $label, $value,
|
||
|
$viewName,
|
||
|
$help,
|
||
|
$style = [];
|
||
|
|
||
|
public function __construct($viewName, $name, $label)
|
||
|
{
|
||
|
$this->id = 'field-'.$name;
|
||
|
$this->name = $name;
|
||
|
$this->label = $label;
|
||
|
$this->viewName = $viewName;
|
||
|
}
|
||
|
|
||
|
// setting attributes via magic method
|
||
|
public function __call($method, $args)
|
||
|
{
|
||
|
static $cssProps = [
|
||
|
'height', 'minHeight', 'maxHeight'
|
||
|
];
|
||
|
|
||
|
if (empty($args)) $args = [''];
|
||
|
|
||
|
$arg = $args[0];
|
||
|
|
||
|
// css
|
||
|
$lccamel = $method;
|
||
|
if (in_array($lccamel, $cssProps)) {
|
||
|
return $this->css(kebab_case($lccamel), $arg);
|
||
|
}
|
||
|
|
||
|
if (property_exists($this, $method)) {
|
||
|
$this->$method = $arg;
|
||
|
}
|
||
|
else {
|
||
|
$this->attributes[$method] = $arg;
|
||
|
}
|
||
|
|
||
|
return $this;
|
||
|
}
|
||
|
|
||
|
public function css($prop, $val)
|
||
|
{
|
||
|
$this->style[$prop] = $val;
|
||
|
return $this;
|
||
|
}
|
||
|
|
||
|
public function __get($name)
|
||
|
{
|
||
|
if ($name == 'attributes') {
|
||
|
return $this->compileAttribs();
|
||
|
}
|
||
|
|
||
|
if ($name == 'value') {
|
||
|
return old($this->name, $this->value);
|
||
|
}
|
||
|
|
||
|
if (property_exists($this, $name)) {
|
||
|
return $this->$name;
|
||
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
public function compileAttribs()
|
||
|
{
|
||
|
// compile attribs string
|
||
|
$attribs_s = array_reduce(array_keys($this->attributes), function ($carry, $key) {
|
||
|
return $carry . ' ' . $key . '="' . e($this->attributes[$key]) . '"';
|
||
|
}, '');
|
||
|
|
||
|
// add a compiled list of styles
|
||
|
if (!empty($this->style)) {
|
||
|
$attribs_s .= 'style="'.trim(e(array_reduce(array_keys($this->style), function ($carry, $key) {
|
||
|
return $carry . $key . ': ' . $this->style[$key] . '; ';
|
||
|
}, ''))).'"';
|
||
|
}
|
||
|
|
||
|
return trim($attribs_s);
|
||
|
}
|
||
|
|
||
|
public function render()
|
||
|
{
|
||
|
return view('form.'.$this->viewName)->with(['w' => $this]);
|
||
|
}
|
||
|
|
||
|
public function __toString()
|
||
|
{
|
||
|
return (string) $this->render();
|
||
|
}
|
||
|
}
|