Skip to content

Commit

Permalink
Переработка дерева концептов
Browse files Browse the repository at this point in the history
  • Loading branch information
delaynore committed May 30, 2024
1 parent 4244922 commit 79ba976
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 52 deletions.
5 changes: 5 additions & 0 deletions resources/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@
-webkit-line-clamp: 3;
overflow: hidden;
}

.active {
@apply bg-blue-200 dark:bg-sky-800 hover:!bg-blue-300 bg-opacity-100 dark:hover:!bg-sky-700 text-black dark:text-white border-blue-500 dark:border-blue-500
}

55 changes: 55 additions & 0 deletions resources/js/tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
document.addEventListener("DOMContentLoaded", function () {
const treeview = document.querySelector('.treeview');

function toggleNode(event) {
const li = event.target.closest('li');
const ul = li.querySelector('ul');
if (ul) {
ul.classList.toggle('hidden');
const isOpen = !ul.classList.contains('hidden');
const icon = li.querySelector('.tree-view-icon');
const block = li.querySelector('div');
if(isOpen) {
icon.style.transform = 'rotate(-90deg)';
block.classList.add('active');
}
else {
icon.style.transform = '';
block.classList.remove('active');
}

const nodeId = li.getAttribute('data-id');
console.log(nodeId);
sessionStorage.setItem(nodeId, isOpen);
console.log(sessionStorage.key('concept-'));
}
}

treeview.querySelectorAll('.toggle').forEach(toggle => {
toggle.addEventListener('click', toggleNode);
});

treeview.querySelectorAll('li[data-id]').forEach(li => {
const nodeId = li.getAttribute('data-id');
const isOpen = sessionStorage.getItem(nodeId) === 'true';
const ul = li.querySelector('ul');
if (ul && isOpen) {
ul.classList.remove('hidden');
console.log(nodeId);
li.querySelector('.tree-view-icon').style.transform = 'rotate(-90deg)';
li.querySelector('div').classList.add('active');
}
});

});

window.addEventListener("load", function () {
const treeview = document.querySelector('.treeview');
treeview.querySelectorAll('div.open-concept').forEach(div => {
div.addEventListener('dblclick', (e) => {
const route = e.currentTarget.dataset.openConceptRoute;
console.log('click', e.currentTarget.dataset);
window.location.href = route;
});
})
});
4 changes: 2 additions & 2 deletions resources/views/components/tree-view/icon.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<svg data-accordion-icon class="w-3 h-3 rotate-180 shrink-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 10 6">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5 5 1 1 5" />
<svg class="w-4 h-4 transition-transform delay-50 shrink-0 tree-view-icon dark:text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="3" stroke="currentColor" class="size-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5 8.25 12l7.5-7.5" />
</svg>
59 changes: 16 additions & 43 deletions resources/views/components/tree-view/tree-item.blade.php
Original file line number Diff line number Diff line change
@@ -1,46 +1,27 @@
@php
$dictionary = $concept->dictionary;
$children = $concept->children()->get();
$unique = Str::random(5);
$hover = 'hover:bg-gray-200 dark:hover:bg-gray-850';
$active = 'bg-blue-200 dark:bg-sky-800 hover:bg-blue-200 dark:hover:bg-sky-800 text-black dark:text-white outline outline-1 outline-blue-500 dark:outline-blue-500';
@endphp

