diff --git a/EMS/core-bundle/assets/js/component/jsonMenuNestedComponent.js b/EMS/core-bundle/assets/js/component/jsonMenuNestedComponent.js index 7c8d61bf4..6a0719946 100644 --- a/EMS/core-bundle/assets/js/component/jsonMenuNestedComponent.js +++ b/EMS/core-bundle/assets/js/component/jsonMenuNestedComponent.js @@ -4,6 +4,9 @@ import ajaxModal from "../helper/ajaxModal"; export default class JsonMenuNestedComponent { id; #tree; + #top; + #header; + #footer; element; #pathPrefix; #loadParentIds = []; @@ -14,6 +17,9 @@ export default class JsonMenuNestedComponent { this.id = element.id; this.element = element; this.#tree = element.querySelector('.jmn-tree'); + this.#top = element.querySelector('.jmn-top'); + this.#header = element.querySelector('.jmn-header'); + this.#footer = element.querySelector('.jmn-footer'); this.#pathPrefix = `/component/json-menu-nested/${element.dataset.hash}`; this._addClickListeners(); this._addClickLongPressListeners(); @@ -22,16 +28,26 @@ export default class JsonMenuNestedComponent { }); } - load({ activeItemId = null, loadChildrenId: loadChildrenId = null} = {}) { + load({ + activeItemId = null, + copyItemId = null, + loadChildrenId = null + } = {}) { this._post('/render', { active_item_id: activeItemId, + copy_item_id: copyItemId, load_parent_ids: this.#loadParentIds, load_children_id: loadChildrenId }).then((json) => { - if (!json.hasOwnProperty('tree') || !json.hasOwnProperty('load_parent_ids')) return; + const { tree, loadParentIds, top, header, footer } = json; - this.#loadParentIds = json.load_parent_ids; - this.#tree.innerHTML = json.tree; + if (top) this.#top.innerHTML = top; + if (header) this.#header.innerHTML = header; + if (footer) this.#footer.innerHTML = footer; + + if (!tree || !loadParentIds) return; + this.#loadParentIds = loadParentIds; + this.#tree.innerHTML = tree; let eventCanceled = this._dispatchEvent('jmn-load', { data: json, elements: this._sortables() }); if (!eventCanceled) this.loading(false); @@ -64,6 +80,7 @@ export default class JsonMenuNestedComponent { if (element.classList.contains('jmn-btn-edit')) this._onClickButtonEdit(element, itemId); if (element.classList.contains('jmn-btn-view')) this._onClickButtonView(element, itemId); if (element.classList.contains('jmn-btn-delete')) this._onClickButtonDelete(itemId); + if (element.classList.contains('jmn-btn-copy')) this._onClickButtonCopy(itemId); if (element.dataset.hasOwnProperty('jmnModalCustom')) this._onClickModalCustom(element, itemId); }, true); @@ -80,6 +97,15 @@ export default class JsonMenuNestedComponent { _onClickButtonDelete(itemId) { this.itemDelete(itemId); } + _onClickButtonCopy(itemId) { + document.dispatchEvent(new CustomEvent('jmn.copy', { + cancelable: true, + detail: { itemId: itemId } + })) + } + onCopy(itemId) { + this.load({ copyItemId: itemId }); + } _onClickModalCustom(element, itemId) { const modalCustomName = element.dataset.jmnModalCustom; this._ajaxModal(element, `/item/${itemId}/modal-custom/${modalCustomName}`, 'jmn-modal-custom'); diff --git a/EMS/core-bundle/assets/js/initEms.js b/EMS/core-bundle/assets/js/initEms.js index 223f67da3..e2302f05e 100644 --- a/EMS/core-bundle/assets/js/initEms.js +++ b/EMS/core-bundle/assets/js/initEms.js @@ -251,12 +251,18 @@ import JsonMenuNestedComponent from "./component/jsonMenuNestedComponent"; function initJsonMenuNestedComponent() { const elements = document.getElementsByClassName('json-menu-nested-component'); - window.jsonMenuNestedComponents = []; + let jsonMenuNestedComponents = []; [].forEach.call(elements, function (element) { - const component = new JsonMenuNestedComponent(element); - if (component.id in window.jsonMenuNestedComponents) throw new Error(`duplicate id : ${component.id}`); - window.jsonMenuNestedComponents[component.id] = component; + const component = new JsonMenuNestedComponent(element) + if (component.id in jsonMenuNestedComponents) throw new Error(`duplicate id : ${component.id}`) + jsonMenuNestedComponents[component.id] = component }); + + document.addEventListener('jmn.copy', (e) => { + Object.values(jsonMenuNestedComponents).forEach((component) => component.onCopy(e.detail.itemId)) + }) + + window.jsonMenuNestedComponents = jsonMenuNestedComponents } function initMediaLibrary() { diff --git a/EMS/core-bundle/src/Core/Component/JsonMenuNested/JsonMenuNestedService.php b/EMS/core-bundle/src/Core/Component/JsonMenuNested/JsonMenuNestedService.php index 7387cbfdb..fe19ae94b 100644 --- a/EMS/core-bundle/src/Core/Component/JsonMenuNested/JsonMenuNestedService.php +++ b/EMS/core-bundle/src/Core/Component/JsonMenuNested/JsonMenuNestedService.php @@ -29,18 +29,26 @@ public function __construct( } /** - * @return array{ load_parent_ids: string[], tree: string } + * @return array{ loadParentIds: string[], tree: string, top: string, footer: string } */ - public function render(JsonMenuNestedConfig $config, ?string $activeItemId, ?string $loadChildrenId, string ...$loadParentIds): array + public function render(JsonMenuNestedConfig $config, ?string $activeItemId, ?string $copyItemId, ?string $loadChildrenId, string ...$loadParentIds): array { $menu = $config->jsonMenuNested; - $renderContext = new JsonMenuNestedRenderContext($menu, $activeItemId, $loadChildrenId, ...$loadParentIds); + $renderContext = new JsonMenuNestedRenderContext( + menu: $menu, + activeItemId: $activeItemId, + copyItemId: $copyItemId, + loadChildrenId: $loadChildrenId + ); + $renderContext->loadParents(...$loadParentIds); $template = $this->jsonMenuNestedTemplateFactory->create($config, ['render' => $renderContext]); return [ - 'load_parent_ids' => $renderContext->getParentIds(), + 'loadParentIds' => $renderContext->getParentIds(), 'tree' => $template->block('jmn_render'), + 'top' => $template->block('jmn_layout_top'), + 'footer' => $template->block('jmn_layout_footer'), ]; } diff --git a/EMS/core-bundle/src/Core/Component/JsonMenuNested/Template/JsonMenuNestedRenderContext.php b/EMS/core-bundle/src/Core/Component/JsonMenuNested/Template/JsonMenuNestedRenderContext.php index e605015c7..0552f626c 100644 --- a/EMS/core-bundle/src/Core/Component/JsonMenuNested/Template/JsonMenuNestedRenderContext.php +++ b/EMS/core-bundle/src/Core/Component/JsonMenuNested/Template/JsonMenuNestedRenderContext.php @@ -9,6 +9,8 @@ class JsonMenuNestedRenderContext { public ?JsonMenuNested $activeItem; + public ?JsonMenuNested $copyItem; + /** @var array */ public array $loadParents = []; /** @var array */ @@ -17,29 +19,52 @@ class JsonMenuNestedRenderContext public function __construct( private readonly JsonMenuNested $menu, ?string $activeItemId, - ?string $loadChildrenId, - string ...$loadParentIds + ?string $copyItemId, + ?string $loadChildrenId ) { $this->addActiveItem($menu); - $this->activeItem = $activeItemId ? $menu->getItemById($activeItemId) : $menu; + $this->activeItem = $activeItemId ? $menu->getItemById($activeItemId) : null; if ($this->activeItem) { - foreach ($this->activeItem->getPath() as $activeParent) { - $this->addParent($activeParent); - } + $this->loadPath($this->activeItem); + } + + $this->copyItem = $copyItemId ? $menu->getItemById($copyItemId) : null; + if ($this->copyItem) { + $this->loadPath($this->copyItem); } $loadChildren = $loadChildrenId ? $this->menu->getItemById($loadChildrenId) : null; if ($loadChildren) { - foreach ($loadChildren as $loadChild) { - if ($loadChild->hasChildren()) { - $this->addParent($loadChild); - } + $this->loadAllChildren($loadChildren); + } + } + + public function isActive(JsonMenuNested $item): bool + { + return $this->activeItem === $item || $this->copyItem === $item; + } + + public function loadPath(JsonMenuNested $item): void + { + foreach ($item->getPath() as $itemParent) { + $this->addParent($itemParent); + } + } + + public function loadAllChildren(JsonMenuNested $item): void + { + foreach ($item as $child) { + if ($child->hasChildren()) { + $this->addParent($child); } } + } + public function loadParents(string ...$loadParentIds): void + { foreach ($loadParentIds as $loadParentId) { - $this->addParent($menu->getItemById($loadParentId)); + $this->addParent($this->menu->getItemById($loadParentId)); } } diff --git a/EMS/core-bundle/src/Resources/views/components/json_menu_nested/component.html.twig b/EMS/core-bundle/src/Resources/views/components/json_menu_nested/component.html.twig index 0250ad804..fccdd3ff3 100644 --- a/EMS/core-bundle/src/Resources/views/components/json_menu_nested/component.html.twig +++ b/EMS/core-bundle/src/Resources/views/components/json_menu_nested/component.html.twig @@ -5,10 +5,10 @@
- {{ template.block('jmn_layout_top', _context)|raw }} +
{{ template.block('jmn_layout_top', _context)|raw }}
- {{ template.block('_jmn_columns', _context)|raw }} +
{{ template.block('jmn_columns', _context)|raw }}
- {{ template.block('jmn_layout_footer', _context)|raw }} +
\ No newline at end of file diff --git a/EMS/core-bundle/src/Resources/views/components/json_menu_nested/template.twig b/EMS/core-bundle/src/Resources/views/components/json_menu_nested/template.twig index f65aef662..a44893fb2 100644 --- a/EMS/core-bundle/src/Resources/views/components/json_menu_nested/template.twig +++ b/EMS/core-bundle/src/Resources/views/components/json_menu_nested/template.twig @@ -1,4 +1,4 @@ -{% trans_default_domain 'EMSCoreBundle' %} +{%- trans_default_domain 'EMSCoreBundle' -%} {# @var config \EMS\CoreBundle\Core\Component\JsonMenuNested\Config\JsonMenuNestedConfig #} {# @var column \EMS\CoreBundle\Core\Component\JsonMenuNested\Config\JsonMenuNestedColumn #} {# @var template \EMS\CoreBundle\Core\Component\JsonMenuNested\Template\JsonMenuNestedTemplate #} @@ -8,10 +8,8 @@ {# @var node \EMS\CoreBundle\Core\Component\JsonMenuNested\Config\JsonMenuNestedNode #} {%- block jmn_layout_top -%} -
- {{ template.block('jmn_title', _context)|raw }} - {{ template.block('jmn_actions', _context)|raw }} -
+ {{ template.block('jmn_title', _context)|raw }} + {{ template.block('jmn_actions', _context)|raw }} {%- endblock jmn_layout_top -%} {%- block jmn_layout_footer -%}{% endblock jmn_layout_footer %} @@ -23,17 +21,15 @@ {{ template.block('jmn_button_add', _context)|raw }} {%- endblock jmn_actions -%} -{%- block _jmn_columns -%} -
- {% for column in config.columns %} -
- {%- if template.hasBlock("jmn_column_#{column.name}") -%} - {{ template.block("jmn_column_#{column.name}", _context)|raw }} - {%- endif -%} -
- {% endfor %} -
-{%- endblock _jmn_columns -%} +{%- block jmn_columns -%} + {% for column in config.columns %} +
+ {%- if template.hasBlock("jmn_column_#{column.name}") -%} + {{ template.block("jmn_column_#{column.name}", _context)|raw }} + {%- endif -%} +
+ {% endfor %} +{%- endblock jmn_columns -%} {% block jmn_render %} {% with { 'menu': menu } %}{{ template.block('jmn_items', _context)|raw }}{% endwith %} @@ -48,7 +44,7 @@ {%- block jmn_item -%} {%- apply spaceless -%} -
+
{{ template.block('jmn_item_row', _context)|raw }} {%- if node.leaf == false -%}
@@ -112,6 +108,10 @@ {%- endblock jmn_button_item_view -%} +{%- block jmn_button_item_copy -%} + +{%- endblock jmn_button_item_copy -%} + {%- block jmn_button_item_add -%} - - {% elseif item.children|length == 0 %} -
  • {{- template.block('jmn_button_item_delete', _context)|raw -}}
  • - {% endif %} + {%- set menuMoreItems = menuMoreItems|default([ + 'jmn_button_item_view', + 'jmn_button_item_edit', + 'button_detach', + 'jmn_button_item_copy', + 'jmn_button_item_delete' + ]) -%} + {{ parent() }} {%- endblock jmn_button_item_more_menu -%} {%- block jmn_button_item_add -%} - {% set buttonLabel = "Create #{addNode.type}" %} + {%- set buttonLabel = "Create #{addNode.type}" -%} {{ parent() }} {%- endblock -%} {%- block jmn_button_item_edit -%} - {% if node.type|default('') == 'page' %}{% set buttonLabel = 'Edit item' %}{% endif %} + {%- if node.type|default('') == 'page' -%}{%- set buttonLabel = 'Edit item' -%}{% endif -%} {{ parent() }} {%- endblock -%} {%- block jmn_button_item_view -%} - {% if node.type|default('') == 'page' %}{% set buttonLabel = 'View item' %}{% endif %} + {%- if node.type|default('') == 'page' -%}{%- set buttonLabel = 'View item' -%}{%- endif -%} {{ parent() }} {%- endblock -%} +{%- block button_detach -%} + {%- if item.type == 'page' and pageInfo -%} + + {%- endif -%} +{%- endblock button_detach -%} + +{%- block jmn_button_item_delete -%} + {%- if item.children|length == 0 and (item.type != 'page' or pageInfo == false) -%} + {{ parent() }} + {%- endif -%} +{%- endblock -%} + {%- block jmn_item_title -%} {% if item.type == 'page' %} {% if page and pageInfo %}