var EventEmitter = require("events").EventEmitter; var opath = require("opath"); var async = require("async"); var getthezip = require("./getthezip"); var unpackzip = require("./unpackzip"); var generatezip = require("./generatezip") var slugger = function(str) { var ret = ("" + str).replace(/\/+/g, '/').replace(/[^\x20-\x7E]/g, '') // ret=decodeURIComponent(ret); ret = ret.replace(/[\<\>]/g, '') if (ret.indexOf("/") == ret.length - 1) { ret = ret.substring(0, ret.length - 1); } return ret } var _ = require("lodash"); function Asystem(oo) { this.oo = oo; this.name = "noname" this._logs = []; } Asystem.prototype.__proto__ = EventEmitter.prototype; Asystem.prototype.slugger = function(str) { return slugger(str); } Asystem.prototype.odelete = function(path) { var self = this; self.omove(path, "trash/" + path) if (self.oo[path]) { _.map(self.oo[path]().children, function(child, name) { self.odelete(child().path) }) delete self.oo[path] } self.emit("status", "deleted " + path) } Asystem.prototype.omove = function(path, topath) { var self = this; var newa = self.oget(topath) var olda = self.oget(path); _.map(olda._relations, function(rels, relsname) { _.map(rels, function(rel, name) { delete rel.ref._relations[rel.context.out] newa.relatesAsToWith(relsname, rel.ref, rel.context.out, rel.context) }) delete olda._relations[relsname] }) _.map(olda._isa, function(isa, name) { newa.isa(isa) delete isa._hasa[name] delete olda._isa[name] }) _.map(olda._hasa, function(hasa, name) { hasa.isa(newa) delete hasa._isa[olda.path] delete olda._hasa[name] }) _.map(olda._inherits, function(inheri, name) { newa.inherits(inheri) delete inheri._extends[olda.path] delete olda._inherits[name] }) _.map(olda._extends, function(extendi, name) { newa.extends(extendi) delete extendi._inherits[olda.path] delete olda._extends[name] }) newa._created = olda._created; newa._data = _.omit(olda._data, []); newa._meta = _.omit(olda._meta, []); _.map(_.omit(olda, electra.ignores), function(val, ke) { newa[ke] = val; }) _.map(olda.parents, function(par, nam) { delete par.children[olda.name]; }) delete olda; self.emit("status", "moved " + path + " to " + topath) } Asystem.prototype.oset = function(path, data) { var self = this; path = self.slugger(path); var ar = path.split("/"); var first = ar.shift(); var prop, obj; if (typeof(self.oo[first]) === "undefined") { self.oo[first] = self.oo(first); self.oo[first]()._isdirty = true; self.emit("status", "auto created " + first + " :" + self.oo(first)().path) } if (ar.length > 0 && ar[ar.length - 1].indexOf(".") === 0) { prop = ar.pop().substring(1); } path = ar.join("/") if (path !== "") { obj = self.oo[first](path)(); } else { obj = self.oo[first]() } if (prop) { obj._isdirty = true; opath.setpath(obj, prop, data); //return obj[prop] || getpath(obj, prop) self.emit("status", "set prop : " + obj.path + " / " + prop + " to" + data) } return obj; } Asystem.prototype.oget = function(path, alternative) { var self = this; path = self.slugger(path); var ar = path.split("/"); var first = ar.shift(); var prop, obj; if (typeof(self.oo[first]) === "undefined") { self.oo[first] = self.oo(first); self.oo[first]()._isdirty = true; self.emit("status", "auto created " + first + " :" + self.oo(first)().path) } if (ar.length > 0 && ar[ar.length - 1].indexOf(".") === 0) { prop = ar.pop().substring(1); } path = ar.join("/") if (path !== "") { obj = self.oo[first](path)(); } else { obj = self.oo[first]() } if (prop) { return obj[prop] || opath.getpath(obj, prop) || alternative; } return obj; } Asystem.prototype.getele = function(action) { var self = this; action = self.slugger(action); var a = action.split("/"); var roo = a.shift(); var ffu; if (a[a.length - 1].indexOf(".") > -1) { //function call; ffu = a.pop().split(".").pop(); } else { //straight } var name = a.join("/"); var eles = self.oo[roo](name)(); if (ffu && eles[ffu]) { return { ele: eles, func: eles[ffu] }; } return { ele: eles } } Asystem.prototype.ofind = function(path) { /* returns {found: "spath"|false , remainder: "rest"|"" , obj: obj}*/ var self = this; path = self.slugger(path); var ar = path.split("/"); var first = ar.shift(); if (typeof(self.oo[first]) === "undefined") { return { found: false } } else { var pa = self.oo[first]() var ch = ""; var rest = []; while (ar.length) { ch = ar.shift(); if (pa.children[ch]) { pa = pa.children[ch](); } else { rest.push(ch) } } return { found: pa.path, /* is false if not found*/ remainder: rest.join("/"), /* is not "" if not found rest of path*/ obj: pa /* the found object*/ } } } Asystem.prototype.rev_conv = function(obj, path) { var self = this; var zzz = self.oget(obj.path); zzz.data(obj.data) zzz._created = new Date(obj.created); zzz.metas(obj.meta); if (_.size(obj.rel) > 0) { _.map(obj.rel, function(rels, name) { _.map(rels, function(dats, relator) { if (name === dats.in) { } else { zzz.relatesAsToWith(dats.out, self.oget(relator), dats.in, dats) } }) }) } if (obj.inherits && obj.inherits.length) { obj.inherits.map(function(alp) { zzz.inherits(self.oget(alp)) }) } if (obj.extends && obj.extends.length) { obj.extends.map(function(alp) { zzz.extends(self.oget(alp)) }) } if (obj.isa && obj.isa.length) { obj.isa.map(function(alp) { zzz.isa(self.oget(alp)) }) } if (obj.hasa && obj.hasa.length) { obj.hasa.map(function(alp) { self.oget(alp).isa(zzz) }) } _.map(_.omit(obj, ["name", "hasa", "meta", "data", "parents", "children", "path", "_rootname", "created", "rel", "inherits", "extends", "isa", "children"]), function( val, ke) { if (typeof(val) === "string" && val.indexOf("$$$FUNCTION$$$") === 0) { // console.log(val.split("$$$FUNCTION$$$").pop()) if (zzz._metas && zzz._metas.protected && zzz._metas.protected[ke]) { } else { var ttt = {}; eval("(function(tt){ tt.func = " + val.split("$$$FUNCTION$$$").pop() + ";})(ttt)"); zzz[ke] = ttt.func; } } else { if (typeof(val) === "object") { var r = {} _.map(val, function(v, kp) { if (typeof(v) === "string" && v.indexOf("$$$FUNCTION$$$") === 0) { var ttt = {}; eval("(function(tt){ tt.func = " + v.split("$$$FUNCTION$$$").pop() + ";})(ttt)"); r[kp] = ttt.func; } else { r[kp] = v } }) zzz[ke] = r; } else { zzz[ke] = val } } }) if (obj.children && obj.children.length) { obj.children.map(function(child) { self.rev_conv(child) }) } } Asystem.prototype.save = function() { var self = this; return new Promise(function(resolve, reject) { $lib.exporter(self, function(err, data) { $lib.generate_thezip(self.name + "-" + $lib.getTimedName() + ".zip", $lib.arrayedToObject(data, "name"), function(err, zz) { console.log(err); resolve(zz) }) }) }) } Asystem.prototype.exporter = function(cb) { var self = this; var obj = self.oo; var stores = _.keys(obj).filter(function(a) { return typeof(obj[a]) === "function" }).map(function(a) { return obj[a]() }).map(function(a) { var da = a.exportSync(""); return da }); stores.unshift({ "name": "namespace.manifest", "data": { "namespace": self.name } }) var ret = {} stores.map(function(a) { ret[a.name] = a; }) cb(null, ret) } $linkstr = '
  • {{title}} {{size}}
  • '; Asystem.prototype.status = function() { var self = this; return new Promise(function(resolve, reject) { var ss = []; ss.push('
    ' + $lib.ooa.oget("root/._data.title") + '

    ') _.keys(self.oo).filter(aa => ["xx"].indexOf(aa) === -1 && typeof(self.oo[aa]) === "function").map(function(key) { ss.push($lib.template($linkstr, { href: $lib.createnav(self, key), title: self.oget(key + "/._data.title", key), iclass: self.oget(key + "/._data.iconclass", "fa fa-database"), liclass: key, size: _.size(self.oo[key]().children) })) /* ss.push('
  • ' + key + '' + ''+ (self.oo[key]().children ? ''+_.size( self.oo[key]().children) : "00")+'
  • ')*/ }) var ts1 = '
    \ \
      '; var ts2 = '
    \
    ' ss.push("

    Roots

    " + ts1 + '' + _.map($lib._pages, function(val, key) { return '
  • ' + key + '
  • ' }).join("\n") + ts2 + '
    ' ) resolve(ss.join("\n")); }) } Asystem.prototype.log = function() {} Asystem.prototype.loadSite = function(url) { var self = this; return new Promise(function(resolve, reject) { var importer = self.importer_zip() importer.on("done", function() { console.log("DONENENENENE"); }) importer.on("link", function() { console.log("DONENENENENE"); }) $lib.getthezip(url) .catch(function() { console.log("ERROR:", arguments); return false; }) .then($lib.unpack_thezip2(importer)) }) } Asystem.prototype.xloadSite = function(url) { console.log("getting:::", url) var self = this; } Asystem.prototype.loadZip = function(url, callback, linked) { var self = this; if (linked) { self._linked = self._linked || {}; self._linked[linked] = url; } getthezip(url) .then(function(data) { self.emit("test","got data "+data.length) unpackzip(self.importer_zip())(data) .then( function() { self.emit("test","early ready yes") if (self._loadqueue && self._loadqueue.length) { var ff = self._loadqueue.shift() self.loadZip(ff[1], callback, ff[0]); } else { if (self.name_imported) { self.name = self.name_imported; } callback(null, self); } } ) .catch(console.log) }) .catch(console.log) } Asystem.prototype.saveZip = function() { var self = this; return new Promise(function(resolve, reject) { self.exporter(function(err, data) { var seperate_save = _.keys(data).filter(function(name) { return self._linked[name + ".link"] }) var ddata = _.omit(data, seperate_save); seperate_save.map(function(om){ var url=self._linked[om + ".link"].split("/"); url.pop(); url.push(self.name + "-" + om + ".zip"); ddata[om+".link"] = url.join("/"); }) generatezip(self.name + "-" + "" + ".zip", ddata, function(err, zz) { async.mapSeries(seperate_save, function(name, next) { var dda = {}; dda[name] = data[name]; generatezip(self.name + "-" + name + ".zip", dda, next) }, function(err, all) { all.push(zz); resolve(all) }) }) }) }) } Asystem.prototype.importer_zip = function() { var self = this; var emname = "system/emmm/a"; //+ $lib.random(0xffffff) var emmm = self.oget(emname); emmm.on("file", function(filename, data) { if (filename.indexOf(".link") > -1) { emmm.emit("link", data); self._loadqueue = self._loadqueue || []; self._loadqueue.push([filename, data]) // self.loadSite(data); } }) emmm.on("json", function(filename, data) { self.emit("gotfile", filename); self.rev_conv(data) }) emmm.on("namespace", function(data, cont) { self.emit("gotfile", "namespace", data.namespace); self.name_imported = data.namespace; cont(); }) emmm.on("nonamespace", function(cont) { //self.name = url.split("/").pop().split(".").shift(); self.emit("gotfile", "nonamespace", self.name); cont(); }) return emmm } module.exports = Asystem;