As asked on Twitter, the code that is needed to add and remove event handlers on HTML elements. that will be handled in the Wasm Code.
Edit: I forgot the small part of javaScript code that is needed also.
Add the following code to your html:
<script>
function WasmEvent(id) {
var e = window.event;
program.WasmEvent(e, id);
}
</script>
and change the WASM code in the html:
var program; //added to the code
WasmForms.instantiate("wasm/WasmForms.wasm").then(function (result) {
console.log("WebAssembly file WasmForms.wasm has been loaded.");
program = result.Program(); //this was var program - ...
program.Main();
});
You need a Module that implements the EventHandling system:
Public Module jsEvents
Public delegate Sub EventDelegate(e As Dynamic)
Private EventIds As New Dictionary(Of String, EventDelegate)
Public Function GetEventDelegate(element As Dynamic, EventName As String) As EventDelegate
dim id As String = element.getAttribute(EventName)
If assigned(id) then
return EventIds(id)
Else
Return nothing
End If
End Function
Public Sub SetEvent(element As Dynamic, EventName As String, del As EventDelegate)
ClearEvent(element, EventName)
If del IsNot Nothing then
Dim Id As String = RemObjects.Elements.System.DateTime.Now.Ticks.ToString
EventIds.Add(Id, del)
element.setAttribute(EventName, $"WasmEvent('{Id}');")
End If
End Sub
Public Sub ClearEvent(element As Dynamic, EventName As String)
dim id As String = element.getAttribute(EventName)
If assigned(id) then
EventIds.Remove(id)
element.setAttribute(EventName, "")
End If
End Sub
Public Function GetEventDelegate(Id As String) As EventDelegate
Return EventIds(Id)
End Function
End Module
Then you add the dispatcher code to the exported WASM class:
Public Sub WasmEvent(e As Dynamic, id As String)
Try
GetEventDelegate(id).Invoke(e)
Catch
End try
End Sub
After that you can add the event handlers from code:
CloseBtn = document.getElementById("myclosebutton")
Dim x As EventDelegate = AddressOf Close
SetEvent(CloseBtn, "onclick", x)
or remove the handler from code:
ClearEvent(CloseBtn, "onclick")
The following Sub will handle the event:
Private Sub Close(e As Dynamic)