dragndrop-code.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. * dragndrop-code.js
  20. *
  21. * Drg-n-drop Code for home.php.
  22. *
  23. * @author Daniele Bonini <my25mb@aol.com>
  24. * @copyrights (c) 2021, 2024, 5 Mode
  25. */
  26. gguid = "";
  27. function onDragStart(tthis, e) {
  28. //e.preventDefault();
  29. tthisorder = parseInt($(tthis).attr("order"));
  30. //objName = document.getElementById("objName").value;
  31. //alert(tthisorder);
  32. jsonData = serialize( cubes[tthisorder-1] ) + window.name;
  33. //alert(jsonData);
  34. e.dataTransfer.setData('text/plain', jsonData);
  35. document.body.style.cursor="move";
  36. }
  37. function onDragOver(e) {
  38. e.preventDefault();
  39. const id = e.dataTransfer.getData('text/plain');
  40. document.body.style.cursor="pointer";
  41. }
  42. function onDragOverOff(e) {
  43. e.preventDefault();
  44. document.body.style.cursor="not-allowed";
  45. }
  46. function onDrop(tthis, e) {
  47. e.preventDefault();
  48. mys=e.dataTransfer.getData('text/plain');
  49. gguid=mys.substr(mys.length-64);
  50. mys=mys.substr(0,mys.length-64);
  51. //alert(mys);
  52. //alert(cubes.length);
  53. newcube = deserialize(mys);
  54. g = window.name; //$(tthis).attr("guid");
  55. if (g === gguid) {
  56. alert("Operation not allowed in same window!");
  57. return;
  58. }
  59. curcat = $(tthis).attr("cat");
  60. n = totcubes + 1;
  61. //alert(curcat);
  62. bfound=false;
  63. var i;
  64. for (i=0;i<(n-1);i++) {
  65. if (cubes[i]) {
  66. if (cubes[i].getguid() === newcube.getguid()) {
  67. bfound=i;
  68. break;
  69. }
  70. } else {
  71. break;
  72. }
  73. }
  74. //alert(newcube.cats);
  75. //alert(newcube.cats.indexOf(curcat));
  76. if (curcat!=="*") {
  77. bcatfound = false;
  78. mycats = " " + newcube.cats + " ";
  79. if ((mycats.indexOf(curcat) === -1)) {
  80. newcube.cats += " " + curcat;
  81. newcube.xml = updCubeXML(newcube.xml, "cats", newcube.cats);
  82. } else {
  83. bcatfound = curcat;
  84. }
  85. //alert(newcube.cats);
  86. } else {
  87. bcatfound = true;
  88. }
  89. //alert("bfound=" + bfound);
  90. //alert("bcatfound=" + bcatfound);
  91. if (bfound===false || bcatfound===false) {
  92. // if same GUID doesn't exists || or the drop happened for a new cat
  93. if (bfound===false) {
  94. // if same GUID doesn't exists
  95. n=totcubes+1;
  96. // check for the same formalName, if exists
  97. bffound=false;
  98. var y;
  99. for (y=0;y<(n-1);y++) {
  100. if (cubes[y]) {
  101. if (cubes[y].formalName === newcube.formalName) {
  102. bffound=y;
  103. break;
  104. }
  105. } else {
  106. break;
  107. }
  108. }
  109. // if same formalName exists, I assign an other formalName to the new snip..
  110. if (bffound===false) {
  111. } else {
  112. newcube.formalName += " (" + rnd(10000, 99999) + "-" + rnd(10000, 99999) + "-" + rnd(10000, 99999) + "-" + rnd(10000, 99999) + ")";
  113. }
  114. // I assign the new snip to the next el in the cubes array
  115. cubes[n-1] = newcube;
  116. } else {
  117. // if the drop happened for a new cat but the snip exists
  118. n=bfound+1;
  119. // I assign the new snip to the existing el in the cubes array
  120. cubes[bfound].xml = newcube.xml;
  121. }
  122. //n = parseInt($(tthis).attr("size")) + 1;
  123. // I insert the the new snip in the GUI..
  124. newCube = "";
  125. newCube += "<div class='link-div' title='" + newcube.desc + "' order='" + n + "' onclick='selCube(this);openDetail()' draggable='true' ondragstart='onDragStart(this, event);' onmouseover='onMouseOver();'>";
  126. newCube += "<div class='link-title'>" + newcube.title + "<div style='width:25px; float:right;'><img class='imgheart' src='/res/unheart.png' style='height:23px;'></a></div></div>";
  127. newCube += "<a href='#'><img class='link-img' src='/res/code.png' alt='" + newcube.title + "'></a><br>";
  128. newCube += " <br>";
  129. newCube += "&nbsp;<a class='link-link' href='http://" + newcube.link.replace(/~/g, "/") + "'>" + newcube.label.replace(/~/g, "/") + "</a><br>";
  130. newCube += "<div style='position:relative;top:-25px;left:-2px;text-align:right;padding-right:1.5px;'>";
  131. newCube += "<img src='/res/fb.png'>";
  132. newCube += "<img src='/res/twitter.png'>";
  133. newCube += "<img src='/res/whatsapp.png'>";
  134. newCube += "</div>";
  135. newCube += "</div>";
  136. oldHtml = $(tthis).html();
  137. if (oldHtml.indexOf("no link found")>-1) {
  138. $(tthis).html(newCube);
  139. } else {
  140. $(tthis).html($(tthis).html() + newCube);
  141. }
  142. } else {
  143. // if same GUID exists..
  144. if (cubes[bfound].formalName !== newcube.formalName) {
  145. alert("A snip by the same GUID already exists. But it has a different name.");
  146. return;
  147. }
  148. if (cubes[bfound].getpassword() === newcube.getpassword()) {
  149. pwd2 = prompt("password confirmation:");
  150. pwd2en = encryptSha2(pwd2);
  151. if (cubes[bfound].getpassword() !== pwd2en) {
  152. alert("A snip by the same GUID already exists. But you typed the wrong password to access it.");
  153. return;
  154. }
  155. } else {
  156. alert("A snip by the same GUID already exists.");
  157. return;
  158. }
  159. // I assign the snip to the existing el in the cubes array
  160. cubes[bfound].xml = newcube.xml;
  161. n=bfound+1;
  162. }
  163. cubes[n-1].savedata();
  164. if (n>totcubes) {
  165. totcubes = n;
  166. }
  167. document.body.style.cursor="normal";
  168. }
  169. function onDropOff(e) {
  170. e.preventDefault();
  171. document.body.style.cursor="not-allowed";
  172. e.stopPropagation();
  173. }
  174. function onMouseOver() {
  175. document.body.style.cursor="pointer";
  176. }