deploy1 6 년 전
부모
커밋
e2f9b9aaab
2개의 변경된 파일520개의 추가작업 그리고 0개의 파일을 삭제
  1. 500 0
      index.js
  2. 20 0
      package.json

+ 500 - 0
index.js

@@ -0,0 +1,500 @@
+var EventEmitter = require("events").EventEmitter;
+
+var opath = require("opath");
+var async = require("async");
+
+
+var getthezip = require("libs/getthezip");
+var unpackzip = require("libs/unpackzip");
+var generatezip = require("libs/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;

+ 20 - 0
package.json

@@ -0,0 +1,20 @@
+{
+  "name": "asystem",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "repository": {
+    "type": "git",
+    "url": "ssh://git@git.tum.dk/tum.dk/asystem.git"
+  },
+  "author": "",
+  "license": "ISC",
+  "dependencies": {
+    "async": "^2.6.1",
+    "libs": "git+ssh://git@git.tum.dk/tum.dk/libs",
+    "opath": "git+ssh://git@git.tum.dk/tum.dk/opath"
+  }
+}