index.js 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146
  1. var EventEmitter = require("events").EventEmitter;
  2. var opath = require("opath");
  3. var async = require("async");
  4. var tool = require("tools2")();
  5. var getthezip = require("libs/getthezip");
  6. var unpackzip = require("libs/unpackzip");
  7. var generatezip = require("libs/generatezip");
  8. var random = require("sharedkey-thing").randoma(0xbadebabe)("#CHANGE BEFORE CHRISTMAS#");
  9. var _ = require("lodash");
  10. var templates = require("./templates");
  11. var ignores =
  12. "_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"
  13. .split(" ");
  14. function template_helpers(self) {
  15. return {
  16. "toTextArea": function(str) {
  17. return '<textarea>' + str + '</textarea>'
  18. },
  19. "toWrap": function(str, tag, attr) {
  20. return '<' + tag + ' ' + attr + '>' + str + '</' + tag + '>';
  21. },
  22. toLowerCase: function(str) {
  23. return str.toLowerCase();
  24. },
  25. toUpperCase: function(str) {
  26. return str.toUpperCase();
  27. },
  28. "pagecontent": function(obj) {
  29. return self.template(obj._data.content, obj);
  30. },
  31. "siblings": function(obj) {
  32. return [_.keys(obj.parents).shift()].map(function(parentpath) {
  33. var ares = _.map(obj.parents[parentpath] ? obj.parents[parentpath].children : [], function(a) {
  34. return a();
  35. })
  36. ares.sort(sorter(function(a) {
  37. return a._metas.menu_order || 100
  38. }, -1));
  39. var ss = _.map(ares, function(child, name) {
  40. return child.path
  41. });
  42. if (ss.length > 12) {
  43. var cindex = ss.indexOf(obj.path);
  44. cindex = cindex < 6 ? 6 : cindex;
  45. var spp = _.map(ares, renderlin(obj))
  46. var sasa = [].concat(spp.slice(cindex - 6, cindex + 7))
  47. } else {
  48. var sasa = _.map(ares, renderlin(obj))
  49. }
  50. return sasa.join("\n")
  51. }).join("\n")
  52. },
  53. "debugbump": function(obj) {
  54. return '<pre>' + obj + '</pre>'
  55. },
  56. "elementstree": function(obj, getfrom) {
  57. var ob_ = obj;
  58. if (getfrom) {
  59. if (getfrom.indexOf("!") === 0) {
  60. if (obj.name == getfrom.substring(1)) {
  61. return "";
  62. }
  63. } else {
  64. if (getfrom === "parents") {
  65. return gpath(obj, []).reverse().map(function(p) {
  66. return self.atypes.elementstree(self.oget(p), {
  67. "path": "_data.elements",
  68. "childrenpath": "elements"
  69. });
  70. }).join("\n");
  71. } else {
  72. if (getfrom === "inherits") {
  73. var tmp = obj.getStuff("_data.elements", function(a) {
  74. return a
  75. }, "_data.templatechild")
  76. var aa = tmp.map(function(a) {
  77. return a.name
  78. });
  79. aa.reverse();
  80. return aa.map(function(p) {
  81. return self.atypes.elementstree(self.oget(p), {
  82. "path": "_data.elements",
  83. "unc": p.replace(/\//g, "__"),
  84. "childrenpath": "elements"
  85. });
  86. }).join("\n");
  87. } else {
  88. ob_ = self.oget(getfrom);
  89. }
  90. }
  91. }
  92. }
  93. return self.atypes.elementstree(ob_, {
  94. "path": "_data.elements",
  95. "childrenpath": "elements"
  96. });
  97. },
  98. "ootree": function(obj, getfrom, datapath, childrenpath) {
  99. var ob_ = obj;
  100. var datapath = datapath || "_data.elements";
  101. var childrenpath = childrenpath || "elements";
  102. if (getfrom && getfrom !== "") {
  103. if (getfrom.indexOf("!") === 0) {
  104. if (obj.name == getfrom.substring(1)) {
  105. return "";
  106. }
  107. } else {
  108. if (getfrom === "parents") {
  109. return gpath(obj, []).reverse().map(function(p) {
  110. return self.atypes.elementstree(self.oget(p), {
  111. "path": datapath,
  112. "childrenpath": childrenpath
  113. });
  114. }).join("\n");
  115. } else {
  116. if (getfrom === "inherits") {
  117. var tmp = obj.getStuff(datapath, function(a) {
  118. return a
  119. }, datapath)
  120. var aa = tmp.map(function(a) {
  121. return a.name
  122. });
  123. aa.reverse();
  124. return aa.map(function(p) {
  125. return self.atypes.elementstree(self.oget(p), {
  126. "path": datapath,
  127. "unc": p.replace(/\//g, "__"),
  128. "childrenpath": childrenpath
  129. });
  130. }).join("\n");
  131. } else {
  132. ob_ = self.oget(getfrom);
  133. }
  134. }
  135. }
  136. }
  137. return self.atypes.elementstree(ob_, {
  138. "path": datapath,
  139. "childrenpath": childrenpath
  140. });
  141. },
  142. "parents": function(obj, ss_, sss_) {
  143. if (typeof(ss_) !== "undefined" && typeof(sss_) !== "undefined") {
  144. return gpath(obj, []).slice(parseInt(ss_), parseInt(sss_)).map(function(p, i) {
  145. return '<a href="#' + p + '" elepath="' + p + '" class="ele elecontent button button-rounded button-' + (i == 0 ? 'small' : 'tiny') + ' button-royal">' +
  146. self.oget(p +
  147. "/._data.title", p) + '</a>';
  148. }).join("\n");
  149. } else {
  150. return gpath(obj, []).slice(1).map(function(p, i) {
  151. return '<a href="#' + p + '" elepath="' + p + '" class="ele elecontent button button-rounded button-' + (i == 0 ? 'small' : 'tiny') + ' button-royal">' +
  152. self.oget(p +
  153. "/._data.title", p) + '</a>';
  154. }).join("\n");
  155. }
  156. },
  157. "parent": function(obj) {
  158. return _.map(obj.parents, function(par, nam) {
  159. self.rendertemplate(par)
  160. }).join("\n");
  161. },
  162. "children2": function(obj) {
  163. var ret = [];
  164. _.map(obj.children, function(child) {
  165. var cho = typeof(child) === "function" ? child() : child;
  166. ret.push(self.rendertemplate(cho))
  167. })
  168. return ret.join("\n")
  169. },
  170. "children": function(obj) {
  171. var ret = [];
  172. _.map(obj.children, function(child) {
  173. var cho = typeof(child) === "function" ? child() : child;
  174. ret.push(renderlin(cho)(cho, cho.name))
  175. })
  176. return ret.join("\n");
  177. },
  178. "children3": function(obj, sortera) {
  179. var ret = [];
  180. var tmp = obj.getStuff3("_data.templatechild", function(a) {
  181. return a
  182. }, "_data.templatechild")
  183. var chos = allchildren(obj).filter(function(a) {
  184. return a.path !== obj.path
  185. })
  186. /* var chos = _.map(obj.children, function(child) {
  187. return typeof(child) === "function" ? child() : child;
  188. })
  189. */
  190. chos.sort(sorter(function(a) {
  191. return opath.get(a, sortera) || -1000;
  192. }, 1));
  193. chos.map(function(cho) {
  194. ret.push(self.template(tmp[0].data, cho))
  195. })
  196. return ret.join("\n")
  197. },
  198. "relations": function(obj) {
  199. var stash = [];
  200. _.map(obj._relations, function(rels, relname) {
  201. var relators = _.keys(rels);
  202. var sss = relators.slice(0, 25).map(function(relator) {
  203. return '<li elepath="' + relator + '"><a href="#' + relator + '" class="elecontent" title="' + JSON.stringify(rels[relator].context).toHtmlEntities() +
  204. '">' + relator + '</a> </li> '
  205. }).join("\n");;
  206. stash.push([relname + "(" + relators.length + ")", sss]);
  207. })
  208. var ss = ('<div class="stash">' + stash.map(function(a) {
  209. return '<div class="stt"> <div class="st1">' + (a[0] === false ? "" : a[0] + "") + '</div><div class="st2">' + a[1] +
  210. '</div> </div>'
  211. }).join("\n") + '</div>');
  212. return ss
  213. },
  214. "grandchildren": function(obj) {
  215. var ret = [];
  216. _.map(obj.children, function(child) {
  217. var cho = typeof(child) === "function" ? child() : child;
  218. ret = [].concat.apply(ret, _.map(cho.children, renderlin(cho)))
  219. })
  220. return ret
  221. },
  222. "created": function(obj) {
  223. return obj._created.toJSON();
  224. },
  225. "_isa": function(obj) {
  226. return _.map(obj._isa, renderlin(obj)).join("\n")
  227. },
  228. "_isa2": function(obj) {
  229. return _.map(obj._isa, function(o, n) {
  230. return self.rendertemplate(o)
  231. }).join("\n")
  232. },
  233. "_isa3": function(obj, a, b) {
  234. return _.map(obj._isa, function(o, n) {
  235. return self.rendertemplate(o)
  236. }).slice(parseInt(a), parseInt(b)).join("\n")
  237. },
  238. "_hasa": function(obj) {
  239. return _.map(obj._hasa, renderlin(obj)).join("\n")
  240. },
  241. "findall": function(obj, withprop, target_) {
  242. var target = target_ || "content";
  243. var aa = []
  244. _.keys(self.oo).filter(aa => ["trash", "system", ""].indexOf(aa) === -1 && typeof(self.oo[aa]) === "function").map(function(key) {
  245. aa = [].concat.apply(aa, allchildren(self.oo[key]()).filter(function(a) {
  246. return a._isdirty
  247. }))
  248. })
  249. return aa.map(function(a) {
  250. return renderlin2(obj, {
  251. target: target
  252. })(a, a.name)
  253. }).join("\n");
  254. },
  255. "jsondata": function(obj) {
  256. return JSON.stringify(obj._data, true, 2).toHtmlEntities();
  257. },
  258. "visualize": function(obj) {
  259. var t = document.getElementById("visa_" + obj.name);
  260. if (t) {
  261. t.parentElement.removeChild(t)
  262. }
  263. setTimeout(function() {
  264. var cols = {
  265. parent: "#000000",
  266. child: "#ff00ff",
  267. extends: "#00ff00",
  268. inherits: "#ff0000",
  269. isa: "#ff9900",
  270. hasa: "#00ff99"
  271. }
  272. function lokup(tp) {
  273. return cols[tp];
  274. }
  275. var graph = Viva.Graph.graph();
  276. var graphics = Viva.Graph.View.svgGraphics(),
  277. nodeSize = 10;
  278. //allchildren(obj.parents[_.keys(obj.parents).shift()])
  279. var oba = obj.parents[_.keys(obj.parents).shift()] || obj;
  280. allchildren(oba).filter(function(c) {
  281. return c.path.indexOf("trash/") === -1 && c.path.indexOf("folders/") === -1 && c.path.indexOf("blobs/") === -1
  282. }).slice(0, 100).map(function(c) {
  283. graph.addNode(c.path);
  284. _.map(c.children, function(child, name) {
  285. graph.addLink(c.path, child().path, 'child')
  286. });
  287. _.map(c._isa, function(child, name) {
  288. graph.addLink(c.path, child.path, 'isa')
  289. });
  290. _.map(c._hasa, function(child, name) {
  291. graph.addLink(c.path, child.path, 'hasa')
  292. });
  293. _.map(c._extends, function(child, name) {
  294. graph.addLink(c.path, child.path, 'extends')
  295. });
  296. _.map(c._inherits, function(child, name) {
  297. graph.addLink(c.path, child.path, 'inherits')
  298. });
  299. _.map(c._relations, function(rels, relname) {
  300. _.map(rels, function(child, name) {
  301. graph.addLink(c.path, name, relname)
  302. })
  303. });
  304. })
  305. var idas = 1;
  306. var ida = function() {
  307. return "ida" + (idas++)
  308. }
  309. graphics.node(function(node) {
  310. var ui = Viva.Graph.svg('g');
  311. var circle = Viva.Graph.svg('circle')
  312. .attr('cx', 2)
  313. .attr('cy', 2)
  314. .attr('r', 2)
  315. .attr('fill', obj.path === node.id ? "#ff0000" : "#dadada");
  316. ui.append(circle);
  317. var svgText = Viva.Graph.svg('text').attr('font-size', '12px').attr('y', '4px').text(node.id.split("/").slice(-2).join("/"));
  318. ui.append(svgText);
  319. return ui;
  320. }).placeNode(function(nodeUI, pos) {
  321. nodeUI.attr('transform',
  322. 'translate(' +
  323. (pos.x - nodeSize / 2) + ',' + (pos.y - nodeSize / 2) +
  324. ')');
  325. });
  326. var createMarker = function(id) {
  327. return Viva.Graph.svg('marker')
  328. .attr('id', id)
  329. .attr('viewBox', "0 0 10 10")
  330. .attr('refX', "10")
  331. .attr('refY', "5")
  332. .attr('markerUnits', "strokeWidth")
  333. .attr('markerWidth', "10")
  334. .attr('markerHeight', "5")
  335. .attr('orient', "auto");
  336. },
  337. marker = createMarker('Triangle');
  338. marker.append('path').attr('d', 'M 0 0 L 10 5 L 0 10 z');
  339. // Marker should be defined only once in <defs> child element of root <svg> element:
  340. var defs = graphics.getSvgRoot().append('defs');
  341. defs.append(marker);
  342. var geom = Viva.Graph.geom();
  343. graphics.link(function(link) {
  344. // Notice the Triangle marker-end attribe:
  345. var isParent = (link.data === 'parent');
  346. var ui = Viva.Graph.svg('g');
  347. var p = Viva.Graph.svg('path')
  348. .attr('stroke', lokup(link.data) || "#000000")
  349. .attr('fill', 'none')
  350. .attr('marker-end', 'url(#Triangle)')
  351. .attr('id', ida())
  352. ui.isParent = isParent; // remember for future.
  353. ui.linka = link.data;
  354. ui.ry = 50 //- (Math.random() * 100);
  355. ui.pp = p;
  356. var svgText = Viva.Graph.svg('text').attr("dy", -4).attr("class", "slabel");
  357. var textPath = Viva.Graph.svg("textPath");
  358. textPath.attr("href", "#" + p.id).attr("startOffset", "25%").text(link.data + " >");
  359. svgText.append(textPath)
  360. ui.tt = svgText;
  361. ////<textPath xlink:href="#'+link.id+'">
  362. // svgText.attr('xlink', link.id);
  363. ui.append(svgText)
  364. ui.append(p);
  365. return ui;
  366. }).placeLink(function(linkUI, fromPos, toPos) {
  367. // using arc command: http://www.w3.org/TR/SVG/paths.html#PathDataEllipticalArcCommands
  368. //var data = 'M' + fromPos.x + ',' + fromPos.y +
  369. // ' A 4,' + linkUI.ry + ',-' + linkUI.ry + ',0,1,' + toPos.x + ',' + toPos.y;
  370. var data = 'M' + fromPos.x + ',' + fromPos.y +
  371. ' C ' + (fromPos.x + 50) + ' ' + (fromPos.y + 10) + ',' + (toPos.x - 50) + ' ' + (toPos.y - 10) + ',' + toPos.x + ' ' + toPos.y;
  372. linkUI.pp.attr("d", data);
  373. return;
  374. // linkUI.tt.attr("y", ((fromPos.y + toPos.y) / 2) - linkUI.ry);
  375. // linkUI.tt.attr("x", ((fromPos.x + toPos.x) / 2) );
  376. });
  377. var layout = Viva.Graph.Layout.forceDirected(graph, {
  378. springLength: 80,
  379. springCoeff: 0.00001,
  380. dragCoeff: 0.001,
  381. gravity: -1.2
  382. });
  383. var renderer = Viva.Graph.View.renderer(graph, {
  384. graphics: graphics,
  385. container: document.getElementById("visa_" + obj.name),
  386. layout: layout
  387. });
  388. renderer.run();
  389. }, 200)
  390. return '<div id="' + "visa_" + obj.name + '" class="svga">v</div>'
  391. }
  392. }
  393. }
  394. function gpath(cc, p) {
  395. if (cc.parents) {
  396. var k = _.keys(cc.parents).shift();
  397. if (k) {
  398. return gpath(cc.parents[k], p.concat(cc.path))
  399. } else {
  400. return p.concat(cc.path)
  401. }
  402. }
  403. }
  404. function renderlin2(obj, opt) {
  405. return function(child, name) {
  406. var pp = typeof(child) === "function" ? child() : child;
  407. return '<a href="#' + pp.path + '" elepath="' + pp.path + '" eletarget="' + opt.target + '" class="elecontent button button-rounded button-small button-' + (pp.path ==
  408. obj.path ? 'royal' : 'inverse') +
  409. '">' + (pp._data.title ?
  410. pp._data.title : pp.name) +
  411. '</a>'
  412. }
  413. }
  414. function renderlin(obj) {
  415. return function(child, name) {
  416. var pp = typeof(child) === "function" ? child() : child;
  417. return '<a href="#' + pp.path + '" elepath="' + pp.path + '" class="elecontent button button-rounded button-small button-' + (pp.path == obj.path ? 'royal' : 'inverse') +
  418. '">' + (pp._data.title ?
  419. pp._data.title : pp.name) +
  420. '</a>'
  421. }
  422. }
  423. function Asystem(oo) {
  424. this.oo = oo;
  425. this.name = "noname"
  426. this._logs = [];
  427. this._linked = {};
  428. this._templatefunction = templates(this, template_helpers(this));
  429. }
  430. Asystem.prototype.__proto__ = EventEmitter.prototype;
  431. Asystem.prototype.slugger = function(str) {
  432. var ret = ("" + str).replace(/\/+/g, '/').replace(/[^\x20-\x7E]/g, '')
  433. // ret=decodeURIComponent(ret);
  434. ret = ret.replace(/[\<\>]/g, '')
  435. if (ret.indexOf("/") == ret.length - 1) {
  436. ret = ret.substring(0, ret.length - 1);
  437. }
  438. return ret
  439. }
  440. Asystem.prototype.t_content = function(path) {
  441. return this.oget(path)._data.content
  442. }
  443. Asystem.prototype.odelete = function(path) {
  444. var self = this;
  445. self.omove(path, "trash/" + path)
  446. if (self.oo[path]) {
  447. _.map(self.oo[path]().children, function(child, name) {
  448. self.odelete(child().path)
  449. })
  450. delete self.oo[path]
  451. }
  452. self.emit("deleted", path)
  453. }
  454. Asystem.prototype.ozap = function(path) {
  455. var t = path.split("/");
  456. var ta = t.pop();
  457. delete this.oget(t.join("/")).children[ta];
  458. this.emit("deleted", path)
  459. }
  460. Asystem.prototype.omove = function(path, topath) {
  461. var self = this;
  462. var newa = self.oget(topath)
  463. var olda = self.oget(path);
  464. var ss = olda.exportSync();
  465. if (!ss) {
  466. return false;
  467. }
  468. ss.name = topath.split("/").pop();
  469. ss.path = topath;
  470. var np = topath.split("/")
  471. np.pop();
  472. _.map(olda._relations, function(rels, relsname) {
  473. _.map(rels, function(rel, name) {
  474. delete rel.ref._relations[rel.context.out]
  475. })
  476. delete olda._relations[relsname]
  477. })
  478. _.map(olda._isa, function(isa, name) {
  479. delete isa._hasa[name]
  480. delete olda._isa[name]
  481. })
  482. _.map(olda._hasa, function(hasa, name) {
  483. delete hasa._isa[olda.path]
  484. delete olda._hasa[name]
  485. })
  486. _.map(olda._inherits, function(inheri, name) {
  487. delete inheri._extends[olda.path]
  488. delete olda._inherits[name]
  489. })
  490. _.map(olda._extends, function(extendi, name) {
  491. delete extendi._inherits[olda.path]
  492. delete olda._extends[name]
  493. })
  494. _.map(olda.parents, function(par, nam) {
  495. delete par.children[olda.name];
  496. })
  497. _.keys(olda.children, function(key) {
  498. delete olda.children[key];
  499. })
  500. delete olda;
  501. self.rev_conv2(ss, np.join("/"));
  502. self.emit("moved", path, topath)
  503. }
  504. Asystem.prototype.oclone = function(path, topath) {
  505. var self = this;
  506. var newa = self.oget(topath)
  507. var olda = self.oget(path);
  508. var ss = olda.exportSync();
  509. ss.name = topath.split("/").pop();
  510. ss.path = topath;
  511. var np = topath.split("/")
  512. np.pop();
  513. self.rev_conv2(ss, np.join("/"));
  514. self.emit("cloned", path, topath)
  515. }
  516. Asystem.prototype.oset = function(path_, data) {
  517. var self = this;
  518. var path = self.slugger(path_);
  519. var ar = path.split("/");
  520. var first = ar.shift();
  521. var prop, obj;
  522. if (typeof(self.oo[first]) === "undefined") {
  523. self.oo[first] = self.oo(first);
  524. self.oo[first]()._isdirty = true;
  525. self.emit("rootcreated", self.oo[first]())
  526. }
  527. if (ar.length > 0 && ar[ar.length - 1].indexOf(".") === 0) {
  528. prop = ar.pop().substring(1);
  529. }
  530. path = ar.join("/")
  531. if (path !== "") {
  532. var exists = self.ofind(path_);
  533. if (exists.remainder !== "") {
  534. obj = self.oo[first](path)();
  535. self.emit("created1", obj)
  536. } else {
  537. obj = self.oo[first](path)();
  538. }
  539. } else {
  540. obj = self.oo[first]()
  541. }
  542. if (prop) {
  543. obj._isdirty = true;
  544. opath.set(obj, prop, data);
  545. //return obj[prop] || getpath(obj, prop)
  546. self.emit("changed", obj.path + "/" + prop, data)
  547. }
  548. return obj;
  549. }
  550. Asystem.prototype.ogetsilent = function(path_, alternative) {
  551. var self = this;
  552. var path = self.slugger(path_);
  553. var ar = path.split("/");
  554. var first = ar.shift();
  555. var prop, obj;
  556. if (typeof(self.oo[first]) === "undefined") {
  557. self.oo[first] = self.oo(first);
  558. }
  559. if (ar.length > 0 && ar[ar.length - 1].indexOf(".") === 0) {
  560. prop = ar.pop().substring(1);
  561. }
  562. path = ar.join("/")
  563. if (path !== "") {
  564. var exists = self.ofind(path_);
  565. if (exists.remainder !== "") {
  566. obj = self.oo[first](path)();
  567. } else {
  568. obj = self.oo[first](path)();
  569. }
  570. } else {
  571. obj = self.oo[first]()
  572. }
  573. if (prop) {
  574. return obj[prop] || opath.get(obj, prop) || alternative;
  575. }
  576. return obj;
  577. }
  578. Asystem.prototype.oget = function(path_, alternative) {
  579. if (typeof(path_) === "undefined") {
  580. return false;
  581. }
  582. var self = this;
  583. var path = self.slugger(path_);
  584. var ar = path.split("/");
  585. var first = ar.shift();
  586. var prop, obj;
  587. if (typeof(self.oo[first]) === "undefined") {
  588. self.oo[first] = self.oo(first);
  589. self.oo[first]()._isdirty = true;
  590. self.emit("rootcreated", self.oo(first)())
  591. }
  592. if (ar.length > 0 && ar[ar.length - 1].indexOf(".") === 0) {
  593. prop = ar.pop().substring(1);
  594. }
  595. path = ar.join("/")
  596. if (path !== "") {
  597. var exists = self.ofind(path_);
  598. if (exists.remainder !== "" && exists.remainder.indexOf("/") > -1) {
  599. obj = self.oo[first](path)();
  600. obj._isdirty = true;
  601. self.emit("created2", obj)
  602. } else {
  603. obj = self.oo[first](path)();
  604. }
  605. } else {
  606. obj = self.oo[first]()
  607. }
  608. if (prop) {
  609. return obj[prop] || opath.get(obj, prop) || alternative;
  610. }
  611. return obj;
  612. }
  613. Asystem.prototype.getele = function(action) {
  614. var self = this;
  615. action = self.slugger(action);
  616. var a = action.split("/");
  617. var roo = a.shift();
  618. var ffu;
  619. if (a[a.length - 1].indexOf(".") > -1) {
  620. //function call;
  621. ffu = a.pop().split(".").pop();
  622. } else {
  623. //straight
  624. }
  625. var name = a.join("/");
  626. var eles = self.oo[roo](name)();
  627. if (ffu && eles[ffu]) {
  628. return {
  629. ele: eles,
  630. func: eles[ffu]
  631. };
  632. }
  633. return {
  634. ele: eles
  635. }
  636. }
  637. Asystem.prototype.ofind = function(path) {
  638. /* returns {found: "spath"|false , remainder: "rest"|"" , obj: obj}*/
  639. var self = this;
  640. path = self.slugger(path);
  641. var ar = path.split("/");
  642. var first = ar.shift();
  643. if (typeof(self.oo[first]) === "undefined") {
  644. return {
  645. found: false
  646. }
  647. } else {
  648. var pa = self.oo[first]()
  649. var ch = "";
  650. var rest = [];
  651. while (ar.length) {
  652. ch = ar.shift();
  653. if (pa.children[ch]) {
  654. pa = pa.children[ch]();
  655. } else {
  656. rest.push(ch)
  657. }
  658. }
  659. return {
  660. found: pa.path,
  661. /* is false if not found*/
  662. remainder: rest.join("/"),
  663. /* is not "" if not found rest of path*/
  664. obj: pa /* the found object*/
  665. }
  666. }
  667. }
  668. Asystem.prototype.rev_conv = function(obj, path) {
  669. if (typeof(obj) !== "object" || obj == null) {
  670. return false;
  671. }
  672. var self = this;
  673. var zzz = self.ogetsilent(obj.path);
  674. zzz.data(obj.data)
  675. zzz._created = new Date(obj.created);
  676. zzz.metas(obj.meta);
  677. if (_.size(obj.rel) > 0) {
  678. _.map(obj.rel, function(rels, name) {
  679. _.map(rels, function(dats, relator) {
  680. if (name === dats.in) {} else {
  681. // console.log("RR: ", obj.path, relator, name, dats.in, dats.out);
  682. zzz.relatesAsToWith(dats.out, self.ogetsilent(relator), dats.in, dats)
  683. }
  684. })
  685. })
  686. }
  687. if (obj.inherits && obj.inherits.length) {
  688. obj.inherits.map(function(alp) {
  689. zzz.inherits(self.ogetsilent(alp))
  690. })
  691. }
  692. if (obj.extends && obj.extends.length) {
  693. obj.extends.map(function(alp) {
  694. zzz.extends(self.ogetsilent(alp))
  695. })
  696. }
  697. if (obj.isa && obj.isa.length) {
  698. obj.isa.map(function(alp) {
  699. zzz.isa(self.ogetsilent(alp))
  700. })
  701. }
  702. if (obj.hasa && obj.hasa.length) {
  703. obj.hasa.map(function(alp) {
  704. self.ogetsilent(alp).isa(zzz)
  705. })
  706. }
  707. _.map(_.omit(obj, ["getStuff3", "name", "hasa", "meta", "data", "parents", "children", "path", "_rootname", "created", "rel", "inherits", "extends", "isa", "children",
  708. "_isdirty"
  709. ]),
  710. function(
  711. val,
  712. ke) {
  713. if (typeof(val) === "string" && val.indexOf("$$$FUNCTION$$$") === 0) {
  714. if (zzz._metas && zzz._metas.protected && zzz._metas.protected[ke]) {} else {
  715. var ttt = {};
  716. try {
  717. eval("(function(tt){ tt.func = " + val.split("$$$FUNCTION$$$").pop() + ";})(ttt)");
  718. zzz[ke] = ttt.func;
  719. } catch (ea) {
  720. console.log("error func: ", ea)
  721. }
  722. }
  723. } else {
  724. if (typeof(val) === "object") {
  725. var r = {}
  726. _.map(val, function(v, kp) {
  727. if (typeof(v) === "string" && v.indexOf("$$$FUNCTION$$$") === 0) {
  728. var ttt = {};
  729. try {
  730. eval("(function(tt){ tt.func = " + v.split("$$$FUNCTION$$$").pop() + ";})(ttt)");
  731. r[kp] = ttt.func;
  732. } catch (ea) {
  733. console.log("error func: ", ea)
  734. }
  735. } else {
  736. r[kp] = v
  737. }
  738. })
  739. zzz[ke] = r;
  740. } else {
  741. zzz[ke] = val
  742. }
  743. }
  744. })
  745. delete obj._isdirty;
  746. if (obj.children && obj.children.length) {
  747. obj.children.map(function(child) {
  748. self.rev_conv(child)
  749. })
  750. }
  751. }
  752. Asystem.prototype.rev_conv2 = function(obj, path) {
  753. var self = this;
  754. var zzz = self.ogetsilent(path + "/" + obj.name);
  755. zzz.data(obj.data)
  756. zzz._created = new Date(obj.created);
  757. zzz.metas(obj.meta);
  758. if (_.size(obj.rel) > 0) {
  759. _.map(obj.rel, function(rels, name) {
  760. _.map(rels, function(dats, relator) {
  761. if (name === dats.in) {} else {
  762. // console.log("RR: ", obj.path, relator, name, dats.in, dats.out);
  763. zzz.relatesAsToWith(dats.out, self.ogetsilent(relator), dats.in, dats)
  764. }
  765. })
  766. })
  767. }
  768. if (obj.inherits && obj.inherits.length) {
  769. obj.inherits.map(function(alp) {
  770. zzz.inherits(self.ogetsilent(alp))
  771. })
  772. }
  773. if (obj.extends && obj.extends.length) {
  774. obj.extends.map(function(alp) {
  775. zzz.extends(self.ogetsilent(alp))
  776. })
  777. }
  778. if (obj.isa && obj.isa.length) {
  779. obj.isa.map(function(alp) {
  780. zzz.isa(self.ogetsilent(alp))
  781. })
  782. }
  783. if (obj.hasa && obj.hasa.length) {
  784. obj.hasa.map(function(alp) {
  785. self.ogetsilent(alp).isa(zzz)
  786. })
  787. }
  788. _.map(_.omit(obj, ["getStuff3", "name", "hasa", "meta", "data", "parents", "children", "path", "_rootname", "created", "rel", "inherits", "extends", "isa", "children",
  789. "_isdirty"
  790. ]),
  791. function(
  792. val,
  793. ke) {
  794. if (typeof(val) === "string" && val.indexOf("$$$FUNCTION$$$") === 0) {
  795. // console.log(val.split("$$$FUNCTION$$$").pop())
  796. if (zzz._metas && zzz._metas.protected && zzz._metas.protected[ke]) {} else {
  797. var ttt = {};
  798. try {
  799. eval("(function(tt){ tt.func = " + val.split("$$$FUNCTION$$$").pop() + ";})(ttt)");
  800. zzz[ke] = ttt.func;
  801. } catch (ea) {
  802. console.log("error func: ", ea)
  803. }
  804. }
  805. } else {
  806. if (typeof(val) === "object") {
  807. var r = {}
  808. _.map(val, function(v, kp) {
  809. if (typeof(v) === "string" && v.indexOf("$$$FUNCTION$$$") === 0) {
  810. var ttt = {};
  811. try {
  812. eval("(function(tt){ tt.func = " + v.split("$$$FUNCTION$$$").pop() + ";})(ttt)");
  813. r[kp] = ttt.func;
  814. } catch (ea) {
  815. console.log("error func: ", ea)
  816. }
  817. } else {
  818. r[kp] = v
  819. }
  820. })
  821. zzz[ke] = r;
  822. } else {
  823. zzz[ke] = val
  824. }
  825. }
  826. })
  827. delete obj._isdirty;
  828. if (obj.children && obj.children.length) {
  829. obj.children.map(function(child) {
  830. self.rev_conv2(child, zzz.path)
  831. })
  832. }
  833. }
  834. /*Asystem.prototype.save = function() {
  835. var self = this;
  836. return new Promise(function(resolve, reject) {
  837. $lib.exporter(self, function(err, data) {
  838. $lib.generate_thezip(self.name + "-" + $lib.getTimedName() + ".zip",
  839. $lib.arrayedToObject(data, "name"), function(err, zz) {
  840. console.log(err);
  841. resolve(zz)
  842. })
  843. })
  844. })
  845. }
  846. */
  847. Asystem.prototype.exporter = function(cb) {
  848. var self = this;
  849. var obj = self.oo;
  850. var stores = _.keys(obj).filter(function(a) {
  851. return typeof(obj[a]) === "function" && a !== "namespace"
  852. }).map(function(a) {
  853. return obj[a]()
  854. }).map(function(a) {
  855. var da = a.exportSync("");
  856. return da
  857. });
  858. stores.unshift({
  859. "name": "namespace.manifest",
  860. "data": self.oget("namespace")._data
  861. })
  862. var ret = {}
  863. stores.map(function(a) {
  864. ret[a.name] = a;
  865. })
  866. cb(null, ret)
  867. }
  868. Asystem.prototype.log = function() {}
  869. Asystem.prototype.template = function() {
  870. return this._templatefunction.apply(this, arguments)
  871. }
  872. Asystem.prototype.loadInto = function(url, callback, linked) {
  873. var self = this;
  874. getthezip(url, function(res) {
  875. return res.json()
  876. })
  877. .then(function(obj) {
  878. self.rev_conv(obj);
  879. self.restart();
  880. })
  881. }
  882. Asystem.prototype.loadZipLocal = function(blob, callback) {
  883. var self = this;
  884. var importer = self.importer_zip();
  885. unpackzip(importer)(blob)
  886. .then(
  887. function() {
  888. self.emit("status", "early ready yes yes")
  889. if (self._loadqueue && self._loadqueue.length) {
  890. var ff = self._loadqueue.shift()
  891. self.ozap(importer.path);
  892. self.loadZip(ff[1], callback, ff[0]);
  893. } else {
  894. if (self.name_imported) {
  895. self.name = self.name_imported;
  896. }
  897. self.ozap(importer.path);
  898. callback(null, self);
  899. }
  900. }
  901. ).catch(console.log)
  902. }
  903. Asystem.prototype.loadZip = function(url, callback, linked) {
  904. var self = this;
  905. if (linked) {
  906. self._linked[linked] = url;
  907. }
  908. getthezip(url)
  909. .then(function(data) {
  910. self.emit("status", "got data " + data.size + " " + data.type)
  911. var importer = self.importer_zip();
  912. unpackzip(importer)(data)
  913. .then(
  914. function() {
  915. self.emit("status", "early ready yes yes")
  916. if (self._loadqueue && self._loadqueue.length) {
  917. var ff = self._loadqueue.shift()
  918. self.ozap(importer.path);
  919. self.loadZip(ff[1], callback, ff[0]);
  920. } else {
  921. if (self.name_imported) {
  922. self.name = self.name_imported;
  923. }
  924. self.ozap(importer.path);
  925. callback(null, self);
  926. }
  927. }
  928. )
  929. .catch(console.log)
  930. })
  931. .catch(console.log)
  932. }
  933. Asystem.prototype.saveZip = function() {
  934. var self = this;
  935. return new Promise(function(resolve, reject) {
  936. self.exporter(function(err, data) {
  937. var seperate_save = _.keys(data).filter(function(name) {
  938. return self._linked && self._linked[name + ".link"]
  939. })
  940. var ddata = _.omit(data, seperate_save);
  941. seperate_save.map(function(om) {
  942. var url = self._linked[om + ".link"].split("/");
  943. url.pop();
  944. url.push(self.name + "-" + om + ".zip");
  945. ddata[om + ".link"] = url.join("/");
  946. })
  947. generatezip(self.name + "-" + "" + ".zip", ddata, function(err, zz) {
  948. async.mapSeries(seperate_save, function(name, next) {
  949. var dda = {};
  950. dda[name] = data[name];
  951. generatezip(self.name + "-" + name + ".zip", dda, next)
  952. }, function(err, all) {
  953. all.push(zz);
  954. resolve(all)
  955. })
  956. })
  957. })
  958. })
  959. }
  960. Asystem.prototype.saveAsJS = function() {
  961. var self = this;
  962. return new Promise(function(resolve, reject) {
  963. self.exporterJS(function(err, data) {
  964. var tm = {}
  965. _.map(data, function(dat, name) {
  966. if (typeof(dat.data) === "object" && dat.data.length) {
  967. tm[name + ".js"] = {
  968. data: dat.data.map(function(a) {
  969. return '$E(' + JSON.stringify(a) + ');'
  970. }).join("\n")
  971. };
  972. } else {
  973. tm[name] = {
  974. data: dat.data
  975. };
  976. }
  977. })
  978. generatezip(self.name + "-" + "" + ".zip", tm, function(err, zz) {
  979. resolve(zz);
  980. })
  981. //console.log("TTTT",tm);
  982. // console.log("DDD",data);
  983. })
  984. })
  985. }
  986. Asystem.prototype.exporterJS = function(cb) {
  987. var self = this;
  988. var obj = self.oo;
  989. var stores = _.keys(obj).filter(function(a) {
  990. return typeof(obj[a]) === "function" && a !== "namespace"
  991. }).map(function(a) {
  992. return obj[a]()
  993. }).map(function(a) {
  994. var da = a.exportFlatSync(a.name);
  995. return {
  996. name: a.name,
  997. data: da
  998. }
  999. });
  1000. stores.unshift({
  1001. "name": "namespace.manifest",
  1002. "data": self.oget("namespace")._data
  1003. })
  1004. var ret = {}
  1005. stores.map(function(a) {
  1006. ret[a.name] = a;
  1007. })
  1008. cb(null, ret)
  1009. }
  1010. Asystem.prototype.importer_zip = function() {
  1011. var self = this;
  1012. self.oget("system/emmm").metas("_hide", true);
  1013. var emname = "system/emmm/a/" + random(0xffffff)
  1014. var emmm = self.oget(emname);
  1015. emmm.metas("unsaveable", true);
  1016. emmm.on("file", function(filename, data, mimen) {
  1017. // console.log("_________________FF: ", filename, mimen)
  1018. if (filename.indexOf(".link") > -1) {
  1019. emmm.emit("link", data);
  1020. self._loadqueue = self._loadqueue || [];
  1021. self._loadqueue.push([filename, data])
  1022. self.emit("status", "gotlink", filename);
  1023. } else {
  1024. self.emit("status", "gotfile", filename + " : " + typeof(data) + " : " + data.substring(0, 20));
  1025. self.oget("folders/" + filename.indexOf("/") > -1 ? filename : "folders/" + filename).data({
  1026. "mime": mimen,
  1027. "data": data
  1028. });
  1029. }
  1030. })
  1031. emmm.on("jsfile", function(filename, data, ns) {
  1032. self.emit("jsfile", filename, data, ns);
  1033. // self.rev_conv(data)
  1034. })
  1035. emmm.on("json", function(filename, data) {
  1036. self.emit("status", "gotfile", filename);
  1037. self.rev_conv(data)
  1038. })
  1039. emmm.on("namespace", function(data, cont) {
  1040. self.emit("status", "gotnamespace", data.namespace);
  1041. self.name_imported = data.namespace;
  1042. self.oget("namespace").data(data).metas({
  1043. "_system": true
  1044. });
  1045. cont();
  1046. })
  1047. emmm.on("nonamespace", function(cont) {
  1048. //self.name = url.split("/").pop().split(".").shift();
  1049. self.emit("status", "gotnonamespace", self.name);
  1050. // self.oget("namespace").data({"namespace":""}).metas({"_system":true});
  1051. cont();
  1052. })
  1053. return emmm
  1054. }
  1055. function createnav(rk, key) {
  1056. return '#' + key;
  1057. }
  1058. $linkstr2 = '<li class="{{liclass}}"><i class="{{iclass}}"></i> <a href="{{href}}" class="eletree">{{title}}</a> <em>{{size}}</em></li>';
  1059. function allchildpaths(obj_) {
  1060. var obj = typeof(obj_) === "function" ? obj_() : obj_;
  1061. return [].concat.apply([obj.path], _.map(obj.children, allchildpaths))
  1062. }
  1063. function allchildren(obj_) {
  1064. var obj = typeof(obj_) === "function" ? obj_() : obj_;
  1065. return [].concat.apply([obj], _.map(obj.children, allchildren))
  1066. }
  1067. function sorter(prop, dir) {
  1068. return function(aa, bb) {
  1069. var a = prop(aa);
  1070. var b = prop(bb);
  1071. return a > b ? dir * -1 : a < b ? dir * 1 : 0;
  1072. }
  1073. }
  1074. Asystem.prototype.t_icon = function(obj) {
  1075. var self = this;
  1076. if (obj._data.iconclass) {
  1077. var dd = self.oget(obj._data.iconclass)._data.data;
  1078. if (!dd) {
  1079. return "";
  1080. }
  1081. if (dd.indexOf("data:") === 0) {
  1082. return '<i><img src="' + dd + '"></i>';
  1083. } else {
  1084. return '<i>' + dd + '</i>';
  1085. }
  1086. }
  1087. return "<i> </i>"
  1088. }
  1089. Asystem.prototype.t_title = function(obj) {
  1090. var self = this;
  1091. //contexts;
  1092. return opath.get(obj._data, "title") || obj.name
  1093. }
  1094. Asystem.prototype.getClasses = function(obj) {
  1095. var s = [];
  1096. if (obj._metas.hide) {
  1097. s.push("hidden");
  1098. }
  1099. if (obj._metas._system) {
  1100. s.push("system");
  1101. }
  1102. if (obj._metas.undeletable) {
  1103. s.push("undeletable");
  1104. }
  1105. return s.join(" ");
  1106. }
  1107. Asystem.prototype.renderme = function(obj, maxlevels, atlevel, viewa) {
  1108. var self = this;
  1109. var ssi = _.size(obj.children);
  1110. var childas = obj;
  1111. if (ssi === 1) {
  1112. var ooj = obj.children[_.keys(obj.children).shift()]()
  1113. if (ooj._metas.skipme) {
  1114. childas = ooj;
  1115. }
  1116. }
  1117. var ssia = allchildpaths(obj).length - 1;
  1118. //(ssi > 0 ? ssi : '') +
  1119. if (obj._metas.pickchild) {
  1120. childas = obj.children[obj._metas.pickchild]();
  1121. }
  1122. if (obj._metas.pickpath) {
  1123. childas = self.oget(obj._metas.pickpath);
  1124. }
  1125. var s = "";
  1126. if (viewa && typeof(viewa) === "function") {
  1127. s += "" + viewa(obj, atlevel);
  1128. }
  1129. if (obj._metas._hide) {
  1130. return "";
  1131. }
  1132. //self.t_title(obj)
  1133. s += '<div class="obj l' + atlevel + ' ' + self.getClasses(obj) + '" eleview="menulink" elepath="' + obj.path + '" atlevel="' + atlevel + '">'
  1134. s += '<a href="#' + obj.path + '" >' + self.t_icon(obj) + obj.name + '<em>' + (ssia > 0 ? ssia : '') + '</em></a>'
  1135. if (atlevel < maxlevels) {
  1136. if (ssi > 0) {
  1137. var xxx = _.map(childas.children, function(cc) {
  1138. return cc()
  1139. })
  1140. xxx.sort(sorter(function(a) {
  1141. return a._metas.menu_order || 100
  1142. }, -1));
  1143. var aa = xxx.slice(0, 25).map(function(cc) {
  1144. return self.renderme(cc, maxlevels, atlevel + 1, viewa)
  1145. }).join("\n")
  1146. return s + '<div class="children">' + aa + '</div></div>'
  1147. } else {
  1148. return s + '</div>';
  1149. }
  1150. } else {
  1151. return s + '</div>'
  1152. }
  1153. }
  1154. Asystem.prototype.render = function() {
  1155. var self = this;
  1156. return new Promise(function(resolve, reject) {
  1157. var rooten = self.oget("root");
  1158. var ss = [self.template('<div id="{{_data.id}}" class="obj root" eleview="" elepath="">', rooten)];
  1159. // ss.push('<pre onclick="$lib.toggle_admin()">' + self.oget("root/._data.title") + '</pre><hr>')
  1160. /* var kka = _.keys(self.oo).filter(aa => ["trash", "system", "root"].indexOf(aa) === -1 && typeof(self.oo[aa]) === "function")
  1161. kka.sort(function(aa, bb) {
  1162. var a = self.oo[aa]()._metas.menu_order;
  1163. var b = self.oo[bb]()._metas.menu_order;
  1164. return a > b ? 1 : a < b ? -1 : 0
  1165. })
  1166. */
  1167. var kkas = _.filter(self.oo, function(sto) {
  1168. return typeof(sto) === "function"
  1169. }).map(function(sto, nam) {
  1170. return sto();
  1171. })
  1172. kkas.sort(sorter(function(a) {
  1173. return a._metas.menu_order || 100
  1174. }, -1));
  1175. var privates = [];
  1176. var publics = [];
  1177. kkas.filter(function(obj) {
  1178. return obj._metas.public !== true
  1179. }).map(function(obj) {
  1180. privates.push(self.rendera("menulink", obj, 0, 0));
  1181. })
  1182. kkas.filter(function(obj) {
  1183. return obj._metas.public === true
  1184. }).map(function(obj) {
  1185. publics.push(self.rendera("menulink", obj, 2, 0));
  1186. })
  1187. publics.map(function(a) {
  1188. ss.push(a);
  1189. })
  1190. /* kka.map(function(key) {
  1191. ss.push(self.rendera("menulink", self.oo[key](), 0, 0));
  1192. })
  1193. */
  1194. ss.push('</div>')
  1195. ss.push('<div id="editorlinks">');
  1196. ss.push('<div id="mmme" class="obj root" eleview="" elepath="">');
  1197. privates.map(function(a) {
  1198. ss.push(a);
  1199. })
  1200. ss.push('</div>');
  1201. ss.push('</div>')
  1202. resolve(ss.join("\n"));
  1203. })
  1204. }
  1205. Asystem.prototype.render_editorlinks = function(levels) {
  1206. var self = this;
  1207. var kkas = _.filter(self.oo, function(sto, k) {
  1208. return typeof(sto) === "function"
  1209. }).map(function(sto, nam) {
  1210. return sto();
  1211. })
  1212. kkas.sort(sorter(function(a) {
  1213. return a._metas.menu_order || 100
  1214. }, -1));
  1215. return kkas.filter(function(obj) {
  1216. return obj._metas._system ? false : true
  1217. }).map(function(obj) {
  1218. return self.renderme(obj, levels || 0, 0);
  1219. }).join("\n") + "<hr>" + kkas.filter(function(obj) {
  1220. return obj._metas._system ? true : false
  1221. }).map(function(obj) {
  1222. return self.renderme(obj, levels || 0, 0);
  1223. }).join("\n");
  1224. }
  1225. Asystem.prototype.rendermenu = function(obj, levels) {
  1226. var self = this;
  1227. var temps = obj.getStuff3("_data.menuobj", function(a) {
  1228. return a
  1229. }, "_data.menuobj")
  1230. if (temps.length > 0) {
  1231. return self.renderme(self.oget(temps[0].data), levels || 0, 0);
  1232. }
  1233. return self.renderme(obj, levels || 0, 0);
  1234. }
  1235. Asystem.prototype.renderq = function(kkey) {
  1236. var self = this;
  1237. return new Promise(function(resolve, reject) {
  1238. var rooten = self.oget("root");
  1239. var ss = [self.template('<div id="{{_data.id}}" class="obj root" eleview="" elepath="">', rooten)];
  1240. // ss.push('<pre onclick="$lib.toggle_admin()">' + self.oget("root/._data.title") + '</pre><hr>')
  1241. if (!self.oo[kkey]) {
  1242. self.oget(kkey);
  1243. }
  1244. ss.push(self.rendera("menulink", self.oo[kkey](), 1, 0));
  1245. ss.push('</div>')
  1246. resolve(ss.join("\n"));
  1247. })
  1248. }
  1249. Asystem.prototype.rendera = function(template) {
  1250. var args = Array.prototype.slice.apply(arguments, [1]);
  1251. if (template == "view") {
  1252. return "VIEW"
  1253. }
  1254. if (template == "menulink") {
  1255. return this.renderme.apply(this, args);
  1256. }
  1257. if (template == "editor") {
  1258. return this.renderEditorTools(args[0], "editor");
  1259. }
  1260. return "NOT VIEW " + template
  1261. }
  1262. //
  1263. Asystem.prototype.renderEditorTools = function(obj, state) {
  1264. var buts = [];
  1265. if (obj.path.indexOf("trash") == 0) {
  1266. buts.push('<button class="elerestore"><i class="fa fa-undo-alt"></i></button>')
  1267. }
  1268. if (state == "editor") {
  1269. buts.push('<button class="eleview"><i class="fa fa-eye"></i></button>')
  1270. if (!(obj._metas.nochildren)) {
  1271. buts.push('<button class="eleplus"><i class="fa fa-plus-circle"></i></button>')
  1272. }
  1273. if (obj.path.indexOf("trash") == -1 && !(obj._metas.undeletable)) {
  1274. buts.push('<button class="eledelete"><i class="fa fa-trash-alt"></i></button>')
  1275. }
  1276. buts.push('<button class="eleexport"><i class="fa fa-file-export"></i></button>')
  1277. } else {
  1278. buts.push('<button class="eleeditor"><i class="fa fa-edit"></i></button>')
  1279. }
  1280. return '<div id="editor" elepath="' + obj.path + '" eleview="editor" class="editorline" ><div class="buttons">' + buts.map(function(a) {
  1281. return a
  1282. }).join(" ") + '</div><div id="editor_context"></div></div>'
  1283. }
  1284. tool.add_filters("editor_sections", function(arr, obj) {
  1285. arr.push({
  1286. title: "Template",
  1287. content: function() {
  1288. return "<div class=\"status\">x</div> <textarea class=htmleditor data-id=\"" + obj.path + "/._data.template\">" + obj._data.template + '</textarea>'
  1289. }
  1290. })
  1291. return arr;
  1292. }, 100, "Template")
  1293. function wrap(tag, obj, content) {
  1294. var args = Array.prototype.slice.apply(arguments, [2]);
  1295. return '<' + tag + ' ' + _.map(obj, (val, prop) => prop + '="' + val + '"').join(" ") + '>' + args.join("\n") + '</' + tag + '>'
  1296. }
  1297. Asystem.prototype.renderEditor = function(obj) {
  1298. var self = this;
  1299. var ss = "";
  1300. //"<div class=\"status\">x</div> <textarea class=htmleditor data-id=\"" + obj.path + "/._data.content\">" + obj._data.content + '</textarea>'
  1301. // ss += self.template(self.oget("system/editor")._data.template,obj);
  1302. var self = this;
  1303. var aa = []
  1304. _.keys(self.oo).filter(aa => ["trash", ""].indexOf(aa) === -1 && typeof(self.oo[aa]) === "function").map(function(key) {
  1305. aa = [].concat.apply(aa, allchildren(self.oo[key]()).filter(function(a) {
  1306. return a.editor_function
  1307. }))
  1308. })
  1309. ss += aa.map(function(a) {
  1310. return wrap("div", {
  1311. elepath: a.path,
  1312. class: "editor_module"
  1313. }, wrap("h3", {}, a.name), wrap("div", {}, a._metas.help), wrap("div", {}, a.editor_function(obj)))
  1314. }).join("\n")
  1315. // return aa.map( function(a){ return renderlin(a)(a,a.name) } ).join("\n");
  1316. ss += '<hr>'
  1317. ss += self.renderEditor2(obj);
  1318. return ss
  1319. }
  1320. String.prototype.toHtmlEntities = function() {
  1321. return this.replace(/./gm, function(s) {
  1322. return "&#" + s.charCodeAt(0) + ";";
  1323. });
  1324. };
  1325. /**
  1326. * Create string from HTML entities
  1327. */
  1328. String.fromHtmlEntities = function(string) {
  1329. return (string + "").replace(/&#\d+;/gm, function(s) {
  1330. return String.fromCharCode(s.match(/\d+/gm)[0]);
  1331. })
  1332. };
  1333. Asystem.prototype.uniq_relnames = function() {
  1334. var self = this;
  1335. var rels = {}
  1336. self.allchildren().map(function(c) {
  1337. _.keys(c._relations).map(function(relname) {
  1338. rels[relname] = rels[relname] || 0;
  1339. rels[relname] = rels[relname] + 1;
  1340. })
  1341. })
  1342. return _.keys(rels).map(function(a) {
  1343. return {
  1344. name: a,
  1345. cnt: rels[a]
  1346. }
  1347. });
  1348. }
  1349. function curryobj(oba, obj) {
  1350. var ret = {}
  1351. _.keys(oba).map(function(name) {
  1352. ret[name] = function() {
  1353. var args = Array.prototype.slice.apply(arguments, [0])
  1354. return oba[name].apply(oba, [obj].concat(args))
  1355. }
  1356. })
  1357. return ret;
  1358. }
  1359. Asystem.prototype.renderEditor2 = function(obj) {
  1360. var self = this;
  1361. var types = self.editorTypes();
  1362. self.otypes = curryobj(types, obj);
  1363. var modas = {}
  1364. // _.map(self.oget("system/modules").children,function(child,name){
  1365. // modas[name] = {name: name, func: child.modulefunc, meta: child._metas}
  1366. //})
  1367. self.allchildren().filter(function(a) {
  1368. return a.modulefunc
  1369. }).map(function(child) {
  1370. modas[child.name] = {
  1371. path: child.path,
  1372. name: child.name,
  1373. func: child.modulefunc,
  1374. meta: child._metas
  1375. }
  1376. })
  1377. // console.log("MODAS", modas);
  1378. /* var allmods = self.allchildren().filter(function(a) {
  1379. return a._metas.modules && a._metas.modules.length > 0;
  1380. })
  1381. console.log("ALLLMODS;", allmods);
  1382. */
  1383. var tabsa = self.oget("system/editor")._metas.tabs || [{
  1384. "title": "Editor",
  1385. modules: [{
  1386. "type": "sourcefulla",
  1387. "title": "Source"
  1388. }]
  1389. }];
  1390. function mkmod2(obj, field, types, fieldholder) {
  1391. var data = "";
  1392. if (modas[field.type]) {
  1393. data = modas[field.type].func.apply(self, [obj, field]);
  1394. } else {
  1395. if (types[field.type]) {
  1396. data = types[field.type](obj, field)
  1397. } else {
  1398. data = self.template(field.data, obj);
  1399. }
  1400. }
  1401. var s = '<div class="module ' + field.type + ' ' + (field.class ? field.class : '') + '" ><div class="head">' + field.title + '</div><div class="content">' + data +
  1402. '</div></div>'
  1403. return s
  1404. }
  1405. function rrtabs(tabs, prevals, level) {
  1406. if (prevals[level]) {} else {
  1407. prevals[level] = (tabs[0] || {}).name
  1408. }
  1409. var s = '<ul class="tabs">' + tabs.map(function(tab) {
  1410. var xx = prevals.slice(0, level + 1);
  1411. xx[level] = tab.name;
  1412. return self.template('<li data-tabis="{{prevalla}}" data-id="{{name}}" class="tablink {{active}}">{{title}}</li>', {
  1413. name: tab.name,
  1414. title: tab.title,
  1415. prevalla: xx.join(","),
  1416. active: (prevals[level] === tab.name ? 'active' : '')
  1417. })
  1418. }).join("\n")
  1419. if (level === 0 && obj._metas.tabs) {
  1420. var xx = prevals.slice(0, level + 1);
  1421. xx[level] = "tabs";
  1422. s += self.template('<li data-tabis="{{prevalla}}" data-id="{{name}}" class="tablink {{active}}">{{title}}</li>', {
  1423. name: "tabs",
  1424. title: "Tabs",
  1425. prevalla: xx.join(","),
  1426. active: (prevals[level] === "tabs" ? 'active' : '')
  1427. })
  1428. }
  1429. s += '</ul>';
  1430. s += '<div class="econ a' + level + ' ' + prevals[level] + '">'
  1431. if (prevals[level] === "tabs") {
  1432. s += mkmod2(obj, {
  1433. "type": "elementseditor",
  1434. "title": "Tabs",
  1435. "path": "_metas.tabs",
  1436. "childrenpath": "tabs"
  1437. }, types)
  1438. }
  1439. s += tabs.map(function(tab) {
  1440. var sa = "";
  1441. if (prevals[level] === tab.name) {
  1442. if (prevals[level] === "custom") {
  1443. var qq = []
  1444. var fff = obj.getStuff("_metas.fields", function(a) {
  1445. return a
  1446. }, "fi");
  1447. fff.map(function(ff) {
  1448. ff.data.map(function(field) {
  1449. if (field.showfor) {
  1450. if (field.showfor == "inheritants" && ff.name !== obj.path) {
  1451. qq.push(field)
  1452. } else {
  1453. if (field.showfor == "children" && obj.path.indexOf(ff.name) > -1 && ff.name !== obj.path) {
  1454. qq.push(field)
  1455. } else {
  1456. if (field.showfor == "all") {
  1457. qq.push(field)
  1458. }
  1459. }
  1460. }
  1461. } else {
  1462. if (field.showifset) {
  1463. if (opath.get(obj, field.showifset)) {
  1464. qq.push(field);
  1465. }
  1466. } else {
  1467. qq.push(field);
  1468. }
  1469. }
  1470. })
  1471. })
  1472. sa += qq.map(function(field, ii) {
  1473. var t = mkmod2(obj, field, types);
  1474. return t;
  1475. }).join("\n");
  1476. }
  1477. if (tab.modules) {
  1478. var qq = []
  1479. tab.modules.map(function(field, ii) {
  1480. if (field.showifset) {
  1481. if (opath.get(obj, field.showifset)) {
  1482. qq.push(field);
  1483. }
  1484. } else {
  1485. if (field.showifsetnot) {
  1486. if (!opath.get(obj, field.showifsetnot)) {
  1487. qq.push(field);
  1488. }
  1489. } else {
  1490. qq.push(field);
  1491. }
  1492. }
  1493. });
  1494. sa += qq.map(function(field, ii) {
  1495. var t = mkmod2(obj, field, types);
  1496. return t;
  1497. }).join("\n");
  1498. }
  1499. if (tab.tabs) {
  1500. sa += rrtabs(tab.tabs, prevals, level + 1);
  1501. }
  1502. }
  1503. return sa
  1504. }).join("");
  1505. s += '</div>';
  1506. return s
  1507. }
  1508. var sss = "";
  1509. var xtabs = tabsa
  1510. if (obj.path.indexOf("system/editor") !== 0) {
  1511. var fff = obj.getStuff("_metas.tabs", function(a) {
  1512. return a
  1513. }, "fi");
  1514. fff.map(function(a) {
  1515. xtabs = xtabs.concat(a.data)
  1516. })
  1517. // sss += rrtabs(obj._metas.tabs || [], self.ctabs, 0);
  1518. // xtabs = xtabs.concat( obj._metas.tabs )
  1519. } else {
  1520. }
  1521. sss += rrtabs(xtabs, self.ctabs, 0)
  1522. return '<div id="editor_inner">' +
  1523. '<div id="timeslider">T</div>' +
  1524. '<div id="editortop" class="' + (self.current !== obj.path ? 'notsame' : 'same') + '"><--view:' + self.current + ' ¬editor:' + obj.path + '</div>' +
  1525. '<div class="eed">' + sss + '</div>' +
  1526. '</div>';
  1527. }
  1528. Asystem.prototype.editorTypes = function() {
  1529. var self = this;
  1530. return require("./editor_types")(self)
  1531. }
  1532. Asystem.prototype.getRenderTemplate = function(obj, which) {
  1533. var self = this;
  1534. var stash = [];
  1535. var templates = []
  1536. var temps = obj.getStuff3("_data.template", function(a) {
  1537. return a
  1538. }, "_data.template")
  1539. var myself;
  1540. var x = {};
  1541. temps.map(function(a) {
  1542. x[a.name] = a
  1543. });
  1544. temps = _.map(x, function(a) {
  1545. return a
  1546. });
  1547. return temps
  1548. }
  1549. Asystem.prototype.rendertemplate2 = function(obj) {
  1550. var self = this;
  1551. var stash = [];
  1552. var templates = []
  1553. var temps = obj.getStuff3("_data.template", function(a) {
  1554. return a
  1555. }, "_data.template")
  1556. var myself;
  1557. var x = {};
  1558. temps.map(function(a) {
  1559. x[a.name] = a
  1560. });
  1561. temps = _.map(x, function(a) {
  1562. return a
  1563. });
  1564. var ss = '<div eletemplate="' + obj.path + '"> ' + self.template(obj._data.template, obj) + ' </div>'
  1565. return ss;
  1566. }
  1567. Asystem.prototype.rendertemplate = function(obj) {
  1568. var self = this;
  1569. var stash = [];
  1570. var templates = []
  1571. var temps = obj.getStuff3("_data.template", function(a) {
  1572. return a
  1573. }, "_data.template")
  1574. var x = {};
  1575. temps.map(function(a) {
  1576. x[a.name] = a
  1577. });
  1578. temps = _.map(x, function(a) {
  1579. return a
  1580. });
  1581. var ss = "";
  1582. temps.reverse();
  1583. var cont = "";
  1584. while (temps.length) {
  1585. var t = temps.pop();
  1586. console.log("T", t.name,t);
  1587. if (t.data.indexOf("{{content}}") > -1) {
  1588. t.data = t.data.replace('{{content}}', cont);
  1589. cont = '<div eletemplate="' + t.name + '">' + self.template(t.data, obj) + '</div>';
  1590. } else {
  1591. cont += '<div eletemplate="' + t.name + '">' + self.template(t.data, obj) + '</div>';
  1592. }
  1593. }
  1594. return cont
  1595. /* var ai = Object.assign({
  1596. "output": output,
  1597. "menu": menus,
  1598. "header": headers,
  1599. "selfie": JSON.stringify(temps, true, 2)
  1600. }, obj);
  1601. var ss = '<div eletemplate="' + outer.name + '"> ' + self.template(otemp, ai) + ' </div>'
  1602. */
  1603. /* var myself;
  1604. var x = {};
  1605. temps.map(function(a) {
  1606. x[a.name] = a
  1607. });
  1608. temps = _.map(x, function(a) {
  1609. return a
  1610. });
  1611. console.log("TEMPLATES:",x, temps);
  1612. if (obj._data.template && obj._data.template.indexOf("{{output}}") > -1) {
  1613. myself = temps.shift();
  1614. }
  1615. var outer = temps.filter(function(a) {
  1616. return a.data.indexOf("{{output}}") > -1
  1617. })
  1618. var otemp = "{{output}}"
  1619. var outera = "";
  1620. if (outer.length) {
  1621. outera = outer[0];
  1622. otemp = outer[0].data;
  1623. }
  1624. var output = temps.filter(function(a) {
  1625. return a.data.indexOf("{{output}}") === -1 && a.name.indexOf("menu") === -1 && a.name.indexOf("header") === -1 && a.from.indexOf("isa_") === -1
  1626. }).map(function(tmp) {
  1627. return '<div eletemplate="' + tmp.name + '"> ' + self.template(tmp.data, obj) + ' </div>';
  1628. }).join("\n")
  1629. var menus = temps.filter(function(a) {
  1630. return a.name.indexOf("menu") > -1
  1631. }).slice(0, 1).map(function(tmp) {
  1632. return '<div eletemplate="' + tmp.name + '"> ' + self.template(tmp.data, obj) + ' </div>';
  1633. }).join("\n")
  1634. var headers = temps.filter(function(a) {
  1635. return a.name.indexOf("header") > -1
  1636. }).slice(0, 1).map(function(tmp) {
  1637. return '<div eletemplate="' + tmp.name + '"> ' + self.template(tmp.data, obj) + ' </div>';
  1638. }).join("\n")*/
  1639. /*
  1640. var isas = temps.filter(function(a) {
  1641. return a.from.indexOf("isa_") > -1
  1642. }).map(function(tmp) {
  1643. console.log("ISA,",tmp);
  1644. if(tmp.inas.length>2){
  1645. var tma=tmp.inas.pop();
  1646. tma=tmp.inas.pop();
  1647. return '<div eletemplate="' + tmp.name + '">IIS ' + self.template(tmp.data, self.oget(tma)) + ' </div>';
  1648. }else{
  1649. return '<div eletemplate="' + tmp.name + '">IIS ' + self.template(tmp.data, self.oget(tmp.name)) + ' </div>';
  1650. }
  1651. }).join("\n")
  1652. */
  1653. /*if (myself) {
  1654. var ai = Object.assign({
  1655. "output": output,
  1656. "menu": menus,
  1657. "header": headers,
  1658. "selfie": JSON.stringify(temps, true, 2)
  1659. }, obj);
  1660. var ss = '<div eletemplate="' + myself.name + '"> ' + self.template(myself.data, ai) + ' </div>'
  1661. } else {
  1662. var ai = Object.assign({
  1663. "output": output,
  1664. "menu": menus,
  1665. "header": headers,
  1666. "selfie": JSON.stringify(temps, true, 2)
  1667. }, obj);
  1668. var ss = '<div eletemplate="' + outer.name + '"> ' + self.template(otemp, ai) + ' </div>'
  1669. }
  1670. */
  1671. return ss
  1672. }
  1673. Asystem.prototype.rendertemplatex = function(obj) {
  1674. var self = this;
  1675. var stash = [];
  1676. var templates = []
  1677. var temps = obj.getStuff3("_data.template", function(a) {
  1678. return a
  1679. }, "_data.template")
  1680. var myself;
  1681. var x = {};
  1682. temps.map(function(a) {
  1683. x[a.name] = a
  1684. });
  1685. temps = _.map(x, function(a) {
  1686. return a
  1687. });
  1688. console.log("TEMPLATES:", x, temps);
  1689. if (obj._data.template && obj._data.template.indexOf("{{output}}") > -1) {
  1690. myself = temps.shift();
  1691. }
  1692. var outer = temps.filter(function(a) {
  1693. return a.data.indexOf("{{output}}") > -1
  1694. })
  1695. var otemp = "{{output}}"
  1696. var outera = "";
  1697. if (outer.length) {
  1698. outera = outer[0];
  1699. otemp = outer[0].data;
  1700. }
  1701. var output = temps.filter(function(a) {
  1702. return a.data.indexOf("{{output}}") === -1 && a.name.indexOf("menu") === -1 && a.name.indexOf("header") === -1 && a.from.indexOf("isa_") === -1
  1703. }).map(function(tmp) {
  1704. return '<div eletemplate="' + tmp.name + '"> ' + self.template(tmp.data, obj) + ' </div>';
  1705. }).join("\n")
  1706. var menus = temps.filter(function(a) {
  1707. return a.name.indexOf("menu") > -1
  1708. }).slice(0, 1).map(function(tmp) {
  1709. return '<div eletemplate="' + tmp.name + '"> ' + self.template(tmp.data, obj) + ' </div>';
  1710. }).join("\n")
  1711. var headers = temps.filter(function(a) {
  1712. return a.name.indexOf("header") > -1
  1713. }).slice(0, 1).map(function(tmp) {
  1714. return '<div eletemplate="' + tmp.name + '"> ' + self.template(tmp.data, obj) + ' </div>';
  1715. }).join("\n")
  1716. /*
  1717. var isas = temps.filter(function(a) {
  1718. return a.from.indexOf("isa_") > -1
  1719. }).map(function(tmp) {
  1720. console.log("ISA,",tmp);
  1721. if(tmp.inas.length>2){
  1722. var tma=tmp.inas.pop();
  1723. tma=tmp.inas.pop();
  1724. return '<div eletemplate="' + tmp.name + '">IIS ' + self.template(tmp.data, self.oget(tma)) + ' </div>';
  1725. }else{
  1726. return '<div eletemplate="' + tmp.name + '">IIS ' + self.template(tmp.data, self.oget(tmp.name)) + ' </div>';
  1727. }
  1728. }).join("\n")
  1729. */
  1730. if (myself) {
  1731. var ai = Object.assign({
  1732. "output": output,
  1733. "menu": menus,
  1734. "header": headers,
  1735. "selfie": JSON.stringify(temps, true, 2)
  1736. }, obj);
  1737. var ss = '<div eletemplate="' + myself.name + '"> ' + self.template(myself.data, ai) + ' </div>'
  1738. } else {
  1739. var ai = Object.assign({
  1740. "output": output,
  1741. "menu": menus,
  1742. "header": headers,
  1743. "selfie": JSON.stringify(temps, true, 2)
  1744. }, obj);
  1745. var ss = '<div eletemplate="' + outer.name + '"> ' + self.template(otemp, ai) + ' </div>'
  1746. }
  1747. return ss
  1748. }
  1749. Asystem.prototype.allchildren = function(ronly) {
  1750. var self = this;
  1751. var arr = [];
  1752. if (ronly) {
  1753. return [].concat.apply(arr, allchildren(self.oo[ronly]))
  1754. }
  1755. _.keys(self.oo).filter((k) => typeof(self.oo[k]) === "function").map(function(key) {
  1756. arr = [].concat.apply(arr, allchildren(self.oo[key]))
  1757. })
  1758. return arr;
  1759. }
  1760. Asystem.prototype.allchildpaths = function(ronly) {
  1761. var self = this;
  1762. var arr = [];
  1763. _.keys(self.oo).filter((k) => typeof(self.oo[k]) === "function").map(function(key) {
  1764. ronly ? (ronly === key ? arr = [].concat.apply(arr, allchildpaths(self.oo[key])) : null) : arr = [].concat.apply(arr, allchildpaths(self.oo[key]))
  1765. })
  1766. return arr;
  1767. }
  1768. Asystem.prototype.jsonstringify = function(obj, pretty, ind) {
  1769. function recur_fy(ob) {
  1770. var re = _.isObject(ob) && !_.isArray(ob) ? {} : [];
  1771. _.map(ob, function(val, key) {
  1772. if ((_.isObject(val) || _.isArray(val)) && typeof(val) !== "function") {
  1773. re[key] = recur_fy(val);
  1774. } else if (typeof(val) === "function") {
  1775. re["$$$" + key] = val + "";
  1776. } else {
  1777. re[key] = val;
  1778. }
  1779. })
  1780. return re;
  1781. }
  1782. return JSON.stringify(recur_fy(obj), pretty, ind);
  1783. }
  1784. Asystem.prototype.jsonparse = function(str) {
  1785. var obj = JSON.parse(str);
  1786. function recur_fy(ob) {
  1787. var re = _.isObject(ob) && !_.isArray(ob) ? {} : [];
  1788. _.map(ob, function(val, key) {
  1789. if ((_.isObject(val) || _.isArray(val)) && typeof(val) !== "function") {
  1790. re[key] = recur_fy(val);
  1791. } else {
  1792. if ((key + "").indexOf("$$$") === 0) {
  1793. var ttt = {};
  1794. try {
  1795. eval("(function(tt){ tt.func = " + val + ";})(ttt)");
  1796. re[key.substring(3)] = ttt.func;
  1797. } catch (ea) {
  1798. console.log("error func: ", ea)
  1799. }
  1800. } else {
  1801. re[key] = val;
  1802. }
  1803. }
  1804. })
  1805. return re;
  1806. }
  1807. return recur_fy(obj);
  1808. }
  1809. module.exports = Asystem;