Skip to content

Commit

Permalink
add part prototypes
Browse files Browse the repository at this point in the history
  • Loading branch information
MartkCz committed Nov 23, 2021
1 parent 89e285e commit e46606e
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/Bridge/Nette/DI/GumletImageStorageExtension.php
Expand Up @@ -39,7 +39,7 @@ public function beforeCompile(): void
if (!$config->bucket && !$config->customDomain) {
throw new InvalidConfigurationException('bucket or customDomain must be set.');
}

$definition = $builder->getDefinitionByType(LinkGeneratorInterface::class);
assert($definition instanceof ServiceDefinition);

Expand Down
43 changes: 19 additions & 24 deletions src/Bridge/Nette/Form/ImageUploadControl.php
Expand Up @@ -21,6 +21,8 @@ final class ImageUploadControl extends UploadControl

private UploadControlEntity $entity;

private Html $containerPart;

private ?FileUpload $fileUpload = null;

private ?Scope $scope = null;
Expand All @@ -32,6 +34,7 @@ final class ImageUploadControl extends UploadControl
public function __construct(?string $label = null)
{
$this->entity = new UploadControlEntity();
$this->containerPart = Html::el('div', ['class' => 'image-upload-container']);

parent::__construct($label);
}
Expand Down Expand Up @@ -108,9 +111,6 @@ public function getValue(): UploadControlEntity
return $this->entity;
}

/**
* @phpstan-return Html<Html|string>|null
*/
public function getControlPart(): ?Html
{
$control = parent::getControl();
Expand All @@ -124,28 +124,27 @@ public function getControlPart(): ?Html
return $control;
}

/**
* @phpstan-return Html<Html|string>|null
*/
public function getRemovePart(): ?Html
public function getContainerPart(): Html
{
return $this->containerPart;
}

public function getRemovePart(): ?ImageRemoveInterface
{
if (!$this->remove) {
return null;
}

return $this->remove->getHtml($this);
return $this->remove;
}

/**
* @phpstan-return Html<Html|string>|null
*/
public function getPreviewPart(): ?Html
public function getPreviewPart(): ?ImagePreviewInterface
{
if (!$this->preview) {
return null;
}

return $this->preview->getHtml($this);
return $this->preview;
}

public function hasPreviewImage(): bool
Expand All @@ -157,24 +156,20 @@ public function hasPreviewImage(): bool
return $this->preview->hasImage($this);
}

