1 Star 0 Fork 0

博尔塔拉/multipleWindow3dScene

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
WindowManager.js 2.94 KB
一键复制 编辑 原始数据 按行查看 历史
Bjørn Gunnar Staal 提交于 2023-11-23 13:07 . cleanup + comments
class WindowManager
{
#windows;
#count;
#id;
#winData;
#winShapeChangeCallback;
#winChangeCallback;
constructor ()
{
let that = this;
// event listener for when localStorage is changed from another window
addEventListener("storage", (event) =>
{
if (event.key == "windows")
{
let newWindows = JSON.parse(event.newValue);
let winChange = that.#didWindowsChange(that.#windows, newWindows);
that.#windows = newWindows;
if (winChange)
{
if (that.#winChangeCallback) that.#winChangeCallback();
}
}
});
// event listener for when current window is about to ble closed
window.addEventListener('beforeunload', function (e)
{
let index = that.getWindowIndexFromId(that.#id);
//remove this window from the list and update local storage
that.#windows.splice(index, 1);
that.updateWindowsLocalStorage();
});
}
// check if theres any changes to the window list
#didWindowsChange (pWins, nWins)
{
if (pWins.length != nWins.length)
{
return true;
}
else
{
let c = false;
for (let i = 0; i < pWins.length; i++)
{
if (pWins[i].id != nWins[i].id) c = true;
}
return c;
}
}
// initiate current window (add metadata for custom data to store with each window instance)
init (metaData)
{
this.#windows = JSON.parse(localStorage.getItem("windows")) || [];
this.#count= localStorage.getItem("count") || 0;
this.#count++;
this.#id = this.#count;
let shape = this.getWinShape();
this.#winData = {id: this.#id, shape: shape, metaData: metaData};
this.#windows.push(this.#winData);
localStorage.setItem("count", this.#count);
this.updateWindowsLocalStorage();
}
getWinShape ()
{
let shape = {x: window.screenLeft, y: window.screenTop, w: window.innerWidth, h: window.innerHeight};
return shape;
}
getWindowIndexFromId (id)
{
let index = -1;
for (let i = 0; i < this.#windows.length; i++)
{
if (this.#windows[i].id == id) index = i;
}
return index;
}
updateWindowsLocalStorage ()
{
localStorage.setItem("windows", JSON.stringify(this.#windows));
}
update ()
{
//console.log(step);
let winShape = this.getWinShape();
//console.log(winShape.x, winShape.y);
if (winShape.x != this.#winData.shape.x ||
winShape.y != this.#winData.shape.y ||
winShape.w != this.#winData.shape.w ||
winShape.h != this.#winData.shape.h)
{
this.#winData.shape = winShape;
let index = this.getWindowIndexFromId(this.#id);
this.#windows[index].shape = winShape;
//console.log(windows);
if (this.#winShapeChangeCallback) this.#winShapeChangeCallback();
this.updateWindowsLocalStorage();
}
}
setWinShapeChangeCallback (callback)
{
this.#winShapeChangeCallback = callback;
}
setWinChangeCallback (callback)
{
this.#winChangeCallback = callback;
}
getWindows ()
{
return this.#windows;
}
getThisWindowData ()
{
return this.#winData;
}
getThisWindowID ()
{
return this.#id;
}
}
export default WindowManager;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/imlx/multipleWindow3dScene.git
git@gitee.com:imlx/multipleWindow3dScene.git
imlx
multipleWindow3dScene
multipleWindow3dScene
main

搜索帮助

0d507c66 1850385 C8b1a773 1850385