single.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. var isMobile = false, isTablet = false, isLaptop = false;
  3. (function ($) {
  4. jQuery(document).ready(function () {
  5. function detectDevice() {
  6. if (window.innerWidth <= 425) {
  7. isMobile = true;
  8. isTablet = false;
  9. isLaptop = false;
  10. } else if (window.innerWidth <= 768) {
  11. isMobile = false;
  12. isTablet = true;
  13. isLaptop = false;
  14. } else {
  15. isMobile = false;
  16. isTablet = false;
  17. isLaptop = true;
  18. }
  19. }
  20. detectDevice();
  21. console.log(isMobile);
  22. // ======= Add table wrapper ===========
  23. function adjustPostCardsHeight() {
  24. if (!isMobile) { // no need to adjust height for mobile devices
  25. let el = document.getElementById("post-cards").children;
  26. let maxHeight = 0;
  27. for (let i = 0; i < el.length; i++) {
  28. if (el[i].children[0].clientHeight > maxHeight) {
  29. maxHeight = el[i].children[0].clientHeight;
  30. }
  31. }
  32. for (let i = 0; i < el.length; i++) {
  33. el[i].children[0].setAttribute("style", "min-height: " + maxHeight + "px;")
  34. }
  35. }
  36. }
  37. adjustPostCardsHeight();
  38. // ============= Sidebar Tre ================
  39. function buildSidebarMenu() {
  40. var openedClass = "fa-minus-circle";
  41. var closedClass = "fa-plus-circle";
  42. // initialize top level
  43. var tree = $("#tree");
  44. // add expand icon to those li who has ul as children
  45. tree.find("li").has("ul").each(function () {
  46. var branch = $(this);
  47. branch.prepend('<i class="fas ' + closedClass + '"></i>');
  48. branch.on('click', function (e) {
  49. if (this.children[1] == e.target) {
  50. // toggle "expand" class and icon
  51. branch.toggleClass("expand");
  52. var icon = $(this).children('i:first');
  53. icon.toggleClass(openedClass + " " + closedClass);
  54. }
  55. });
  56. });
  57. // remove "expnad" class from siblings of the clicked item
  58. tree.find("li").on("click", function () {
  59. var item = $(this);
  60. var shiblings = item.siblings().each(function () {
  61. var sibling = $(this);
  62. if (sibling.hasClass("expand")) {
  63. sibling.removeClass("expand");
  64. var icon = sibling.children('i:first');
  65. icon.toggleClass(openedClass + " " + closedClass);
  66. }
  67. });
  68. });
  69. // focus the cliked item
  70. tree.find("a").on("click", function () {
  71. // clear other focused link
  72. tree.find("a.focused").each(function () {
  73. $(this).removeClass("focused");
  74. });
  75. // focus cliked link
  76. $(this).addClass("focused");
  77. });
  78. }
  79. buildSidebarMenu();
  80. // initialize filterizr
  81. filterizd = $(".filtr-container").filterizr({ layout: 'sameWidth' });
  82. });
  83. })(jQuery);
  84. // toggle sidebar on click
  85. function toggleSidebar() {
  86. document.getElementById("sidebar").classList.toggle("hide");
  87. document.getElementById("content").classList.toggle("overley");
  88. // if it is mobile device. then scroll to top.
  89. if (isMobile && $("#sidebar").hasClass("hide")) {
  90. document.body.scrollTop = 0;
  91. document.documentElement.scrollTop = 0;
  92. }
  93. setTimeout(function () {
  94. filterizd.filterizr('sort');
  95. }, 300);
  96. }