Js Declaration Component added
This commit is contained in:
parent
411999c01f
commit
709df5de89
65
app/View/Components/Script/Declaration.php
Normal file
65
app/View/Components/Script/Declaration.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Components\Script;
|
||||
|
||||
use App\Abstracts\View\Component;
|
||||
use Illuminate\Support\Js;
|
||||
|
||||
class Declaration extends Component
|
||||
{
|
||||
/** @var string */
|
||||
public $kind;
|
||||
|
||||
/** @var array */
|
||||
public $value;
|
||||
|
||||
public $scripts = null;
|
||||
|
||||
/**
|
||||
* Create a new component instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(
|
||||
string $kind = 'var',array $value = []
|
||||
) {
|
||||
$this->kind = $kind;
|
||||
$this->value = $value;
|
||||
|
||||
$this->scripts = $this->getScripts();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the view / contents that represent the component.
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\View|string
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return view('components.script.declaration');
|
||||
}
|
||||
|
||||
protected function getScripts()
|
||||
{
|
||||
$scripts = '';
|
||||
|
||||
foreach ($this->value as $key => $value) {
|
||||
$scripts .= $this->getScript($key, $value) . "\n";
|
||||
}
|
||||
|
||||
return $scripts;
|
||||
}
|
||||
|
||||
protected function getScript($key, $value = null)
|
||||
{
|
||||
$script = $this->kind . ' ' . $key;
|
||||
|
||||
if (! is_null($value)) {
|
||||
$script .= ' = ' . Js::from($value)->toHtml() . ';';
|
||||
} else {
|
||||
$script .= ' = ' . 'null;';
|
||||
}
|
||||
|
||||
return $script;
|
||||
}
|
||||
}
|
5
resources/views/components/script/declaration.blade.php
Normal file
5
resources/views/components/script/declaration.blade.php
Normal file
@ -0,0 +1,5 @@
|
||||
@push('scripts_start')
|
||||
<script type="text/javascript">
|
||||
{!! $scripts !!}
|
||||
</script>
|
||||
@endpush
|
Loading…
x
Reference in New Issue
Block a user