123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- 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 = '<li class="{{liclass}}"><i class="{{iclass}}"></i> <a href="{{href}}" class="ele">{{title}}</a> <em>{{size}}</em></li>';
- Asystem.prototype.status = function() {
- var self = this;
- return new Promise(function(resolve, reject) {
- var ss = [];
- ss.push('<pre onclick="$lib.toggle_admin()">' + $lib.ooa.oget("root/._data.title") + '</pre><hr>')
- _.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('<li><i class="fa fa-database"></i><a href="' + $lib.createnav(self, key) + '"> ' + key + '' +
- '</a>'+ (self.oo[key]().children ?
- '<em>'+_.size(
- self.oo[key]().children) :
- "00")+'</li>')*/
- })
- var ts1 =
- '<div class="sela"><span class="button-dropdown button-dropdown button-dropdown-normal" data-buttons="dropdown">\
- <button class="button button-stacked">\
- ' +
- $lib.ooa.name + '\
- <i class="fa fa-bars"></i></button>\
- <ul class="button-dropdown-list is-below">';
- var ts2 = '</ul>\
- </span></div>'
- ss.push("<br><br><div id=\"menu_roots\"><pre>Roots</pre><hr>" + ts1 +
- '' + _.map($lib._pages, function(val, key) {
- return '<li><a class=" ' + '" onclick="$lib.setCurrentPage(\'' +
- key + '\');">' + key + '</a></li>'
- }).join("\n") + ts2 + '</div>'
- )
- 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;
|