dragndrop-code.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. bcatfound = false;
  77. mycats = " " + newcube.cats + " ";
  78. if ((mycats.indexOf(curcat) === -1)) {
  79. newcube.cats += " " + curcat;
  80. newcube.xml = updCubeXML(newcube.xml, "cats", newcube.cats);
  81. } else {
  82. bcatfound = curcat;
  83. }
  84. //alert(newcube.cats);
  85. //alert("bfound=" + bfound);
  86. //alert("bcatfound=" + bcatfound);
  87. if (bfound===false || bcatfound===false) {
  88. // if same GUID doesn't exists || or the drop happened for a new cat
  89. if (bfound===false) {
  90. // if same GUID doesn't exists
  91. n=totcubes+1;
  92. // check for the same formalName, if exists
  93. bffound=false;
  94. var y;
  95. for (y=0;y<(n-1);y++) {
  96. if (cubes[y]) {
  97. if (cubes[y].formalName === newcube.formalName) {
  98. bffound=y;
  99. break;
  100. }
  101. } else {
  102. break;
  103. }
  104. }
  105. // if same formalName exists, I assign an other formalName to the new snip..
  106. if (bffound===false) {
  107. } else {
  108. newcube.formalName += " (" + rnd(10000, 99999) + "-" + rnd(10000, 99999) + "-" + rnd(10000, 99999) + "-" + rnd(10000, 99999) + ")";
  109. }
  110. // I assign the new snip to the next el in the cubes array
  111. cubes[n-1] = newcube;
  112. } else {
  113. // if the drop happened for a new cat but the snip exists
  114. n=bfound+1;
  115. // I assign the new snip to the existing el in the cubes array
  116. cubes[bfound].xml = newcube.xml;
  117. }
  118. //n = parseInt($(tthis).attr("size")) + 1;
  119. // I insert the the new snip in the GUI..
  120. newCube = "";
  121. newCube += "<div class='link-div' style='width:250px; float:left; padding: 10px; margin:5px;' title='" + newcube.desc + "' order='" + n + "' onclick='selCube(this);openDetail()' draggable='true' ondragstart='onDragStart(this, event);' onmouseover='onMouseOver();'>";
  122. newCube += "<div style='color:#ed6a43; padding-bottom:8px;'>" + newcube.title + "</div>";
  123. newCube += "<a href='#'><img src='/res/code.png' style='width:232px; height:124px; border:1px solid darkgray;' alt='" + newcube.title + "'></a><br>";
  124. newCube += "&nbsp;<a style='font-style:italic; color:green; font-size:10px; padding-top:5px;' href='http://" + newcube.link.replace(/~/g, "/") + "'>" + newcube.label.replace(/~/g, "/") + "</a><br>";
  125. newCube += "</div>";
  126. oldHtml = $(tthis).html();
  127. if (oldHtml.indexOf("no link found")>-1) {
  128. $(tthis).html(newCube);
  129. } else {
  130. $(tthis).html($(tthis).html() + newCube);
  131. }
  132. } else {
  133. // if same GUID exists..
  134. if (cubes[bfound].formalName !== newcube.formalName) {
  135. alert("A snip by the same GUID already exists. But it has a different name.");
  136. return;
  137. }
  138. if (cubes[bfound].getpassword() === newcube.getpassword()) {
  139. pwd2 = prompt("password confirmation:");
  140. pwd2en = encryptSha2(pwd2);
  141. if (cubes[bfound].getpassword() !== pwd2en) {
  142. alert("A snip by the same GUID already exists. But you typed the wrong password to access it.");
  143. return;
  144. }
  145. } else {
  146. alert("A snip by the same GUID already exists.");
  147. return;
  148. }
  149. // I assign the snip to the existing el in the cubes array
  150. cubes[bfound].xml = newcube.xml;
  151. n=bfound+1;
  152. }
  153. cubes[n-1].savedata();
  154. if (n>totcubes) {
  155. totcubes = n;
  156. }
  157. document.body.style.cursor="normal";
  158. }
  159. function onDropOff(e) {
  160. e.preventDefault();
  161. document.body.style.cursor="not-allowed";
  162. e.stopPropagation();
  163. }
  164. function onMouseOver() {
  165. document.body.style.cursor="pointer";
  166. }