python-hserver/hserver/manage/api.js
2018-03-12 20:38:33 +01:00

93 lines
2 KiB
JavaScript

function $(id){
return document.getElementById(id);
}
function pathSplit(path){
var s = path.split("/");
if (s[0] == ""){
s.splice(0,1);
}
return s;
}
function callURL(url, content, asyncReceiver = null, method = "GET"){
var r = new XMLHttpRequest();
var async = asyncReceiver != null;
r.open(method,url,async);
if (async) {
r.onload = asyncReceiver;
}
r.send(content);
if (!async){
return r.response;
}
}
function callJson(method, parameters, asyncReceiver = null){
var url = "<%=request.self(0)%>_hm_json";
var reply = callURL(url, JSON.stringify( { "method": method, "parameters": parameters } ), null, "POST" );
return JSON.parse( reply );
}
function treeRefresh(objPath){
var treediv = $("tree");
var url = objPath + "/_hm_tree";
var treesource = callURL(url);
treediv.innerHTML = treesource;
editorLoad(objPath);
}
function editorLoad(objPath){
var editor = $("editor");
var url = objPath + "/_hm_edit";
var treesource = callURL(url);
editor.innerHTML = treesource;
}
function objCreate( objPath, objTypeName, objName){
var reply = callJson( "objCreate", { "objTypeName" : objTypeName, "objName" : objName, "objPath" : objPath } )
return reply;
}
function objRemove( objPath ){
return callJson( "objRemove", { "objPath": objPath } );
}
function onAddObjectButton(button){
var objPath = pathSplit( button.getAttribute("objPath") );
var objName = $("objName").value;
var objTypeName = $("objTypeName").value;
if (objPath[0] == ""){
objPath.splice(0,1);
}
var reply = objCreate( objPath, objTypeName, objName );
if (reply.result != true){
alert("The request didn't return success.\n" + reply.exception);
} else {
treeRefresh("<%=request.SCRIPT_NAME%>" + objPath);
}
}
function onRemoveButton(button){
var objPath = pathSplit( button.getAttribute("objPath") );
var reply = objRemove( objPath );
if (reply.result == true){
treeRefresh("<%=request.SCRIPT_NAME%>" + $("currentnode").getAttribute("objPath"));
} else {
alert("The request didn't return success.\n" + reply.exception);
}
}