Skip to content

Commit

Permalink
Not found returns (0 0) for size and posn, use else ifs for switch
Browse files Browse the repository at this point in the history
The many if statements formed a quasi switch statement, and as
there was no default, it worked fine. I know need a default
and it's easier to avoid mistakes if we use else if statements.

Don't crash on invalid IDs for size and pos, return [0, 0] as
sentinel values.
  • Loading branch information
cursork committed Jan 12, 2025
1 parent 9492bf9 commit 5ee1ed4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 51 deletions.
83 changes: 32 additions & 51 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const App = () => {
};

const dataRef = useRef({});
// Convenience for being able to check the current state of the tree in the
// browser console.
window.ewcDataRef = dataRef;
const appRef = useRef(null);

const wsSend = (d) => webSocketRef.current.send(JSON.stringify(d));
Expand Down Expand Up @@ -660,8 +663,7 @@ const App = () => {
},
})
);
}
if (Type == "Form") {
} else if (Type == "Form") {
console.log("Coming in forms");

const supportedProperties = ["Posn", "Size"];
Expand Down Expand Up @@ -724,9 +726,7 @@ const App = () => {
console.log("server event is as2", event);
webSocket.send(event);
return;
}

if (Type == "Edit") {
} else if (Type == "Edit") {
const { Text = "", Value, SelText } = Properties;
const supportedProperties = ["Text", "Value", "SelText"];
// setTimeout(() => {},100)
Expand Down Expand Up @@ -847,9 +847,7 @@ const App = () => {
},
})
);
}

if (Type == "Combo") {
} else if (Type == "Combo") {
console.log("Properties are", Properties);
const { SelItems, Items, Text } = Properties;
console.log("Coming in form but combo");
Expand Down Expand Up @@ -969,10 +967,7 @@ const App = () => {

console.log("server event is as2", message);
return webSocket.send(updateAndStringify(message));
}
console.log("Coming here in nnnnn");

if (Type == "List") {
} else if (Type == "List") {
console.log("Coming here");
const { SelItems } = Properties;

Expand Down Expand Up @@ -1055,9 +1050,7 @@ const App = () => {
},
})
);
}

if (Type == "Scroll") {
} else if (Type == "Scroll") {
const { Thumb = 1 } = Properties;
const supportedProperties = ["Thumb"];

Expand Down Expand Up @@ -1138,9 +1131,7 @@ const App = () => {
},
})
);
}

if (Type == "Splitter") {
} else if (Type == "Splitter") {
console.log("Coming in form but splitter");

const { Posn } = Properties;
Expand Down Expand Up @@ -1223,9 +1214,7 @@ const App = () => {
},
})
);
}

if (Type == "SubForm") {
} else if (Type == "SubForm") {
console.log("Coming in form but suss");
const supportedProperties = ["Posn", "Size"];

Expand Down Expand Up @@ -1343,9 +1332,7 @@ const App = () => {
},
})
);
}

if (Type == "Button") {
} else if (Type == "Button") {
console.log("Coming here in buttons")
const { State } = Properties;
const supportedProperties = ["State", "Posn", "Size"];
Expand Down Expand Up @@ -1405,9 +1392,7 @@ const App = () => {
console.log(event);

return webSocket.send(event);
}

if (Type == "TreeView") {
} else if (Type == "TreeView") {
const supportedProperties = ["SelItems"];
const result = checkSupportedProperties(
supportedProperties,
Expand All @@ -1433,9 +1418,7 @@ const App = () => {

console.log(event);
return webSocket.send(event);
}

if (Type == "Timer") {
} else if (Type == "Timer") {
const supportedProperties = ["FireOnce"];
const result = checkSupportedProperties(
supportedProperties,
Expand All @@ -1460,9 +1443,7 @@ const App = () => {
});
console.log(event);
return webSocket.send(event);
}

if (Type == "ListView") {
} else if (Type == "ListView") {
const supportedProperties = ["SelItems"];
const result = checkSupportedProperties(
supportedProperties,
Expand All @@ -1488,8 +1469,7 @@ const App = () => {

console.log(event);
return webSocket.send(event);
}
if (Type === "ApexChart") {
} else if (Type === "ApexChart") {
const supportedProperties = ["SVG"];
const { SVG } = Properties;
const data = JSON.parse(
Expand All @@ -1509,24 +1489,25 @@ const App = () => {
// console.log(event);
return webSocket.send(event);

}
// TODO size and posn
if (Type === "Upload") return Upload.WG(wsSend, serverEvent);
// Generic WG
const replyProps = {};
for (const prop in serverEvent.Properties) {
if (refData.Properties[prop]) {
replyProps[prop] = refData[prop];
} else if (Type === "Upload") {
// TODO size and posn
return Upload.WG(wsSend, serverEvent);
} else {
const replyProps = {};
for (const prop in serverEvent.Properties) {
if (refData.Properties[prop]) {
replyProps[prop] = refData[prop];
}
}
}

return webSocket.send(updateAndStringify({
WG: {
ID: serverEvent.ID,
Properties: replyProps,
WGID: serverEvent.WGID,
}
}));
return webSocket.send(updateAndStringify({
WG: {
ID: serverEvent.ID,
Properties: replyProps,
WGID: serverEvent.WGID,
}
}));
}
} catch (e) {
// There should be a proper error response here, but for now, we just log.
// This is because we know something failed, but APL doesn't and
Expand Down
3 changes: 3 additions & 0 deletions src/utils/sizeposn.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function boundingBox(id) {
const el = document.getElementById(id + ".$CONTAINER") || document.getElementById(id);
if (el === null) return null;
return el.getBoundingClientRect();
}

Expand All @@ -11,6 +12,7 @@ function parentId(id) {

function posn(id) {
const bb = boundingBox(id);
if (bb === null) return [0, 0];
const pid = parentId(id);
if (pid === null) return [bb.y, bb.x];
const pbb = boundingBox(pid);
Expand All @@ -19,6 +21,7 @@ function posn(id) {

function size(id) {
const bb = boundingBox(id);
if (bb === null) return [0, 0];
return [bb.height, bb.width];
}

Expand Down

0 comments on commit 5ee1ed4

Please sign in to comment.