/**
* @phpstan-return Html<Html|string>
*/
public function getControl(): Html
{
$container = Html::el(
'div',
[
'class' => ['image-upload-container'],
]
);
$container = clone $this->containerPart;

if ($preview = $this->getPreviewPart()) {
$container->insert(null, $preview);
if ($previewHtml = $preview->getHtml($this)) {
$container->insert(null, $previewHtml);
}
}

if ($remove = $this->getRemovePart()) {
$container->insert(null, $remove);
if ($removeHtml = $remove->getHtml($this)) {
$container->insert(null, $removeHtml);
}
}

return $container->insert(null, parent::getControl());
Expand Down
44 changes: 29 additions & 15 deletions src/Bridge/Nette/Form/Preview/ImagePreview.php
Expand Up @@ -12,6 +12,10 @@
class ImagePreview implements ImagePreviewInterface
{

private Html $imagePrototype;

private Html $wrapperPrototype;

private LinkGeneratorInterface $linkGenerator;

private ?ImageFilter $filter = null;
Expand All @@ -23,6 +27,18 @@ class ImagePreview implements ImagePreviewInterface
public function __construct(LinkGeneratorInterface $linkGenerator)
{
$this->linkGenerator = $linkGenerator;
$this->imagePrototype = Html::el('img', ['class' => 'image-upload-preview']);
$this->wrapperPrototype = Html::el('div', ['class' => 'image-upload-preview-wrapper']);
}

public function getImagePart(): Html
{
return $this->imagePrototype;
}

public function getWrapperPart(): Html
{
return $this->wrapperPrototype;
}

/**
Expand Down Expand Up @@ -88,24 +104,22 @@ public function getHtml(ImageUploadControl $input): ?Html
$placeholder = $this->linkGenerator->link($this->placeholder);
}

$wrapper = Html::el('div', [
'class' => ['image-upload-preview-wrapper'],
]);
$wrapper = clone $this->wrapperPrototype;

if ($default = $value->getDefault()) {
$wrapper->create('img', [
'src' => $this->linkGenerator->link(
$default->withFilterObject($this->filter)
),
'class' => ['image-upload-preview'],
'data-placeholder' => $placeholder,
]);
$img = clone $this->imagePrototype;

$img->setAttribute('src', $this->linkGenerator->link($default->withFilterObject($this->filter)));
$img->setAttribute('data-placeholder', $placeholder);

$wrapper->insert(0, $img);
} elseif ($placeholder) {
$wrapper->create('img', [
'src' => $placeholder,
'class' => ['image-upload-preview'],
'data-placeholder' => $placeholder,
]);
$img = clone $this->imagePrototype;

$img->setAttribute('src', $placeholder);
$img->setAttribute('data-placeholder', $placeholder);

$wrapper->insert(0, $img);
} else {
$wrapper->appendAttribute('class', 'empty');
}
Expand Down
7 changes: 4 additions & 3 deletions src/Bridge/Nette/Form/Preview/ImagePreviewInterface.php
Expand Up @@ -10,6 +10,10 @@
interface ImagePreviewInterface
{

public function getWrapperPart(): Html;

public function getImagePart(): Html;

/**
* @return static
*/
Expand All @@ -26,9 +30,6 @@ public function setFilterObject(?ImageFilter $filter);
*/
public function setFilter(string $name, array $options = []);

/**
* @phpstan-return Html<Html|string>|null
*/
public function getHtml(ImageUploadControl $input): ?Html;

public function hasImage(ImageUploadControl $input): bool;
Expand Down
47 changes: 36 additions & 11 deletions src/Bridge/Nette/Form/Remove/ImageRemove.php
Expand Up @@ -10,11 +10,36 @@
class ImageRemove implements ImageRemoveInterface
{

private Html $wrapperPart;

private Html $labelPart;

private Html $controlPart;

private string $caption;

public function __construct(string $caption)
{
$this->caption = $caption;

$this->wrapperPart = Html::el('div', ['class' => 'image-upload-remove-wrapper']);
$this->labelPart = Html::el('label');
$this->controlPart = Html::el('input', ['type' => 'checkbox']);
}

public function getWrapperPart(): Html
{
return $this->wrapperPart;
}

public function getLabelPart(): Html
{
return $this->labelPart;
}

public function getControlPart(): Html
{
return $this->controlPart;
}

public function getHttpData(ImageUploadControl $input): bool
Expand All @@ -35,19 +60,19 @@ public function getHtml(ImageUploadControl $input): ?Html
return null;
}

$wrapper = Html::el('div', [
'class' => ['image-upload-remove-wrapper'],
]);

$label = $wrapper->create('label');
$label->create('input', [
'type' => 'checkbox',
'id' => $input->getHtmlId() . '_remove',
'name' => $input->getName() . '_remove',
'checked' => $this->getHttpData($input),
]);
$wrapper = clone $this->wrapperPart;
$label = clone $this->labelPart;
$control = clone $this->controlPart;

$control->setAttribute('id', $input->getHtmlId() . '_remove');
$control->setAttribute('name', $input->getName() . '_remove');
$control->setAttribute('checked', $this->getHttpData($input));

$label->insert(null, $control);
$label->create('')->setText(' ' . $this->caption);

$wrapper->insert(null, $label);

return $wrapper;
}

Expand Down
9 changes: 6 additions & 3 deletions src/Bridge/Nette/Form/Remove/ImageRemoveInterface.php
Expand Up @@ -8,11 +8,14 @@
interface ImageRemoveInterface
{

public function getWrapperPart(): Html;

public function getLabelPart(): Html;

public function getControlPart(): Html;

public function getHttpData(ImageUploadControl $input): bool;

/**
* @phpstan-return Html<Html|string>|null
*/
public function getHtml(ImageUploadControl $input): ?Html;

}

0 comments on commit e46606e

Please sign in to comment.