Files
openmct/src/ui/components/controls/pane.vue
2018-08-15 13:52:34 -07:00

97 lines
3.0 KiB
Vue

<template>
<div class="multipane__pane"
:class="{
'multipane__pane--collapsed': collapsed
}">
<div v-if="splitter"
class="multipane__splitter"
:class="{
'multipane__splitter--before': splitter === 'before',
'multipane__splitter--after': splitter === 'after'
}"
@mousedown="start"
>
<a class="multipane__splitter__button"
@click="toggleCollapse"
v-if="collapsable"></a>
</div>
<slot></slot>
</div>
</template>
<script>
export default {
props: {
splitter: {
type: String,
validator: function (value) {
return ['before', 'after'].indexOf(value) !== -1;
}
},
collapsable: Boolean
},
data() {
return {
collapsed: false
}
},
mounted() {
this.type = this.$parent.type;
if (this.type === 'horizontal') {
this.styleProp = 'width';
} else {
this.styleProp = 'height';
}
this.trackSize();
},
methods: {
toggleCollapse: function () {
this.collapsed = !this.collapsed;
if (this.collapsed) {
this.currentSize = this.$el.style[this.styleProp];
this.$el.style[this.styleProp] = '';
} else {
this.$el.style[this.styleProp] = this.currentSize;
delete this.currentSize;
}
},
trackSize: function() {
if (this.type === 'vertical') {
this.initial = this.$el.offsetHeight;
} else if (this.type === 'horizontal') {
this.initial = this.$el.offsetWidth;
}
},
getPosition: function (event) {
return this.type === 'horizontal' ?
event.pageX :
event.pageY;
},
getNewSize: function (event) {
let delta = this.startPosition - this.getPosition(event);
if (this.splitter === "before") {
return `${this.initial + delta}px`;
}
if (this.splitter === "after") {
return `${this.initial - delta}px`;
}
},
updatePosition: function (event) {
let size = this.getNewSize(event);
this.$el.style[this.styleProp] = size;
},
start: function (event) {
this.startPosition = this.getPosition(event);
this.trackSize();
document.body.addEventListener('mousemove', this.updatePosition);
document.body.addEventListener('mouseup', this.end);
},
end: function (event) {
document.body.removeEventListener('mousemove', this.updatePosition);
document.body.removeEventListener('mouseup', this.end);
this.trackSize();
}
}
}
</script>