cube-code.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /**
  2. * Copyright 2021, 2024 5 Mode
  3. *
  4. * This file is part of SnipSwap.
  5. *
  6. * SnipSwap is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * SnipSwap is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with SnipSwap. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. * cube-code.js
  20. *
  21. * Cube Code for home.php.
  22. *
  23. * @author Daniele Bonini <my25mb@aol.com>
  24. * @copyrights (c) 2021, 2024, 5 Mode
  25. */
  26. var cubes = []; // Cube array
  27. var offers = []; // Cube array
  28. var curcube; // Current cube
  29. var _selectedcube = 0; // Selected cube
  30. var totcubes = 0;
  31. /*
  32. * myCube Class
  33. *
  34. * @param string myname Current name of the cube
  35. * @param array mymap Face map
  36. * @param string myformalName Formal name of the cube
  37. * @returns myCube
  38. */
  39. function myCube(myname, myformalName, myAPP_HOST) {
  40. // Examples of cube moves:
  41. // 0, 0, 1, 0, 0
  42. // 0, 0, 5, 0, 0 hcur = [3, 4]
  43. // 1, 2, 3, 4, 1 vcur = [3, 5] shift(hlist[1,4]); set jolly = hlist[4];
  44. // 0, 0, 6, 0, 0 jolly = 1
  45. // 0, 0, 1, 0, 0
  46. // 1 turn 1 face up
  47. // 0, 0, 5, 0, 0
  48. // 0, 0, 3, 0, 0 hcur = [6, 4]
  49. // 5, 2, 6, 4, 5 vcur = [6, 3] shift(vlist[1,4]); set jolly = vlist[4];
  50. // 0, 0, 1, 0, 0 jolly = 5
  51. // 0, 0, 5, 0, 0
  52. // 1 turn 1 face left
  53. // 0, 0, 4, 0, 0
  54. // 0, 0, 5, 0, 0 hcur = [2, 3]
  55. // 4, 1, 2, 3, 4 vcur = [2, 5] shift(hlist[1,4]); set jolly = hlist[4];
  56. // 0, 0, 6, 0, 0 jolly = 4
  57. // 0, 0, 4, 0, 0
  58. // 1 turn 1 face left
  59. // 0, 0, 3, 0, 0
  60. // 0, 0, 5, 0, 0 hcur = [1, 2]
  61. // 3, 4, 1, 2, 3 vcur = [1, 5] shift(hlist[1,4]); set jolly = hlist[4];
  62. // 0, 0, 6, 0, 0 jolly = 3
  63. // 0, 0, 3, 0, 0
  64. // 1 turn 1 face rght
  65. // 0, 0, 4, 0, 0
  66. // 0, 0, 5, 0, 0 hcur = [1, 2]
  67. // 4, 1, 2, 3, 4 vcur = [1, 5] shift(hlist[1,4]); set jolly = hlist[4];
  68. // 0, 0, 6, 0, 0 jolly = 3
  69. // 0, 0, 4, 0, 0
  70. // Properties
  71. this.name = myname;
  72. this.map = ["Address", "Contacts", "Other Info", "Menu", "Pictures", "Password"];
  73. this.formalName = myformalName;
  74. this.guid = "";
  75. this.password = "";
  76. this.cats = "";
  77. this.desc = "";
  78. this.title = "";
  79. this.label = "";
  80. this.link = "";
  81. this.xml = "";
  82. this.hcube = [0,1,2,3,0];
  83. this.vcube = [0,5,2,4,0];
  84. this.hlist = [1,2,3,0];
  85. this.vlist = [5,2,4,0];
  86. this.jolly = 0;
  87. this.hcur = [this.hcube[2], this.hcube[3]];
  88. this.vcur = [this.vcube[2], this.vcube[3]];
  89. // Methods
  90. this.updateCube = myupdateCube;
  91. this.turnLeft = myturnLeft;
  92. this.turnRight = myturnRight;
  93. this.turnUp = myturnUp;
  94. this.turnDown = myturnDown;
  95. this.getFace = mygetFace;
  96. this.gethcube = mygethcube;
  97. this.getvcube = mygetvcube;
  98. this.gethcur = mygethcur;
  99. this.getvcur = mygetvcur;
  100. this.getname = mygetname;
  101. this.getguid = mygetguid;
  102. this.getpassword = mygetpassword;
  103. this.getxml = mygetxml;
  104. this.savedata = mysavedata;
  105. this.start = mystart;
  106. // -- Methods End
  107. /*
  108. * Execute horizontal / vertical movement of the cube
  109. *
  110. * @param char Update direction
  111. * @returns void
  112. */
  113. function myupdateCube(tthis, upddir) {
  114. if (upddir=='h') {
  115. tthis.hcube[0] = tthis.jolly;
  116. for(i=1;i<=tthis.hlist.length;i++) {
  117. tthis.hcube[i] = tthis.hlist[i-1];
  118. }
  119. tthis.vcube[2] = tthis.hcube[2];
  120. tthis.vcube[0] = tthis.jolly;
  121. tthis.vcube[4] = tthis.jolly;
  122. } else {
  123. tthis.vcube[0] = tthis.jolly;
  124. for(i=1;i<=tthis.vlist.length;i++) {
  125. tthis.vcube[i] = tthis.vlist[i-1];
  126. }
  127. tthis.hcube[2] = tthis.vcube[2];
  128. tthis.hcube[0] = tthis.jolly;
  129. tthis.hcube[4] = tthis.jolly;
  130. }
  131. tthis.hcur = [tthis.hcube[2], tthis.hcube[3]];
  132. tthis.vcur = [tthis.vcube[2], tthis.vcube[3]];
  133. }
  134. /*
  135. * Move the current cube to the left
  136. *
  137. * @returns void
  138. */
  139. function myturnLeft() {
  140. var newhlist = [];
  141. var newfirst = this.hlist.pop();
  142. newhlist[0] = newfirst;
  143. newhlist[1] = this.hlist[0];
  144. newhlist[2] = this.hlist[1];
  145. newhlist[3] = this.hlist[2];
  146. this.hlist = newhlist;
  147. this.jolly = this.hlist[3];
  148. // Save the horizzontal movement..
  149. this.updateCube(this, 'h');
  150. }
  151. /*
  152. * Move the current cube to the right
  153. *
  154. * @returns void
  155. */
  156. function myturnRight() {
  157. var newhlist = [];
  158. var newlast = this.hlist.shift();
  159. newhlist[0] = this.hlist[0];
  160. newhlist[1] = this.hlist[1];
  161. newhlist[2] = this.hlist[2];
  162. newhlist[3] = newlast;
  163. this.hlist = newhlist;
  164. this.jolly = this.hlist[3];
  165. // Save the horizzontal movement..
  166. this.updateCube(this, 'h');
  167. }
  168. /*
  169. * Move the current cube upward
  170. *
  171. * @returns void
  172. */
  173. function myturnUp() {
  174. var newvlist = [];
  175. var newfirst = this.vlist.pop();
  176. newvlist[0] = newfirst;
  177. newvlist[1] = this.vlist[0];
  178. newvlist[2] = this.vlist[1];
  179. newvlist[3] = this.vlist[2];
  180. this.vlist = newvlist;
  181. this.jolly = this.vlist[3];
  182. // Save the vertical movement..
  183. this.updateCube(this, 'v');
  184. }
  185. /*
  186. * Move the current cube downward
  187. *
  188. * @returns void
  189. */
  190. function myturnDown() {
  191. var newvlist = [];
  192. var newlast = this.vlist.shift();
  193. newvlist[0] = this.vlist[0];
  194. newvlist[1] = this.vlist[1];
  195. newvlist[2] = this.vlist[2];
  196. newvlist[3] = newlast;
  197. this.vlist = newvlist;
  198. this.jolly = this.vlist[3];
  199. // Save the vertical movement..
  200. this.updateCube(this, 'v');
  201. }
  202. /*
  203. * Get the cube face associated to the given coordinates [v0|v1|h0|h1]
  204. *
  205. * @returns string
  206. */
  207. function mygetFace(iface) {
  208. // v0 = front
  209. // v1 = up
  210. // h0 = front
  211. // h2 = right
  212. switch (iface) {
  213. case "h0":
  214. return this.map[this.hcur[0]];
  215. break;
  216. case "h1":
  217. return this.map[this.hcur[1]];
  218. break;
  219. case "v0":
  220. return this.map[this.vcur[0]];
  221. break;
  222. case "v1":
  223. return this.map[this.vcur[1]];
  224. break;
  225. }
  226. }
  227. /*
  228. * Expose the current horizontal list of the cube, for debug purpose
  229. *
  230. * @returns string
  231. */
  232. function mygethcube() {
  233. return this.hcube.toString();
  234. }
  235. /*
  236. * Expose the current vertical list of the cube, for debug purpose
  237. *
  238. * @returns string
  239. */
  240. function mygetvcube() {
  241. return this.vcube.toString();
  242. }
  243. /*
  244. * Expose the current horizontal position of the cube, for debug purpose
  245. *
  246. * @returns string
  247. */
  248. function mygethcur() {
  249. return this.hcur.toString();
  250. }
  251. /*
  252. * Expose the current vertical position of the cube, for debug purpose
  253. *
  254. * @returns string
  255. */
  256. function mygetvcur() {
  257. return this.vcur.toString();
  258. }
  259. /*
  260. * Get the cube name
  261. *
  262. * @returns string
  263. */
  264. function mygetname() {
  265. return this.name;
  266. }
  267. /*
  268. * Save the cube data locally
  269. *
  270. * @returns void
  271. */
  272. function mysavedata() {
  273. //fileName = myformalName;
  274. var ffileName = this.formalName;
  275. var xxml = this.xml;
  276. //alert(ffileName);
  277. $.ajax({
  278. method: "POST",
  279. async: false,
  280. url: "/putxml",
  281. dataType: "json",
  282. jsonp: false,
  283. data: {
  284. f: ffileName,
  285. xml: xxml
  286. },
  287. success: function( data ) {
  288. // Handle 'no match' indicated by [ "" ] response
  289. //response( data.length === 1 && data[0].length === 0 ? [] : data );
  290. if (data.length < 1 || data.length === 1) {
  291. alert("An error happened saving the data (#1)!");
  292. } else if (data.length === 2 && data[0] === 200) {
  293. //alert("Data saved successfully!");
  294. } else {
  295. alert("An error happened saving the data (#2)!");
  296. //alert(data[0]);
  297. //alert(data[1]);
  298. //alert(data[2]);
  299. //alert(data[3]);
  300. //alert(data[4]);
  301. }
  302. },
  303. error: function (responseData, textStatus, errorThrown) {
  304. alert("POST failed: " + errorThrown + " data: " + responseData.responseText);
  305. }
  306. });
  307. }
  308. /*
  309. * Cube init procedure:
  310. * - read the xml data associated with the cube
  311. *
  312. * @returns void
  313. */
  314. function mystart() {
  315. bConnectionOK = false;
  316. var xhttp = new XMLHttpRequest();
  317. //alert(myname);
  318. var xmluri = "http://" + myAPP_HOST + "/getxml?f=" + this.formalName;
  319. //alert(xmluri);
  320. xhttp.open("GET", xmluri, true);
  321. xhttp.send();
  322. xhttp.onreadystatechange = function () {
  323. if (this.readyState == 4 && this.status == 200) {
  324. bConnectionOK = true;
  325. xml = this.responseText
  326. var z;
  327. var myObj;
  328. z = parseInt(myname.substr(5));
  329. //alert(z);
  330. myObj = cubes[z-1];
  331. myObj.xml = xml;
  332. re = new RegExp("\<guid\>(.*)\<\/guid\>", "igsu");
  333. a = re.exec(xml);
  334. if (!a || a.length===0) {
  335. document.write("Data access error (#2)<br><br>");
  336. return;
  337. } else {
  338. myObj.guid = a[1];
  339. }
  340. re = new RegExp("\<password\>(.*)\<\/password\>", "igsu");
  341. a = re.exec(xml);
  342. if (!a || a.length===0) {
  343. document.write("Data access error (#3)<br><br>");
  344. return;
  345. } else {
  346. myObj.password = a[1];
  347. }
  348. re = new RegExp("\<cats\>(.*)\<\/cats\>", "igsu");
  349. a = re.exec(xml);
  350. if (!a || a.length===0) {
  351. document.write("Data access error (#4)<br><br>");
  352. return;
  353. } else {
  354. myObj.cats = a[1];
  355. }
  356. re = new RegExp("\<desc\>(.*)\<\/desc\>", "igsu");
  357. a = re.exec(xml);
  358. if (!a || a.length===0) {
  359. document.write("Data access error (#5)<br><br>");
  360. return;
  361. } else {
  362. myObj.desc = a[1];
  363. }
  364. re = new RegExp("\<title\>(.*)\<\/title\>", "igsu");
  365. a = re.exec(xml);
  366. if (!a || a.length===0) {
  367. document.write("Data access error (#6)<br><br>");
  368. return;
  369. } else {
  370. myObj.title = a[1];
  371. }
  372. re = new RegExp("\<label\>(.*)\<\/label\>", "igsu");
  373. a = re.exec(xml);
  374. if (!a || a.length===0) {
  375. document.write("Data access error (#7)<br><br>");
  376. return;
  377. } else {
  378. myObj.label = a[1];
  379. }
  380. re = new RegExp("\<link\>(.*)\<\/link\>", "igsu");
  381. a = re.exec(xml);
  382. if (!a || a.length===0) {
  383. document.write("Data access error (#8)<br><br>");
  384. return;
  385. } else {
  386. myObj.link = a[1];
  387. }
  388. cubes[z-1] = myObj;
  389. } else {
  390. if ((this.readyState == 4 && this.status == 0) && (!bConnectionOK)) {
  391. document.write("Data access error (#1)<br><br>");
  392. }
  393. }
  394. }
  395. //if (offers[0]) {
  396. // alert(offers[0].xml);
  397. //}
  398. }
  399. /*
  400. * Get the guid of the cube
  401. *
  402. * @returns string
  403. */
  404. function mygetguid() {
  405. return this.guid;
  406. }
  407. /*
  408. * Get the editor password of the cube
  409. *
  410. * @returns string
  411. */
  412. function mygetpassword() {
  413. return this.password;
  414. }
  415. /*
  416. * Get the xml data associated with the cube
  417. *
  418. * @returns string
  419. */
  420. function mygetxml() {
  421. return this.xml;
  422. }
  423. }
  424. // -- End myCube class
  425. /*
  426. * Select a cube
  427. *
  428. * @param <interfaceEl> tthis selected cube
  429. * @returns void
  430. */
  431. function _selCube(tthis) {
  432. //alert($(tthis).attr("order"));
  433. var _cubeorder = parseInt($(tthis).attr("order"));
  434. _selectedcube = _cubeorder-1;
  435. curcube = cubes[_selectedcube];
  436. //curcube.start();
  437. }
  438. function updCubeXML(xmlStr, nodeName, newVal) {
  439. switch (nodeName) {
  440. case "title":
  441. re = /(\<title>).*(\<\/title>)/gs;
  442. break;
  443. case "desc":
  444. re = /(\<desc>).*(\<\/desc>)/gs;
  445. break;
  446. case "code":
  447. re = /(\<code>\<\!\[CDATA\[).*(\]\]\>\<\/code>)/gs;
  448. break;
  449. case "tags":
  450. re = /(\<tags>).*(\<\/tags>)/gs;
  451. break;
  452. case "cats":
  453. re = /(\<cats>).*(\<\/cats>)/gs;
  454. break;
  455. case "label":
  456. re = /(\<label>).*(\<\/label>)/gs;
  457. break;
  458. case "link":
  459. re = /(\<link>).*(\<\/link>)/gs;
  460. break;
  461. case "email":
  462. re = /(\<email>).*(\<\/email>)/gs;
  463. break;
  464. }
  465. xmlStr = xmlStr.replace(re, "$1" + newVal + "$2");
  466. return xmlStr;
  467. }