scinote-web/app/javascript/vue/navigation/navigator.vue

167 lines
5.2 KiB
Vue
Raw Normal View History

<template>
2023-11-09 02:32:43 +08:00
<Vue3DraggableResizable
:initW="getNavigatorWidth()"
2023-10-12 20:40:13 +08:00
ref="vueResizable"
2023-11-09 02:32:43 +08:00
:minW="208"
2023-11-20 23:28:43 +08:00
v-model:w="width"
2023-11-09 02:32:43 +08:00
:disabledH="true"
:handles="['mr']"
:resizable="true"
:draggable="false"
class="max-w-[400px] !h-full"
@resize-start="onResizeStart"
@resizing="onResizeMove"
@resize-end="onResizeEnd"
2023-10-12 20:40:13 +08:00
>
2023-11-20 23:28:43 +08:00
<div class="ml-4 h-full w-[calc(100%_-_1rem)] border rounded bg-sn-white flex flex-col right-0 absolute navigator-container">
2023-10-12 20:40:13 +08:00
<div class="px-3 py-2.5 flex items-center relative leading-4">
<i class="sn-icon sn-icon-navigator"></i>
<div class="font-bold text-base pl-3">
{{ i18n.t('navigator.title') }}
</div>
<i @click="$emit('navigator:colapse')" class="sn-icon sn-icon-close ml-auto cursor-pointer absolute right-2.5 top-2.5"></i>
</div>
2023-11-20 23:28:43 +08:00
<perfect-scrollbar @ps-scroll-y="onScrollY" @ps-scroll-x="onScrollX" ref="scrollContainer" class="grow py-2 relative px-2 scroll-container w-[calc(100%_-_.25rem)]">
2023-10-12 20:40:13 +08:00
<NavigatorItem v-for="item in sortedMenuItems"
:key="item.id"
:currentItemId="currentItemId"
:item="item"
:firstLevel="true"
:reloadCurrentLevel="reloadCurrentLevel"
:paddingLeft="0"
:reloadChildrenLevel="reloadChildrenLevel"
:archived="archived" />
</perfect-scrollbar>
</div>
2023-11-09 02:32:43 +08:00
</Vue3DraggableResizable>
</template>
<script>
import NavigatorItem from './navigator_item.vue'
2023-10-12 20:40:13 +08:00
import axios from '../../packs/custom_axios.js';
2023-11-09 02:32:43 +08:00
import Vue3DraggableResizable from 'vue3-draggable-resizable'
export default {
name : 'NavigatorContainer',
components: {
2023-10-12 20:40:13 +08:00
NavigatorItem,
2023-11-09 02:32:43 +08:00
Vue3DraggableResizable
},
data() {
return {
menuItems: [],
2023-04-21 21:25:52 +08:00
navigatorCollapsed: false,
navigatorUrl: null,
navigatorYScroll: 0,
navigatorXScroll: 0,
currentItemId: null,
2023-11-09 02:32:43 +08:00
archived: null,
2023-11-20 23:28:43 +08:00
width: null
}
},
2023-04-26 16:24:50 +08:00
props: {
reloadCurrentLevel: Boolean,
reloadChildrenLevel: Boolean
},
computed: {
sortedMenuItems() {
2023-04-26 16:24:50 +08:00
return this.menuItems.sort((a, b) => {
2023-05-11 18:38:54 +08:00
if (a.name.toLowerCase() < b.name.toLowerCase()) {
2023-04-26 16:24:50 +08:00
return -1;
}
2023-05-11 18:38:54 +08:00
if (a.name.toLowerCase() > b.name.toLowerCase()) {
2023-04-26 16:24:50 +08:00
return 1;
}
return 0;
});
}
},
created() {
2023-04-21 21:25:52 +08:00
this.changePage();
$(document).on('turbolinks:load', () => {
this.$refs.scrollContainer.$el.scrollTop = this.navigatorYScroll;
this.$refs.scrollContainer.$el.scrollLeft = this.navigatorXScroll;
2023-04-21 21:25:52 +08:00
this.changePage();
if ($(`[navigator-item-id="${this.currentItemId}"]`).length === 0) {
this.loadTree();
}
});
},
2023-10-12 20:40:13 +08:00
mounted() {
this.$refs.vueResizable.style.width = this.getNavigatorWidth()
},
watch: {
archived() {
this.loadTree();
2023-04-26 16:24:50 +08:00
},
reloadCurrentLevel: function() {
if (this.reloadCurrentLevel && (
this.currentItemId.length == 0 ||
this.menuItems.filter(item => item.id == this.currentItemId)
)) {
this.loadTree();
}
}
},
2023-04-21 21:25:52 +08:00
methods: {
changePage() {
this.navigatorUrl = $('#active_navigator_url').val();
this.currentItemId = $('#active_navigator_item').val();
this.archived = $('#active_navigator_archived').val() === 'true';
2023-04-21 21:25:52 +08:00
},
2023-10-12 20:40:13 +08:00
async loadTree() {
2023-04-21 21:25:52 +08:00
if (!this.navigatorUrl) return;
2023-10-12 20:40:13 +08:00
try {
const { data } = await axios.get(this.navigatorUrl, {
params: { archived: this.archived }
});
2023-10-12 20:40:13 +08:00
this.menuItems = data.items;
} catch (error) {
console.error('An error occurred while fetching the data', error);
}
},
onScrollY({target}) {
this.navigatorYScroll = target.scrollTop;
},
onScrollX({target}) {
this.navigatorXScroll = target.scrollLeft;
2023-10-12 20:40:13 +08:00
},
getNavigatorWidth() {
const computedStyle = getComputedStyle(document.documentElement);
2023-11-09 02:32:43 +08:00
return parseInt(computedStyle.getPropertyValue('--navigator-navigation-width').trim());
2023-10-12 20:40:13 +08:00
},
onResizeMove(event) {
2023-11-09 02:32:43 +08:00
if (event.w > 400) event.w = 400;
document.documentElement.style.setProperty('--navigator-navigation-width', event.w + 'px');
2023-10-12 20:40:13 +08:00
},
onResizeStart() {
document.body.style.cursor = 'url(/images/icon_small/Resize.svg) 0 0, auto';
$('.sci--layout-navigation-navigator').addClass('!transition-none');
$('.sci--layout').addClass('!transition-none');
2023-10-12 20:40:13 +08:00
},
onResizeEnd(event) {
2023-11-20 23:28:43 +08:00
if (event.w > 400) {
event.w = 400;
this.width = 400;
}
2023-10-12 20:40:13 +08:00
document.body.style.cursor = 'default';
$('.sci--layout-navigation-navigator').removeClass('!transition-none');
$('.sci--layout').removeClass('!transition-none');
2023-11-09 02:32:43 +08:00
this.changeNavigatorState(event.w)
2023-10-12 20:40:13 +08:00
},
async changeNavigatorState(newWidth) {
try {
const navigatorContainer = document.getElementById('sciNavigationNavigatorContainer');
const stateUrl = navigatorContainer.getAttribute('data-navigator-state-url');
await axios.post(stateUrl, { width: newWidth });
} catch (error) {
console.error('An error occurred while sending the request', error);
}
}
2023-04-21 21:25:52 +08:00
},
}
</script>