apxtrib/nationchains/www/adminapx/static/js/apxapp.js
2023-04-28 13:21:02 +02:00

158 lines
5.5 KiB
JavaScript

/*eslint no-undef:0*/
/*eslint-env browser*/
"use strict";
var app = app || {};
app.createIdentity=(alias,passphrase="")=>{
if (alias.length<3 || apx.data.object['index_pagans_alias_all'].includes(alias) ){
alert('Please chose another alias')
return;
}
const keys = apx.generateKey(alias,passphrase)
for(let tag of ['inputalias','inputpassphrase']){
document.getElementById(tag).classList.add('disabled')
}
document.getElementById('privatekey').setAttribute("key",keys.privateKey);
document.getElementById('publickey').setAttribute("key",keys.publicKey);
document.getElementById('generatekeys').classList.add('d-none');
document.getElementById('downloadkeys').classList.remove('d-none');
document.getElementById('createId').classList.remove('d-none');
}
app.registerIdentity=()=>{
const data={
alias:document.getElementById('inputalias').value,
privateKey:document.getElementById('privatekey').getAttribute('key'),
publicKey:document.getElementById('publickey').getAttribute('key'),
passphrase:document.getElementById('inputpassphrase').value
}
axios.post('api/pagans',data,apx.data.headers)
.then(rep=>{
// genere authentification headers.xalias, xhash store object.pagans_alias={privateKey, passphrase}; console.log(rep)})
// if check trust add data email recovery axios.post('api/persons,data, apx.data.headers)
})
.catch(err=>{
console.log('sorry',err);
})
}
app.search = (elt) => {
//@todo get search string from input then return tpldata.listofarticles={"search":[]}
console.log("A FAIRE");
};
app.downloadlink = (keys, object, prefix) => {
/**
*
* @param {string} split(.) to naviagte in object until a subobject
* @param {object} a json object
*
* Create a link that download subobject as a textfile
* example:
* const obj={pagans:{privateKey:"123", publicKey:"AZE"}}
* const prefix = "appname"
* const keys="pagans.privateKey"
*
* in <html>
* <button class="btn btn-outline-primary" onclick="app.downloadlink('pagans.privateKey',obj,prefix);" >Download PrivateKey</button>
*
*
*/
const dwn = (fn, t) => {
var element = document.createElement("a");
element.setAttribute(
"href",
"data:text/plain;charset=utf-8," + encodeURIComponent(t)
);
element.setAttribute("download", fn);
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
};
const subobj = keys.split(".").reduce((val, k) => (val = val[k]), object);
const txt = typeof subobj == "string" ? subobj : JSON.stringify(subobj);
dwn(`${prefix}_${keys.split(".").pop()}.txt`, txt);
};
app.setupdata = () => {
//todo generate tpldata.listofarticle get html from /static/html
apx.data.tpldata.listofarticle = {
mostread: [{}],
news: [{}],
search: [],
articles: "html",
};
const list = {}
document.querySelectorAll("[add2data]").forEach((e) => {
/** Collect from any tag with add2data url attribute (tpl tpldata object)
* name is the filename :
* for tpl and tpldata (relativepath/filename_xx.ext) name=filename
* For public object (pagans, towns, nations, block) not tribes object are gettable (only)
* with /nationchains/objectName/idx/existingindex.json
* or for an item /itm//primarykey_value_of_item.json
*
* For tribe object get or for modification you need to use api and to have accessright setup properly:
* for object index (/api/odmdb/objectname/idx/filename.json) name =objectname_filename
* for object item (/api/odmdb/objectname/itm/primarykey.json) name =objectname_primarykey
*/
if (e.getAttribute('object')){
const url = e.getAttribute('object')
const spliturlobj=url.split("/").slice(-3);
const objectname=spliturlobj[0];
const objecttype=spliturlobj[1];
const objectkey=spliturlobj[2].substring(0,spliturlobj[2].length-5)
if (!list[objectname]) list[objectname]={};
list[objectname][`${objecttype}${objectkey}`]= url;
};
for (let k of ["tpl", "tpldata"]) {
if (e.getAttribute(k)){
const url=e.getAttribute(k);
let localname=url.split('/').slice(-1)[0];
if (url.includes('.mustache')) localname=localname.substring(0,localname.length-12);
if (url.includes('.html') || url.includes('.json') ) localname=localname.substring(0,localname.length-8);
if (!list[k]) list[k]={};
list[k][localname]=url;
}
}
});
// load external template and data
for (let k of Object.keys(list) ) {
console.log(k, list[k]);
apx.loadfile(list[k], k);
}
//alert(apx.data.firsttimeload)
if (apx.data.firsttimeload) {
// Need to wait the first time apx.data fully load
setTimeout(() => {
app.setuppage();
}, 300);
delete apx.data.firsttimeload;
apx.save();
}else{
app.setuppage();
}
};
app.load = (idtoload, tplname, tpldata) => {
const tpl = apx.data.tpl[tplname]
? apx.data.tpl[tplname]
: `missing template ${tplname}`;
const data = typeof tpldata == "string" ? apx.data.tpldata[tpldata] : tpldata;
document.getElementById(idtoload).innerHTML = Mustache.render(tpl, data);
};
app.setuppage = () => {
// load partial template
document.querySelectorAll("[apptoload]").forEach((e) => {
console.log(e.getAttribute("apptoload"));
eval(e.getAttribute("apptoload"));
});
};
apx.ready(app.setupdata);