index.astro 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. ---
  2. import Layout from "../layouts/Layout.astro";
  3. import Taskbar from "../components/taskbar.astro";
  4. import App from "../components/app.astro";
  5. import Aboutme from "../components/aboutme.astro";
  6. import "../styles/global.css";
  7. import me from "../assets/apps/me.png";
  8. import github from "../assets/apps/github.png";
  9. import pastwork from "../assets/apps/pastWork.png";
  10. import projects from "../assets/apps/projects.png";
  11. import blog from "../assets/apps/blog.png";
  12. import contact from "../assets/apps/contact.png";
  13. import Redirect from "../components/redirect.astro";
  14. import Contact from "../components/contact.astro";
  15. function dragElement(elmnt) {
  16. var pos1 = 0,
  17. pos2 = 0,
  18. pos3 = 0,
  19. pos4 = 0;
  20. if (document.getElementById(elmnt.id + "header")) {
  21. // If present, the header is where you move the DIV from:
  22. document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  23. document.getElementById(elmnt.id + "header").ontouchstart = dragMouseDown;
  24. } else {
  25. // Otherwise, move the DIV from anywhere inside the DIV:
  26. elmnt.onmousedown = dragMouseDown;
  27. elmnt.ontouchstart = dragMouseDown;
  28. }
  29. function dragMouseDown(e) {
  30. // e.preventDefault();
  31. raiseUpSpec(elmnt.id);
  32. // Get the initial touch point for touch events
  33. if (e.type == "touchstart") {
  34. pos3 = e.touches[0].clientX;
  35. pos4 = e.touches[0].clientY;
  36. } else {
  37. pos3 = e.clientX;
  38. pos4 = e.clientY;
  39. }
  40. document.onmouseup = closeDragElement;
  41. document.ontouchend = closeDragElement;
  42. document.onmousemove = elementDrag;
  43. document.ontouchmove = elementDrag;
  44. }
  45. function elementDrag(e) {
  46. // e.preventDefault();
  47. if (e.type == "touchmove") {
  48. // Calculate the new cursor position for touch
  49. pos1 = pos3 - e.touches[0].clientX;
  50. pos2 = pos4 - e.touches[0].clientY;
  51. pos3 = e.touches[0].clientX;
  52. pos4 = e.touches[0].clientY;
  53. } else {
  54. // Calculate the new cursor position for mouse
  55. pos1 = pos3 - e.clientX;
  56. pos2 = pos4 - e.clientY;
  57. pos3 = e.clientX;
  58. pos4 = e.clientY;
  59. }
  60. // Set the element's new position
  61. elmnt.style.top = elmnt.offsetTop - pos2 + "px";
  62. elmnt.style.left = elmnt.offsetLeft - pos1 + "px";
  63. }
  64. function closeDragElement() {
  65. // Stop moving when mouse button or touch is released
  66. document.onmouseup = null;
  67. document.ontouchend = null;
  68. document.onmousemove = null;
  69. document.ontouchmove = null;
  70. }
  71. }
  72. /** Raise up window with given ID */
  73. function raiseUpSpec(id: string) {
  74. console.log("raising ", id);
  75. let els = document.getElementsByClassName("window");
  76. let elArray = Array.from(els as HTMLCollectionOf<HTMLElement>);
  77. // find the highest z-index
  78. let highestIndex = 0;
  79. for (let element in elArray) {
  80. let zindex = parseInt(elArray[element].style.zIndex);
  81. if (zindex > highestIndex) {
  82. highestIndex = zindex;
  83. }
  84. }
  85. // set the highest z-index plus one
  86. document.getElementById(id).style.zIndex = (highestIndex + 1).toString();
  87. }
  88. function updateRedirectMessage() {
  89. const redirectElement = document.getElementById("redirect");
  90. const redirectUrl = redirectElement.dataset.redirecturl; // Access the data attribute
  91. const messageElement = redirectElement.querySelector("h2");
  92. messageElement.innerHTML = `You are about to be redirected to <a class="redirUrl" style=" cursor: pointer;
  93. color: blue;
  94. text-decoration: underline; " href="${redirectUrl}"">${redirectUrl}</a>`;
  95. const buttonElement = redirectElement.querySelector("#okButton");
  96. // open redirect url
  97. buttonElement.setAttribute(
  98. "onclick",
  99. `window.location.href = "${redirectUrl}"`,
  100. );
  101. }
  102. let apps: app[] = [
  103. {
  104. name: "About Me",
  105. logo: me.src,
  106. onclick: () => {
  107. raiseUpSpec("aboutme");
  108. document.getElementById("aboutme").style.display = "block";
  109. },
  110. row: 1,
  111. },
  112. {
  113. name: "Blog",
  114. logo: blog.src,
  115. onclick: () => {
  116. // ignore
  117. raiseUpSpec("redirect");
  118. document.getElementById("redirect").style.display = "block";
  119. document.getElementById("redirect").dataset.redirecturl =
  120. "https://blog.simo.ng/";
  121. updateRedirectMessage();
  122. },
  123. row: 1,
  124. },
  125. {
  126. name: "Projects",
  127. logo: projects.src,
  128. row: 1,
  129. },
  130. {
  131. name: "Past Work",
  132. logo: pastwork.src,
  133. row: 1,
  134. },
  135. {
  136. name: "Github",
  137. logo: github.src,
  138. onclick: () => {
  139. // ignore
  140. raiseUpSpec("redirect");
  141. document.getElementById("redirect").style.display = "block";
  142. document.getElementById("redirect").dataset.redirecturl =
  143. "https://github.com/s1monlol/";
  144. updateRedirectMessage();
  145. },
  146. row: 2,
  147. },
  148. {
  149. name: "Contact",
  150. logo: contact.src,
  151. row: 2,
  152. onclick: () => {
  153. raiseUpSpec("contact");
  154. document.getElementById("contact").style.display = "block";
  155. },
  156. },
  157. ];
  158. const row1Items = apps.filter((item) => item.row === 1);
  159. const row2Items = apps.filter((item) => item.row === 2);
  160. let clientFunctions: Function[] | String[] = [
  161. raiseUpSpec,
  162. updateRedirectMessage,
  163. dragElement,
  164. ];
  165. clientFunctions = clientFunctions.map((item) => String(item));
  166. apps = apps.map((item) => {
  167. item.onclick = String(item.onclick);
  168. return item;
  169. });
  170. ---
  171. <Layout title="Simo">
  172. <div class="flex">
  173. <div class="h-80 flex flex-col items-start">
  174. {row1Items.map((item) => <App {item} />)}
  175. </div>
  176. <div class="h-80 flex flex-col items-start">
  177. {row2Items.map((item) => <App {item} />)}
  178. </div>
  179. </div>
  180. </Layout>
  181. <Aboutme />
  182. <Redirect />
  183. <Contact />
  184. <Taskbar />
  185. <script define:vars={{ apps, clientFunctions }}>
  186. for (func in clientFunctions) {
  187. eval(clientFunctions[func]);
  188. }
  189. Array.from(document.getElementsByClassName("window")).forEach((window) => {
  190. dragElement(window);
  191. window.addEventListener("click", () => {
  192. raiseUpSpec(window.id);
  193. });
  194. Array.from(window.getElementsByClassName("close")).forEach((button) => {
  195. button.addEventListener("click", () => {
  196. window.style.display = "none";
  197. });
  198. });
  199. });
  200. apps.forEach((app) => {
  201. let itemName = app.name.replace(" ", "");
  202. // console.info(document.getElementById(app.name));
  203. if (app.onclick == "undefined") {
  204. return;
  205. }
  206. document.getElementById(itemName).addEventListener("click", () => {
  207. const appFunc = eval(app.onclick);
  208. appFunc();
  209. });
  210. });
  211. </script>