2020-04-06 15:53:32 +02:00
|
|
|
<template>
|
2021-04-16 14:09:39 +02:00
|
|
|
<div class="ui-tree">
|
2020-05-20 13:49:50 +02:00
|
|
|
<ui-header-bar class="ui-tree-header" :title="header" :back-button="false" v-if="header">
|
|
|
|
|
<ui-dot-button @click="onActionsClicked(null, $event)" />
|
|
|
|
|
</ui-header-bar>
|
2020-06-03 15:55:49 +02:00
|
|
|
<slot></slot>
|
2020-04-06 18:45:23 +02:00
|
|
|
<span v-if="status === 'loading'" class="ui-tree-item-loading"><i></i></span>
|
|
|
|
|
<template v-for="item in items">
|
2021-09-07 15:10:40 +02:00
|
|
|
<ui-tree-item :value="item" :active-id="active" :depth="depth" :selected="selection.indexOf(item.id) > -1"
|
|
|
|
|
@rightclick="onRightClicked" @click="onSelect(item, $event)" @actions="onActionsClicked" @open="toggle" @setactive="onActiveSet"
|
|
|
|
|
/>
|
|
|
|
|
<ui-tree v-if="item.hasChildren && item.isOpen && status != 'loading'" v-bind="{ get, parent: item.id, depth: depth + 1, active, mode, selection, selectionLimit }" @select="onChildSelect" @setactive="onActiveSet">
|
2020-08-25 13:17:41 +02:00
|
|
|
<template v-slot:actions="props">
|
|
|
|
|
<slot name="actions" v-bind="props"></slot>
|
|
|
|
|
</template>
|
|
|
|
|
</ui-tree>
|
2020-04-06 18:45:23 +02:00
|
|
|
</template>
|
2020-06-04 15:50:21 +02:00
|
|
|
<slot name="bottom"></slot>
|
2020-08-25 13:17:41 +02:00
|
|
|
<ui-dropdown ref="dropdown" align="top" theme="dark" class="ui-tree-dropdown">
|
|
|
|
|
<slot name="actions" v-bind="actionProps"></slot>
|
|
|
|
|
</ui-dropdown>
|
2020-04-06 15:53:32 +02:00
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
2021-12-10 14:59:35 +01:00
|
|
|
<script lang="ts">
|
|
|
|
|
import { defineComponent } from 'vue';
|
|
|
|
|
import UiTreeItem from './ui-tree-item.vue';
|
2020-05-20 11:19:43 +02:00
|
|
|
|
2021-12-10 14:59:35 +01:00
|
|
|
export default defineComponent({
|
2020-04-06 15:53:32 +02:00
|
|
|
name: 'uiTree',
|
|
|
|
|
|
2021-12-10 14:59:35 +01:00
|
|
|
components: { UiTreeItem },
|
|
|
|
|
|
2020-04-06 15:53:32 +02:00
|
|
|
props: {
|
2020-04-06 18:45:23 +02:00
|
|
|
depth: {
|
|
|
|
|
type: Number,
|
|
|
|
|
default: 0
|
|
|
|
|
},
|
2020-05-20 11:19:43 +02:00
|
|
|
active: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: null
|
|
|
|
|
},
|
2020-04-06 18:45:23 +02:00
|
|
|
parent: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: null
|
|
|
|
|
},
|
2020-05-20 13:49:50 +02:00
|
|
|
header: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: null
|
|
|
|
|
},
|
2020-04-06 15:53:32 +02:00
|
|
|
get: {
|
|
|
|
|
type: Function,
|
|
|
|
|
required: true
|
2020-05-20 11:19:43 +02:00
|
|
|
},
|
2020-08-25 13:17:41 +02:00
|
|
|
hasActions: {
|
|
|
|
|
type: Function,
|
|
|
|
|
default: null
|
2021-02-10 14:53:56 +01:00
|
|
|
},
|
|
|
|
|
mode: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: 'link'
|
|
|
|
|
},
|
|
|
|
|
selection: {
|
|
|
|
|
type: Array,
|
|
|
|
|
default: () => []
|
|
|
|
|
},
|
|
|
|
|
selectionLimit: {
|
|
|
|
|
type: Number,
|
|
|
|
|
default: 1
|
2020-05-20 11:19:43 +02:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-04-06 18:45:23 +02:00
|
|
|
data: () => ({
|
|
|
|
|
items: [],
|
2020-05-20 11:19:43 +02:00
|
|
|
status: 'none',
|
2020-08-25 13:17:41 +02:00
|
|
|
actionProps: {
|
|
|
|
|
item: null
|
2020-08-28 11:40:50 +02:00
|
|
|
}
|
2020-04-06 18:45:23 +02:00
|
|
|
}),
|
|
|
|
|
|
2020-08-25 13:17:41 +02:00
|
|
|
computed: {
|
|
|
|
|
actionsDefined()
|
|
|
|
|
{
|
2021-12-16 16:59:27 +01:00
|
|
|
return this.$slots.hasOwnProperty('actions');
|
2020-08-25 13:17:41 +02:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-05-20 11:19:43 +02:00
|
|
|
mounted()
|
2020-04-06 15:53:32 +02:00
|
|
|
{
|
2020-08-28 11:40:50 +02:00
|
|
|
this.refresh();
|
2020-04-06 15:53:32 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
|
|
2021-09-07 15:10:40 +02:00
|
|
|
onActiveSet(val)
|
|
|
|
|
{
|
|
|
|
|
this.$emit('setactive', val);
|
|
|
|
|
},
|
2020-05-20 11:19:43 +02:00
|
|
|
|
2020-08-28 11:40:50 +02:00
|
|
|
// refreshes the whole tree
|
2020-05-29 15:28:03 +02:00
|
|
|
refresh()
|
|
|
|
|
{
|
|
|
|
|
this.load(this.parent);
|
|
|
|
|
},
|
|
|
|
|
|
2020-08-28 11:40:50 +02:00
|
|
|
|
2020-04-06 15:53:32 +02:00
|
|
|
// loads children of the given parent id or on root if empty
|
|
|
|
|
load(parent)
|
|
|
|
|
{
|
2020-04-06 18:45:23 +02:00
|
|
|
this.setStatus('loading', this.items);
|
2020-04-06 15:53:32 +02:00
|
|
|
|
2020-05-20 11:19:43 +02:00
|
|
|
let promise = this.get(parent, this.active);
|
2020-04-06 15:53:32 +02:00
|
|
|
|
|
|
|
|
promise.then(response =>
|
|
|
|
|
{
|
2020-04-06 18:45:23 +02:00
|
|
|
this.items = response;
|
|
|
|
|
this.setStatus('loaded', this.items);
|
2020-04-06 15:53:32 +02:00
|
|
|
})
|
|
|
|
|
.catch(error =>
|
|
|
|
|
{
|
2021-01-30 16:52:36 +01:00
|
|
|
console.error(error);
|
2020-04-06 18:45:23 +02:00
|
|
|
this.setStatus('error', this.items, error);
|
2020-04-06 15:53:32 +02:00
|
|
|
// TODO handle errors
|
|
|
|
|
});
|
2020-04-06 18:45:23 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// updates the status for the current tree
|
|
|
|
|
setStatus(status)
|
|
|
|
|
{
|
|
|
|
|
this.status = status;
|
2020-04-11 21:32:25 +02:00
|
|
|
this.$emit('onStatusChange', status);
|
2020-04-06 18:45:23 +02:00
|
|
|
},
|
|
|
|
|
|
2020-08-28 11:40:50 +02:00
|
|
|
|
2020-06-04 15:50:21 +02:00
|
|
|
// toggles children of an item
|
|
|
|
|
toggle(item)
|
|
|
|
|
{
|
|
|
|
|
item.isOpen = !item.isOpen;
|
|
|
|
|
},
|
|
|
|
|
|
2020-08-28 11:40:50 +02:00
|
|
|
|
|
|
|
|
// selected an item
|
2020-06-05 15:34:13 +02:00
|
|
|
onSelect(item, ev)
|
|
|
|
|
{
|
2021-02-10 14:53:56 +01:00
|
|
|
if (this.mode === 'select')
|
|
|
|
|
{
|
|
|
|
|
let index = this.selection.indexOf(item.id);
|
|
|
|
|
if (index > -1)
|
|
|
|
|
{
|
|
|
|
|
this.selection.splice(index, 1);
|
|
|
|
|
}
|
|
|
|
|
else if (this.selectionLimit === 1)
|
|
|
|
|
{
|
|
|
|
|
this.selection.splice(0, this.selection.length);
|
|
|
|
|
this.selection.push(item.id);
|
|
|
|
|
}
|
|
|
|
|
else if (this.selection.length < this.selectionLimit)
|
|
|
|
|
{
|
|
|
|
|
this.selection.push(item.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.$emit('select', this.selectionLimit > 1 ? this.selection : (this.selection.length > 0 ? this.selection[0] : null), ev);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
this.$emit('select', item, ev);
|
|
|
|
|
}
|
2020-06-05 15:34:13 +02:00
|
|
|
},
|
|
|
|
|
|
2020-08-28 11:40:50 +02:00
|
|
|
|
2021-04-15 15:46:16 +02:00
|
|
|
onChildSelect(item, ev)
|
|
|
|
|
{
|
|
|
|
|
this.$emit('select', item, ev);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
2020-05-20 13:15:20 +02:00
|
|
|
// right clicked on an item
|
|
|
|
|
onRightClicked(item, ev)
|
|
|
|
|
{
|
2020-08-25 13:17:41 +02:00
|
|
|
if (this.actionsDefined && (!item || item.hasActions))
|
2020-05-20 13:15:20 +02:00
|
|
|
{
|
2020-08-25 13:17:41 +02:00
|
|
|
ev.preventDefault();
|
2020-05-20 13:15:20 +02:00
|
|
|
this.onActionsClicked(item, ev);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-08-28 11:40:50 +02:00
|
|
|
|
2020-05-20 13:15:20 +02:00
|
|
|
// actions button clicked on item
|
|
|
|
|
onActionsClicked(item, ev)
|
|
|
|
|
{
|
2020-08-25 13:17:41 +02:00
|
|
|
let dropdown = this.$refs.dropdown;
|
2020-05-20 13:15:20 +02:00
|
|
|
|
2020-08-25 13:17:41 +02:00
|
|
|
if (!this.actionsDefined || (item && !item.hasActions) || (typeof this.hasActions === 'function' && !this.hasActions(item)))
|
2020-05-20 13:49:50 +02:00
|
|
|
{
|
2020-05-20 13:15:20 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-25 13:17:41 +02:00
|
|
|
this.actionProps.item = item;
|
|
|
|
|
this.actionProps.event = ev;
|
2020-08-25 12:06:40 +02:00
|
|
|
|
2020-08-25 13:17:41 +02:00
|
|
|
dropdown.toggle();
|
|
|
|
|
|
|
|
|
|
if (!dropdown.open)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-05-20 13:15:20 +02:00
|
|
|
|
|
|
|
|
this.$nextTick(() =>
|
|
|
|
|
{
|
|
|
|
|
let target = ev.target;
|
|
|
|
|
do
|
|
|
|
|
{
|
2020-05-20 13:49:50 +02:00
|
|
|
if (target.classList.contains('ui-tree-item') || target.classList.contains('ui-tree-header'))
|
2020-05-20 13:15:20 +02:00
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (target = target.parentElement);
|
|
|
|
|
|
2020-05-20 13:49:50 +02:00
|
|
|
target = target.querySelector('.ui-dot-button');
|
2020-05-20 13:15:20 +02:00
|
|
|
|
|
|
|
|
var rect = target.getBoundingClientRect();
|
|
|
|
|
var width = 240;
|
|
|
|
|
|
|
|
|
|
var position = {
|
|
|
|
|
x: rect.left - width + rect.width,
|
|
|
|
|
y: rect.top + rect.height
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-25 13:17:41 +02:00
|
|
|
let element = dropdown.$el.querySelector('.ui-dropdown');
|
2020-08-25 12:06:40 +02:00
|
|
|
|
2020-08-25 13:17:41 +02:00
|
|
|
element.style.top = position.y + 'px';
|
|
|
|
|
element.style.left = position.x + 'px';
|
|
|
|
|
element.style.width = width + 'px';
|
2020-05-20 13:15:20 +02:00
|
|
|
});
|
2020-04-06 15:53:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2021-12-10 14:59:35 +01:00
|
|
|
})
|
2020-04-06 18:45:23 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
|
.ui-tree
|
|
|
|
|
{
|
|
|
|
|
position: relative;
|
2021-02-10 14:53:56 +01:00
|
|
|
overflow-x: hidden;
|
2020-04-06 18:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-25 13:17:41 +02:00
|
|
|
.ui-tree-dropdown .ui-dropdown
|
2020-05-20 13:15:20 +02:00
|
|
|
{
|
|
|
|
|
position: fixed;
|
|
|
|
|
min-width: 200px;
|
|
|
|
|
}
|
2020-04-06 18:45:23 +02:00
|
|
|
</style>
|