dropjs.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. *
  3. * Copyright (c) 2016, 2024, 5 Mode's contributors
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither 5 Mode nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. **/
  29. function onDragStart(e) {
  30. //e.preventDefault();
  31. objName = document.getElementById("objName").value;
  32. //alert(objName);
  33. jsonData = serialize( getObj(objName) );
  34. //alert(jsonData);
  35. e.dataTransfer.setData('text/plain', jsonData);
  36. document.body.style.cursor="move";
  37. }
  38. function onDragOver(e) {
  39. e.preventDefault();
  40. const id = e.dataTransfer.getData('text/plain');
  41. document.body.style.cursor="pointer";
  42. }
  43. function onDragOverOff(e) {
  44. e.preventDefault();
  45. document.body.style.cursor="not-allowed";
  46. }
  47. function onDrop(e) {
  48. e.preventDefault();
  49. mys=e.dataTransfer.getData('text/plain');
  50. //alert(mys);
  51. var newObj = deserialize(mys);
  52. newObj.start();
  53. document.body.style.cursor="normal";
  54. }
  55. function onDropMoon(e) {
  56. e.preventDefault();
  57. mys=e.dataTransfer.getData('text/plain');
  58. mys=mys.substr(0,mys.length-1) + ",\"startMoon\":function mystartMoon() { alert(\"Hello Moon!\"); }}";
  59. //alert(mys);
  60. var newObj = deserialize(mys);
  61. newObj.startMoon();
  62. document.body.style.cursor="normal";
  63. }
  64. function onDropJSON(e) {
  65. e.preventDefault();
  66. mys=e.dataTransfer.getData('text/plain');
  67. alert(mys);
  68. document.body.style.cursor="normal";
  69. }
  70. function countMatches(matches) {
  71. i=0;
  72. for (const match of matches) {
  73. i++;
  74. }
  75. return i;
  76. }
  77. function onDropINFO(e) {
  78. const regexp1 = /\:function/g;
  79. const regexp2 = /\:/g;
  80. e.preventDefault();
  81. mys=e.dataTransfer.getData('text/plain');
  82. res1a = mys.matchAll(regexp1);
  83. res2a = mys.matchAll(regexp2);
  84. totFunctions = countMatches(res1a);
  85. totEntities = countMatches(res2a);
  86. res = "";
  87. res += "\ntotal properties: " + (totEntities - totFunctions);
  88. res += "\ntotal functions: " + totFunctions;
  89. alert(res);
  90. document.body.style.cursor="normal";
  91. }
  92. function onDropJS(e) {
  93. const regexp1 = /\"(.+)\"\:\"(.+)\"[,|}]/g;
  94. const regexp2 = /\"(.+)\"\:(function.+{.+})[,|}]/g;
  95. e.preventDefault();
  96. mys=e.dataTransfer.getData('text/plain');
  97. mys=mys.replaceAll("\n","");
  98. mys=mys.replaceAll(",",",\n");
  99. mys=mys.replaceAll(" "," ");
  100. res1a = mys.matchAll(regexp1);
  101. res2a = mys.matchAll(regexp2);
  102. res = "OBJECT\t[";
  103. i=0;
  104. for (const match of res1a) {
  105. res += "\n\tvar " + match[1] + " = '" + match[2] + "'";
  106. i++;
  107. }
  108. i=0;
  109. for (const match of res2a) {
  110. res += "\n\tvar " + match[1] + " = " + match[2] + ";";
  111. i++;
  112. }
  113. res += "\n\];";
  114. alert(res);
  115. document.body.style.cursor="normal";
  116. }
  117. function onDropOff(e) {
  118. e.preventDefault();
  119. document.body.style.cursor="not-allowed";
  120. e.stopPropagation();
  121. }
  122. function onMouseOver() {
  123. document.body.style.cursor="pointer";
  124. }