Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Dec 26, 2023
1 parent c6176b2 commit 1b8eb0c
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 27 deletions.
2 changes: 1 addition & 1 deletion config/root.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
],
'xaxis' => ['type' => 'datetime'],
'yaxis' => ['min' => 0],
'colors' => ['#fff'],
'colors' => ['var(--root-base-color-heading)'],
'tooltip' => [
'enabled' => true,
'marker' => ['show' => false],
Expand Down
3 changes: 0 additions & 3 deletions resources/views/widgets/widget.blade.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Fields/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(string $label, Closure|string|null $modelAttribute =
{
parent::__construct($label, $modelAttribute);

$this->setAttribute('class', 'form-control combobox__control');
$this->class(['form-control', 'combobox__control']);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function __construct(string $label, Closure|string|null $modelAttribute =
$this->label($label);
$this->name($this->modelAttribute);
$this->id($this->modelAttribute);
$this->setAttribute('class', 'form-control');
$this->class('form-control');
$this->value($this->computed ? $modelAttribute : null);
}

Expand Down
111 changes: 111 additions & 0 deletions src/Support/ClassList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Cone\Root\Support;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Stringable;

class ClassList implements Arrayable, Stringable
{
/**
* The class list items.
*/
protected array $classes = [];

/**
* Create a new class list instance.
*/
public function __construct(array $classes = [])
{
$this->classes = $classes;
}

/**
* Add new classes to the class list.
*/
public function add(string|array $value): static
{
$this->classes = array_values(array_unique(
array_merge($this->classes, (array) $value)
));

return $this;
}

/**
* Remove classes from the class list.
*/
public function remove(string|array $value): static
{
$this->classes = array_values(
array_diff($this->classes, (array) $value)
);

return $this;
}

/**
* Replace the given values in the class list.
*/
public function replace(string $old, string $new): static
{
$index = array_search($old, $this->classes);

if ($index !== false) {
$this->classes[$index] = $new;
}

return $this;
}

/**
* Toggle a class in the class list.
*/
public function toggle(string $value, ?bool $force = null): static
{
if (is_null($force)) {
$this->contains($value) ? $this->remove($value) : $this->add($value);
} elseif ($force) {
$this->add($value);
} else {
$this->remove($value);
}

return $this;
}

/**
* Determine whether the class is peresent in the class list.

Check warning on line 79 in src/Support/ClassList.php

View workflow job for this annotation

GitHub Actions / 文A Typos check

"peresent" should be "present" or "presents" or "presence" or "percent".
*/
public function contains(string $value): bool
{
return in_array($value, $this->classes);
}

/**
* Clear the class list.
*/
public function clear(): static
{
$this->classes = [];

return $this;
}

/**
* Convert the class list to an array.
*/
public function toArray(): array
{
return array_values(array_unique($this->classes));
}

/**
* Convert the class list to a string.
*/
public function __toString(): string
{
return Arr::toCssClasses($this->toArray());
}
}
40 changes: 36 additions & 4 deletions src/Traits/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Cone\Root\Traits;

use Closure;
use Cone\Root\Support\ClassList;
use Illuminate\Support\Arr;

use Illuminate\View\ComponentAttributeBag;

trait HasAttributes
Expand All @@ -21,6 +23,28 @@ public function id(string $value): static
return $this->setAttribute('id', strtolower($value));
}

/**
* Add a "class" HTML attribute.
*/
public function class(string|array $value): static
{
$this->classList()->add($value);

return $this;
}

/**
* Get the class list.
*/
public function classList(): ClassList
{
if (! isset($this->attributes['class'])) {
$this->attributes['class'] = new ClassList();
}

return $this->attributes['class'];
}

/**
* Get the attributes.
*/
Expand All @@ -34,7 +58,9 @@ public function getAttributes(): array
*/
public function setAttributes(array $attributes): static
{
$this->attributes = array_replace($this->attributes, $attributes);
foreach ($attributes as $key => $value) {
$this->setAttribute($key, $value);
}

return $this;
}
Expand All @@ -52,15 +78,21 @@ public function hasAttribute(string $key): bool
*/
public function getAttribute(string $key, mixed $default = null): mixed
{
return $this->attributes[$key] ?? $default;
return match ($key) {
'class' => $this->classList()->__toString(),
default => $this->attributes[$key] ?? $default,
};
}

/**
* Set the given attribute.
*/
public function setAttribute(string $key, mixed $value): static
{
$this->attributes[$key] = $value;
match ($key) {
'class' => $this->classList()->clear()->add((array) $value),
default => $this->attributes[$key] = $value,
};

return $this;
}
Expand Down Expand Up @@ -121,7 +153,7 @@ public function resolveAttribute(string $key): mixed
$value = $value instanceof Closure ? call_user_func_array($value, [$this]) : $value;

return match ($key) {
'class' => Arr::toCssClasses((array) $value),
'class' => (string) $value,
'style' => Arr::toCssStyles((array) $value),
default => $value,
};
Expand Down
24 changes: 23 additions & 1 deletion src/Widgets/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use Closure;
use Cone\Root\Exceptions\QueryResolutionException;
use Cone\Root\Http\Controllers\WidgetController;
use Cone\Root\Widgets\Results\Result;
use DateInterval;
use DatePeriod;
use DateTimeImmutable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;

abstract class Metric extends Widget
{
Expand Down Expand Up @@ -72,7 +74,19 @@ public function resolveQuery(Request $request): Builder
*/
public function getCurrentRange(Request $request): string
{
return $request->input('range', 'MONTH');
$default = $this->getDetaultRange();

Check warning on line 77 in src/Widgets/Metric.php

View workflow job for this annotation

GitHub Actions / 文A Typos check

"Detault" should be "Default".

$range = $request->input('range', $default);

return in_array($range, array_keys($this->ranges())) ? $range : $default;
}

/**
* Get the default range.
*/
public function getDetaultRange(): string

Check warning on line 87 in src/Widgets/Metric.php

View workflow job for this annotation

GitHub Actions / 文A Typos check

"Detault" should be "Default".
{
return 'MONTH';
}

/**
Expand Down Expand Up @@ -207,6 +221,14 @@ public function calculate(Request $request): Result
return $this->count($request, $this->resolveQuery($request));
}

/**
* The routes should be registered.
*/
public function routes(Router $router): void
{
$router->get('/', WidgetController::class);
}

/**
* {@inheritdoc}
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Widgets/Trend.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ abstract class Trend extends Metric
/**
* The interval.
*/
protected string $interval = 'day';
protected string $interval = 'month';

/**
* Create a new trend chart instance.
*/
public function __construct()
{
$this->config = Config::get('root.widgets.trend', []);
parent::__construct();

$this->setAttribute('class', 'app-widget');
$this->config = Config::get('root.widgets.trend', []);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Widgets/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ abstract class Value extends Metric
*/
public function __construct()
{
$this->setAttribute('class', 'app-widget app-widget--summary');
parent::__construct();

$this->class('app-widget--summary');
}

/**
Expand Down
14 changes: 2 additions & 12 deletions src/Widgets/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Cone\Root\Widgets;

use Cone\Root\Http\Controllers\WidgetController;
use Cone\Root\Traits\Authorizable;
use Cone\Root\Traits\HasAttributes;
use Cone\Root\Traits\Makeable;
Expand All @@ -13,7 +12,6 @@
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\View as ViewFactory;
use Illuminate\Support\Str;

Expand All @@ -28,14 +26,14 @@ abstract class Widget implements Arrayable, Responsable
/**
* The Blade template.
*/
protected string $template = 'root::widgets.widget';
protected string $template;

/**
* Create a new widget instance.
*/
public function __construct()
{
$this->setAttribute('class', 'app-widget');
$this->class('app-widget');
}

/**
Expand Down Expand Up @@ -80,14 +78,6 @@ public function data(Request $request): array
]);
}

/**
* The routes should be registered.
*/
public function routes(Router $router): void
{
$router->get('/', WidgetController::class);
}

/**
* Convert the widget to an array.
*/
Expand Down

0 comments on commit 1b8eb0c

Please sign in to comment.