-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindow.js
119 lines (98 loc) · 2.67 KB
/
window.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
class WindowManager {
#windows;
#count;
#id;
#winData;
#winShapeChangeCallback;
#winChangeCallback;
constructor() {
let that = this;
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();
}
}
});
window.addEventListener("beforeunload", function (e) {
let index = that.getWindowIndexFromId(that.#id);
that.#windows.splice(index, 1);
that.updateWindowsLocalStorage();
});
}
#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;
}
}
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() {
let winShape = this.getWinShape();
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;
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;