primevue-mirror/components/lib/treetable/BodyCell.vue

206 lines
7.3 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-05-11 08:11:49 +00:00
<td :style="containerStyle" :class="containerClass" role="cell" v-bind="{ ...getColumnPTOptions('root'), ...getColumnPTOptions('bodyCell') }">
<button v-if="columnProp('expander')" v-ripple type="button" class="p-treetable-toggler p-link" @click="toggle" :style="togglerStyle" tabindex="-1" v-bind="getColumnPTOptions('rowToggler')">
<component v-if="templates['togglericon']" :is="templates['togglericon']" :node="node" :expanded="expanded" class="p-tree-toggler-icon" />
2023-05-11 08:11:49 +00:00
<component v-else-if="expanded" :is="node.expandedIcon ? 'span' : 'ChevronDownIcon'" class="p-tree-toggler-icon" v-bind="getColumnPTOptions('rowTogglerIcon')" />
<component v-else :is="node.collapsedIcon ? 'span' : 'ChevronRightIcon'" class="p-tree-toggler-icon" v-bind="getColumnPTOptions('rowTogglerIcon')" />
2022-09-06 12:03:37 +00:00
</button>
2023-05-11 08:11:49 +00:00
<div v-if="checkboxSelectionMode && columnProp('expander')" :class="['p-checkbox p-treetable-checkbox p-component', { 'p-checkbox-focused': checkboxFocused }]" @click="toggleCheckbox" v-bind="getColumnPTOptions('checkboxWrapper')">
<div class="p-hidden-accessible" v-bind="getColumnPTOptions('hiddenInputWrapper')">
<input type="checkbox" @focus="onCheckboxFocus" @blur="onCheckboxBlur" tabindex="-1" v-bind="getColumnPTOptions('hiddenInput')" />
2022-09-06 12:03:37 +00:00
</div>
2023-05-11 08:11:49 +00:00
<div ref="checkboxEl" :class="checkboxClass" v-bind="getColumnCheckboxPTOptions('checkbox')">
<component v-if="templates['checkboxicon']" :is="templates['checkboxicon']" :checked="checked" :partialChecked="partialChecked" class="p-checkbox-icon" />
2023-05-11 08:11:49 +00:00
<component v-else :is="checked ? 'CheckIcon' : partialChecked ? 'MinusIcon' : null" class="p-checkbox-icon" v-bind="getColumnCheckboxPTOptions('checkboxIcon')" />
2022-09-06 12:03:37 +00:00
</div>
</div>
2022-09-14 11:26:01 +00:00
<component v-if="column.children && column.children.body" :is="column.children.body" :node="node" :column="column" />
<template v-else>
2023-05-11 08:11:49 +00:00
<span v-bind="getColumnPTOptions('cellContent')">{{ resolveFieldData(node.data, columnProp('field')) }}</span>
</template>
2022-09-06 12:03:37 +00:00
</td>
</template>
<script>
2023-05-11 08:11:49 +00:00
import BaseComponent from 'primevue/basecomponent';
import CheckIcon from 'primevue/icons/check';
import ChevronDownIcon from 'primevue/icons/chevrondown';
import ChevronRightIcon from 'primevue/icons/chevronright';
import MinusIcon from 'primevue/icons/minus';
2022-09-14 11:26:01 +00:00
import Ripple from 'primevue/ripple';
2022-12-08 11:04:25 +00:00
import { DomHandler, ObjectUtils } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
export default {
name: 'BodyCell',
2023-05-11 08:11:49 +00:00
extends: BaseComponent,
2022-09-14 11:26:01 +00:00
emits: ['node-toggle', 'checkbox-toggle'],
2022-09-06 12:03:37 +00:00
props: {
node: {
type: Object,
default: null
},
column: {
type: Object,
default: null
},
level: {
type: Number,
default: 0
},
indentation: {
type: Number,
default: 1
},
leaf: {
type: Boolean,
default: false
},
expanded: {
type: Boolean,
default: false
},
selectionMode: {
type: String,
default: null
},
checked: {
type: Boolean,
default: false
},
partialChecked: {
type: Boolean,
default: false
},
templates: {
type: Object,
default: null
2022-09-06 12:03:37 +00:00
}
},
data() {
return {
styleObject: {},
checkboxFocused: false
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
mounted() {
if (this.columnProp('frozen')) {
this.updateStickyPosition();
}
},
updated() {
if (this.columnProp('frozen')) {
this.updateStickyPosition();
}
},
methods: {
toggle() {
this.$emit('node-toggle', this.node);
},
columnProp(prop) {
return ObjectUtils.getVNodeProp(this.column, prop);
},
2023-05-11 08:11:49 +00:00
getColumnPTOptions(key) {
return this.ptmo(this.getColumnProp(), key, {
props: this.column.props,
parent: {
props: this.$props,
state: this.$data
}
});
},
getColumnCheckboxPTOptions(key) {
return this.ptmo(this.getColumnProp(), key, {
props: this.column.props,
parent: {
props: this.$props,
state: this.$data
},
context: {
checked: this.checked,
focused: this.checkboxFocused,
partialChecked: this.partialChecked
}
});
},
getColumnProp() {
return this.column.props && this.column.props.pt ? this.column.props.pt : undefined; //@todo
},
2022-09-06 12:03:37 +00:00
updateStickyPosition() {
if (this.columnProp('frozen')) {
let align = this.columnProp('alignFrozen');
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (align === 'right') {
let right = 0;
let next = this.$el.nextElementSibling;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (next) {
right = DomHandler.getOuterWidth(next) + parseFloat(next.style.right || 0);
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.styleObject.right = right + 'px';
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
let left = 0;
let prev = this.$el.previousElementSibling;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (prev) {
left = DomHandler.getOuterWidth(prev) + parseFloat(prev.style.left || 0);
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.styleObject.left = left + 'px';
}
}
},
resolveFieldData(rowData, field) {
return ObjectUtils.resolveFieldData(rowData, field);
},
toggleCheckbox() {
this.$emit('checkbox-toggle');
},
onCheckboxFocus() {
this.checkboxFocused = true;
},
onCheckboxBlur() {
this.checkboxFocused = false;
}
},
computed: {
containerClass() {
2022-09-14 11:26:01 +00:00
return [
this.columnProp('bodyClass'),
this.columnProp('class'),
{
'p-frozen-column': this.columnProp('frozen')
}
];
2022-09-06 12:03:37 +00:00
},
containerStyle() {
let bodyStyle = this.columnProp('bodyStyle');
let columnStyle = this.columnProp('style');
2022-09-14 11:26:01 +00:00
return this.columnProp('frozen') ? [columnStyle, bodyStyle, this.styleObject] : [columnStyle, bodyStyle];
2022-09-06 12:03:37 +00:00
},
togglerStyle() {
return {
marginLeft: this.level * this.indentation + 'rem',
visibility: this.leaf ? 'hidden' : 'visible'
};
},
checkboxSelectionMode() {
return this.selectionMode === 'checkbox';
},
checkboxClass() {
2022-09-14 11:26:01 +00:00
return ['p-checkbox-box', { 'p-highlight': this.checked, 'p-focus': this.checkboxFocused, 'p-indeterminate': this.partialChecked }];
2022-09-06 12:03:37 +00:00
}
},
components: {
ChevronRightIcon: ChevronRightIcon,
ChevronDownIcon: ChevronDownIcon,
CheckIcon: CheckIcon,
MinusIcon: MinusIcon
},
2022-09-06 12:03:37 +00:00
directives: {
2022-09-14 11:26:01 +00:00
ripple: Ripple
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>