index.astro 6.1 KB

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