代码拉取完成,页面将自动刷新
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;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。