Skip to content

HTMLRenderer

Paul Mansour edited this page Apr 14, 2023 · 17 revisions

The HTML project contains a light framework for working with the Dyalog HTMLRenderer object. In the following code, A is a reference to the Abacus API (#.Abacus.Main).

Consider a document in APLDOM form (an object):

d

A new instance of the HTMLRenderer is created using the A.NewHTMLRenderer function, which takes the document as an argument:

hr←p A.NewHTMLRenderer d  

The left argument to NewHTMLRenderer is an optional parent form (of ⎕WC fame). The function creates a new instance of the HTMLRenderer object, sets the HTML into the browser, and links the document d to the HTMLRenderer instance. Thus, if we want to, say, delete an element

      e←d A.GetElementById 'myid'   
      _←e A.SetInnerHTML '<h1>Header 1</h1>'

No callback should be assigned to the HTMLRenderer's HTTPRequest method. Instead, assign APL functions to the OnClick property (or other events) of HTML elements:

      ...
      b←h A.New 'button'
      b.id←'b1'
      b.Onclick←'MyAPLFoo'

When this button is clicked, MyAPLFoo will be called in APL. Note the capitalization of the first letter: the Onclick property is part of the framework, and should not be confused with the onclick attribute which would be handled directly by JavaScript. Note that either Onclick or onclick should be used, but not both.

The APL callback function is expected to be in the same space from which HR.New was called, and receives as its right argument a namespace containing the following properties:

Property Description
HTMLRenderer The HTMRenderer object instance
Target The element that fired the event
CurrentTarget The element that handled the event
Event The name of the event (e.g.: click)

The target element is the element that fires the event. The CurrentTarget is either the Target element or an ancestor element that handles the event. A single click on an element will fire a callback on that element, as well as any ancestor elements that have callbacks for that event. This is known as bubbling, and why two elements are of interest whenever a callback is executed.

Additional properties that may be present depending on the type of event include:

Property Description
Key The value of the pressed key
CtrlKey Is the Ctrl key pressed
AltKey Is the Alt key pressed
ShiftKey Is the shift key pressed

The callback function may directly make changes to the HTML page by calling the HTMLRenderer.ExecuteJavaScript method, or any of a variety of utility cover functions including HR.SetClassById, HR.SetInnerHTMLbyId, HR.DeleteElementById, etc.

The callback function must return a 0, which is completely ignored. Future enhancements may do something interesting with a non-zero result.