primevue-mirror/components/treetable/TreeTableRow.vue

273 lines
9.1 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<tr :class="containerClass" @click="onClick" @keydown="onKeyDown" @touchend="onTouchEnd" :style="node.style" tabindex="0">
2022-09-14 11:26:01 +00:00
<template v-for="(col, i) of columns" :key="columnProp(col, 'columnKey') || columnProp(col, 'field') || i">
<TTBodyCell
v-if="!columnProp(col, 'hidden')"
:column="col"
:node="node"
:level="level"
:leaf="leaf"
:indentation="indentation"
:expanded="expanded"
:selectionMode="selectionMode"
:checked="checked"
:partialChecked="partialChecked"
@node-toggle="$emit('node-toggle', $event)"
@checkbox-toggle="toggleCheckbox"
></TTBodyCell>
2022-09-06 12:03:37 +00:00
</template>
</tr>
<template v-if="expanded && node.children && node.children.length">
2022-09-14 11:26:01 +00:00
<TreeTableRow
v-for="childNode of node.children"
:key="childNode.key"
:columns="columns"
:node="childNode"
:parentNode="node"
:level="level + 1"
:expandedKeys="expandedKeys"
:selectionMode="selectionMode"
:selectionKeys="selectionKeys"
:indentation="indentation"
@node-toggle="$emit('node-toggle', $event)"
@node-click="$emit('node-click', $event)"
@checkbox-change="onCheckboxChange"
/>
2022-09-06 12:03:37 +00:00
</template>
</template>
<script>
2022-09-14 11:26:01 +00:00
import { DomHandler, ObjectUtils } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
import BodyCell from './BodyCell.vue';
export default {
name: 'TreeTableRow',
2022-09-14 11:26:01 +00:00
emits: ['node-click', 'node-toggle', 'checkbox-change', 'nodeClick', 'nodeToggle', 'checkboxChange'],
2022-09-06 12:03:37 +00:00
props: {
node: {
type: null,
default: null
},
parentNode: {
type: null,
default: null
},
columns: {
type: null,
default: null
},
expandedKeys: {
type: null,
default: null
},
selectionKeys: {
type: null,
default: null
},
selectionMode: {
type: String,
default: null
},
level: {
type: Number,
default: 0
},
indentation: {
type: Number,
default: 1
}
},
nodeTouched: false,
methods: {
columnProp(col, prop) {
return ObjectUtils.getVNodeProp(col, prop);
},
toggle() {
this.$emit('node-toggle', this.node);
},
onClick(event) {
2022-09-14 11:26:01 +00:00
if (DomHandler.isClickable(event.target) || DomHandler.hasClass(event.target, 'p-treetable-toggler') || DomHandler.hasClass(event.target.parentElement, 'p-treetable-toggler')) {
2022-09-06 12:03:37 +00:00
return;
}
this.$emit('node-click', {
2022-09-14 11:26:01 +00:00
originalEvent: event,
nodeTouched: this.nodeTouched,
node: this.node
});
2022-09-06 12:03:37 +00:00
this.nodeTouched = false;
},
onTouchEnd() {
this.nodeTouched = true;
},
onKeyDown(event) {
if (event.target === this.$el) {
const rowElement = this.$el;
switch (event.which) {
//down arrow
case 40: {
const nextRow = rowElement.nextElementSibling;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (nextRow) {
nextRow.focus();
}
event.preventDefault();
break;
}
//up arrow
case 38: {
const previousRow = rowElement.previousElementSibling;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (previousRow) {
previousRow.focus();
}
event.preventDefault();
break;
}
//right-left arrows
case 37:
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
case 39: {
if (!this.leaf) {
this.$emit('node-toggle', this.node);
event.preventDefault();
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
break;
}
//enter
case 13: {
this.onClick(event);
event.preventDefault();
break;
}
default:
//no op
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
}
}
},
toggleCheckbox() {
2022-09-14 11:26:01 +00:00
let _selectionKeys = this.selectionKeys ? { ...this.selectionKeys } : {};
2022-09-06 12:03:37 +00:00
const _check = !this.checked;
this.propagateDown(this.node, _check, _selectionKeys);
this.$emit('checkbox-change', {
node: this.node,
check: _check,
selectionKeys: _selectionKeys
});
},
propagateDown(node, check, selectionKeys) {
2022-09-14 11:26:01 +00:00
if (check) selectionKeys[node.key] = { checked: true, partialChecked: false };
else delete selectionKeys[node.key];
2022-09-06 12:03:37 +00:00
if (node.children && node.children.length) {
for (let child of node.children) {
this.propagateDown(child, check, selectionKeys);
}
}
},
propagateUp(event) {
let check = event.check;
2022-09-14 11:26:01 +00:00
let _selectionKeys = { ...event.selectionKeys };
2022-09-06 12:03:37 +00:00
let checkedChildCount = 0;
let childPartialSelected = false;
2022-09-14 11:26:01 +00:00
for (let child of this.node.children) {
if (_selectionKeys[child.key] && _selectionKeys[child.key].checked) checkedChildCount++;
else if (_selectionKeys[child.key] && _selectionKeys[child.key].partialChecked) childPartialSelected = true;
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
if (check && checkedChildCount === this.node.children.length) {
_selectionKeys[this.node.key] = { checked: true, partialChecked: false };
} else {
2022-09-06 12:03:37 +00:00
if (!check) {
delete _selectionKeys[this.node.key];
}
2022-09-14 11:26:01 +00:00
if (childPartialSelected || (checkedChildCount > 0 && checkedChildCount !== this.node.children.length)) _selectionKeys[this.node.key] = { checked: false, partialChecked: true };
else _selectionKeys[this.node.key] = { checked: false, partialChecked: false };
2022-09-06 12:03:37 +00:00
}
this.$emit('checkbox-change', {
node: event.node,
check: event.check,
selectionKeys: _selectionKeys
});
},
onCheckboxChange(event) {
let check = event.check;
2022-09-14 11:26:01 +00:00
let _selectionKeys = { ...event.selectionKeys };
2022-09-06 12:03:37 +00:00
let checkedChildCount = 0;
let childPartialSelected = false;
2022-09-14 11:26:01 +00:00
for (let child of this.node.children) {
if (_selectionKeys[child.key] && _selectionKeys[child.key].checked) checkedChildCount++;
else if (_selectionKeys[child.key] && _selectionKeys[child.key].partialChecked) childPartialSelected = true;
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
if (check && checkedChildCount === this.node.children.length) {
_selectionKeys[this.node.key] = { checked: true, partialChecked: false };
} else {
2022-09-06 12:03:37 +00:00
if (!check) {
delete _selectionKeys[this.node.key];
}
2022-09-14 11:26:01 +00:00
if (childPartialSelected || (checkedChildCount > 0 && checkedChildCount !== this.node.children.length)) _selectionKeys[this.node.key] = { checked: false, partialChecked: true };
else _selectionKeys[this.node.key] = { checked: false, partialChecked: false };
2022-09-06 12:03:37 +00:00
}
this.$emit('checkbox-change', {
node: event.node,
check: event.check,
selectionKeys: _selectionKeys
});
}
},
computed: {
containerClass() {
2022-09-14 11:26:01 +00:00
return [
this.node.styleClass,
{
'p-highlight': this.selected
}
];
2022-09-06 12:03:37 +00:00
},
hasChildren() {
return this.node.children && this.node.children.length > 0;
},
expanded() {
return this.expandedKeys && this.expandedKeys[this.node.key] === true;
},
leaf() {
return this.node.leaf === false ? false : !(this.node.children && this.node.children.length);
},
selected() {
2022-09-14 11:26:01 +00:00
return this.selectionMode && this.selectionKeys ? this.selectionKeys[this.node.key] === true : false;
2022-09-06 12:03:37 +00:00
},
childLevel() {
return this.level + 1;
},
checked() {
2022-09-14 11:26:01 +00:00
return this.selectionKeys ? this.selectionKeys[this.node.key] && this.selectionKeys[this.node.key].checked : false;
2022-09-06 12:03:37 +00:00
},
partialChecked() {
2022-09-14 11:26:01 +00:00
return this.selectionKeys ? this.selectionKeys[this.node.key] && this.selectionKeys[this.node.key].partialChecked : false;
2022-09-06 12:03:37 +00:00
}
},
components: {
2022-09-14 11:26:01 +00:00
TTBodyCell: BodyCell
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>