@if ($children->count() > 0)
<h2 id="header-{{$concept->id}} " class="w-full">
<div data-dropdown-trigger="click" data-dropdown-toggle="dropdown{{$concept->id}}" data-dropdown-placement="right" data-dropdown-offset-skidding="100">
<button type="button" class="{{$hover}} flex items-center justify-between w-full px-2 py-1 font-medium text-gray-700 dark:text-gray-200 dark:hover:bg-gray-800" data-accordion-target="#body-{{$concept->id}}" aria-expanded="false" aria-controls="body-{{$concept->id}}">
<div class="inline-flex items-start text-left">
@if ($concept->fk_parent_concept_id)
<span class="text-gray-400 -ml-[10px] mr-1">-</span>
@endif
<span class="overflow-hidden text-gray-700 select-text dark:text-gray-200">{{$concept->name}}</span>
</div>
<x-tree-view.icon />
</button>
</div>
</h2>
<div id="body-{{$concept->id}}" class="hidden" aria-labelledby="header-{{$concept->id}}">
<div class="border-l-2 border-gray-400 select-text ms-1">
<!-- Nested accordion -->
<div id="{{$unique}}" data-accordion="open" data-inactive-classes="text-gray-700 dark:text-gray-200" data-active-classes="{{$active}} {{$hover}}">
@each('components.tree-view.tree-item', $children, 'concept')
<li data-id="concept-{{$concept->id}}" class="cursor-pointer">
<div class="mb-[1px] inline-flex items-center justify-between w-full p-1 transition-transform delay-100 border-r border-gray-100 dark:border-gray-900 hover:bg-gray-200 dark:hover:bg-gray-700 toggle">
<div data-open-concept-route="{{route('concept.show',[$dictionary, $concept])}}" class="inline-flex items-start flex-grow text-left open-concept" data-dropdown-trigger="click" data-dropdown-toggle="dropdown{{$concept->id}}" data-dropdown-placement="right" data-dropdown-offset-skidding="100">
@if ($concept->fk_parent_concept_id)
<span class="text-gray-400 -ml-[5px] mr-1">-</span>
@endif
<span class="overflow-hidden text-gray-900 select-text dark:text-gray-200">{{$concept->name}}</span>
</div>
<!-- End: Nested accordion -->
</div>
</div>
@else

<h2 class="w-full">
<div data-dropdown-trigger="click" data-dropdown-toggle="dropdown{{$concept->id}}" data-dropdown-placement="right" data-dropdown-offset-skidding="100" class="{{$hover}} justify-start flex items-center w-full px-2 py-1 font-medium text-gray-700 focus:ring-4 focus:ring-gray-200 dark:focus:ring-gray-800 dark:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-800">
@if ($concept->fk_parent_concept_id)
<span class="text-gray-400 -ml-[10px] mr-1">-</span>
@if ($children->count() > 0)
<x-tree-view.icon></x-tree-view.icon>
@endif
{{$concept->name}}
</div>
</h2>
@endif
@if ($children->count() > 0)
<ul class="hidden border-l-2 border-gray-400 select-text ms-1">
@each('components.tree-view.tree-item', $children, 'concept')
</ul>
@endif
</li>



<div id="dropdown{{$concept->id}}" class="z-10 hidden bg-white divide-y divide-gray-100 rounded-lg shadow w-44 dark:bg-gray-700">
Expand Down Expand Up @@ -85,14 +66,6 @@
</li>
@endcan
</ul>
<div class="py-1 text-gray-700 dark:text-gray-200">
<a href="#" class="flex items-center justify-between px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white">
{{ __('shared.link')}}
<svg class="w-4 h-4" data-slot="icon" aria-hidden="true" fill="none" stroke-width="2" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path d="M13.19 8.688a4.5 4.5 0 0 1 1.242 7.244l-4.5 4.5a4.5 4.5 0 0 1-6.364-6.364l1.757-1.757m13.35-.622 1.757-1.757a4.5 4.5 0 0 0-6.364-6.364l-4.5 4.5a4.5 4.5 0 0 0 1.242 7.244" stroke-linecap="round" stroke-linejoin="round"></path>
</svg>
</a>
</div>
@can('must-be-owner', $dictionary)
<div class="py-1 text-gray-700 dark:text-gray-200">
<form action="{{route('concept.destroy', [$dictionary ,$concept->id])}}" method="post">
Expand Down
17 changes: 10 additions & 7 deletions resources/views/components/tree-view/tree-view.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
@php
$active = 'bg-blue-200 dark:bg-sky-950 hover:bg-blue-200 dark:hover:bg-sky-950 text-black dark:text-white outline-1 outline-blue-500 dark:outline-blue-500 outline';
@endphp

<div id="accordion-nested-parent" data-accordion="open" class="w-full cursor-pointer" data-active-classes="{{ $active }}" data-inactive-classes="text-inherit">
<ul class="pb-12 treeview">
@foreach ($concepts as $concept)
@include('components.tree-view.tree-item', ['concept' => $concept])
@include('components.tree-view.tree-item', ['concept' => $concept])
@endforeach
</div>
</ul>
@once
@vite(['resources/js/tree.js'])
@endonce




0 comments on commit 79ba976

Please sign in to comment.