-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite channel selector to use updated message types #1345
Changes from all commits
75e5afa
ada459d
ae5c4d5
e592a49
39e57b2
de29cd8
f0ff8c3
110ac58
4577e2b
79e32ff
18ede6b
926a8af
c9bf4b0
763419f
97aa62b
52be447
2cd919e
20175f1
15d209d
f3553c1
6bbd210
13ae485
ebe8b7d
469cca6
e484919
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
import { ChannelDetails, SelectorMessageChannels } from "@kite9/fdc3-common"; | ||
import { IframeChannelsPayload, Channel } from "@kite9/fdc3-common"; | ||
|
||
|
||
var channels: ChannelDetails[] = [] | ||
var channelId: string | null = null | ||
|
||
function fillChannels(data: ChannelDetails[], selected: string | null, messageClickedChannel: (s: String) => void) { | ||
const fillChannels = (data: Channel[], selected: string | null, messageClickedChannel: (s: string | null) => void) => { | ||
const list = document.getElementById('list')!!; | ||
list.innerHTML = ''; | ||
|
||
|
@@ -14,17 +11,25 @@ function fillChannels(data: ChannelDetails[], selected: string | null, messageCl | |
|
||
const span = document.createElement('span'); | ||
span.classList.add('glyph'); | ||
span.style.color = displayMetadata.color; | ||
span.style.borderColor = displayMetadata.color; | ||
span.textContent = displayMetadata.glyph ?? ''; | ||
|
||
if(displayMetadata?.color){ | ||
span.style.color = displayMetadata.color; | ||
span.style.borderColor = displayMetadata.color; | ||
} | ||
span.textContent = displayMetadata?.glyph ?? ''; | ||
node.appendChild(span); | ||
const span2 = document.createElement('span'); | ||
span2.classList.add('name'); | ||
span2.textContent = displayMetadata.name; | ||
node.appendChild(span2); | ||
list.appendChild(node); | ||
node.addEventListener('click', () => messageClickedChannel(id)); | ||
|
||
if(displayMetadata?.name){ | ||
const span2 = document.createElement('span'); | ||
span2.classList.add('name'); | ||
span2.textContent = displayMetadata.name; | ||
node.appendChild(span2); | ||
} | ||
|
||
list.appendChild(node); | ||
node.addEventListener('click', () => { | ||
messageClickedChannel(id) | ||
}); | ||
|
||
if (id === selected) { | ||
node.setAttribute("aria-selected", "true"); | ||
|
@@ -35,40 +40,95 @@ function fillChannels(data: ChannelDetails[], selected: string | null, messageCl | |
|
||
window.addEventListener("load", () => { | ||
const parent = window.parent; | ||
const logo = document.getElementById("logo")!! | ||
const logo = document.getElementById("logo")!; | ||
|
||
const mc = new MessageChannel(); | ||
const myPort = mc.port1 | ||
myPort.start() | ||
|
||
parent.postMessage({ type: "SelectorMessageInitialize" }, "*", [mc.port2]); | ||
|
||
function changeSize(expanded: boolean) { | ||
document.body.setAttribute("data-expanded", "" + expanded); | ||
myPort.postMessage({ type: "SelectorMessageResize", expanded }) | ||
} | ||
|
||
myPort.addEventListener("message", (e: MessageEvent) => { | ||
if (e.data.type == 'SelectorMessageChannels') { | ||
const details = e.data as SelectorMessageChannels | ||
// console.log(JSON.stringify("CHANNEL DETAILS: " + JSON.stringify(details))) | ||
channels = details.channels | ||
channelId = details.selected | ||
|
||
const selectedColor = (channelId ? (channels.find(c => c.id == channelId)?.displayMetadata?.color) : null) ?? 'black' | ||
logo.style.fill = selectedColor | ||
const myPort = mc.port1; | ||
myPort.start(); | ||
myPort.onmessage = ({data}) => { | ||
console.debug("Received message: ", data); | ||
switch(data.type){ | ||
case "iframeHandshake": { | ||
collapse(); | ||
break; | ||
} | ||
case "fdc3UserInterfaceChannels": { | ||
logo.removeEventListener("click", expand); | ||
const {userChannels, selected} = data.payload as IframeChannelsPayload; | ||
fillChannels(userChannels, selected, (channelStr) => { | ||
myPort.postMessage({ | ||
type: "fdc3UserInterfaceSelected", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be better to initialise the message as an object and use the Fdc3UserInterfaceChannelSelected type to check it, then post it. |
||
payload: { | ||
selected: channelStr || null | ||
} | ||
}); | ||
collapse(); | ||
}); | ||
const selectedChannel = userChannels.find((c) => c.id === selected); | ||
logo.style.fill = selectedChannel?.displayMetadata?.color ?? "white"; | ||
logo.addEventListener("click", expand); | ||
break; | ||
} | ||
} | ||
}) | ||
|
||
logo.addEventListener("click", () => { | ||
fillChannels(channels, channelId, (id) => { | ||
changeSize(false) | ||
myPort.postMessage({ type: "SelectorMessageChoice", channelId: id }) | ||
}) | ||
|
||
// ask the parent container to increase the window size | ||
changeSize(true) | ||
}) | ||
}; | ||
|
||
parent.postMessage({ | ||
type: "fdc3UserInterfaceHello", | ||
payload: { | ||
initialCSS: { | ||
width: `${8*4}px`, | ||
height: `${8*5}px`, | ||
right: "2px", | ||
bottom: "2px", | ||
zIndex: "1000", | ||
"z-index": "1000", | ||
position: "fixed" | ||
|
||
} | ||
} | ||
}, "*", [mc.port2]); | ||
|
||
const expand = () => { | ||
document.body.setAttribute("data-expanded", "true"); | ||
myPort.postMessage({ | ||
type: "fdc3UserInterfaceRestyle", | ||
payload: { | ||
updatedCSS: { | ||
width: `100%`, | ||
height: `100%`, | ||
top: 0, | ||
left: 0, | ||
zIndex: "1000", | ||
"z-index": "1000", | ||
position: "fixed" | ||
} | ||
} | ||
}); | ||
} | ||
|
||
const collapse = () => { | ||
myPort.postMessage({ | ||
type: "fdc3UserInterfaceRestyle", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above - I would set up the message as an object and apply typing, then post it. |
||
payload: { | ||
updatedCSS: { | ||
width: `${8*4}px`, | ||
height: `${8*5}px`, | ||
right: "2px", | ||
bottom: "2px", | ||
zIndex: "1000", | ||
"z-index": "1000", | ||
position: "fixed" | ||
} | ||
} | ||
}); | ||
|
||
// If you immediately change to the logo, before the iframe has a chance to finish restyling, | ||
// you see a flicker of a giant, colored logo. | ||
// Here, we wait a negligible amount of time, and hope that the restyling has finished. This avoids the flicker. | ||
// It's not a *good* idea, it's just the best available, since we don't know when the restyle finishes. | ||
setTimeout(() => { | ||
document.body.setAttribute("data-expanded", "false"); | ||
}, 15); | ||
} | ||
Comment on lines
+125
to
+131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doh, do we need to add a response message to the restyle message so that the DA can indicate that it has applied the change? That's likely to add some lag however, so we might be just exchanging one artefact for another... This is exactly the sort of issue that we need to explore and provide advice on handling so thank you for identifying it. We'll need to think though reliable solutions / designs that avoid this... |
||
|
||
}) | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IframeChannelsPayload needs updating to the new type name.
However, you are also using one of the generated components of the top level type. The naming of these is unstable and they should not be used directly. Instead, you should type the
data
element asFdc3UserInterfaceChannels
instead and rework accordingly