Admin User, created Apr 17. 2025
/**
* Modern Albufeira Prolog Interpreter
*
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
import {add, check_atom, check_nonvar, Compound, check_integer,
copy_term, get_ctx, make_error, deref, exec_build,
callback, launch,
exec_unify, check_clause, make_check, register_interrupt
} from "../../nova/core.mjs";
import {
check_element
} from "./domlib.mjs";
/*****************************************************************/
/* DOM API II */
/*****************************************************************/
/**
* dom_cell_clear(E):
* The predicate removes the children of element E.
*/
function test_dom_cell_clear(args) {
let at = deref(exec_build(args[0]));
check_element(at);
at.innerHTML = "";
return true;
}
/**
* dom_cell_goto(I, E):
* The predicate succeeds in E with the element of id I.
*/
function test_dom_cell_goto(args) {
let id = deref(exec_build(args[0]));
check_atom(id);
return exec_unify(args[1], document.getElementById(id));
}
/*****************************************************************/
/* Event Handling */
/*****************************************************************/
/**
* dom_cell_listen(E, T, N, F):
* The predicate adds a type T event handler compiled clause
* N to the element E with capture flag F.
*/
function test_dom_cell_listen(args) {
let at = deref(exec_build(args[0]));
check_element(at);
let type = deref(exec_build(args[1]));
check_atom(type);
let clause = deref(exec_build(args[2]));
check_clause(clause);
let flag = deref(exec_build(args[3]));
check_integer(flag);
let buf = get_ctx();
at.addEventListener(type, event => callback(clause, buf, [event]),
((flag & 1) !== 0));
return true;
}
/**
* dom_cell_promise(E, T, N, F, Q):
* The predicate succeeds in Q with a promise that adds a type T
* event handler compiled clause N to the element E with capture
* flag F. If the event handler succeeds the promise resolves
* and then the event handler gets removed.
*/
function test_dom_cell_promise(args) {
let buf = get_ctx();
let at = deref(exec_build(args[0]));
check_element(at);
let type = deref(exec_build(args[1]));
check_atom(type);
let clause = deref(exec_build(args[2]));
check_clause(clause);
let flag = deref(exec_build(args[3]));
check_integer(flag);
let prom = dom_cell_promise(buf, at, type, clause, flag);
return exec_unify(args[4], prom);
}
function dom_cell_promise(buf, at, type, clause, flag) {
return new Promise(resolve => {
function handler(event) {
if (launch(clause, buf, [event])) {
at.removeEventListener(type, handler);
resolve();
}
}
at.addEventListener(type, handler, ((flag & 1) !== 0));
register_interrupt(buf, () => {
at.removeEventListener(type, handler);
resolve();
});
}).then(() => register_interrupt(buf, () => {}));
}
/*****************************************************************/
/* Browser Behavior */
/*****************************************************************/
/**
* dom_prevent_default(E):
* The predicate prevents default behaviour and stops propagation of the event E.
*/
function test_dom_prevent_default(args) {
let event = deref(exec_build(args[0]));
check_event(event);
event.preventDefault();
return true;
}
/**
* dom_stop_propagation(E):
* The predicate prevents default behaviour and stops propagation of the event E.
*/
function test_dom_stop_propagation(args) {
let event = deref(exec_build(args[0]));
check_event(event);
event.stopPropagation();
return true;
}
function check_event(beta) {
if (!(beta instanceof Event)) {
check_nonvar(beta);
beta = copy_term(beta);
throw make_error(new Compound("type_error", ["event", beta]));
}
}
/*********************************************************************/
/* Emit Lib Init */
/*********************************************************************/
export function main() {
add("dom_cell_clear", 1, make_check(test_dom_cell_clear));
add("dom_cell_goto", 2, make_check(test_dom_cell_goto));
add("dom_cell_listen", 4, make_check(test_dom_cell_listen));
add("dom_cell_promise", 5, make_check(test_dom_cell_promise));
add("dom_prevent_default", 1, make_check(test_dom_prevent_default));
add("dom_stop_propagation", 1, make_check(test_dom_stop_propagation));
}