refactor: window.vue to composition api

This commit is contained in:
Norm 2022-08-19 19:10:06 -04:00
parent aa144e803c
commit 7b33ee807e
Signed by untrusted user: norm
GPG key ID: 7123E30E441E80DE

View file

@ -1,7 +1,7 @@
<template> <template>
<transition :name="$store.state.animation ? 'window' : ''" appear @after-leave="$emit('closed')"> <transition :name="$store.state.animation ? 'window' : ''" appear @after-leave="emit('closed')">
<div v-if="showing" class="ebkgocck"> <div v-if="showing" ref="main" class="ebkgocck">
<div class="body _shadow _narrow_" @mousedown="onBodyMousedown" @keydown="onKeydown"> <div class="body _shadow _narrow_" @mousedown="moveToTop" @keydown="onKeydown">
<div class="header" :class="{ mini }" @contextmenu.prevent.stop="onContextmenu"> <div class="header" :class="{ mini }" @contextmenu.prevent.stop="onContextmenu">
<span class="left"> <span class="left">
<button v-for="button in buttonsLeft" v-tooltip="button.title" class="button _button" :class="{ highlighted: button.highlighted }" @click="button.onClick"><i :class="button.icon"></i></button> <button v-for="button in buttonsLeft" v-tooltip="button.title" class="button _button" :class="{ highlighted: button.highlighted }" @click="button.onClick"><i :class="button.icon"></i></button>
@ -32,148 +32,121 @@
</transition> </transition>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent } from 'vue'; import { onMounted, onUnmounted, provide } from 'vue';
import contains from '@/scripts/contains'; import contains from '@/scripts/contains';
import * as os from '@/os'; import * as os from '@/os';
import { MenuItem } from '@/types/menu';
const minHeight = 50; const minHeight = 50;
const minWidth = 250; const minWidth = 250;
function dragListen(fn) { function addMouseListener(fn: (this: Window, ev: MouseEvent) => void): void {
window.addEventListener('mousemove', fn); window.addEventListener('mousemove', fn);
window.addEventListener('touchmove', fn); window.addEventListener('mouseleave', removeMouseListener.bind(null, fn));
window.addEventListener('mouseleave', dragClear.bind(null, fn)); window.addEventListener('mouseup', removeMouseListener.bind(null, fn));
window.addEventListener('mouseup', dragClear.bind(null, fn));
window.addEventListener('touchend', dragClear.bind(null, fn));
} }
function dragClear(fn) { function removeMouseListener(fn: (this: Window, ev: MouseEvent) => void): void {
window.removeEventListener('mousemove', fn); window.removeEventListener('mousemove', fn);
window.removeEventListener('touchmove', fn); window.removeEventListener('mouseleave', removeMouseListener.bind(null, fn));
window.removeEventListener('mouseleave', dragClear); window.removeEventListener('mouseup', removeMouseListener.bind(null, fn));
window.removeEventListener('mouseup', dragClear);
window.removeEventListener('touchend', dragClear);
} }
export default defineComponent({ function addTouchListener(fn: (this: Window, ev: TouchEvent) => void): void {
provide: { window.addEventListener('touchmove', fn);
inWindow: true, window.addEventListener('touchend', removeTouchListener.bind(null, fn));
}, }
props: { function removeTouchListener(fn: (this: Window, ev: TouchEvent) => void): void {
initialWidth: { window.removeEventListener('touchmove', fn);
type: Number, window.removeEventListener('touchend', removeTouchListener.bind(null, fn));
required: false, }
default: 400,
},
initialHeight: {
type: Number,
required: false,
default: null,
},
canResize: {
type: Boolean,
required: false,
default: false,
},
closeButton: {
type: Boolean,
required: false,
default: true,
},
mini: {
type: Boolean,
required: false,
default: false,
},
front: {
type: Boolean,
required: false,
default: false,
},
contextmenu: {
type: Array,
required: false,
},
buttonsLeft: {
type: Array,
required: false,
default: () => [],
},
buttonsRight: {
type: Array,
required: false,
default: () => [],
},
},
emits: ['closed'], const props = withDefaults(defineProps<{
initialWidth?: number;
initialHeight?: number;
canResize?: boolean;
closeButton?: boolean;
mini?: boolean;
front?: boolean;
contextmenu?: MenuItem[];
buttonsLeft?: any[];
buttonsRight?: any[];
}>(), {
initialWidth: 400,
canResize: false,
closeButton: true,
mini: false,
front: false,
contextmenu: () => [] as MenuItem[],
buttonsLeft: () => [],
buttonsRight: () => [],
});
data() { const emit = defineEmits<{
return { (ev: 'closed'): void;
showing: true, }>();
id: Math.random().toString(), // TODO: UUID
};
},
mounted() { provide('inWindow', true);
if (this.initialWidth) this.applyTransformWidth(this.initialWidth);
if (this.initialHeight) this.applyTransformHeight(this.initialHeight);
this.applyTransformTop((window.innerHeight / 2) - (this.$el.offsetHeight / 2)); let showing = $ref(true);
this.applyTransformLeft((window.innerWidth / 2) - (this.$el.offsetWidth / 2)); let main: HTMLElement = $ref();
onMounted(() => {
if (props.initialWidth) applyTransformWidth(props.initialWidth);
if (props.initialHeight) applyTransformHeight(props.initialHeight);
applyTransformTop((window.innerHeight / 2) - (main.offsetHeight / 2));
applyTransformLeft((window.innerWidth / 2) - (main.offsetWidth / 2));
// //
this.top(); moveToTop();
window.addEventListener('resize', this.onBrowserResize); window.addEventListener('resize', onBrowserResize);
}, });
unmounted() { onUnmounted(() => {
window.removeEventListener('resize', this.onBrowserResize); window.removeEventListener('resize', onBrowserResize);
}, });
methods: { function close(): void {
close() { showing = false;
this.showing = false; }
},
onKeydown(evt) { function onKeydown(evt: KeyboardEvent): void {
if (evt.which === 27) { // Esc if (evt.key === 'Esc') {
evt.preventDefault(); evt.preventDefault();
evt.stopPropagation(); evt.stopPropagation();
this.close(); close();
} }
}, }
onContextmenu(ev: MouseEvent) { function onContextmenu(ev: MouseEvent): void {
if (this.contextmenu) { if (props.contextmenu) {
os.contextMenu(this.contextmenu, ev); os.contextMenu(props.contextmenu, ev);
} }
}, }
// function moveToTop(): void {
top() { main.style.zIndex = os.claimZIndex(props.front ? 'middle' : 'low').toString();
(this.$el as any).style.zIndex = os.claimZIndex(this.front ? 'middle' : 'low'); }
},
onBodyMousedown() { function getClickPos(evt: MouseEvent | TouchEvent): [number, number] {
this.top(); if (evt instanceof MouseEvent) {
}, return [evt.clientX, evt.clientY];
}
return [evt.touches[0].clientX, evt.touches[0].clientY];
}
onHeaderMousedown(evt: MouseEvent) { function onHeaderMousedown(evt: MouseEvent | TouchEvent): void {
// // Right-click ignored as it is likely to have attempted to open a context menu
if (evt.button === 2) return; if (evt instanceof MouseEvent && evt.button === 2) return;
const main = this.$el as any;
if (!contains(main, document.activeElement)) main.focus(); if (!contains(main, document.activeElement)) main.focus();
const position = main.getBoundingClientRect(); const position = main.getBoundingClientRect();
const clickX = evt.touches && evt.touches.length > 0 ? evt.touches[0].clientX : evt.clientX; const [clickX, clickY] = getClickPos(evt);
const clickY = evt.touches && evt.touches.length > 0 ? evt.touches[0].clientY : evt.clientY;
const moveBaseX = clickX - position.left; const moveBaseX = clickX - position.left;
const moveBaseY = clickY - position.top; const moveBaseY = clickY - position.top;
const browserWidth = window.innerWidth; const browserWidth = window.innerWidth;
@ -182,9 +155,8 @@ export default defineComponent({
const windowHeight = main.offsetHeight; const windowHeight = main.offsetHeight;
// //
dragListen(me => { const listener = (me: MouseEvent | TouchEvent): void => {
const x = me.touches && me.touches.length > 0 ? me.touches[0].clientX : me.clientX; const [x, y] = getClickPos(me);
const y = me.touches && me.touches.length > 0 ? me.touches[0].clientY : me.clientY;
let moveLeft = x - moveBaseX; let moveLeft = x - moveBaseX;
let moveTop = y - moveBaseY; let moveTop = y - moveBaseY;
@ -201,169 +173,164 @@ export default defineComponent({
// //
if (moveLeft + windowWidth > browserWidth) moveLeft = browserWidth - windowWidth; if (moveLeft + windowWidth > browserWidth) moveLeft = browserWidth - windowWidth;
this.$el.style.left = moveLeft + 'px'; main.style.left = moveLeft + 'px';
this.$el.style.top = moveTop + 'px'; main.style.top = moveTop + 'px';
}); };
},
// if (evt instanceof MouseEvent) {
onTopHandleMousedown(evt) { addMouseListener((me: MouseEvent) => listener(me));
const main = this.$el as any; } else {
addTouchListener((me: TouchEvent) => listener(me));
}
}
//
function onTopHandleMousedown(evt: MouseEvent): void {
const base = evt.clientY; const base = evt.clientY;
const height = parseInt(getComputedStyle(main, '').height, 10); const height = parseInt(getComputedStyle(main, '').height, 10);
const top = parseInt(getComputedStyle(main, '').top, 10); const top = parseInt(getComputedStyle(main, '').top, 10);
// //
dragListen(me => { addMouseListener((me: MouseEvent) => {
const move = me.clientY - base; const move = me.clientY - base;
if (top + move > 0) { if (top + move > 0) {
if (height + -move > minHeight) { if (height + -move > minHeight) {
this.applyTransformHeight(height + -move); applyTransformHeight(height + -move);
this.applyTransformTop(top + move); applyTransformTop(top + move);
} else { // } else { //
this.applyTransformHeight(minHeight); applyTransformHeight(minHeight);
this.applyTransformTop(top + (height - minHeight)); applyTransformTop(top + (height - minHeight));
} }
} else { // } else { //
this.applyTransformHeight(top + height); applyTransformHeight(top + height);
this.applyTransformTop(0); applyTransformTop(0);
} }
}); });
}, }
//
onRightHandleMousedown(evt) {
const main = this.$el as any;
//
function onRightHandleMousedown(evt: MouseEvent): void {
const base = evt.clientX; const base = evt.clientX;
const width = parseInt(getComputedStyle(main, '').width, 10); const width = parseInt(getComputedStyle(main, '').width, 10);
const left = parseInt(getComputedStyle(main, '').left, 10); const left = parseInt(getComputedStyle(main, '').left, 10);
const browserWidth = window.innerWidth; const browserWidth = window.innerWidth;
// //
dragListen(me => { addMouseListener((me: MouseEvent) => {
const move = me.clientX - base; const move = me.clientX - base;
if (left + width + move < browserWidth) { if (left + width + move < browserWidth) {
if (width + move > minWidth) { if (width + move > minWidth) {
this.applyTransformWidth(width + move); applyTransformWidth(width + move);
} else { // } else { //
this.applyTransformWidth(minWidth); applyTransformWidth(minWidth);
} }
} else { // } else { //
this.applyTransformWidth(browserWidth - left); applyTransformWidth(browserWidth - left);
} }
}); });
}, }
//
onBottomHandleMousedown(evt) {
const main = this.$el as any;
//
function onBottomHandleMousedown(evt: MouseEvent): void {
const base = evt.clientY; const base = evt.clientY;
const height = parseInt(getComputedStyle(main, '').height, 10); const height = parseInt(getComputedStyle(main, '').height, 10);
const top = parseInt(getComputedStyle(main, '').top, 10); const top = parseInt(getComputedStyle(main, '').top, 10);
const browserHeight = window.innerHeight; const browserHeight = window.innerHeight;
// //
dragListen(me => { addMouseListener((me: MouseEvent) => {
const move = me.clientY - base; const move = me.clientY - base;
if (top + height + move < browserHeight) { if (top + height + move < browserHeight) {
if (height + move > minHeight) { if (height + move > minHeight) {
this.applyTransformHeight(height + move); applyTransformHeight(height + move);
} else { // } else { //
this.applyTransformHeight(minHeight); applyTransformHeight(minHeight);
} }
} else { // } else { //
this.applyTransformHeight(browserHeight - top); applyTransformHeight(browserHeight - top);
} }
}); });
}, }
//
onLeftHandleMousedown(evt) {
const main = this.$el as any;
//
function onLeftHandleMousedown(evt: MouseEvent): void {
const base = evt.clientX; const base = evt.clientX;
const width = parseInt(getComputedStyle(main, '').width, 10); const width = parseInt(getComputedStyle(main, '').width, 10);
const left = parseInt(getComputedStyle(main, '').left, 10); const left = parseInt(getComputedStyle(main, '').left, 10);
// //
dragListen(me => { addMouseListener((me: MouseEvent) => {
const move = me.clientX - base; const move = me.clientX - base;
if (left + move > 0) { if (left + move > 0) {
if (width + -move > minWidth) { if (width + -move > minWidth) {
this.applyTransformWidth(width + -move); applyTransformWidth(width + -move);
this.applyTransformLeft(left + move); applyTransformLeft(left + move);
} else { // } else { //
this.applyTransformWidth(minWidth); applyTransformWidth(minWidth);
this.applyTransformLeft(left + (width - minWidth)); applyTransformLeft(left + (width - minWidth));
} }
} else { // } else { //
this.applyTransformWidth(left + width); applyTransformWidth(left + width);
this.applyTransformLeft(0); applyTransformLeft(0);
} }
}); });
}, }
// //
onTopLeftHandleMousedown(evt) { function onTopLeftHandleMousedown(evt: MouseEvent): void {
this.onTopHandleMousedown(evt); onTopHandleMousedown(evt);
this.onLeftHandleMousedown(evt); onLeftHandleMousedown(evt);
}, }
// //
onTopRightHandleMousedown(evt) { function onTopRightHandleMousedown(evt: MouseEvent): void {
this.onTopHandleMousedown(evt); onTopHandleMousedown(evt);
this.onRightHandleMousedown(evt); onRightHandleMousedown(evt);
}, }
// //
onBottomRightHandleMousedown(evt) { function onBottomRightHandleMousedown(evt: MouseEvent): void {
this.onBottomHandleMousedown(evt); onBottomHandleMousedown(evt);
this.onRightHandleMousedown(evt); onRightHandleMousedown(evt);
}, }
// //
onBottomLeftHandleMousedown(evt) { function onBottomLeftHandleMousedown(evt: MouseEvent): void {
this.onBottomHandleMousedown(evt); onBottomHandleMousedown(evt);
this.onLeftHandleMousedown(evt); onLeftHandleMousedown(evt);
}, }
// //
applyTransformHeight(height) { function applyTransformHeight(height: number): void {
(this.$el as any).style.height = Math.min(height, window.innerHeight) + 'px'; main.style.height = Math.min(height, window.innerHeight) + 'px';
}, }
// //
applyTransformWidth(width) { function applyTransformWidth(width: number): void {
(this.$el as any).style.width = Math.min(width, window.innerWidth) + 'px'; main.style.width = Math.min(width, window.innerWidth) + 'px';
}, }
// Y // Y
applyTransformTop(top) { function applyTransformTop(top: number): void {
(this.$el as any).style.top = top + 'px'; main.style.top = top + 'px';
}, }
// X // X
applyTransformLeft(left) { function applyTransformLeft(left: number): void {
(this.$el as any).style.left = left + 'px'; main.style.left = left + 'px';
}, }
onBrowserResize() { function onBrowserResize(): void {
const main = this.$el as any;
const position = main.getBoundingClientRect(); const position = main.getBoundingClientRect();
const browserWidth = window.innerWidth; const browserWidth = window.innerWidth;
const browserHeight = window.innerHeight; const browserHeight = window.innerHeight;
const windowWidth = main.offsetWidth; const windowWidth = main.offsetWidth;
const windowHeight = main.offsetHeight; const windowHeight = main.offsetHeight;
if (position.left < 0) main.style.left = 0; // if (position.left < 0) main.style.left = '0'; //
if (position.top + windowHeight > browserHeight) main.style.top = browserHeight - windowHeight + 'px'; // if (position.top + windowHeight > browserHeight) main.style.top = (browserHeight - windowHeight) + 'px'; //
if (position.left + windowWidth > browserWidth) main.style.left = browserWidth - windowWidth + 'px'; // if (position.left + windowWidth > browserWidth) main.style.left = (browserWidth - windowWidth) + 'px'; //
if (position.top < 0) main.style.top = 0; // if (position.top < 0) main.style.top = '0'; //
}, }
},
});
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>