sidebar.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. (function () {
  2. const body = document.querySelector("body");
  3. const wrapper = document.querySelector(".page-wrapper");
  4. // active menu js
  5. let slideUp = (target, duration = 500) => {
  6. if (target) {
  7. target.style.transitionProperty = "height, padding";
  8. target.style.transitionDuration = duration + "ms";
  9. target.style.boxSizing = "border-box";
  10. target.style.height = target.offsetHeight + "px";
  11. target.offsetHeight;
  12. target.style.overflow = "hidden";
  13. target.style.height = 0;
  14. target.style.paddingTop = 0;
  15. window.setTimeout(() => {
  16. target.style.display = "none";
  17. target.style.removeProperty("height");
  18. target.style.removeProperty("padding-top");
  19. target.style.removeProperty("overflow");
  20. target.style.removeProperty("transition-duration");
  21. target.style.removeProperty("transition-property");
  22. }, duration);
  23. }
  24. };
  25. let slideDown = (target, duration = 500) => {
  26. if (target) {
  27. target.style.removeProperty("display");
  28. let display = window.getComputedStyle(target).display;
  29. if (display === "none") display = "flex";
  30. target.style.display = display;
  31. let height = target.offsetHeight;
  32. target.style.overflow = "hidden";
  33. target.style.height = 0;
  34. target.style.paddingTop = 0;
  35. target.offsetHeight;
  36. target.style.boxSizing = "border-box";
  37. target.style.transitionProperty = "height, padding";
  38. target.style.transitionDuration = duration + "ms";
  39. target.style.height = height + "px";
  40. target.style.removeProperty("padding-top");
  41. window.setTimeout(() => {
  42. target.style.removeProperty("height");
  43. target.style.removeProperty("overflow");
  44. target.style.removeProperty("transition-duration");
  45. target.style.removeProperty("transition-property");
  46. }, duration);
  47. }
  48. };
  49. const sidebarListItems = document.querySelectorAll(".sidebar-link");
  50. // Add onclick event listener to each sidebar-list item
  51. sidebarListItems.forEach((item) => {
  52. item.addEventListener("click", () => {
  53. item.classList.toggle("active");
  54. const submenu = item
  55. .closest(".sidebar-list")
  56. .querySelector(".sidebar-submenu");
  57. if (submenu) {
  58. submenu.style.display = item.classList.contains("active")
  59. ? "block"
  60. : "none";
  61. }
  62. sidebarListItems.forEach((otherList) => {
  63. if (otherList !== item) {
  64. otherList.classList.remove("active");
  65. const otherSubmenu = otherList
  66. .closest(".sidebar-list")
  67. .querySelector(".sidebar-submenu");
  68. if (otherSubmenu) {
  69. otherSubmenu.style.display = "none";
  70. }
  71. }
  72. });
  73. });
  74. });
  75. // Sidebar toggle js
  76. const sidebarToggle = document.querySelector(".toggle-sidebar");
  77. if (sidebarToggle) {
  78. sidebarToggle.addEventListener("click", function () {
  79. wrapper.classList.toggle("sidebar-open");
  80. const wrapperClose = wrapper.classList.contains("sidebar-open");
  81. });
  82. }
  83. })();
  84. // Sidebar pin-drops
  85. const pinTitle = document.querySelector(".pin-title");
  86. if (pinTitle) {
  87. let pinIcon = document.querySelectorAll(".sidebar-list .fa-thumbtack");
  88. function togglePinnedName() {
  89. if (document.getElementsByClassName("pined").length) {
  90. if (!pinTitle.classList.contains("show")) pinTitle.classList.add("show");
  91. } else {
  92. pinTitle.classList.remove("show");
  93. }
  94. }
  95. pinIcon.forEach((item, index) => {
  96. var linkName = item.parentNode.querySelector("h6").innerHTML;
  97. var InitialLocalStorage = JSON.parse(localStorage.getItem("pins") || false);
  98. if (InitialLocalStorage && InitialLocalStorage.includes(linkName)) {
  99. item.parentNode.classList.add("pined");
  100. }
  101. item.addEventListener("click", (event) => {
  102. var localStoragePins = JSON.parse(localStorage.getItem("pins") || false);
  103. item.parentNode.classList.toggle("pined");
  104. if (localStoragePins?.length) {
  105. if (item.parentNode.classList.contains("pined")) {
  106. !localStoragePins?.includes(linkName) &&
  107. (localStoragePins = [...localStoragePins, linkName]);
  108. } else {
  109. localStoragePins?.includes(linkName) &&
  110. localStoragePins.splice(localStoragePins.indexOf(linkName), 1);
  111. }
  112. localStorage.setItem("pins", JSON.stringify(localStoragePins));
  113. } else {
  114. localStorage.setItem("pins", JSON.stringify([linkName]));
  115. }
  116. var elem = item;
  117. var topPos = elem.offsetTop;
  118. togglePinnedName();
  119. if (item.parentElement.parentElement.classList.contains("pined")) {
  120. scrollTo(
  121. document.getElementsByClassName("main-sidebar")[0],
  122. topPos - 30,
  123. 600
  124. );
  125. } else {
  126. scrollTo(
  127. document.getElementsByClassName("main-sidebar")[0],
  128. elem.parentNode.offsetTop - 30,
  129. 600
  130. );
  131. }
  132. });
  133. function scrollTo(element, to, duration) {
  134. var start = element.scrollTop,
  135. change = to - start,
  136. currentTime = 0,
  137. increment = 20;
  138. var animateScroll = function () {
  139. currentTime += increment;
  140. var val = Math.easeInOutQuad(currentTime, start, change, duration);
  141. element.scrollTop = val;
  142. if (currentTime < duration) {
  143. setTimeout(animateScroll, increment);
  144. }
  145. };
  146. animateScroll();
  147. }
  148. Math.easeInOutQuad = function (t, b, c, d) {
  149. t /= d / 2;
  150. if (t < 1) return (c / 2) * t * t + b;
  151. t--;
  152. return (-c / 2) * (t * (t - 2) - 1) + b;
  153. };
  154. });
  155. togglePinnedName();
  156. }
  157. // scrollTop sidebar in active link in JS
  158. $(document).ready(function () {
  159. var activeLink = $(".simplebar-wrapper .simplebar-content-wrapper a.active");
  160. if (
  161. activeLink.length > 0 &&
  162. $("#pageWrapper").hasClass("compact-wrapper")
  163. ) {
  164. var scrollTop = activeLink.offset().top - 400;
  165. $(".simplebar-wrapper .simplebar-content-wrapper").animate(
  166. {
  167. scrollTop: scrollTop,
  168. },
  169. 1000
  170. );
  171. }
  172. });
  173. const submenuTitles = document.querySelectorAll(".submenu-title");
  174. // Add onclick event listener to each submenu-title item
  175. submenuTitles.forEach((title) => {
  176. title.addEventListener("click", () => {
  177. const parentLi = title.closest("li");
  178. parentLi.classList.toggle("active");
  179. const submenu = parentLi.querySelector(".according-submenu");
  180. if (submenu) {
  181. submenu.style.display =
  182. submenu.style.display === "block" ? "none" : "block";
  183. }
  184. submenuTitles.forEach((otherTitle) => {
  185. if (otherTitle !== title) {
  186. const otherParentLi = otherTitle.closest("li");
  187. const otherSubmenu =
  188. otherParentLi.querySelector(".according-submenu");
  189. if (otherSubmenu) {
  190. otherSubmenu.style.display = "none";
  191. }
  192. otherParentLi.classList.remove("active");
  193. }
  194. });
  195. });
  196. });
  197. var url = window.location.href;
  198. const urlLink = url.includes("#") ? url.split("#")[0] : url;
  199. const submenuLinks = document.querySelectorAll(".sidebar-menu li a");
  200. submenuLinks.forEach((el) => {
  201. var linkHref = el.href;
  202. if (urlLink === linkHref) {
  203. el.classList.add("active");
  204. const submenu = el.closest(".sidebar-submenu");
  205. if (submenu && submenu.previousElementSibling) {
  206. submenu.previousElementSibling.classList.add("active");
  207. }
  208. if (submenu) {
  209. submenu.style.display = "block";
  210. }
  211. const parentLi = el.closest(".sidebar-submenu > li");
  212. if (parentLi) {
  213. parentLi.classList.add("active");
  214. const submenu = parentLi.querySelector(".according-submenu");
  215. if (submenu) {
  216. submenu.style.display = "block";
  217. }
  218. }
  219. }
  220. });
  221. // RESPONSIVE SIDEBAR 1200<
  222. document.addEventListener("DOMContentLoaded", function () {
  223. "use strict";
  224. var pageWrapper = document.querySelector(".page-wrapper");
  225. var toggleSidebarButton = document.querySelector(".toggle-sidebar");
  226. var widthWindow = window.innerWidth;
  227. if (widthWindow <= 1199) {
  228. pageWrapper.classList.add("sidebar-open");
  229. toggleSidebarButton.classList.add("close");
  230. }
  231. window.addEventListener("resize", function () {
  232. var widthWindow = window.innerWidth;
  233. if (widthWindow <= 1199) {
  234. pageWrapper.classList.add("sidebar-open");
  235. toggleSidebarButton.classList.add("close");
  236. } else {
  237. pageWrapper.classList.remove("sidebar-open");
  238. toggleSidebarButton.classList.remove("close");
  239. }
  240. });
  241. });