class Ark {
static type(type){
let presets = {
"container": "ark"
}
return presets[type];
}
static forPolicy(){ return {
"owner": "_owner",
"ver": "_ver",
"mut": "_mut"
}}
static presetKeys(){return {
...Ark.forPolicy(),
"path": "_path",
"value": "val",
"type": "_type"
}}
static keys_sys(){return Object.keys(Ark.presetKeys());}
static keys_policies(){return Object.values(Ark.forPolicy());}
static keys_values(){return Object.values(Ark.presetKeys());}
static key(key){
return Ark.presetKeys()[key];
}
static fromObj(obj){
let aark = new Ark("")
Ark.keys_sys().forEach(k=>{
if ( Ark.key(k) in obj) aark[Ark.key(k)] = obj[Ark.key(k)]
})
if (obj[Ark.key("type")] == Ark.type("container") ){
obj[Ark.key("value")].forEach(val=>{
if (val in obj) aark[val] = Ark.fromObj(obj[val])
})
}
return aark;
}
constructor(pathString, logging){
this[Ark.key("path")] = pathString;
if (logging) {
Ark.log= new Log()
//console.log(Ark.log)
this[Ark.key("type")] = Ark.type("container")
this[Ark.key("value")] = []
Ark.log.log("write", pathString)
}
}
toStore() {
let st = function(o){
localStorage.setItem(o[Ark.key("path")],JSON.stringify(o))
}
this.apply2(st)
}
apply2(called){
let o = {}
Ark.keys_sys().forEach(k=>{
o[Ark.key(k)] = this[Ark.key(k)]
})
let ca = called(o)
let aark = ca? ca: new Ark("")
if (this[Ark.key("type")] == Ark.type("container")) {
//console.log(this[Ark.key("value")],this)
this[Ark.key("value")].forEach(x=>{aark[x] = this[x].apply2(called)})
}
return aark;
}
/* not used
apply(called){
let ca = called(this)
let aark = ca? ca: new Ark("")
if (this[Ark.key("type")] == Ark.type("container")) {
//console.log(this[Ark.key("value")],this)
this[Ark.key("value")].forEach(x=>{aark[x] = this[x].apply(called)})
}
return aark;
}
static _ls_ark_get(key) {
let ans = localStorage.getItem(key);
if (ans[0]=="[" || ans[0] == "{") {
let parsed = JSON.parse(ans)
ans = new Ark("")
ans = ans.fromObj(parsed)
//console.log(parsed,ans)
}
return ans;
}
*/
static fromStore(path){
let aark = Ark.fromObj(JSON.parse(localStorage.getItem(path)))
//console.log(aark[Ark.key("type")],aark)
if (aark[Ark.key("type")] == Ark.type("container")) {
aark[Ark.key("value")].forEach(x=>{
//console.log(path+".||"+x)
aark[x] = Ark.fromStore(path+"."+x)
})
}
return aark;
}
static to_ls(path){
let aark = new Ark(path)
let temp = Ark._ls_get(path)
Object.values(Ark.presetKeys()).forEach(k=>{
if (k in temp) aark[k] = temp[k]
})
if (temp[Ark.key("type")] == Ark.type("container")) {
temp[Ark.key("value")].forEach(x=>{aark[x] = Ark._ls_ark_get(path+"."+x)})
}
return aark;
}
static _ls_show(){
function forEachKey(callback) {
for (var i = 0; i < localStorage.length; i++) {
callback(localStorage.key(i));
}
}
forEachKey(x=>{console.log(x, localStorage.getItem(x))})
}
static _ls_set(key, value) {
if (typeof value != "string") value = JSON.stringify(value);
return localStorage.setItem(key, value);
}
static _ls_get(key) {
let ans = localStorage.getItem(key);
if (ans[0]=="[" || ans[0] == "{") {
let parsed = JSON.parse(ans)
return parsed;
}
return ans;
}
get(path, wAttrs, attrs) {
if (path == this[Ark.key("path")]) {
if (Ark.log) Ark.log.log("read", path)
if (wAttrs) {
let ans = {attrs: attrs}
ans[Ark.key("value")] = this
return ans
} else {
return this;
}
} else {
let apath = path.split(".");
//console.log("aaa",this,apath[1],this[apath[1]])
if (apath[1] in this){
if (wAttrs) {
attrs = attrs? attrs : {}
Ark.keys_policies().forEach(k=>{
if (k in this) attrs[k] = this[k]
})
return this[apath[1]].get(path, wAttrs, attrs)
} else {
return this[apath[1]].get(path)
}
} else {
console.log("No such path: "+path+" in obj: ",this)
}
}
}
set(path, value) {
if (path == this[Ark.key("path")]) {
console.log("try to change this from a container Ark")
return false;
} else {
let apath = path.split(".");
console.log(this[Ark.key("path")] == apath.slice(0, apath.length-1).join("."), apath.slice(0, apath.length-1).join("."))
if (this[Ark.key("path")] == apath.slice(0, apath.length-1).join(".")) {
let newa = new Ark(this[Ark.key("path")]+"."+apath[apath.length-1])
newa[Ark.key("value")] = value;
if (this[Ark.key("type")] != Ark.type("container")) {
this[Ark.key("type")] = Ark.type("container")
this[Ark.key("value")] = [apath[apath.length-1]]
} else {
console.log(this, this[Ark.key("value")],apath[apath.length-1],this[Ark.key("value")].includes(apath[apath.length-1]))
if (this[Ark.key("value")].includes(apath[apath.length-1])) {
console.log("includes",apath[apath.length-1])
} else {
this[Ark.key("value")].push(apath[apath.length-1])
}
}
this[apath[apath.length-1]] = newa
return newa
}
if (this[Ark.key("value")] && this[Ark.key("type")] == Ark.type("container")) {
console.log("here1",this,apath[0])
this[apath[1]].set(path, value)
} else {
console.log("No path: "+path+" in ",this)
return false;
}
}
if (Ark.log) Ark.log.log("write", path)
}
}