kwebapp-javascriptimplement web application JavaScript objects


kwebapp-javascript [config]


The kwebapp-javascript utility accepts a configuration file config (defaulting to standard input) and creates the JavaScript objects accepting the JSON produced by kwebapp-c-header(1) and kwebapp-c-source(1).
The output, a standards-compliant JavaScript file, formats JSON output into a DOM tree. The method for acquiring the JSON is not managed by kwebapp-javascript.
To use the interface, simply include the generated file as a script, create objects given the exported JSON of kwebapp-c-source(1), and invoke the JavaScript object's fill(), fillInner(), and fillArray() methods with a DOM tree node.
For example, given a structure “foo” and an AJAX response “response”, this might look like:
var obj = JSON.parse(response); 
var e = document.getElementById('foo'); 
new foo(obj).fill(e);
For each field in the structure, this method will operate on elements within (and including) the element with id “foo” having classes as follows:
Replaces the contents of the element with the field value. This is only applicable for non-blob native types.
Sets the “value” attribute (as in a form submission) with the field value. This is only applicable for non-blob native types.
Remove the “hide” class if the object is null, otherwise add the “hide” class.
Add the “hide” class if the object is null, otherwise remove the “hide” class.
For structures, creates and invokes the fillInner() method on the nested structure at the given element and its descendents. This is only applicable for structure types.
Alternatively, the fillInner() method may be used to exclude the root element. Lastly, the fillArray() method invokes fill() repeatedly over an array of objects by removing, then subsequently cloning and appending the first element of the give DOM root.
All functions accept an additional, optional argument for providing custom per-field or per-structure callbacks. Keys in the object must consist of the structure name, followed by a dash, followed by the field name. For example, assuming a structure “client” with a field “dob” consisting of a UNIX epoch:
var custom = { 'client-dob': formatDate }; 
new client(obj).fillInner(document, custom);
And letting a formatting function be:
function formatDate(e, name, value) 
  var list, i; 
  list = e.getElementsByClassName('client-dob-date'); 
  for (i = 0; i < list.length; i++) 
    list[i].innerHTML = 
This invokes the “moment.js” formatter to create dates.
The same can be applied to structures instead of to fields within structures. The keys for these are simply the structure name.
var custom = { 'client': formatClient }; 
new client(obj).fillInner(document, custom);
The callback will then be provided the full client object.
In either case, the value for the custom key may also be an array of functions just as above. Each will be invoked in the order given, in the same way.
var custom = { 'client': [ format1, format2 ] };
The callback function (or functions) will be invoked regardless of whether the value has been set. In the event of an unset field value, the function is passed null.
Enumeration fields are also generated from the input file. These are appended to the global namespace as a series of objects consisting of variables assigned the enumeration value. For example, given an enumeration “foo” with a field “bar” assigned the value 12, this would generate:
>>> 12
All JavaScript methods and variables are fully documented in the jsdoc(1) format.


The kwebapp-javascript utility exits 0 on success, and >0 if an error occurs.


jsdoc(1), kwebapp-c-header(1), kwebapp-c-source(1), kwebapp(5)
December 12, 2017 OpenBSD 6.2