sidebar.js 8.0 KB

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