Skip to content

Commit

Permalink
feat(admin/twig): on copy save json into session and render get copie…
Browse files Browse the repository at this point in the history
…d item
  • Loading branch information
Davidmattei committed Oct 7, 2024
1 parent f09c928 commit 0f38d4f
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 24 deletions.
11 changes: 11 additions & 0 deletions EMS/common-bundle/src/Json/JsonMenuNested.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,17 @@ public function changeId(): JsonMenuNested
return $this;
}

public function changeIds(): JsonMenuNested
{
$this->changeId();

foreach ($this->getIterator() as $child) {
$child->changeId();
}

return $this;
}

public function getItemById(string $id): ?JsonMenuNested
{
foreach ($this->getIterator() as $child) {
Expand Down
22 changes: 13 additions & 9 deletions EMS/core-bundle/assets/js/component/jsonMenuNestedComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ export default class JsonMenuNestedComponent {
itemAdd(itemId, add, position = null) {
return this._post(`/item/${itemId}/add`, { 'position': position, 'add': add });
}
itemDelete(nodeId) {
this._post(`/item/${nodeId}/delete`).then((json) => {
let eventCanceled = this._dispatchEvent('jmn-delete', { data: json, nodeId: nodeId });
itemDelete(itemId) {
this._post(`/item/${itemId}/delete`).then((json) => {
let eventCanceled = this._dispatchEvent('jmn-delete', { data: json, itemId: itemId });
if (!eventCanceled) this.load();
});
}
Expand Down Expand Up @@ -98,13 +98,17 @@ export default class JsonMenuNestedComponent {
this.itemDelete(itemId);
}
_onClickButtonCopy(itemId) {
document.dispatchEvent(new CustomEvent('jmn.copy', {
cancelable: true,
detail: { itemId: itemId }
}))
this._post(`/item/${itemId}/copy`).then((json) => {
const {success, copyId} = json
if (!success) return
document.dispatchEvent(new CustomEvent('jmn.copy', {
cancelable: true,
detail: { copyId: copyId, originalId: itemId }
}))
})
}
onCopy(itemId) {
this.load({ copyItemId: itemId });
onCopy({ originalId } = event) {
this.load({ activeItemId: originalId });
}
_onClickModalCustom(element, itemId) {
const modalCustomName = element.dataset.jmnModalCustom;
Expand Down
2 changes: 1 addition & 1 deletion EMS/core-bundle/assets/js/initEms.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ import JsonMenuNestedComponent from "./component/jsonMenuNestedComponent";
});

document.addEventListener('jmn.copy', (e) => {
Object.values(jsonMenuNestedComponents).forEach((component) => component.onCopy(e.detail.itemId))
Object.values(jsonMenuNestedComponents).forEach((component) => component.onCopy(e.detail))
})

window.jsonMenuNestedComponents = jsonMenuNestedComponents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ public function itemMove(Request $request, JsonMenuNestedConfig $config, string
}
}

public function itemCopy(JsonMenuNestedConfig $config, string $itemId): JsonResponse
{
try {
$copyItem = $config->jsonMenuNested->giveItemById($itemId);
$this->jsonMenuNestedService->itemCopy($copyItem);

return $this->responseSuccess(['copyId' => $copyItem->getId()]);
} catch (JsonMenuNestedException $e) {
return $this->responseWarning($e->getMessage());
}
}

private function clearFlashes(Request $request): void
{
/** @var Session $session */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,33 @@
use EMS\CoreBundle\Service\Revision\RevisionService;
use EMS\CoreBundle\Service\UserService;
use EMS\Helpers\Standard\Json;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PropertyAccess\PropertyAccessor;

class JsonMenuNestedService
{
private const SESSION_COPY_KEY = 'jmn_copy';

public function __construct(
private readonly JsonMenuNestedTemplateFactory $jsonMenuNestedTemplateFactory,
private readonly RevisionService $revisionService,
private readonly UserService $userService,
private readonly ElasticaService $elasticaService
private readonly ElasticaService $elasticaService,
private readonly RequestStack $requestStack
) {
}

/**
* @return array{ loadParentIds: string[], tree: string, top: string, footer: string }
*/
public function render(JsonMenuNestedConfig $config, ?string $activeItemId, ?string $copyItemId, ?string $loadChildrenId, string ...$loadParentIds): array
public function render(JsonMenuNestedConfig $config, ?string $activeItemId, ?string $loadChildrenId, string ...$loadParentIds): array
{
$menu = $config->jsonMenuNested;
$renderContext = new JsonMenuNestedRenderContext(
menu: $menu,
activeItemId: $activeItemId,
copyItemId: $copyItemId,
copyItem: $this->getCopiedItem(),
loadChildrenId: $loadChildrenId
);
$renderContext->loadParents(...$loadParentIds);
Expand Down Expand Up @@ -115,6 +119,15 @@ public function itemDelete(JsonMenuNestedConfig $config, JsonMenuNested $item):
$this->saveStructure($config);
}

public function itemCopy(JsonMenuNested $item): void
{
$session = $this->requestStack->getSession();
$session->set(self::SESSION_COPY_KEY, Json::encode($item
->changeIds()
->toArrayStructure(true)
));
}

/**
* @param array<string, mixed> $context
*/
Expand Down Expand Up @@ -175,4 +188,12 @@ private function saveStructure(JsonMenuNestedConfig $config): void
$this->revisionService->updateRawData($config->revision, $rawData, $username);
$this->elasticaService->refresh($config->revision->giveContentType()->giveEnvironment()->getAlias());
}

private function getCopiedItem(): ?JsonMenuNested
{
$session = $this->requestStack->getSession();
$copiedJson = $session->get(self::SESSION_COPY_KEY);

return $copiedJson ? JsonMenuNested::fromStructure(Json::decode($copiedJson)) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
class JsonMenuNestedRenderContext
{
public ?JsonMenuNested $activeItem;
public ?JsonMenuNested $copyItem;

/** @var array<string, JsonMenuNested> */
public array $loadParents = [];
Expand All @@ -18,9 +17,9 @@ class JsonMenuNestedRenderContext

public function __construct(
private readonly JsonMenuNested $menu,
?string $activeItemId,
?string $copyItemId,
?string $loadChildrenId
?string $activeItemId = null,
public ?JsonMenuNested $copyItem = null,
?string $loadChildrenId = null
) {
$this->addActiveItem($menu);

Expand All @@ -29,7 +28,6 @@ public function __construct(
$this->loadPath($this->activeItem);
}

$this->copyItem = $copyItemId ? $menu->getItemById($copyItemId) : null;
if ($this->copyItem) {
$this->loadPath($this->copyItem);
}
Expand All @@ -40,11 +38,6 @@ public function __construct(
}
}

public function isActive(JsonMenuNested $item): bool
{
return $this->activeItem === $item || $this->copyItem === $item;
}

public function loadPath(JsonMenuNested $item): void
{
foreach ($item->getPath() as $itemParent) {
Expand Down
2 changes: 2 additions & 0 deletions EMS/core-bundle/src/Entity/UserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

interface UserInterface extends \Symfony\Component\Security\Core\User\UserInterface
{
public function getId(): int;

public function getCreated(): \DateTimeInterface;

public function getModified(): \DateTimeInterface;
Expand Down
1 change: 1 addition & 0 deletions EMS/core-bundle/src/Resources/config/core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<argument type="service" id="ems.service.revision" />
<argument type="service" id="ems.service.user" />
<argument type="service" id="ems_common.service.elastica" />
<argument type="service" id="request_stack"/>
</service>

<!-- Media Library -->
Expand Down
6 changes: 6 additions & 0 deletions EMS/core-bundle/src/Resources/config/routing/component.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
<requirement key="hash">.*</requirement>
<requirement key="itemId">.*</requirement>
</route>
<route id="emsco.json_menu_nested.item_copy" path="/json-menu-nested/{hash}/item/{itemId}/copy"
controller="EMS\CoreBundle\Controller\Component\JsonMenuNestedController::itemCopy"
methods="POST">
<requirement key="hash">.*</requirement>
<requirement key="itemId">.*</requirement>
</route>
<route id="emsco.json_menu_nested.item" path="/json-menu-nested/{hash}/item/{itemId}"
controller="EMS\CoreBundle\Controller\Component\JsonMenuNestedController::item"
methods="GET">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

{%- block jmn_item -%}
{%- apply spaceless -%}
<div class="{{ html_classes('jmn-node', { 'jmn-collapsible': item.hasChildren, 'jmn-node-active': render.isActive(item) }) }}" data-id="{{ item.id }}" data-type="{{ item.type }}">
<div class="{{ html_classes('jmn-node', { 'jmn-collapsible': item.hasChildren, 'jmn-node-active': item == render.activeItem }) }}" data-id="{{ item.id }}" data-type="{{ item.type }}">
{{ template.block('jmn_item_row', _context)|raw }}
{%- if node.leaf == false -%}
<div id="{{ "children-#{item.id}" }}" class="jmn-children jmn-sortable" data-types="{{ config.nodes.types(item.type)|json_encode|e('html_attr') }}">
Expand Down

0 comments on commit 0f38d4f

Please sign in to comment.