blog.js 3.2 KB

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