index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. var EventEmitter = require("events").EventEmitter;
  2. var opath = require("opath");
  3. var async = require("async");
  4. var getthezip = require("libs/getthezip");
  5. var unpackzip = require("libs/unpackzip");
  6. var generatezip = require("libs/generatezip")
  7. var slugger = function(str) {
  8. var ret = ("" + str).replace(/\/+/g, '/').replace(/[^\x20-\x7E]/g, '')
  9. // ret=decodeURIComponent(ret);
  10. ret = ret.replace(/[\<\>]/g, '')
  11. if (ret.indexOf("/") == ret.length - 1) {
  12. ret = ret.substring(0, ret.length - 1);
  13. }
  14. return ret
  15. }
  16. var _ = require("lodash");
  17. function Asystem(oo) {
  18. this.oo = oo;
  19. this.name = "noname"
  20. this._logs = [];
  21. }
  22. Asystem.prototype.__proto__ = EventEmitter.prototype;
  23. Asystem.prototype.slugger = function(str) {
  24. return slugger(str);
  25. }
  26. Asystem.prototype.odelete = function(path) {
  27. var self = this;
  28. self.omove(path, "trash/" + path)
  29. if (self.oo[path]) {
  30. _.map(self.oo[path]().children, function(child, name) {
  31. self.odelete(child().path)
  32. })
  33. delete self.oo[path]
  34. }
  35. self.emit("status", "deleted " + path)
  36. }
  37. Asystem.prototype.omove = function(path, topath) {
  38. var self = this;
  39. var newa = self.oget(topath)
  40. var olda = self.oget(path);
  41. _.map(olda._relations, function(rels, relsname) {
  42. _.map(rels, function(rel, name) {
  43. delete rel.ref._relations[rel.context.out]
  44. newa.relatesAsToWith(relsname, rel.ref, rel.context.out, rel.context)
  45. })
  46. delete olda._relations[relsname]
  47. })
  48. _.map(olda._isa, function(isa, name) {
  49. newa.isa(isa)
  50. delete isa._hasa[name]
  51. delete olda._isa[name]
  52. })
  53. _.map(olda._hasa, function(hasa, name) {
  54. hasa.isa(newa)
  55. delete hasa._isa[olda.path]
  56. delete olda._hasa[name]
  57. })
  58. _.map(olda._inherits, function(inheri, name) {
  59. newa.inherits(inheri)
  60. delete inheri._extends[olda.path]
  61. delete olda._inherits[name]
  62. })
  63. _.map(olda._extends, function(extendi, name) {
  64. newa.extends(extendi)
  65. delete extendi._inherits[olda.path]
  66. delete olda._extends[name]
  67. })
  68. newa._created = olda._created;
  69. newa._data = _.omit(olda._data, []);
  70. newa._meta = _.omit(olda._meta, []);
  71. _.map(_.omit(olda, electra.ignores), function(val, ke) {
  72. newa[ke] = val;
  73. })
  74. _.map(olda.parents, function(par, nam) {
  75. delete par.children[olda.name];
  76. })
  77. delete olda;
  78. self.emit("status", "moved " + path + " to " + topath)
  79. }
  80. Asystem.prototype.oset = function(path, data) {
  81. var self = this;
  82. path = self.slugger(path);
  83. var ar = path.split("/");
  84. var first = ar.shift();
  85. var prop, obj;
  86. if (typeof(self.oo[first]) === "undefined") {
  87. self.oo[first] = self.oo(first);
  88. self.oo[first]()._isdirty = true;
  89. self.emit("status", "auto created " + first + " :" + self.oo(first)().path)
  90. }
  91. if (ar.length > 0 && ar[ar.length - 1].indexOf(".") === 0) {
  92. prop = ar.pop().substring(1);
  93. }
  94. path = ar.join("/")
  95. if (path !== "") {
  96. obj = self.oo[first](path)();
  97. } else {
  98. obj = self.oo[first]()
  99. }
  100. if (prop) {
  101. obj._isdirty = true;
  102. opath.setpath(obj, prop, data);
  103. //return obj[prop] || getpath(obj, prop)
  104. self.emit("status", "set prop : " + obj.path + " / " + prop + " to" + data)
  105. }
  106. return obj;
  107. }
  108. Asystem.prototype.oget = function(path, alternative) {
  109. var self = this;
  110. path = self.slugger(path);
  111. var ar = path.split("/");
  112. var first = ar.shift();
  113. var prop, obj;
  114. if (typeof(self.oo[first]) === "undefined") {
  115. self.oo[first] = self.oo(first);
  116. self.oo[first]()._isdirty = true;
  117. self.emit("status", "auto created " + first + " :" + self.oo(first)().path)
  118. }
  119. if (ar.length > 0 && ar[ar.length - 1].indexOf(".") === 0) {
  120. prop = ar.pop().substring(1);
  121. }
  122. path = ar.join("/")
  123. if (path !== "") {
  124. obj = self.oo[first](path)();
  125. } else {
  126. obj = self.oo[first]()
  127. }
  128. if (prop) {
  129. return obj[prop] || opath.getpath(obj, prop) || alternative;
  130. }
  131. return obj;
  132. }
  133. Asystem.prototype.getele = function(action) {
  134. var self = this;
  135. action = self.slugger(action);
  136. var a = action.split("/");
  137. var roo = a.shift();
  138. var ffu;
  139. if (a[a.length - 1].indexOf(".") > -1) {
  140. //function call;
  141. ffu = a.pop().split(".").pop();
  142. } else {
  143. //straight
  144. }
  145. var name = a.join("/");
  146. var eles = self.oo[roo](name)();
  147. if (ffu && eles[ffu]) {
  148. return {
  149. ele: eles,
  150. func: eles[ffu]
  151. };
  152. }
  153. return {
  154. ele: eles
  155. }
  156. }
  157. Asystem.prototype.ofind = function(path) {
  158. /* returns {found: "spath"|false , remainder: "rest"|"" , obj: obj}*/
  159. var self = this;
  160. path = self.slugger(path);
  161. var ar = path.split("/");
  162. var first = ar.shift();
  163. if (typeof(self.oo[first]) === "undefined") {
  164. return {
  165. found: false
  166. }
  167. } else {
  168. var pa = self.oo[first]()
  169. var ch = "";
  170. var rest = [];
  171. while (ar.length) {
  172. ch = ar.shift();
  173. if (pa.children[ch]) {
  174. pa = pa.children[ch]();
  175. } else {
  176. rest.push(ch)
  177. }
  178. }
  179. return {
  180. found: pa.path,
  181. /* is false if not found*/
  182. remainder: rest.join("/"),
  183. /* is not "" if not found rest of path*/
  184. obj: pa /* the found object*/
  185. }
  186. }
  187. }
  188. Asystem.prototype.rev_conv = function(obj, path) {
  189. var self = this;
  190. var zzz = self.oget(obj.path);
  191. zzz.data(obj.data)
  192. zzz._created = new Date(obj.created);
  193. zzz.metas(obj.meta);
  194. if (_.size(obj.rel) > 0) {
  195. _.map(obj.rel, function(rels, name) {
  196. _.map(rels, function(dats, relator) {
  197. if (name === dats.in) {
  198. } else {
  199. zzz.relatesAsToWith(dats.out, self.oget(relator), dats.in, dats)
  200. }
  201. })
  202. })
  203. }
  204. if (obj.inherits && obj.inherits.length) {
  205. obj.inherits.map(function(alp) {
  206. zzz.inherits(self.oget(alp))
  207. })
  208. }
  209. if (obj.extends && obj.extends.length) {
  210. obj.extends.map(function(alp) {
  211. zzz.extends(self.oget(alp))
  212. })
  213. }
  214. if (obj.isa && obj.isa.length) {
  215. obj.isa.map(function(alp) {
  216. zzz.isa(self.oget(alp))
  217. })
  218. }
  219. if (obj.hasa && obj.hasa.length) {
  220. obj.hasa.map(function(alp) {
  221. self.oget(alp).isa(zzz)
  222. })
  223. }
  224. _.map(_.omit(obj, ["name", "hasa", "meta", "data", "parents", "children", "path", "_rootname", "created", "rel", "inherits", "extends", "isa", "children"]), function(
  225. val,
  226. ke) {
  227. if (typeof(val) === "string" && val.indexOf("$$$FUNCTION$$$") === 0) {
  228. // console.log(val.split("$$$FUNCTION$$$").pop())
  229. if (zzz._metas && zzz._metas.protected && zzz._metas.protected[ke]) {
  230. } else {
  231. var ttt = {};
  232. eval("(function(tt){ tt.func = " + val.split("$$$FUNCTION$$$").pop() + ";})(ttt)");
  233. zzz[ke] = ttt.func;
  234. }
  235. } else {
  236. if (typeof(val) === "object") {
  237. var r = {}
  238. _.map(val, function(v, kp) {
  239. if (typeof(v) === "string" && v.indexOf("$$$FUNCTION$$$") === 0) {
  240. var ttt = {};
  241. eval("(function(tt){ tt.func = " + v.split("$$$FUNCTION$$$").pop() + ";})(ttt)");
  242. r[kp] = ttt.func;
  243. } else {
  244. r[kp] = v
  245. }
  246. })
  247. zzz[ke] = r;
  248. } else {
  249. zzz[ke] = val
  250. }
  251. }
  252. })
  253. if (obj.children && obj.children.length) {
  254. obj.children.map(function(child) {
  255. self.rev_conv(child)
  256. })
  257. }
  258. }
  259. Asystem.prototype.save = function() {
  260. var self = this;
  261. return new Promise(function(resolve, reject) {
  262. $lib.exporter(self, function(err, data) {
  263. $lib.generate_thezip(self.name + "-" + $lib.getTimedName() + ".zip",
  264. $lib.arrayedToObject(data, "name"), function(err, zz) {
  265. console.log(err);
  266. resolve(zz)
  267. })
  268. })
  269. })
  270. }
  271. Asystem.prototype.exporter = function(cb) {
  272. var self = this;
  273. var obj = self.oo;
  274. var stores = _.keys(obj).filter(function(a) {
  275. return typeof(obj[a]) === "function"
  276. }).map(function(a) {
  277. return obj[a]()
  278. }).map(function(a) {
  279. var da = a.exportSync("");
  280. return da
  281. });
  282. stores.unshift({
  283. "name": "namespace.manifest",
  284. "data": {
  285. "namespace": self.name
  286. }
  287. })
  288. var ret = {}
  289. stores.map(function(a) {
  290. ret[a.name] = a;
  291. })
  292. cb(null, ret)
  293. }
  294. $linkstr = '<li class="{{liclass}}"><i class="{{iclass}}"></i> <a href="{{href}}" class="ele">{{title}}</a> <em>{{size}}</em></li>';
  295. Asystem.prototype.status = function() {
  296. var self = this;
  297. return new Promise(function(resolve, reject) {
  298. var ss = [];
  299. ss.push('<pre onclick="$lib.toggle_admin()">' + $lib.ooa.oget("root/._data.title") + '</pre><hr>')
  300. _.keys(self.oo).filter(aa => ["xx"].indexOf(aa) === -1 && typeof(self.oo[aa]) === "function").map(function(key) {
  301. ss.push($lib.template($linkstr, {
  302. href: $lib.createnav(self, key),
  303. title: self.oget(key + "/._data.title", key),
  304. iclass: self.oget(key + "/._data.iconclass", "fa fa-database"),
  305. liclass: key,
  306. size: _.size(self.oo[key]().children)
  307. }))
  308. /* ss.push('<li><i class="fa fa-database"></i><a href="' + $lib.createnav(self, key) + '"> ' + key + '' +
  309. '</a>'+ (self.oo[key]().children ?
  310. '<em>'+_.size(
  311. self.oo[key]().children) :
  312. "00")+'</li>')*/
  313. })
  314. var ts1 =
  315. '<div class="sela"><span class="button-dropdown button-dropdown button-dropdown-normal" data-buttons="dropdown">\
  316. <button class="button button-stacked">\
  317. ' +
  318. $lib.ooa.name + '\
  319. <i class="fa fa-bars"></i></button>\
  320. <ul class="button-dropdown-list is-below">';
  321. var ts2 = '</ul>\
  322. </span></div>'
  323. ss.push("<br><br><div id=\"menu_roots\"><pre>Roots</pre><hr>" + ts1 +
  324. '' + _.map($lib._pages, function(val, key) {
  325. return '<li><a class=" ' + '" onclick="$lib.setCurrentPage(\'' +
  326. key + '\');">' + key + '</a></li>'
  327. }).join("\n") + ts2 + '</div>'
  328. )
  329. resolve(ss.join("\n"));
  330. })
  331. }
  332. Asystem.prototype.log = function() {}
  333. Asystem.prototype.loadSite = function(url) {
  334. var self = this;
  335. return new Promise(function(resolve, reject) {
  336. var importer = self.importer_zip()
  337. importer.on("done", function() {
  338. console.log("DONENENENENE");
  339. })
  340. importer.on("link", function() {
  341. console.log("DONENENENENE");
  342. })
  343. $lib.getthezip(url)
  344. .catch(function() {
  345. console.log("ERROR:", arguments);
  346. return false;
  347. })
  348. .then($lib.unpack_thezip2(importer))
  349. })
  350. }
  351. Asystem.prototype.xloadSite = function(url) {
  352. console.log("getting:::", url)
  353. var self = this;
  354. }
  355. Asystem.prototype.loadZip = function(url, callback, linked) {
  356. var self = this;
  357. if (linked) {
  358. self._linked = self._linked || {};
  359. self._linked[linked] = url;
  360. }
  361. getthezip(url)
  362. .then(function(data) {
  363. self.emit("test","got data "+data.length)
  364. unpackzip(self.importer_zip())(data)
  365. .then(
  366. function() {
  367. self.emit("test","early ready yes")
  368. if (self._loadqueue && self._loadqueue.length) {
  369. var ff = self._loadqueue.shift()
  370. self.loadZip(ff[1], callback, ff[0]);
  371. } else {
  372. if (self.name_imported) {
  373. self.name = self.name_imported;
  374. }
  375. callback(null, self);
  376. }
  377. }
  378. )
  379. .catch(console.log)
  380. })
  381. .catch(console.log)
  382. }
  383. Asystem.prototype.saveZip = function() {
  384. var self = this;
  385. return new Promise(function(resolve, reject) {
  386. self.exporter(function(err, data) {
  387. var seperate_save = _.keys(data).filter(function(name) {
  388. return self._linked[name + ".link"]
  389. })
  390. var ddata = _.omit(data, seperate_save);
  391. seperate_save.map(function(om){
  392. var url=self._linked[om + ".link"].split("/");
  393. url.pop();
  394. url.push(self.name + "-" + om + ".zip");
  395. ddata[om+".link"] = url.join("/");
  396. })
  397. generatezip(self.name + "-" + "" + ".zip", ddata, function(err, zz) {
  398. async.mapSeries(seperate_save, function(name, next) {
  399. var dda = {};
  400. dda[name] = data[name];
  401. generatezip(self.name + "-" + name + ".zip", dda, next)
  402. }, function(err, all) {
  403. all.push(zz);
  404. resolve(all)
  405. })
  406. })
  407. })
  408. })
  409. }
  410. Asystem.prototype.importer_zip = function() {
  411. var self = this;
  412. var emname = "system/emmm/a"; //+ $lib.random(0xffffff)
  413. var emmm = self.oget(emname);
  414. emmm.on("file", function(filename, data) {
  415. if (filename.indexOf(".link") > -1) {
  416. emmm.emit("link", data);
  417. self._loadqueue = self._loadqueue || [];
  418. self._loadqueue.push([filename, data])
  419. // self.loadSite(data);
  420. }
  421. })
  422. emmm.on("json", function(filename, data) {
  423. self.emit("gotfile", filename);
  424. self.rev_conv(data)
  425. })
  426. emmm.on("namespace", function(data, cont) {
  427. self.emit("gotfile", "namespace", data.namespace);
  428. self.name_imported = data.namespace;
  429. cont();
  430. })
  431. emmm.on("nonamespace", function(cont) {
  432. //self.name = url.split("/").pop().split(".").shift();
  433. self.emit("gotfile", "nonamespace", self.name);
  434. cont();
  435. })
  436. return emmm
  437. }
  438. module.exports = Asystem;