-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathiframed.js
187 lines (161 loc) · 7.79 KB
/
iframed.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
function extractUUIDs(inputString) {
// Regular expression to match UUIDs
const uuidRegex = /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/g;
// Find all matches in the input string
const matches = inputString.match(uuidRegex);
// Return the matches or an empty array if none found
return matches || [];
}
function extractModel(inputString) {
const relevantModels = ["member", "building", "install", "node", "device", "sector", "link"];
return relevantModels.find(element => inputString.includes(element));
}
function getCurrentTarget(url){
let path = url.replace(/^\/admin\/meshapi\//, "");
path = path.replace(/\/$/, "");
const [type, id, action] = path.split("/");
return [type, id, action];
}
// XXX (wdn): This function is plural, but it's actually just one node?
async function getNewSelectedNodes(url){
//const [type, id, action] = getCurrentTarget(url);
const objectUUIDs = extractUUIDs(url);
const type = extractModel(url);
// Guard against looking up an empty UUID
if (objectUUIDs.length == 0) {
console.log("Found no UUID")
return null;
}
const id = objectUUIDs[0];
let nodeId = null;
if (type === "install") {
if (!id) return null;
const installResponse = await fetch(`/api/v1/installs/${id}/`);
if (!installResponse.ok) return null;
const install = await installResponse.json();
nodeId = install.install_number;
} else if (type === "node") {
if (!id) return null;
const nodeResponse = await fetch(`/api/v1/nodes/${id}/`);
if (!nodeResponse.ok) return null;
const node = await nodeResponse.json();
if (node.network_number) {
nodeId = node.network_number;
} else {
nodeId = node.installs[0].install_number;
}
} else if (type === "building") {
if (!id) return null;
const buildingResponse = await fetch(`/api/v1/buildings/${id}/`);
if (!buildingResponse.ok) return null;
const building = await buildingResponse.json();
if (building.primary_node && building.primary_node.network_number) {
nodeId = building.primary_node.network_number;
} else if (building.installs) {
nodeId = building.installs[0].install_number;
}
} else if (["device", "sector", "accesspoint"].indexOf(type) !== -1) {
if (!id) return null;
const deviceResponse = await fetch(`/api/v1/devices/${id}/`);
if (!deviceResponse.ok) return null;
const device = await deviceResponse.json();
nodeId = device.node.network_number;
} else if (type === "member") {
if (!id) return null;
const memberResponse = await fetch(`/api/v1/members/${id}/`);
if (!memberResponse.ok) return null;
const member = await memberResponse.json();
nodeId = member.installs.map(install => install.install_number).join("-");
} else if (type === "link") {
if (!id) return null;
const linkResponse = await fetch(`/api/v1/links/${id}/`);
if (!linkResponse.ok) return null;
const link = await linkResponse.json();
const device1Response = await fetch(`/api/v1/devices/${link.from_device.id}/`);
if (!device1Response.ok) return null;
const device1 = await device1Response.json();
const device2Response = await fetch(`/api/v1/devices/${link.to_device.id}/`);
if (!device2Response.ok) return null;
const device2 = await device2Response.json();
if (device1.node.network_number && device2.node.network_number) {
nodeId = `${device1.node.network_number}-${device2.node.network_number}`;
}
} else if (type === "los") {
if (!id) return null;
const losResponse = await fetch(`/api/v1/loses/${id}/`);
if (!losResponse.ok) return null;
const los = await losResponse.json();
let b1NodeId = null;
const buildingResponse1 = await fetch(`/api/v1/buildings/${los.from_building.id}/`);
if (!buildingResponse1.ok) return null;
const building1 = await buildingResponse1.json();
if (building1.primary_node && building1.primary_node.network_number) {
b1NodeId = building1.primary_node.network_number;
} else if (building1.installs) {
b1NodeId = building1.installs[0].install_number;
}
let b2NodeId = null;
const buildingResponse2 = await fetch(`/api/v1/buildings/${los.to_building.id}/`);
if (!buildingResponse2.ok) return null;
const building2 = await buildingResponse2.json();
if (building2.primary_node && building2.primary_node.network_number) {
b2NodeId = building2.primary_node.network_number;
} else if (building2.installs) {
b2NodeId = building2.installs[0].install_number;
}
if (b1NodeId && b2NodeId) nodeId = `${b1NodeId}-${b2NodeId}`;
}
return nodeId ? `${nodeId}` : null;
}
// FIXME: Refreshes reset your location. Need to make sure that the iframe gets the refresh/forward/backward stuff. Check andrew's code.
// FIXME: Also need to make sure that admin/members/uuid directs you to this iframe setup properly
async function adminPanelLoaded() {
const iframe_panel_url = document.getElementById("iframe_panel").contentWindow.location.href;
const selectedNodes = await getNewSelectedNodes(iframe_panel_url);
document.getElementById("iframe_url").innerHTML = `${iframe_panel_url} ---> Selected Nodes: ${selectedNodes}`;
if (selectedNodes === null) {
console.log("No node");
return;
}
console.log(`Got node: ${selectedNodes}`);
//const selectedEvent = new Event("setMapNode");//, {detail: {selectedNodes: selectedNodes}});
//selectedEvent.selectedNodes = selectedNodes;
document.getElementById("map_panel").contentWindow.postMessage({selectedNodes: selectedNodes}, "http://127.0.0.1:3001");
}
async function updateAdminPanelLocation(selectedNodes) {
if (!selectedNodes) return;
if (selectedNodes.indexOf("-") !== -1) return;
let selectedNodeInt = parseInt(selectedNodes);
if (selectedNodeInt >= 1000000) {
/* Hack for APs to not break things. We unfortantely can't do a lot better than this without much pain*/
return;
}
const installResponse = await fetch(`/api/v1/installs/${selectedNodes}/`);
const nodeResponse = await fetch(`/api/v1/nodes/${selectedNodes}/`);
if (installResponse.ok){
const installJson = await installResponse.json();
if (installJson.node && installJson.node.network_number) {
const newAdminPanelURL = new URL(`/admin/panel/meshapi/node/${installJson.node.id}/change`, document.location);
document.getElementById("iframe_panel").src = `panel/meshapi/node/${installJson.node.id}/change`;
//updateMapForLocation(installJson.node.network_number.toString());
} else {
const newAdminPanelURL = new URL(`/admin/panel/meshapi/install/${installJson.id}/change`, document.location);
document.getElementById("iframe_panel").src = `panel/meshapi/install/${installJson.id}/change`;
}
} else {
if (nodeResponse.ok) {
const nodeJson = await nodeResponse.json();
const newAdminPanelURL = new URL(`/admin/panel/meshapi/node/${nodeJson.id}/change`, document.location);
document.getElementById("iframe_panel").src = `panel/meshapi/node/${nodeJson.id}/change`;
}
}
}
window.addEventListener("message", ({ data, source }) => {
console.log(`Admin Panel got event: ${JSON.stringify(data)}`);
// Check if we have the correct data
// look up the id of the node
// replace admin panel window location
//this.updateSelected.bind(this)(data.selectedNodes, false);
updateAdminPanelLocation(data.selectedNodes);
}
);