refactor(widgets): migrate Calculator, Timer, ImageRef to event listeners
Replace monkey-patching of WidgetManager.close/minimize/openWidget with WidgetManager.on() event listeners. Eliminates 3-deep closure chain.
This commit is contained in:
+12
-21
@@ -689,41 +689,32 @@ const Calculator = {
|
|||||||
await this.open();
|
await this.open();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close-Event abfangen: WidgetManager.close() ueberschreiben
|
// Widget-Lifecycle-Events
|
||||||
const origClose = WidgetManager.close.bind(WidgetManager);
|
|
||||||
const self = this;
|
const self = this;
|
||||||
WidgetManager.close = function(id) {
|
WidgetManager.on('widget:close', (e) => {
|
||||||
origClose(id);
|
if (e.detail.id === self.WIDGET_ID) {
|
||||||
if (id === self.WIDGET_ID) {
|
|
||||||
self.onClose();
|
self.onClose();
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
// Minimize-Event abfangen
|
WidgetManager.on('widget:minimize', (e) => {
|
||||||
const origMinimize = WidgetManager.minimize.bind(WidgetManager);
|
if (e.detail.id === self.WIDGET_ID) {
|
||||||
WidgetManager.minimize = async function(id) {
|
|
||||||
await origMinimize(id);
|
|
||||||
if (id === self.WIDGET_ID) {
|
|
||||||
self._isOpen = false;
|
self._isOpen = false;
|
||||||
await self.save();
|
self.save();
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
// Open-Event abfangen
|
WidgetManager.on('widget:open', (e) => {
|
||||||
const origOpen = WidgetManager.openWidget.bind(WidgetManager);
|
if (e.detail.id === self.WIDGET_ID) {
|
||||||
WidgetManager.openWidget = async function(id) {
|
|
||||||
await origOpen(id);
|
|
||||||
if (id === self.WIDGET_ID) {
|
|
||||||
self._isOpen = true;
|
self._isOpen = true;
|
||||||
// Body neu rendern (war durch minimize entfernt)
|
|
||||||
const body = WidgetManager.getBody(self.WIDGET_ID);
|
const body = WidgetManager.getBody(self.WIDGET_ID);
|
||||||
if (body && body.children.length === 0) {
|
if (body && body.children.length === 0) {
|
||||||
self.renderBody(body);
|
self.renderBody(body);
|
||||||
}
|
}
|
||||||
const entry = WidgetManager._widgets.get(self.WIDGET_ID);
|
const entry = WidgetManager._widgets.get(self.WIDGET_ID);
|
||||||
if (entry) self._bindKeyboard(entry.el);
|
if (entry) self._bindKeyboard(entry.el);
|
||||||
await self.save();
|
self.save();
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
+15
-24
@@ -460,41 +460,32 @@ const ImageRef = {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close-Event abfangen
|
// Widget-Lifecycle-Events
|
||||||
const self = this;
|
const self = this;
|
||||||
const prevClose = WidgetManager.close;
|
WidgetManager.on('widget:close', (e) => {
|
||||||
WidgetManager.close = function(id) {
|
const isImage = self._images.some(img => img.id === e.detail.id);
|
||||||
prevClose.call(WidgetManager, id);
|
|
||||||
// Pruefen ob es ein Image-Widget ist
|
|
||||||
const isImage = self._images.some(img => img.id === id);
|
|
||||||
if (isImage) {
|
if (isImage) {
|
||||||
self.onClose(id);
|
self.onClose(e.detail.id);
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
// Minimize-Event abfangen
|
WidgetManager.on('widget:minimize', (e) => {
|
||||||
const prevMinimize = WidgetManager.minimize;
|
const isImage = self._images.some(img => img.id === e.detail.id);
|
||||||
WidgetManager.minimize = async function(id) {
|
|
||||||
await prevMinimize.call(WidgetManager, id);
|
|
||||||
const isImage = self._images.some(img => img.id === id);
|
|
||||||
if (isImage) {
|
if (isImage) {
|
||||||
await self.save();
|
self.save();
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
// Open-Event abfangen
|
WidgetManager.on('widget:open', (e) => {
|
||||||
const prevOpen = WidgetManager.openWidget;
|
const imgData = self._images.find(img => img.id === e.detail.id);
|
||||||
WidgetManager.openWidget = async function(id) {
|
|
||||||
await prevOpen.call(WidgetManager, id);
|
|
||||||
const imgData = self._images.find(img => img.id === id);
|
|
||||||
if (imgData) {
|
if (imgData) {
|
||||||
const body = WidgetManager.getBody(id);
|
const body = WidgetManager.getBody(e.detail.id);
|
||||||
if (body && body.children.length === 0) {
|
if (body && body.children.length === 0) {
|
||||||
const dataUrl = self._getSessionImage(id);
|
const dataUrl = self._getSessionImage(e.detail.id);
|
||||||
self.renderBody(imgData, body, dataUrl);
|
self.renderBody(imgData, body, dataUrl);
|
||||||
}
|
}
|
||||||
await self.save();
|
self.save();
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
+12
-21
@@ -720,32 +720,23 @@ const Timer = {
|
|||||||
await this.open();
|
await this.open();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close-Event abfangen
|
// Widget-Lifecycle-Events
|
||||||
const origClose = WidgetManager.close.bind(WidgetManager);
|
|
||||||
const self = this;
|
const self = this;
|
||||||
const prevClose = WidgetManager.close;
|
WidgetManager.on('widget:close', (e) => {
|
||||||
WidgetManager.close = function(id) {
|
if (e.detail.id === self.WIDGET_ID) {
|
||||||
prevClose.call(WidgetManager, id);
|
|
||||||
if (id === self.WIDGET_ID) {
|
|
||||||
self.onClose();
|
self.onClose();
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
// Minimize-Event abfangen
|
WidgetManager.on('widget:minimize', (e) => {
|
||||||
const prevMinimize = WidgetManager.minimize;
|
if (e.detail.id === self.WIDGET_ID) {
|
||||||
WidgetManager.minimize = async function(id) {
|
|
||||||
await prevMinimize.call(WidgetManager, id);
|
|
||||||
if (id === self.WIDGET_ID) {
|
|
||||||
self._isOpen = false;
|
self._isOpen = false;
|
||||||
await self.save();
|
self.save();
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
// Open-Event abfangen
|
WidgetManager.on('widget:open', (e) => {
|
||||||
const prevOpen = WidgetManager.openWidget;
|
if (e.detail.id === self.WIDGET_ID) {
|
||||||
WidgetManager.openWidget = async function(id) {
|
|
||||||
await prevOpen.call(WidgetManager, id);
|
|
||||||
if (id === self.WIDGET_ID) {
|
|
||||||
self._isOpen = true;
|
self._isOpen = true;
|
||||||
const body = WidgetManager.getBody(self.WIDGET_ID);
|
const body = WidgetManager.getBody(self.WIDGET_ID);
|
||||||
if (body && body.children.length === 0) {
|
if (body && body.children.length === 0) {
|
||||||
@@ -753,8 +744,8 @@ const Timer = {
|
|||||||
}
|
}
|
||||||
const entry = WidgetManager._widgets.get(self.WIDGET_ID);
|
const entry = WidgetManager._widgets.get(self.WIDGET_ID);
|
||||||
if (entry) self._bindKeyboard(entry.el);
|
if (entry) self._bindKeyboard(entry.el);
|
||||||
await self.save();
|
self.save();
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user