python-hserver/hserver/manage/api.js

190 lines
3.7 KiB
JavaScript

var hm = null;
function $(id){
return document.getElementById(id);
}
function pathSplit(path){
var s = path.split("/");
if (s[0] == ""){
s.splice(0,1);
}
return s;
}
function pathJoin(path){
return path.join("/");
}
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 loadHTML(container,source){
container.innerHTML = source;
var scripts = container.getElementsByTagName("script");
for (var n=0;n<scripts.length;n++){
eval.call(window,scripts[n].innerText);
}
}
function treeRefresh(objPath){
var treediv = $("tree");
var url = "/_hm_tree";
var treesource = callURL(url);
treediv.innerHTML = treesource;
}
function editorClear(){
hm.editor.fields = {}
}
function editorDeclareControl(ctl, field = null){
if (field == null){
field = ctl.getAttribute("name");
}
hm.editor.fields[ field ] = ctl;
}
function editorLoad(objPath){
editorClear();
var editor = $("editor");
var url = objPath + "/_hm_edit";
var treesource = callURL(url);
hm.editor.objPath = objPath;
loadHTML(editor, treesource);
}
function editorApply(objPath){
try {
var formData = new FormData();
for (var field in hm.editor.fields){
if (hm.editor.fields.hasOwnProperty( field )){
formData.append( field, hm.editor.fields[ field ].value );
}
}
var reply = callURL( objPath + "/_hm_apply", formData, null, "POST" );
} catch (e){
alert(e);
}
}
function editorSave(){
editorApply(hm.editor.objPath);
return false;
}
function editorExport(){
var objPath = hm.editor.objPath;
var reply = callJson( "objExport", { "objPath" : pathSplit(objPath) } )
var a = document.createElement("a");
a.setAttribute('href', "data:text/xml," + escape(reply.result));
a.setAttribute('download', 'object.xml');
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
function editorImport(){
var files = $("importxmlfile").files;
if (files.length == 0){
alert("First, you need to select a file to import into this node!");
}
}
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,objPath,parentID){
var ctlObjName = "objName-" + parentID;
var objName = $(ctlObjName).value;
var objTypeName = $("objTypeName-" + parentID).value;
var reply = objCreate( pathSplit(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,objPath){
var reply = objRemove( pathSplit(objPath) );
if (reply.result == true){
var path = pathSplit(objPath);
path.splice(path.length-1, 1);
treeRefresh("<%=request.SCRIPT_NAME%>" + pathJoin( path ) );
} else {
alert("The request didn't return success.\n" + reply.exception);
}
}
function frameLoad(){
var _hm = window.sessionStorage.getItem("hm");
if (!_hm){
hm = {
editor: {},
tree: {}
};
} else {
hm = JSON.parse(_hm);
}
treeRefresh("");
if (hm.editor.objPath){
editorLoad(hm.editor.objPath);
}
}
function frameUnload(){
window.sessionStorage.setItem("hm",JSON.stringify(hm));
}