-
Notifications
You must be signed in to change notification settings - Fork 0
/
edirom-windows.js
211 lines (172 loc) · 6.73 KB
/
edirom-windows.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
class EdiromWindows extends HTMLElement {
constructor() {
super();
// Define the default properties
this.windows = [];
this.windowsDefaults = {
border: "0.3em",
background: "#ccc"
}
// Create shadow DOM
this.attachShadow({ mode: 'open' });
// Load the winbox library
const winboxScript = document.createElement('script');
winboxScript.src = "https://rawcdn.githack.com/daniel-jettka/winbox/0.2.82/dist/js/winbox.min.js";
winboxScript.defer = true;
this.shadowRoot.appendChild(winboxScript);
// Add the winbox css
const winboxCss = document.createElement('link');
winboxCss.rel = "stylesheet";
winboxCss.href = "https://rawcdn.githack.com/daniel-jettka/winbox/0.2.82/dist/css/winbox.min.css";
this.shadowRoot.appendChild(winboxCss);
// When the winbox library is loaded
winboxScript.onload = () => {
// Loop through the windows array
for (var i = 0; i < this.windows.length; i++) {
// Add the default properties
for (var key in this.windowsDefaults) {
if (!this.windows[i].hasOwnProperty(key)) {
this.windows[i][key] = this.windowsDefaults[key];
}
}
// Add root key
this.windows[i].root = this.shadowRoot;
// Create the window
const wb = new WinBox( this.windows[i] );
console.log("Created WinBox with id '" + wb.id+"'");
}
};
}
// Register the attributes to be observed
static get observedAttributes() {
return ['set', 'add', 'remove', 'update', 'arrange'];
}
// Attribute change
attributeChangedCallback(property, oldValue, newValue) {
// Custom event for the attribute change
const event = new CustomEvent('communicate-windows-' + property, {
detail: { [property]: newValue },
bubbles: true
});
this.dispatchEvent(event);
// Check the property
switch (property) {
case "set":
// Remove all winbox windows from DOM
this.shadowRoot.querySelectorAll('.winbox').forEach(e => e.remove());
// Add the new windows
if (newValue != "") {
this.add(JSON.parse(newValue));
}
break;
case "add":
if (newValue != "") {
this.add(JSON.parse(newValue));
}
break;
case "remove":
if (newValue != "") {
this.remove(newValue);
}
break;
case "update":
if (newValue != "") {
this.update(JSON.parse(newValue));
}
break;
case "arrange":
if (newValue != "") {
this.arrange(newValue);
}
break;
default:
break;
}
}
// Add a window
add(windows) {
// Loop through the windows array
for (var i = 0; i < windows.length; i++) {
// Add the default properties
for (var key in this.windowsDefaults) {
if (!windows[i].hasOwnProperty(key)) {
windows[i][key] = this.windowsDefaults[key];
}
}
// Add the window to the global windows property
this.windows.push(windows[i]);
// Add root key
windows[i].root = this.shadowRoot;
// Create the window
new WinBox(windows[i]);
}
}
// Remove a window
remove(id) {
// Remove the window from the global windows property
this.windows = this.windows.filter(function (obj) {
return obj.id !== id;
});
// Remove the window from the DOM
const element = this.shadowRoot.getElementById(id);
if (element) {
element.remove();
}
}
// Update windows
update(windows) {
// Case 1: update all the windows with the same values eg. arranging the windows,
// windows = [{"height": "95%"}]
// Case 2: Update each window with different values
// Loop through the json string sent to update function
for (var i = 0; i < windows.length; i++) {
// Check if there is an id
if (!windows[i].hasOwnProperty("id")) {
console.error("Property 'id' missing in update statement: "+JSON.stringify(windows[i]));
return;
}
// Loop through all the existing edirom windows
for (var j = 0; j < this.windows.length; j++) {
// Check if the id of the input json string is the same as this.windows
if (windows[i]["id"] == this.windows[j]["id"]) {
for (var key in windows[i]) {
this.windows[j][key] = windows[i][key];
}
// TODO: update the window with the new values
// remove the window from the DOM
//this.remove(windows[i]["id"]);
// add the window to the DOM
//this.add([this.windows[j]]);
}
}
}
}
arrange(type){
// Log all top-level div elements inside the shadow root
var topLevelDivs = Array.from(this.shadowRoot.querySelectorAll('.winbox'));
var screenWidth = screen.width;
var screenHeight = screen.height;
var contentWidth = screenWidth * 0.8
var contentHeight = screenHeight * 0.85
var gridWidth = contentWidth / topLevelDivs.length
var gridHeight = contentHeight / topLevelDivs.length
topLevelDivs.forEach((div, index) => {
if(type == "vertical") {
div.style.width = gridWidth+"px" ;
div.style.left = index * gridWidth + "px"
div.style.top = 0
div.style.height = "95%"
} if(type == "horizontal") {
div.style.height = gridHeight+"px"
div.style.top = index * gridHeight + "px"
div.style.left = 0
div.style.width = "80%" ;
}
});
}
// Listen for communicate-update-update event and handle updates
connectedCallback() {
}
}
// Define the custom element
customElements.define('edirom-windows', EdiromWindows);