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

124 lines
3.5 KiB
Vue
Raw Normal View History

<template>
<div class="sn-color--sn-blue pl-7 w-64 flex justify-center flex-col" :navigator-item-id="item.id">
<div class="p-2 flex items-center whitespace-nowrap" :class="{ 'sn-background--sn-light-grey': activeItem }">
2023-04-21 21:25:52 +08:00
<div class="w-5 mr-2 flex justify-start shrink-0">
<i v-if="hasChildren"
class="fas cursor-pointer"
:class="{'fa-chevron-right': !childrenExpanded, 'fa-chevron-down': childrenExpanded }"
@click="toggleChildren"></i>
</div>
<a :href="item.url"
class="text-ellipsis overflow-hidden hover:no-underline"
:class="{
'pointer-events-none': (!item.archived && archived),
'sn-color--sn-grey': (!item.archived && archived)
}">
<i v-if="itemIcon" class="mr-2" :class="itemIcon"></i>
<template v-if="item.archived">(A)</template>
2023-04-21 21:25:52 +08:00
{{ item.name }}
</a>
</div>
2023-04-21 21:25:52 +08:00
<div class="basis-full" :class="{'hidden': !childrenExpanded}">
<NavigatorItem v-for="item in sortedMenuItems"
@item:expand="treeExpand"
:key="item.id"
:currentItemId="currentItemId"
2023-04-26 16:24:50 +08:00
:reloadCurrentLevel="reloadCurrentLevel"
:reloadChildrenLevel="reloadChildrenLevel"
:item="item"
:archived="archived" />
</div>
</div>
</template>
<script>
export default {
name: 'NavigatorItem',
props: {
2023-04-21 21:25:52 +08:00
item: Object,
currentItemId: String,
2023-04-26 16:24:50 +08:00
archived: Boolean,
reloadCurrentLevel: Boolean,
reloadChildrenLevel: Boolean
},
data: function() {
return {
2023-04-21 21:25:52 +08:00
childrenExpanded: false,
children: []
};
},
computed: {
2023-04-21 21:25:52 +08:00
hasChildren: function() {
return this.item.has_children;
},
sortedMenuItems: function() {
2023-04-26 16:24:50 +08:00
return this.children.sort((a, b) => {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
});
2023-04-21 21:25:52 +08:00
},
activeItem: function() {
return this.item.id == this.currentItemId;
},
itemIcon: function() {
switch(this.item.type) {
case 'folder':
return 'fas fa-folder';
default:
return null;
}
}
},
created: function() {
if (this.item.children) this.children = this.item.children;
},
mounted: function() {
this.selectItem();
},
watch: {
currentItemId: function() {
this.selectItem();
2023-04-26 16:24:50 +08:00
},
reloadChildrenLevel: function() {
if (this.reloadChildrenLevel && this.item.id == this.currentItemId) {
this.loadChildren();
}
},
reloadCurrentLevel: function() {
if (this.reloadCurrentLevel && this.children.find((item) => item.id == this.currentItemId)) {
this.loadChildren();
}
2023-04-21 21:25:52 +08:00
}
},
methods: {
toggleChildren: function() {
this.childrenExpanded = !this.childrenExpanded;
if (this.childrenExpanded) this.loadChildren();
},
loadChildren: function() {
$.get(this.item.children_url, {archived: this.archived}, (data) => {
2023-04-21 21:25:52 +08:00
this.children = data.items;
});
},
treeExpand: function() {
this.childrenExpanded = true;
this.$emit('item:expand');
},
selectItem: function() {
if (this.activeItem && !this.childrenExpanded) {
this.$emit('item:expand');
if (this.hasChildren) {
this.childrenExpanded = true;
this.loadChildren();
}
}
}
},
}
</script>