sidebar.js 8.5 KB

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