1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146 |
- var EventEmitter = require("events").EventEmitter;
- var opath = require("opath");
- var async = require("async");
- var tool = require("tools2")();
- var getthezip = require("libs/getthezip");
- var unpackzip = require("libs/unpackzip");
- var generatezip = require("libs/generatezip");
- var random = require("sharedkey-thing").randoma(0xbadebabe)("#CHANGE BEFORE CHRISTMAS#");
- var _ = require("lodash");
- var templates = require("./templates");
- var ignores =
- "_created _data _events _extends _hasa _inherits _isa _maxListeners _metas _references _relations _rootname addListener children data data2 domain emit emita emitas eventNames export export2 exportFlatSync exportSerialised exportSync exporta extends getMaxListeners getStuff getStuff2 getStuff3 inherits innerExport isa listenerCount listeners metas metas2 metas3 name on once parents path pickdeep pingup prependListener prependOnceListener rawListeners relatesAsToWith removeAllListeners removeListener setMaxListeners test unextends uninherits unisa withAll withAllSync"
- .split(" ");
- function template_helpers(self) {
- return {
- "toTextArea": function(str) {
- return '<textarea>' + str + '</textarea>'
- },
- "toWrap": function(str, tag, attr) {
- return '<' + tag + ' ' + attr + '>' + str + '</' + tag + '>';
- },
- toLowerCase: function(str) {
- return str.toLowerCase();
- },
- toUpperCase: function(str) {
- return str.toUpperCase();
- },
- "pagecontent": function(obj) {
- return self.template(obj._data.content, obj);
- },
- "siblings": function(obj) {
- return [_.keys(obj.parents).shift()].map(function(parentpath) {
- var ares = _.map(obj.parents[parentpath] ? obj.parents[parentpath].children : [], function(a) {
- return a();
- })
- ares.sort(sorter(function(a) {
- return a._metas.menu_order || 100
- }, -1));
- var ss = _.map(ares, function(child, name) {
- return child.path
- });
- if (ss.length > 12) {
- var cindex = ss.indexOf(obj.path);
- cindex = cindex < 6 ? 6 : cindex;
- var spp = _.map(ares, renderlin(obj))
- var sasa = [].concat(spp.slice(cindex - 6, cindex + 7))
- } else {
- var sasa = _.map(ares, renderlin(obj))
- }
- return sasa.join("\n")
- }).join("\n")
- },
- "debugbump": function(obj) {
- return '<pre>' + obj + '</pre>'
- },
- "elementstree": function(obj, getfrom) {
- var ob_ = obj;
- if (getfrom) {
- if (getfrom.indexOf("!") === 0) {
- if (obj.name == getfrom.substring(1)) {
- return "";
- }
- } else {
- if (getfrom === "parents") {
- return gpath(obj, []).reverse().map(function(p) {
- return self.atypes.elementstree(self.oget(p), {
- "path": "_data.elements",
- "childrenpath": "elements"
- });
- }).join("\n");
- } else {
- if (getfrom === "inherits") {
- var tmp = obj.getStuff("_data.elements", function(a) {
- return a
- }, "_data.templatechild")
- var aa = tmp.map(function(a) {
- return a.name
- });
- aa.reverse();
- return aa.map(function(p) {
- return self.atypes.elementstree(self.oget(p), {
- "path": "_data.elements",
- "unc": p.replace(/\//g, "__"),
- "childrenpath": "elements"
- });
- }).join("\n");
- } else {
- ob_ = self.oget(getfrom);
- }
- }
- }
- }
- return self.atypes.elementstree(ob_, {
- "path": "_data.elements",
- "childrenpath": "elements"
- });
- },
- "ootree": function(obj, getfrom, datapath, childrenpath) {
- var ob_ = obj;
- var datapath = datapath || "_data.elements";
- var childrenpath = childrenpath || "elements";
- if (getfrom && getfrom !== "") {
- if (getfrom.indexOf("!") === 0) {
- if (obj.name == getfrom.substring(1)) {
- return "";
- }
- } else {
- if (getfrom === "parents") {
- return gpath(obj, []).reverse().map(function(p) {
- return self.atypes.elementstree(self.oget(p), {
- "path": datapath,
- "childrenpath": childrenpath
- });
- }).join("\n");
- } else {
- if (getfrom === "inherits") {
- var tmp = obj.getStuff(datapath, function(a) {
- return a
- }, datapath)
- var aa = tmp.map(function(a) {
- return a.name
- });
- aa.reverse();
- return aa.map(function(p) {
- return self.atypes.elementstree(self.oget(p), {
- "path": datapath,
- "unc": p.replace(/\//g, "__"),
- "childrenpath": childrenpath
- });
- }).join("\n");
- } else {
- ob_ = self.oget(getfrom);
- }
- }
- }
- }
- return self.atypes.elementstree(ob_, {
- "path": datapath,
- "childrenpath": childrenpath
- });
- },
- "parents": function(obj, ss_, sss_) {
- if (typeof(ss_) !== "undefined" && typeof(sss_) !== "undefined") {
- return gpath(obj, []).slice(parseInt(ss_), parseInt(sss_)).map(function(p, i) {
- return '<a href="#' + p + '" elepath="' + p + '" class="ele elecontent button button-rounded button-' + (i == 0 ? 'small' : 'tiny') + ' button-royal">' +
- self.oget(p +
- "/._data.title", p) + '</a>';
- }).join("\n");
- } else {
- return gpath(obj, []).slice(1).map(function(p, i) {
- return '<a href="#' + p + '" elepath="' + p + '" class="ele elecontent button button-rounded button-' + (i == 0 ? 'small' : 'tiny') + ' button-royal">' +
- self.oget(p +
- "/._data.title", p) + '</a>';
- }).join("\n");
- }
- },
- "parent": function(obj) {
- return _.map(obj.parents, function(par, nam) {
- self.rendertemplate(par)
- }).join("\n");
- },
- "children2": function(obj) {
- var ret = [];
- _.map(obj.children, function(child) {
- var cho = typeof(child) === "function" ? child() : child;
- ret.push(self.rendertemplate(cho))
- })
- return ret.join("\n")
- },
- "children": function(obj) {
- var ret = [];
- _.map(obj.children, function(child) {
- var cho = typeof(child) === "function" ? child() : child;
- ret.push(renderlin(cho)(cho, cho.name))
- })
- return ret.join("\n");
- },
- "children3": function(obj, sortera) {
- var ret = [];
- var tmp = obj.getStuff3("_data.templatechild", function(a) {
- return a
- }, "_data.templatechild")
- var chos = allchildren(obj).filter(function(a) {
- return a.path !== obj.path
- })
- /* var chos = _.map(obj.children, function(child) {
- return typeof(child) === "function" ? child() : child;
- })
- */
- chos.sort(sorter(function(a) {
- return opath.get(a, sortera) || -1000;
- }, 1));
- chos.map(function(cho) {
- ret.push(self.template(tmp[0].data, cho))
- })
- return ret.join("\n")
- },
- "relations": function(obj) {
- var stash = [];
- _.map(obj._relations, function(rels, relname) {
- var relators = _.keys(rels);
- var sss = relators.slice(0, 25).map(function(relator) {
- return '<li elepath="' + relator + '"><a href="#' + relator + '" class="elecontent" title="' + JSON.stringify(rels[relator].context).toHtmlEntities() +
- '">' + relator + '</a> </li> '
- }).join("\n");;
- stash.push([relname + "(" + relators.length + ")", sss]);
- })
- var ss = ('<div class="stash">' + stash.map(function(a) {
- return '<div class="stt"> <div class="st1">' + (a[0] === false ? "" : a[0] + "") + '</div><div class="st2">' + a[1] +
- '</div> </div>'
- }).join("\n") + '</div>');
- return ss
- },
- "grandchildren": function(obj) {
- var ret = [];
- _.map(obj.children, function(child) {
- var cho = typeof(child) === "function" ? child() : child;
- ret = [].concat.apply(ret, _.map(cho.children, renderlin(cho)))
- })
- return ret
- },
- "created": function(obj) {
- return obj._created.toJSON();
- },
- "_isa": function(obj) {
- return _.map(obj._isa, renderlin(obj)).join("\n")
- },
- "_isa2": function(obj) {
- return _.map(obj._isa, function(o, n) {
- return self.rendertemplate(o)
- }).join("\n")
- },
- "_isa3": function(obj, a, b) {
- return _.map(obj._isa, function(o, n) {
- return self.rendertemplate(o)
- }).slice(parseInt(a), parseInt(b)).join("\n")
- },
- "_hasa": function(obj) {
- return _.map(obj._hasa, renderlin(obj)).join("\n")
- },
- "findall": function(obj, withprop, target_) {
- var target = target_ || "content";
- var aa = []
- _.keys(self.oo).filter(aa => ["trash", "system", ""].indexOf(aa) === -1 && typeof(self.oo[aa]) === "function").map(function(key) {
- aa = [].concat.apply(aa, allchildren(self.oo[key]()).filter(function(a) {
- return a._isdirty
- }))
- })
- return aa.map(function(a) {
- return renderlin2(obj, {
- target: target
- })(a, a.name)
- }).join("\n");
- },
- "jsondata": function(obj) {
- return JSON.stringify(obj._data, true, 2).toHtmlEntities();
- },
- "visualize": function(obj) {
- var t = document.getElementById("visa_" + obj.name);
- if (t) {
- t.parentElement.removeChild(t)
- }
- setTimeout(function() {
- var cols = {
- parent: "#000000",
- child: "#ff00ff",
- extends: "#00ff00",
- inherits: "#ff0000",
- isa: "#ff9900",
- hasa: "#00ff99"
- }
- function lokup(tp) {
- return cols[tp];
- }
- var graph = Viva.Graph.graph();
- var graphics = Viva.Graph.View.svgGraphics(),
- nodeSize = 10;
- //allchildren(obj.parents[_.keys(obj.parents).shift()])
- var oba = obj.parents[_.keys(obj.parents).shift()] || obj;
- allchildren(oba).filter(function(c) {
- return c.path.indexOf("trash/") === -1 && c.path.indexOf("folders/") === -1 && c.path.indexOf("blobs/") === -1
- }).slice(0, 100).map(function(c) {
- graph.addNode(c.path);
- _.map(c.children, function(child, name) {
- graph.addLink(c.path, child().path, 'child')
- });
- _.map(c._isa, function(child, name) {
- graph.addLink(c.path, child.path, 'isa')
- });
- _.map(c._hasa, function(child, name) {
- graph.addLink(c.path, child.path, 'hasa')
- });
- _.map(c._extends, function(child, name) {
- graph.addLink(c.path, child.path, 'extends')
- });
- _.map(c._inherits, function(child, name) {
- graph.addLink(c.path, child.path, 'inherits')
- });
- _.map(c._relations, function(rels, relname) {
- _.map(rels, function(child, name) {
- graph.addLink(c.path, name, relname)
- })
- });
- })
- var idas = 1;
- var ida = function() {
- return "ida" + (idas++)
- }
- graphics.node(function(node) {
- var ui = Viva.Graph.svg('g');
- var circle = Viva.Graph.svg('circle')
- .attr('cx', 2)
- .attr('cy', 2)
- .attr('r', 2)
- .attr('fill', obj.path === node.id ? "#ff0000" : "#dadada");
- ui.append(circle);
- var svgText = Viva.Graph.svg('text').attr('font-size', '12px').attr('y', '4px').text(node.id.split("/").slice(-2).join("/"));
- ui.append(svgText);
- return ui;
- }).placeNode(function(nodeUI, pos) {
- nodeUI.attr('transform',
- 'translate(' +
- (pos.x - nodeSize / 2) + ',' + (pos.y - nodeSize / 2) +
- ')');
- });
- var createMarker = function(id) {
- return Viva.Graph.svg('marker')
- .attr('id', id)
- .attr('viewBox', "0 0 10 10")
- .attr('refX', "10")
- .attr('refY', "5")
- .attr('markerUnits', "strokeWidth")
- .attr('markerWidth', "10")
- .attr('markerHeight', "5")
- .attr('orient', "auto");
- },
- marker = createMarker('Triangle');
- marker.append('path').attr('d', 'M 0 0 L 10 5 L 0 10 z');
- // Marker should be defined only once in <defs> child element of root <svg> element:
- var defs = graphics.getSvgRoot().append('defs');
- defs.append(marker);
- var geom = Viva.Graph.geom();
- graphics.link(function(link) {
- // Notice the Triangle marker-end attribe:
- var isParent = (link.data === 'parent');
- var ui = Viva.Graph.svg('g');
- var p = Viva.Graph.svg('path')
- .attr('stroke', lokup(link.data) || "#000000")
- .attr('fill', 'none')
- .attr('marker-end', 'url(#Triangle)')
- .attr('id', ida())
- ui.isParent = isParent; // remember for future.
- ui.linka = link.data;
- ui.ry = 50 //- (Math.random() * 100);
- ui.pp = p;
- var svgText = Viva.Graph.svg('text').attr("dy", -4).attr("class", "slabel");
- var textPath = Viva.Graph.svg("textPath");
- textPath.attr("href", "#" + p.id).attr("startOffset", "25%").text(link.data + " >");
- svgText.append(textPath)
- ui.tt = svgText;
- ////<textPath xlink:href="#'+link.id+'">
- // svgText.attr('xlink', link.id);
- ui.append(svgText)
- ui.append(p);
- return ui;
- }).placeLink(function(linkUI, fromPos, toPos) {
- // using arc command: http://www.w3.org/TR/SVG/paths.html#PathDataEllipticalArcCommands
- //var data = 'M' + fromPos.x + ',' + fromPos.y +
- // ' A 4,' + linkUI.ry + ',-' + linkUI.ry + ',0,1,' + toPos.x + ',' + toPos.y;
- var data = 'M' + fromPos.x + ',' + fromPos.y +
- ' C ' + (fromPos.x + 50) + ' ' + (fromPos.y + 10) + ',' + (toPos.x - 50) + ' ' + (toPos.y - 10) + ',' + toPos.x + ' ' + toPos.y;
- linkUI.pp.attr("d", data);
- return;
- // linkUI.tt.attr("y", ((fromPos.y + toPos.y) / 2) - linkUI.ry);
- // linkUI.tt.attr("x", ((fromPos.x + toPos.x) / 2) );
- });
- var layout = Viva.Graph.Layout.forceDirected(graph, {
- springLength: 80,
- springCoeff: 0.00001,
- dragCoeff: 0.001,
- gravity: -1.2
- });
- var renderer = Viva.Graph.View.renderer(graph, {
- graphics: graphics,
- container: document.getElementById("visa_" + obj.name),
- layout: layout
- });
- renderer.run();
- }, 200)
- return '<div id="' + "visa_" + obj.name + '" class="svga">v</div>'
- }
- }
- }
- function gpath(cc, p) {
- if (cc.parents) {
- var k = _.keys(cc.parents).shift();
- if (k) {
- return gpath(cc.parents[k], p.concat(cc.path))
- } else {
- return p.concat(cc.path)
- }
- }
- }
- function renderlin2(obj, opt) {
- return function(child, name) {
- var pp = typeof(child) === "function" ? child() : child;
- return '<a href="#' + pp.path + '" elepath="' + pp.path + '" eletarget="' + opt.target + '" class="elecontent button button-rounded button-small button-' + (pp.path ==
- obj.path ? 'royal' : 'inverse') +
- '">' + (pp._data.title ?
- pp._data.title : pp.name) +
- '</a>'
- }
- }
- function renderlin(obj) {
- return function(child, name) {
- var pp = typeof(child) === "function" ? child() : child;
- return '<a href="#' + pp.path + '" elepath="' + pp.path + '" class="elecontent button button-rounded button-small button-' + (pp.path == obj.path ? 'royal' : 'inverse') +
- '">' + (pp._data.title ?
- pp._data.title : pp.name) +
- '</a>'
- }
- }
- function Asystem(oo) {
- this.oo = oo;
- this.name = "noname"
- this._logs = [];
- this._linked = {};
- this._templatefunction = templates(this, template_helpers(this));
- }
- Asystem.prototype.__proto__ = EventEmitter.prototype;
- Asystem.prototype.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
- }
- Asystem.prototype.t_content = function(path) {
- return this.oget(path)._data.content
- }
- 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("deleted", path)
- }
- Asystem.prototype.ozap = function(path) {
- var t = path.split("/");
- var ta = t.pop();
- delete this.oget(t.join("/")).children[ta];
- this.emit("deleted", path)
- }
- Asystem.prototype.omove = function(path, topath) {
- var self = this;
- var newa = self.oget(topath)
- var olda = self.oget(path);
- var ss = olda.exportSync();
- if (!ss) {
- return false;
- }
- ss.name = topath.split("/").pop();
- ss.path = topath;
- var np = topath.split("/")
- np.pop();
- _.map(olda._relations, function(rels, relsname) {
- _.map(rels, function(rel, name) {
- delete rel.ref._relations[rel.context.out]
- })
- delete olda._relations[relsname]
- })
- _.map(olda._isa, function(isa, name) {
- delete isa._hasa[name]
- delete olda._isa[name]
- })
- _.map(olda._hasa, function(hasa, name) {
- delete hasa._isa[olda.path]
- delete olda._hasa[name]
- })
- _.map(olda._inherits, function(inheri, name) {
- delete inheri._extends[olda.path]
- delete olda._inherits[name]
- })
- _.map(olda._extends, function(extendi, name) {
- delete extendi._inherits[olda.path]
- delete olda._extends[name]
- })
- _.map(olda.parents, function(par, nam) {
- delete par.children[olda.name];
- })
- _.keys(olda.children, function(key) {
- delete olda.children[key];
- })
- delete olda;
- self.rev_conv2(ss, np.join("/"));
- self.emit("moved", path, topath)
- }
- Asystem.prototype.oclone = function(path, topath) {
- var self = this;
- var newa = self.oget(topath)
- var olda = self.oget(path);
- var ss = olda.exportSync();
- ss.name = topath.split("/").pop();
- ss.path = topath;
- var np = topath.split("/")
- np.pop();
- self.rev_conv2(ss, np.join("/"));
- self.emit("cloned", path, topath)
- }
- Asystem.prototype.oset = function(path_, data) {
- var self = this;
- var 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("rootcreated", self.oo[first]())
- }
- if (ar.length > 0 && ar[ar.length - 1].indexOf(".") === 0) {
- prop = ar.pop().substring(1);
- }
- path = ar.join("/")
- if (path !== "") {
- var exists = self.ofind(path_);
- if (exists.remainder !== "") {
- obj = self.oo[first](path)();
- self.emit("created1", obj)
- } else {
- obj = self.oo[first](path)();
- }
- } else {
- obj = self.oo[first]()
- }
- if (prop) {
- obj._isdirty = true;
- opath.set(obj, prop, data);
- //return obj[prop] || getpath(obj, prop)
- self.emit("changed", obj.path + "/" + prop, data)
- }
- return obj;
- }
- Asystem.prototype.ogetsilent = function(path_, alternative) {
- var self = this;
- var 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);
- }
- if (ar.length > 0 && ar[ar.length - 1].indexOf(".") === 0) {
- prop = ar.pop().substring(1);
- }
- path = ar.join("/")
- if (path !== "") {
- var exists = self.ofind(path_);
- if (exists.remainder !== "") {
- obj = self.oo[first](path)();
- } else {
- obj = self.oo[first](path)();
- }
- } else {
- obj = self.oo[first]()
- }
- if (prop) {
- return obj[prop] || opath.get(obj, prop) || alternative;
- }
- return obj;
- }
- Asystem.prototype.oget = function(path_, alternative) {
- if (typeof(path_) === "undefined") {
- return false;
- }
- var self = this;
- var 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("rootcreated", self.oo(first)())
- }
- if (ar.length > 0 && ar[ar.length - 1].indexOf(".") === 0) {
- prop = ar.pop().substring(1);
- }
- path = ar.join("/")
- if (path !== "") {
- var exists = self.ofind(path_);
- if (exists.remainder !== "" && exists.remainder.indexOf("/") > -1) {
- obj = self.oo[first](path)();
- obj._isdirty = true;
- self.emit("created2", obj)
- } else {
- obj = self.oo[first](path)();
- }
- } else {
- obj = self.oo[first]()
- }
- if (prop) {
- return obj[prop] || opath.get(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) {
- if (typeof(obj) !== "object" || obj == null) {
- return false;
- }
- var self = this;
- var zzz = self.ogetsilent(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 {
- // console.log("RR: ", obj.path, relator, name, dats.in, dats.out);
- zzz.relatesAsToWith(dats.out, self.ogetsilent(relator), dats.in, dats)
- }
- })
- })
- }
- if (obj.inherits && obj.inherits.length) {
- obj.inherits.map(function(alp) {
- zzz.inherits(self.ogetsilent(alp))
- })
- }
- if (obj.extends && obj.extends.length) {
- obj.extends.map(function(alp) {
- zzz.extends(self.ogetsilent(alp))
- })
- }
- if (obj.isa && obj.isa.length) {
- obj.isa.map(function(alp) {
- zzz.isa(self.ogetsilent(alp))
- })
- }
- if (obj.hasa && obj.hasa.length) {
- obj.hasa.map(function(alp) {
- self.ogetsilent(alp).isa(zzz)
- })
- }
- _.map(_.omit(obj, ["getStuff3", "name", "hasa", "meta", "data", "parents", "children", "path", "_rootname", "created", "rel", "inherits", "extends", "isa", "children",
- "_isdirty"
- ]),
- function(
- val,
- ke) {
- if (typeof(val) === "string" && val.indexOf("$$$FUNCTION$$$") === 0) {
- if (zzz._metas && zzz._metas.protected && zzz._metas.protected[ke]) {} else {
- var ttt = {};
- try {
- eval("(function(tt){ tt.func = " + val.split("$$$FUNCTION$$$").pop() + ";})(ttt)");
- zzz[ke] = ttt.func;
- } catch (ea) {
- console.log("error func: ", ea)
- }
- }
- } else {
- if (typeof(val) === "object") {
- var r = {}
- _.map(val, function(v, kp) {
- if (typeof(v) === "string" && v.indexOf("$$$FUNCTION$$$") === 0) {
- var ttt = {};
- try {
- eval("(function(tt){ tt.func = " + v.split("$$$FUNCTION$$$").pop() + ";})(ttt)");
- r[kp] = ttt.func;
- } catch (ea) {
- console.log("error func: ", ea)
- }
- } else {
- r[kp] = v
- }
- })
- zzz[ke] = r;
- } else {
- zzz[ke] = val
- }
- }
- })
- delete obj._isdirty;
- if (obj.children && obj.children.length) {
- obj.children.map(function(child) {
- self.rev_conv(child)
- })
- }
- }
- Asystem.prototype.rev_conv2 = function(obj, path) {
- var self = this;
- var zzz = self.ogetsilent(path + "/" + obj.name);
- 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 {
- // console.log("RR: ", obj.path, relator, name, dats.in, dats.out);
- zzz.relatesAsToWith(dats.out, self.ogetsilent(relator), dats.in, dats)
- }
- })
- })
- }
- if (obj.inherits && obj.inherits.length) {
- obj.inherits.map(function(alp) {
- zzz.inherits(self.ogetsilent(alp))
- })
- }
- if (obj.extends && obj.extends.length) {
- obj.extends.map(function(alp) {
- zzz.extends(self.ogetsilent(alp))
- })
- }
- if (obj.isa && obj.isa.length) {
- obj.isa.map(function(alp) {
- zzz.isa(self.ogetsilent(alp))
- })
- }
- if (obj.hasa && obj.hasa.length) {
- obj.hasa.map(function(alp) {
- self.ogetsilent(alp).isa(zzz)
- })
- }
- _.map(_.omit(obj, ["getStuff3", "name", "hasa", "meta", "data", "parents", "children", "path", "_rootname", "created", "rel", "inherits", "extends", "isa", "children",
- "_isdirty"
- ]),
- 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 = {};
- try {
- eval("(function(tt){ tt.func = " + val.split("$$$FUNCTION$$$").pop() + ";})(ttt)");
- zzz[ke] = ttt.func;
- } catch (ea) {
- console.log("error func: ", ea)
- }
- }
- } else {
- if (typeof(val) === "object") {
- var r = {}
- _.map(val, function(v, kp) {
- if (typeof(v) === "string" && v.indexOf("$$$FUNCTION$$$") === 0) {
- var ttt = {};
- try {
- eval("(function(tt){ tt.func = " + v.split("$$$FUNCTION$$$").pop() + ";})(ttt)");
- r[kp] = ttt.func;
- } catch (ea) {
- console.log("error func: ", ea)
- }
- } else {
- r[kp] = v
- }
- })
- zzz[ke] = r;
- } else {
- zzz[ke] = val
- }
- }
- })
- delete obj._isdirty;
- if (obj.children && obj.children.length) {
- obj.children.map(function(child) {
- self.rev_conv2(child, zzz.path)
- })
- }
- }
- /*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" && a !== "namespace"
- }).map(function(a) {
- return obj[a]()
- }).map(function(a) {
- var da = a.exportSync("");
- return da
- });
- stores.unshift({
- "name": "namespace.manifest",
- "data": self.oget("namespace")._data
- })
- var ret = {}
- stores.map(function(a) {
- ret[a.name] = a;
- })
- cb(null, ret)
- }
- Asystem.prototype.log = function() {}
- Asystem.prototype.template = function() {
- return this._templatefunction.apply(this, arguments)
- }
- Asystem.prototype.loadInto = function(url, callback, linked) {
- var self = this;
- getthezip(url, function(res) {
- return res.json()
- })
- .then(function(obj) {
- self.rev_conv(obj);
- self.restart();
- })
- }
- Asystem.prototype.loadZipLocal = function(blob, callback) {
- var self = this;
- var importer = self.importer_zip();
- unpackzip(importer)(blob)
- .then(
- function() {
- self.emit("status", "early ready yes yes")
- if (self._loadqueue && self._loadqueue.length) {
- var ff = self._loadqueue.shift()
- self.ozap(importer.path);
- self.loadZip(ff[1], callback, ff[0]);
- } else {
- if (self.name_imported) {
- self.name = self.name_imported;
- }
- self.ozap(importer.path);
- callback(null, self);
- }
- }
- ).catch(console.log)
- }
- Asystem.prototype.loadZip = function(url, callback, linked) {
- var self = this;
- if (linked) {
- self._linked[linked] = url;
- }
- getthezip(url)
- .then(function(data) {
- self.emit("status", "got data " + data.size + " " + data.type)
- var importer = self.importer_zip();
- unpackzip(importer)(data)
- .then(
- function() {
- self.emit("status", "early ready yes yes")
- if (self._loadqueue && self._loadqueue.length) {
- var ff = self._loadqueue.shift()
- self.ozap(importer.path);
- self.loadZip(ff[1], callback, ff[0]);
- } else {
- if (self.name_imported) {
- self.name = self.name_imported;
- }
- self.ozap(importer.path);
- 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 && 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.saveAsJS = function() {
- var self = this;
- return new Promise(function(resolve, reject) {
- self.exporterJS(function(err, data) {
- var tm = {}
- _.map(data, function(dat, name) {
- if (typeof(dat.data) === "object" && dat.data.length) {
- tm[name + ".js"] = {
- data: dat.data.map(function(a) {
- return '$E(' + JSON.stringify(a) + ');'
- }).join("\n")
- };
- } else {
- tm[name] = {
- data: dat.data
- };
- }
- })
- generatezip(self.name + "-" + "" + ".zip", tm, function(err, zz) {
- resolve(zz);
- })
- //console.log("TTTT",tm);
- // console.log("DDD",data);
- })
- })
- }
- Asystem.prototype.exporterJS = function(cb) {
- var self = this;
- var obj = self.oo;
- var stores = _.keys(obj).filter(function(a) {
- return typeof(obj[a]) === "function" && a !== "namespace"
- }).map(function(a) {
- return obj[a]()
- }).map(function(a) {
- var da = a.exportFlatSync(a.name);
- return {
- name: a.name,
- data: da
- }
- });
- stores.unshift({
- "name": "namespace.manifest",
- "data": self.oget("namespace")._data
- })
- var ret = {}
- stores.map(function(a) {
- ret[a.name] = a;
- })
- cb(null, ret)
- }
- Asystem.prototype.importer_zip = function() {
- var self = this;
- self.oget("system/emmm").metas("_hide", true);
- var emname = "system/emmm/a/" + random(0xffffff)
- var emmm = self.oget(emname);
- emmm.metas("unsaveable", true);
- emmm.on("file", function(filename, data, mimen) {
- // console.log("_________________FF: ", filename, mimen)
- if (filename.indexOf(".link") > -1) {
- emmm.emit("link", data);
- self._loadqueue = self._loadqueue || [];
- self._loadqueue.push([filename, data])
- self.emit("status", "gotlink", filename);
- } else {
- self.emit("status", "gotfile", filename + " : " + typeof(data) + " : " + data.substring(0, 20));
- self.oget("folders/" + filename.indexOf("/") > -1 ? filename : "folders/" + filename).data({
- "mime": mimen,
- "data": data
- });
- }
- })
- emmm.on("jsfile", function(filename, data, ns) {
- self.emit("jsfile", filename, data, ns);
- // self.rev_conv(data)
- })
- emmm.on("json", function(filename, data) {
- self.emit("status", "gotfile", filename);
- self.rev_conv(data)
- })
- emmm.on("namespace", function(data, cont) {
- self.emit("status", "gotnamespace", data.namespace);
- self.name_imported = data.namespace;
- self.oget("namespace").data(data).metas({
- "_system": true
- });
- cont();
- })
- emmm.on("nonamespace", function(cont) {
- //self.name = url.split("/").pop().split(".").shift();
- self.emit("status", "gotnonamespace", self.name);
- // self.oget("namespace").data({"namespace":""}).metas({"_system":true});
- cont();
- })
- return emmm
- }
- function createnav(rk, key) {
- return '#' + key;
- }
- $linkstr2 = '<li class="{{liclass}}"><i class="{{iclass}}"></i> <a href="{{href}}" class="eletree">{{title}}</a> <em>{{size}}</em></li>';
- function allchildpaths(obj_) {
- var obj = typeof(obj_) === "function" ? obj_() : obj_;
- return [].concat.apply([obj.path], _.map(obj.children, allchildpaths))
- }
- function allchildren(obj_) {
- var obj = typeof(obj_) === "function" ? obj_() : obj_;
- return [].concat.apply([obj], _.map(obj.children, allchildren))
- }
- function sorter(prop, dir) {
- return function(aa, bb) {
- var a = prop(aa);
- var b = prop(bb);
- return a > b ? dir * -1 : a < b ? dir * 1 : 0;
- }
- }
- Asystem.prototype.t_icon = function(obj) {
- var self = this;
- if (obj._data.iconclass) {
- var dd = self.oget(obj._data.iconclass)._data.data;
- if (!dd) {
- return "";
- }
- if (dd.indexOf("data:") === 0) {
- return '<i><img src="' + dd + '"></i>';
- } else {
- return '<i>' + dd + '</i>';
- }
- }
- return "<i> </i>"
- }
- Asystem.prototype.t_title = function(obj) {
- var self = this;
- //contexts;
- return opath.get(obj._data, "title") || obj.name
- }
- Asystem.prototype.getClasses = function(obj) {
- var s = [];
- if (obj._metas.hide) {
- s.push("hidden");
- }
- if (obj._metas._system) {
- s.push("system");
- }
- if (obj._metas.undeletable) {
- s.push("undeletable");
- }
- return s.join(" ");
- }
- Asystem.prototype.renderme = function(obj, maxlevels, atlevel, viewa) {
- var self = this;
- var ssi = _.size(obj.children);
- var childas = obj;
- if (ssi === 1) {
- var ooj = obj.children[_.keys(obj.children).shift()]()
- if (ooj._metas.skipme) {
- childas = ooj;
- }
- }
- var ssia = allchildpaths(obj).length - 1;
- //(ssi > 0 ? ssi : '') +
- if (obj._metas.pickchild) {
- childas = obj.children[obj._metas.pickchild]();
- }
- if (obj._metas.pickpath) {
- childas = self.oget(obj._metas.pickpath);
- }
- var s = "";
- if (viewa && typeof(viewa) === "function") {
- s += "" + viewa(obj, atlevel);
- }
- if (obj._metas._hide) {
- return "";
- }
- //self.t_title(obj)
- s += '<div class="obj l' + atlevel + ' ' + self.getClasses(obj) + '" eleview="menulink" elepath="' + obj.path + '" atlevel="' + atlevel + '">'
- s += '<a href="#' + obj.path + '" >' + self.t_icon(obj) + obj.name + '<em>' + (ssia > 0 ? ssia : '') + '</em></a>'
- if (atlevel < maxlevels) {
- if (ssi > 0) {
- var xxx = _.map(childas.children, function(cc) {
- return cc()
- })
- xxx.sort(sorter(function(a) {
- return a._metas.menu_order || 100
- }, -1));
- var aa = xxx.slice(0, 25).map(function(cc) {
- return self.renderme(cc, maxlevels, atlevel + 1, viewa)
- }).join("\n")
- return s + '<div class="children">' + aa + '</div></div>'
- } else {
- return s + '</div>';
- }
- } else {
- return s + '</div>'
- }
- }
- Asystem.prototype.render = function() {
- var self = this;
- return new Promise(function(resolve, reject) {
- var rooten = self.oget("root");
- var ss = [self.template('<div id="{{_data.id}}" class="obj root" eleview="" elepath="">', rooten)];
- // ss.push('<pre onclick="$lib.toggle_admin()">' + self.oget("root/._data.title") + '</pre><hr>')
- /* var kka = _.keys(self.oo).filter(aa => ["trash", "system", "root"].indexOf(aa) === -1 && typeof(self.oo[aa]) === "function")
- kka.sort(function(aa, bb) {
- var a = self.oo[aa]()._metas.menu_order;
- var b = self.oo[bb]()._metas.menu_order;
- return a > b ? 1 : a < b ? -1 : 0
- })
- */
- var kkas = _.filter(self.oo, function(sto) {
- return typeof(sto) === "function"
- }).map(function(sto, nam) {
- return sto();
- })
- kkas.sort(sorter(function(a) {
- return a._metas.menu_order || 100
- }, -1));
- var privates = [];
- var publics = [];
- kkas.filter(function(obj) {
- return obj._metas.public !== true
- }).map(function(obj) {
- privates.push(self.rendera("menulink", obj, 0, 0));
- })
- kkas.filter(function(obj) {
- return obj._metas.public === true
- }).map(function(obj) {
- publics.push(self.rendera("menulink", obj, 2, 0));
- })
- publics.map(function(a) {
- ss.push(a);
- })
- /* kka.map(function(key) {
- ss.push(self.rendera("menulink", self.oo[key](), 0, 0));
- })
- */
- ss.push('</div>')
- ss.push('<div id="editorlinks">');
- ss.push('<div id="mmme" class="obj root" eleview="" elepath="">');
- privates.map(function(a) {
- ss.push(a);
- })
- ss.push('</div>');
- ss.push('</div>')
- resolve(ss.join("\n"));
- })
- }
- Asystem.prototype.render_editorlinks = function(levels) {
- var self = this;
- var kkas = _.filter(self.oo, function(sto, k) {
- return typeof(sto) === "function"
- }).map(function(sto, nam) {
- return sto();
- })
- kkas.sort(sorter(function(a) {
- return a._metas.menu_order || 100
- }, -1));
- return kkas.filter(function(obj) {
- return obj._metas._system ? false : true
- }).map(function(obj) {
- return self.renderme(obj, levels || 0, 0);
- }).join("\n") + "<hr>" + kkas.filter(function(obj) {
- return obj._metas._system ? true : false
- }).map(function(obj) {
- return self.renderme(obj, levels || 0, 0);
- }).join("\n");
- }
- Asystem.prototype.rendermenu = function(obj, levels) {
- var self = this;
- var temps = obj.getStuff3("_data.menuobj", function(a) {
- return a
- }, "_data.menuobj")
- if (temps.length > 0) {
- return self.renderme(self.oget(temps[0].data), levels || 0, 0);
- }
- return self.renderme(obj, levels || 0, 0);
- }
- Asystem.prototype.renderq = function(kkey) {
- var self = this;
- return new Promise(function(resolve, reject) {
- var rooten = self.oget("root");
- var ss = [self.template('<div id="{{_data.id}}" class="obj root" eleview="" elepath="">', rooten)];
- // ss.push('<pre onclick="$lib.toggle_admin()">' + self.oget("root/._data.title") + '</pre><hr>')
- if (!self.oo[kkey]) {
- self.oget(kkey);
- }
- ss.push(self.rendera("menulink", self.oo[kkey](), 1, 0));
- ss.push('</div>')
- resolve(ss.join("\n"));
- })
- }
- Asystem.prototype.rendera = function(template) {
- var args = Array.prototype.slice.apply(arguments, [1]);
- if (template == "view") {
- return "VIEW"
- }
- if (template == "menulink") {
- return this.renderme.apply(this, args);
- }
- if (template == "editor") {
- return this.renderEditorTools(args[0], "editor");
- }
- return "NOT VIEW " + template
- }
- //
- Asystem.prototype.renderEditorTools = function(obj, state) {
- var buts = [];
- if (obj.path.indexOf("trash") == 0) {
- buts.push('<button class="elerestore"><i class="fa fa-undo-alt"></i></button>')
- }
- if (state == "editor") {
- buts.push('<button class="eleview"><i class="fa fa-eye"></i></button>')
- if (!(obj._metas.nochildren)) {
- buts.push('<button class="eleplus"><i class="fa fa-plus-circle"></i></button>')
- }
- if (obj.path.indexOf("trash") == -1 && !(obj._metas.undeletable)) {
- buts.push('<button class="eledelete"><i class="fa fa-trash-alt"></i></button>')
- }
- buts.push('<button class="eleexport"><i class="fa fa-file-export"></i></button>')
- } else {
- buts.push('<button class="eleeditor"><i class="fa fa-edit"></i></button>')
- }
- return '<div id="editor" elepath="' + obj.path + '" eleview="editor" class="editorline" ><div class="buttons">' + buts.map(function(a) {
- return a
- }).join(" ") + '</div><div id="editor_context"></div></div>'
- }
- tool.add_filters("editor_sections", function(arr, obj) {
- arr.push({
- title: "Template",
- content: function() {
- return "<div class=\"status\">x</div> <textarea class=htmleditor data-id=\"" + obj.path + "/._data.template\">" + obj._data.template + '</textarea>'
- }
- })
- return arr;
- }, 100, "Template")
- function wrap(tag, obj, content) {
- var args = Array.prototype.slice.apply(arguments, [2]);
- return '<' + tag + ' ' + _.map(obj, (val, prop) => prop + '="' + val + '"').join(" ") + '>' + args.join("\n") + '</' + tag + '>'
- }
- Asystem.prototype.renderEditor = function(obj) {
- var self = this;
- var ss = "";
- //"<div class=\"status\">x</div> <textarea class=htmleditor data-id=\"" + obj.path + "/._data.content\">" + obj._data.content + '</textarea>'
- // ss += self.template(self.oget("system/editor")._data.template,obj);
- var self = this;
- var aa = []
- _.keys(self.oo).filter(aa => ["trash", ""].indexOf(aa) === -1 && typeof(self.oo[aa]) === "function").map(function(key) {
- aa = [].concat.apply(aa, allchildren(self.oo[key]()).filter(function(a) {
- return a.editor_function
- }))
- })
- ss += aa.map(function(a) {
- return wrap("div", {
- elepath: a.path,
- class: "editor_module"
- }, wrap("h3", {}, a.name), wrap("div", {}, a._metas.help), wrap("div", {}, a.editor_function(obj)))
- }).join("\n")
- // return aa.map( function(a){ return renderlin(a)(a,a.name) } ).join("\n");
- ss += '<hr>'
- ss += self.renderEditor2(obj);
- return ss
- }
- String.prototype.toHtmlEntities = function() {
- return this.replace(/./gm, function(s) {
- return "&#" + s.charCodeAt(0) + ";";
- });
- };
- /**
- * Create string from HTML entities
- */
- String.fromHtmlEntities = function(string) {
- return (string + "").replace(/&#\d+;/gm, function(s) {
- return String.fromCharCode(s.match(/\d+/gm)[0]);
- })
- };
- Asystem.prototype.uniq_relnames = function() {
- var self = this;
- var rels = {}
- self.allchildren().map(function(c) {
- _.keys(c._relations).map(function(relname) {
- rels[relname] = rels[relname] || 0;
- rels[relname] = rels[relname] + 1;
- })
- })
- return _.keys(rels).map(function(a) {
- return {
- name: a,
- cnt: rels[a]
- }
- });
- }
- function curryobj(oba, obj) {
- var ret = {}
- _.keys(oba).map(function(name) {
- ret[name] = function() {
- var args = Array.prototype.slice.apply(arguments, [0])
- return oba[name].apply(oba, [obj].concat(args))
- }
- })
- return ret;
- }
- Asystem.prototype.renderEditor2 = function(obj) {
- var self = this;
- var types = self.editorTypes();
- self.otypes = curryobj(types, obj);
- var modas = {}
- // _.map(self.oget("system/modules").children,function(child,name){
- // modas[name] = {name: name, func: child.modulefunc, meta: child._metas}
- //})
- self.allchildren().filter(function(a) {
- return a.modulefunc
- }).map(function(child) {
- modas[child.name] = {
- path: child.path,
- name: child.name,
- func: child.modulefunc,
- meta: child._metas
- }
- })
- // console.log("MODAS", modas);
- /* var allmods = self.allchildren().filter(function(a) {
- return a._metas.modules && a._metas.modules.length > 0;
- })
- console.log("ALLLMODS;", allmods);
- */
- var tabsa = self.oget("system/editor")._metas.tabs || [{
- "title": "Editor",
- modules: [{
- "type": "sourcefulla",
- "title": "Source"
- }]
- }];
- function mkmod2(obj, field, types, fieldholder) {
- var data = "";
- if (modas[field.type]) {
- data = modas[field.type].func.apply(self, [obj, field]);
- } else {
- if (types[field.type]) {
- data = types[field.type](obj, field)
- } else {
- data = self.template(field.data, obj);
- }
- }
- var s = '<div class="module ' + field.type + ' ' + (field.class ? field.class : '') + '" ><div class="head">' + field.title + '</div><div class="content">' + data +
- '</div></div>'
- return s
- }
- function rrtabs(tabs, prevals, level) {
- if (prevals[level]) {} else {
- prevals[level] = (tabs[0] || {}).name
- }
- var s = '<ul class="tabs">' + tabs.map(function(tab) {
- var xx = prevals.slice(0, level + 1);
- xx[level] = tab.name;
- return self.template('<li data-tabis="{{prevalla}}" data-id="{{name}}" class="tablink {{active}}">{{title}}</li>', {
- name: tab.name,
- title: tab.title,
- prevalla: xx.join(","),
- active: (prevals[level] === tab.name ? 'active' : '')
- })
- }).join("\n")
- if (level === 0 && obj._metas.tabs) {
- var xx = prevals.slice(0, level + 1);
- xx[level] = "tabs";
- s += self.template('<li data-tabis="{{prevalla}}" data-id="{{name}}" class="tablink {{active}}">{{title}}</li>', {
- name: "tabs",
- title: "Tabs",
- prevalla: xx.join(","),
- active: (prevals[level] === "tabs" ? 'active' : '')
- })
- }
- s += '</ul>';
- s += '<div class="econ a' + level + ' ' + prevals[level] + '">'
- if (prevals[level] === "tabs") {
- s += mkmod2(obj, {
- "type": "elementseditor",
- "title": "Tabs",
- "path": "_metas.tabs",
- "childrenpath": "tabs"
- }, types)
- }
- s += tabs.map(function(tab) {
- var sa = "";
- if (prevals[level] === tab.name) {
- if (prevals[level] === "custom") {
- var qq = []
- var fff = obj.getStuff("_metas.fields", function(a) {
- return a
- }, "fi");
- fff.map(function(ff) {
- ff.data.map(function(field) {
- if (field.showfor) {
- if (field.showfor == "inheritants" && ff.name !== obj.path) {
- qq.push(field)
- } else {
- if (field.showfor == "children" && obj.path.indexOf(ff.name) > -1 && ff.name !== obj.path) {
- qq.push(field)
- } else {
- if (field.showfor == "all") {
- qq.push(field)
- }
- }
- }
- } else {
- if (field.showifset) {
- if (opath.get(obj, field.showifset)) {
- qq.push(field);
- }
- } else {
- qq.push(field);
- }
- }
- })
- })
- sa += qq.map(function(field, ii) {
- var t = mkmod2(obj, field, types);
- return t;
- }).join("\n");
- }
- if (tab.modules) {
- var qq = []
- tab.modules.map(function(field, ii) {
- if (field.showifset) {
- if (opath.get(obj, field.showifset)) {
- qq.push(field);
- }
- } else {
- if (field.showifsetnot) {
- if (!opath.get(obj, field.showifsetnot)) {
- qq.push(field);
- }
- } else {
- qq.push(field);
- }
- }
- });
- sa += qq.map(function(field, ii) {
- var t = mkmod2(obj, field, types);
- return t;
- }).join("\n");
- }
- if (tab.tabs) {
- sa += rrtabs(tab.tabs, prevals, level + 1);
- }
- }
- return sa
- }).join("");
- s += '</div>';
- return s
- }
- var sss = "";
- var xtabs = tabsa
- if (obj.path.indexOf("system/editor") !== 0) {
- var fff = obj.getStuff("_metas.tabs", function(a) {
- return a
- }, "fi");
- fff.map(function(a) {
- xtabs = xtabs.concat(a.data)
- })
- // sss += rrtabs(obj._metas.tabs || [], self.ctabs, 0);
- // xtabs = xtabs.concat( obj._metas.tabs )
- } else {
- }
- sss += rrtabs(xtabs, self.ctabs, 0)
- return '<div id="editor_inner">' +
- '<div id="timeslider">T</div>' +
- '<div id="editortop" class="' + (self.current !== obj.path ? 'notsame' : 'same') + '"><--view:' + self.current + ' ¬editor:' + obj.path + '</div>' +
- '<div class="eed">' + sss + '</div>' +
- '</div>';
- }
- Asystem.prototype.editorTypes = function() {
- var self = this;
- return require("./editor_types")(self)
- }
- Asystem.prototype.getRenderTemplate = function(obj, which) {
- var self = this;
- var stash = [];
- var templates = []
- var temps = obj.getStuff3("_data.template", function(a) {
- return a
- }, "_data.template")
- var myself;
- var x = {};
- temps.map(function(a) {
- x[a.name] = a
- });
- temps = _.map(x, function(a) {
- return a
- });
- return temps
- }
- Asystem.prototype.rendertemplate2 = function(obj) {
- var self = this;
- var stash = [];
- var templates = []
- var temps = obj.getStuff3("_data.template", function(a) {
- return a
- }, "_data.template")
- var myself;
- var x = {};
- temps.map(function(a) {
- x[a.name] = a
- });
- temps = _.map(x, function(a) {
- return a
- });
- var ss = '<div eletemplate="' + obj.path + '"> ' + self.template(obj._data.template, obj) + ' </div>'
- return ss;
- }
- Asystem.prototype.rendertemplate = function(obj) {
- var self = this;
- var stash = [];
- var templates = []
- var temps = obj.getStuff3("_data.template", function(a) {
- return a
- }, "_data.template")
- var x = {};
- temps.map(function(a) {
- x[a.name] = a
- });
- temps = _.map(x, function(a) {
- return a
- });
- var ss = "";
- temps.reverse();
- var cont = "";
- while (temps.length) {
- var t = temps.pop();
- console.log("T", t.name,t);
- if (t.data.indexOf("{{content}}") > -1) {
- t.data = t.data.replace('{{content}}', cont);
- cont = '<div eletemplate="' + t.name + '">' + self.template(t.data, obj) + '</div>';
- } else {
- cont += '<div eletemplate="' + t.name + '">' + self.template(t.data, obj) + '</div>';
- }
- }
- return cont
- /* var ai = Object.assign({
- "output": output,
- "menu": menus,
- "header": headers,
- "selfie": JSON.stringify(temps, true, 2)
- }, obj);
- var ss = '<div eletemplate="' + outer.name + '"> ' + self.template(otemp, ai) + ' </div>'
- */
- /* var myself;
- var x = {};
- temps.map(function(a) {
- x[a.name] = a
- });
- temps = _.map(x, function(a) {
- return a
- });
- console.log("TEMPLATES:",x, temps);
- if (obj._data.template && obj._data.template.indexOf("{{output}}") > -1) {
- myself = temps.shift();
- }
- var outer = temps.filter(function(a) {
- return a.data.indexOf("{{output}}") > -1
- })
- var otemp = "{{output}}"
- var outera = "";
- if (outer.length) {
- outera = outer[0];
- otemp = outer[0].data;
- }
- var output = temps.filter(function(a) {
- return a.data.indexOf("{{output}}") === -1 && a.name.indexOf("menu") === -1 && a.name.indexOf("header") === -1 && a.from.indexOf("isa_") === -1
- }).map(function(tmp) {
- return '<div eletemplate="' + tmp.name + '"> ' + self.template(tmp.data, obj) + ' </div>';
- }).join("\n")
- var menus = temps.filter(function(a) {
- return a.name.indexOf("menu") > -1
- }).slice(0, 1).map(function(tmp) {
- return '<div eletemplate="' + tmp.name + '"> ' + self.template(tmp.data, obj) + ' </div>';
- }).join("\n")
- var headers = temps.filter(function(a) {
- return a.name.indexOf("header") > -1
- }).slice(0, 1).map(function(tmp) {
- return '<div eletemplate="' + tmp.name + '"> ' + self.template(tmp.data, obj) + ' </div>';
- }).join("\n")*/
- /*
- var isas = temps.filter(function(a) {
- return a.from.indexOf("isa_") > -1
- }).map(function(tmp) {
- console.log("ISA,",tmp);
- if(tmp.inas.length>2){
- var tma=tmp.inas.pop();
- tma=tmp.inas.pop();
- return '<div eletemplate="' + tmp.name + '">IIS ' + self.template(tmp.data, self.oget(tma)) + ' </div>';
- }else{
- return '<div eletemplate="' + tmp.name + '">IIS ' + self.template(tmp.data, self.oget(tmp.name)) + ' </div>';
- }
- }).join("\n")
- */
- /*if (myself) {
- var ai = Object.assign({
- "output": output,
- "menu": menus,
- "header": headers,
- "selfie": JSON.stringify(temps, true, 2)
- }, obj);
- var ss = '<div eletemplate="' + myself.name + '"> ' + self.template(myself.data, ai) + ' </div>'
- } else {
- var ai = Object.assign({
- "output": output,
- "menu": menus,
- "header": headers,
- "selfie": JSON.stringify(temps, true, 2)
- }, obj);
- var ss = '<div eletemplate="' + outer.name + '"> ' + self.template(otemp, ai) + ' </div>'
- }
- */
- return ss
- }
- Asystem.prototype.rendertemplatex = function(obj) {
- var self = this;
- var stash = [];
- var templates = []
- var temps = obj.getStuff3("_data.template", function(a) {
- return a
- }, "_data.template")
- var myself;
- var x = {};
- temps.map(function(a) {
- x[a.name] = a
- });
- temps = _.map(x, function(a) {
- return a
- });
- console.log("TEMPLATES:", x, temps);
- if (obj._data.template && obj._data.template.indexOf("{{output}}") > -1) {
- myself = temps.shift();
- }
- var outer = temps.filter(function(a) {
- return a.data.indexOf("{{output}}") > -1
- })
- var otemp = "{{output}}"
- var outera = "";
- if (outer.length) {
- outera = outer[0];
- otemp = outer[0].data;
- }
- var output = temps.filter(function(a) {
- return a.data.indexOf("{{output}}") === -1 && a.name.indexOf("menu") === -1 && a.name.indexOf("header") === -1 && a.from.indexOf("isa_") === -1
- }).map(function(tmp) {
- return '<div eletemplate="' + tmp.name + '"> ' + self.template(tmp.data, obj) + ' </div>';
- }).join("\n")
- var menus = temps.filter(function(a) {
- return a.name.indexOf("menu") > -1
- }).slice(0, 1).map(function(tmp) {
- return '<div eletemplate="' + tmp.name + '"> ' + self.template(tmp.data, obj) + ' </div>';
- }).join("\n")
- var headers = temps.filter(function(a) {
- return a.name.indexOf("header") > -1
- }).slice(0, 1).map(function(tmp) {
- return '<div eletemplate="' + tmp.name + '"> ' + self.template(tmp.data, obj) + ' </div>';
- }).join("\n")
- /*
- var isas = temps.filter(function(a) {
- return a.from.indexOf("isa_") > -1
- }).map(function(tmp) {
- console.log("ISA,",tmp);
- if(tmp.inas.length>2){
- var tma=tmp.inas.pop();
- tma=tmp.inas.pop();
- return '<div eletemplate="' + tmp.name + '">IIS ' + self.template(tmp.data, self.oget(tma)) + ' </div>';
- }else{
- return '<div eletemplate="' + tmp.name + '">IIS ' + self.template(tmp.data, self.oget(tmp.name)) + ' </div>';
- }
- }).join("\n")
- */
- if (myself) {
- var ai = Object.assign({
- "output": output,
- "menu": menus,
- "header": headers,
- "selfie": JSON.stringify(temps, true, 2)
- }, obj);
- var ss = '<div eletemplate="' + myself.name + '"> ' + self.template(myself.data, ai) + ' </div>'
- } else {
- var ai = Object.assign({
- "output": output,
- "menu": menus,
- "header": headers,
- "selfie": JSON.stringify(temps, true, 2)
- }, obj);
- var ss = '<div eletemplate="' + outer.name + '"> ' + self.template(otemp, ai) + ' </div>'
- }
- return ss
- }
- Asystem.prototype.allchildren = function(ronly) {
- var self = this;
- var arr = [];
- if (ronly) {
- return [].concat.apply(arr, allchildren(self.oo[ronly]))
- }
- _.keys(self.oo).filter((k) => typeof(self.oo[k]) === "function").map(function(key) {
- arr = [].concat.apply(arr, allchildren(self.oo[key]))
- })
- return arr;
- }
- Asystem.prototype.allchildpaths = function(ronly) {
- var self = this;
- var arr = [];
- _.keys(self.oo).filter((k) => typeof(self.oo[k]) === "function").map(function(key) {
- ronly ? (ronly === key ? arr = [].concat.apply(arr, allchildpaths(self.oo[key])) : null) : arr = [].concat.apply(arr, allchildpaths(self.oo[key]))
- })
- return arr;
- }
- Asystem.prototype.jsonstringify = function(obj, pretty, ind) {
- function recur_fy(ob) {
- var re = _.isObject(ob) && !_.isArray(ob) ? {} : [];
- _.map(ob, function(val, key) {
- if ((_.isObject(val) || _.isArray(val)) && typeof(val) !== "function") {
- re[key] = recur_fy(val);
- } else if (typeof(val) === "function") {
- re["$$$" + key] = val + "";
- } else {
- re[key] = val;
- }
- })
- return re;
- }
- return JSON.stringify(recur_fy(obj), pretty, ind);
- }
- Asystem.prototype.jsonparse = function(str) {
- var obj = JSON.parse(str);
- function recur_fy(ob) {
- var re = _.isObject(ob) && !_.isArray(ob) ? {} : [];
- _.map(ob, function(val, key) {
- if ((_.isObject(val) || _.isArray(val)) && typeof(val) !== "function") {
- re[key] = recur_fy(val);
- } else {
- if ((key + "").indexOf("$$$") === 0) {
- var ttt = {};
- try {
- eval("(function(tt){ tt.func = " + val + ";})(ttt)");
- re[key.substring(3)] = ttt.func;
- } catch (ea) {
- console.log("error func: ", ea)
- }
- } else {
- re[key] = val;
- }
- }
- })
- return re;
- }
- return recur_fy(obj);
- }
- module.exports = Asystem;
|