main.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. "use strict";
  2. var projectCards;
  3. var isMobile = false, isTablet = false, isLaptop = false;
  4. (function ($) {
  5. jQuery(document).ready(function () {
  6. function detectDevice() {
  7. if (window.innerWidth <= 425) {
  8. isMobile = true;
  9. isTablet = false;
  10. isLaptop = false;
  11. } else if (window.innerWidth <= 768) {
  12. isMobile = false;
  13. isTablet = true;
  14. isLaptop = false;
  15. } else {
  16. isMobile = false;
  17. isTablet = false;
  18. isLaptop = true;
  19. }
  20. }
  21. detectDevice();
  22. // ================= Smooth Scroll ===================
  23. function addSmoothScroll() {
  24. // ref: https://css-tricks.com/snippets/jquery/smooth-scrolling/
  25. // Select all links with hashes
  26. $('a[href*="#"]')
  27. // Remove links that don't actually link to anything
  28. .not('[href="#"]')
  29. .not('[href="#0"]')
  30. .click(function (event) {
  31. // On-page links
  32. if (
  33. location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
  34. &&
  35. location.hostname == this.hostname
  36. ) {
  37. // Figure out element to scroll to
  38. var target = $(decodeURI(this.hash));
  39. target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
  40. // Does a scroll target exist?
  41. if (target.length) {
  42. // Only prevent default if animation is actually gonna happen
  43. event.preventDefault();
  44. let offset = 60;
  45. if (isMobile) {
  46. offset = 710;
  47. } else if (isTablet) {
  48. offset = 60;
  49. }
  50. $('html, body').animate({
  51. scrollTop: target.offset().top - offset
  52. }, 1000, function () {
  53. // Callback after animation
  54. // Must change focus!
  55. var $target = $(target);
  56. $target.focus();
  57. if ($target.is(":focus")) { // Checking if the target was focused
  58. return false;
  59. } else {
  60. $target.attr('tabindex', '-1'); // Adding tabindex for elements not focusable
  61. $target.focus(); // Set focus again
  62. };
  63. });
  64. // Add hash (#) to URL when done scrolling (default click behavior)
  65. window.location.hash = this.hash
  66. }
  67. }
  68. });
  69. }
  70. addSmoothScroll();
  71. // ===================== Video Player ==================
  72. function renderVideoPlayer(){
  73. var videos = document.getElementsByClassName("video-player");
  74. for (var i =0; i< videos.length; i++ ){
  75. const player = new Plyr("#"+videos[i].id);
  76. }
  77. }
  78. renderVideoPlayer();
  79. // re-render custom functions on window resize
  80. window.onresize = function () {
  81. detectDevice();
  82. addSmoothScroll();
  83. };
  84. });
  85. })(jQuery);
  86. // Toggle sidebar on click. Here, class "hide" open the sidebar
  87. function toggleSidebar() {
  88. let sidebar = document.getElementById("sidebar-section");
  89. if (sidebar == null) {
  90. return
  91. }
  92. if (sidebar.classList.contains("hide")) {
  93. sidebar.classList.remove("hide")
  94. } else {
  95. // if toc-section is open, then close it first
  96. let toc = document.getElementById("toc-section");
  97. if (toc != null && toc.classList.contains("hide")) {
  98. toc.classList.remove("hide");
  99. }
  100. // add "hide" class
  101. sidebar.classList.add("hide")
  102. // if it is mobile device. then scroll to top.
  103. if (isMobile && sidebar.classList.contains("hide")) {
  104. document.body.scrollTop = 0;
  105. document.documentElement.scrollTop = 0;
  106. if (document.getElementById("hero-area") != null) {
  107. document.getElementById("hero-area").classList.toggle("hide");
  108. }
  109. }
  110. }
  111. if (document.getElementById("content-section") != null) {
  112. document.getElementById("content-section").classList.toggle("hide");
  113. }
  114. }
  115. // Toggle Table of Contents on click. Here, class "hide" open the toc
  116. function toggleTOC() {
  117. let toc = document.getElementById("toc-section");
  118. if (toc == null) {
  119. return
  120. }
  121. if (toc.classList.contains("hide")) {
  122. toc.classList.remove("hide");
  123. } else {
  124. // if sidebar-section is open, then close it first
  125. let sidebar = document.getElementById("sidebar-section");
  126. if (sidebar != null && sidebar.classList.contains("hide")) {
  127. sidebar.classList.remove("hide");
  128. }
  129. // add "hide" class
  130. toc.classList.add("hide")
  131. // if it is mobile device. then scroll to top.
  132. if (isMobile && toc.classList.contains("hide")) {
  133. document.body.scrollTop = 0;
  134. document.documentElement.scrollTop = 0;
  135. }
  136. }
  137. if (document.getElementById("hero-area") != null) {
  138. document.getElementById("hero-area").classList.toggle("hide");
  139. }
  140. }
  141. // Show more rows in the taken courses table
  142. function toggleCourseVisibility(elem) {
  143. // find the courses
  144. let courses = elem.parentNode.getElementsByClassName("course");
  145. if (courses == null) {
  146. return
  147. }
  148. // toggle hidden-course class from the third elements
  149. for (const course of courses) {
  150. if (course.classList.contains("hidden-course") || course.classList.contains("toggled-hidden-course")) {
  151. course.classList.toggle("hidden-course");
  152. course.classList.add("toggled-hidden-course");
  153. }
  154. }
  155. // toggle the buttons visibility
  156. let buttonsToToggle = elem.parentNode.getElementsByClassName("show-more-btn");
  157. for (const buttonToToggle of buttonsToToggle) {
  158. buttonToToggle.classList.toggle("hidden");
  159. }
  160. }