hippie/source/code/windows.js

207 lines
4.7 KiB
JavaScript
Raw Normal View History

class HippieTaskBar {
constructor(element, placeholder, options) {
this.element = element;
this.placeholder = placeholder;
this.date = null;
this.isDragging = false;
this.barSize = '';
// TODO: Erweitern auf allgemeine Möglichkeiten und dann aus JSON-Datei laden
this.options = options || {
direction: 0,
date: {
year: 'numeric',
month: '2-digit',
day: '2-digit'
}
};
this.init();
}
// TODO: Ereignisse besser delegieren
init() {
this.element.addEventListener('mousedown', this.onMouseDown.bind(this));
document.addEventListener('mousemove', this.onMouseMove.bind(this));
document.addEventListener('mouseup', this.onMouseUp.bind(this));
const dateElement = document.createElement('span');
dateElement.id = 'date';
this.element.querySelector('.clock').appendChild(dateElement);
this.date = new DateDisplay(dateElement, this.options.date);
this.setOptions('bottom');
}
onMouseDown(event) {
if (checkButtonAndTarget(event, this.element, 0)) {
console.debug('Drag mode enabled');
this.isDragging = true;
this.showPlaceholder();
centerElementUnderCursor(event, this.placeholder);
}
event.preventDefault();
}
onMouseMove(event) {
if (this.isDragging) {
if (!this.isDragging) return;
const closestEdge = getClosestEdge(this.placeholder);
2025-11-02 13:12:04 +01:00
const borderRadius = '4px';
const attributes = {
top: {
2025-11-02 13:12:04 +01:00
className: 'top',
styles: {
2025-11-02 13:12:04 +01:00
flexDirection: 'row',
borderStyle: '',
borderColor: '',
borderTopStyle: 'solid',
borderTopColor: 'white',
borderTopRightRadius: '',
borderBottomRightRadius: borderRadius,
borderBottomLeftRadius: borderRadius,
2025-11-02 13:12:04 +01:00
borderTopLeftRadius: ''
}
},
right: {
2025-11-02 13:12:04 +01:00
className: 'right',
styles: {
2025-11-02 13:12:04 +01:00
flexDirection: 'column',
borderStyle: '',
borderColor: '',
borderRightStyle: 'solid',
borderRightColor: 'white',
borderTopRightRadius: '',
borderBottomRightRadius: '',
borderBottomLeftRadius: borderRadius,
2025-11-02 13:12:04 +01:00
borderTopLeftRadius: borderRadius
}
},
bottom: {
2025-11-02 13:12:04 +01:00
className: 'bottom',
styles: {
2025-11-02 13:12:04 +01:00
flexDirection: 'row',
borderStyle: '',
borderColor: '',
borderBottomStyle: 'solid',
borderBottomColor: 'white',
borderTopRightRadius: borderRadius,
borderBottomRightRadius: '',
borderBottomLeftRadius: '',
2025-11-02 13:12:04 +01:00
borderTopLeftRadius: borderRadius
}
},
left: {
2025-11-02 13:12:04 +01:00
className: 'left',
styles: {
2025-11-02 13:12:04 +01:00
flexDirection: 'column',
borderStyle: '',
borderColor: '',
borderLeftStyle: 'solid',
borderLeftColor: 'white',
borderTopRightRadius: borderRadius,
borderBottomRightRadius: borderRadius,
borderBottomLeftRadius: '',
2025-11-02 13:12:04 +01:00
borderTopLeftRadius: ''
}
}
};
setAttributesAccordingToPosition(this.placeholder, closestEdge, attributes);
centerElementUnderCursor(event, this.placeholder);
}
}
onMouseUp() {
if (event.target === this.placeholder) {
console.debug('Drag mode disabled');
if (!this.isDragging) return;
this.isDragging = false;
this.snapToEdges();
this.hidePlaceholder();
}
}
showPlaceholder() {
this.element.style.display = 'none';
this.placeholder.style.display = 'flex';
}
hidePlaceholder() {
this.placeholder.style.display = 'none';
this.element.style.display = '';
}
// TODO: Prüfung auf Ziel auslagern und schon bei Mausbewegung verfügbar machen
snapToEdges() {
const closestEdge = getClosestEdge(this.placeholder);
this.setOptions(closestEdge);
this.date.changeFormat(this.options.date, this.options.direction);
}
setOptions(position) {
const attributes = {
top: {
className: 'top',
styles: {
top: '0', right: '0', bottom: '', left: '0',
width: '', height: this.barSize,
flexDirection: 'row'
}
},
right: {
className: 'right',
styles: {
top: '0', right: '0', bottom: '0', left: '',
width: this.barSize, height: '',
flexDirection: 'column'
}
},
bottom: {
className: 'bottom',
styles: {
top: '', right: '0', bottom: '0', left: '0',
width: '', height: this.barSize,
flexDirection: 'row'
}
},
left: {
className: 'left',
styles: {
top: '0',
right: '',
bottom: '0',
left: '0',
width: this.barSize,
height: '',
flexDirection: 'column'
}
}
};
setAttributesAccordingToPosition(this.element, position, attributes);
switch (position) {
case 'right':
case 'left':
this.options.date = {year: '2-digit', month: '2-digit', day: '2-digit'};
this.options.direction = 1;
break;
case 'top':
case 'bottom':
default:
this.options.date = {year: 'numeric', month: '2-digit', day: '2-digit'};
this.options.direction = 0;
break;
}
}
}