sidebar.ts 7.44 KB
Newer Older
Ooh-Ao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
import { fromEvent } from 'rxjs';

export function switcherArrowFn() {
  const slideLeft: any = document.querySelector('.slide-left');
  const slideRight: any = document.querySelector('.slide-right');

  checkHoriMenu();

  if (slideLeft) {
    fromEvent(slideLeft, 'click').subscribe(() => {
      const menuNav: any = document.querySelector('.main-menu');
      const Html: any = document.querySelector('html');
      const mainContainer1: any = document.querySelector('.main-sidebar');
      const marginLeftValue = Math.ceil(
        Number(window.getComputedStyle(menuNav).marginLeft.split('px')[0])
      );
      const marginRightValue = Math.ceil(
        Number(window.getComputedStyle(menuNav).marginRight.split('px')[0])
      );
      const mainContainer1Width = mainContainer1.offsetWidth;
      if (menuNav.scrollWidth > mainContainer1.offsetWidth) {
        if (!(Html.getAttribute('dir') === 'rtl')) {
          if (
            marginLeftValue < 0 &&
            !(Math.abs(marginLeftValue) < mainContainer1Width)
          ) {
            menuNav.style.marginRight = 0;
            menuNav.style.marginLeft =
              Number(menuNav.style.marginLeft.split('px')[0]) +
              Math.abs(mainContainer1Width) +
              'px';
            slideRight.classList.remove('hidden');
          } else if (marginLeftValue >= 0) {
            menuNav.style.marginLeft = '0px';
            slideLeft.classList.add('hidden');
            slideRight.classList.remove('hidden');
          } else {
            menuNav.style.marginLeft = '0px';
            slideLeft.classList.add('hidden');
            slideRight.classList.remove('hidden');
          }
        } else {
          if (
            marginRightValue < 0 &&
            !(Math.abs(marginRightValue) < mainContainer1Width)
          ) {
            menuNav.style.marginLeft = 0;
            menuNav.style.marginRight =
              Number(menuNav.style.marginRight.split('px')[0]) +
              Math.abs(mainContainer1Width) +
              'px';
            slideRight.classList.remove('hidden');
          } else if (marginRightValue >= 0) {
            menuNav.style.marginRight = '0px';
            slideLeft.classList.add('hidden');
            slideRight.classList.remove('hidden');
          } else {
            menuNav.style.marginRight = '0px';
            slideLeft.classList.add('hidden');
            slideRight.classList.remove('hidden');
          }
        }
      } else {
        menuNav.style.marginLeft = '0px';
        menuNav.style.marginRight = '0px';
      }
      const element = document.querySelector('.main-menu > .slide.open');
      const element1: any = document.querySelector(
        '.main-menu > .slide.open >ul'
      );
      if (element) {
        element.classList.remove('active');
      }
      if (element1) {
        element1.style.display = 'none';
      }
    });
  }

  if (slideRight) {
    fromEvent(slideRight, 'click').subscribe(() => {
      const menuNav: any = document.querySelector('.main-menu');
      const Html: any = document.querySelector('html');
      const mainContainer1: any = document.querySelector('.main-sidebar');
      const marginLeftValue = Math.ceil(
        Number(window.getComputedStyle(menuNav).marginLeft.split('px')[0])
      );
      const marginRightValue = Math.ceil(
        Number(window.getComputedStyle(menuNav).marginRight.split('px')[0])
      );
      const check = menuNav.scrollWidth - mainContainer1.offsetWidth;
      let mainContainer1Width = mainContainer1.offsetWidth;
      if (menuNav.scrollWidth > mainContainer1.offsetWidth) {
        if (!(Html.getAttribute('dir') === 'rtl')) {
          if (Math.abs(check) > Math.abs(marginLeftValue)) {
            menuNav.style.marginRight = 0;
            if (
              !(
                Math.abs(check) >
                Math.abs(marginLeftValue) + mainContainer1Width
              )
            ) {
              mainContainer1Width = Math.abs(check) - Math.abs(marginLeftValue);
              slideRight.classList.add('hidden');
            }
            menuNav.style.marginLeft =
              Number(menuNav.style.marginLeft.split('px')[0]) -
              Math.abs(mainContainer1Width) +
              'px';
            slideLeft.classList.remove('hidden');
          }
        } else {
          if (Math.abs(check) > Math.abs(marginRightValue)) {
            menuNav.style.marginLeft = 0;
            if (
              !(
                Math.abs(check) >
                Math.abs(marginRightValue) + mainContainer1Width
              )
            ) {
              mainContainer1Width =
                Math.abs(check) - Math.abs(marginRightValue);
              slideRight.classList.add('hidden');
            }
            menuNav.style.marginRight =
              Number(menuNav.style.marginRight.split('px')[0]) -
              Math.abs(mainContainer1Width) +
              'px';
            slideLeft.classList.remove('hidden');
          }
        }
      }
      const element = document.querySelector('.main-menu > .slide.open');
      const element1: any = document.querySelector(
        '.main-menu > .slide.open >ul'
      );
      if (element) {
        element.classList.remove('active');
      }
      if (element1) {
        element1.style.display = 'none';
      }
    });
  }
}
export function checkHoriMenu() {
  if (document.querySelector('.slide-left')) {
    const menuNav: any = document.querySelector('.main-menu');
    const mainContainer1: any = document.querySelector('.main-sidebar');
    const slideLeft: any = document.querySelector('.slide-left');
    const slideRight: any = document.querySelector('.slide-right');
    const Html: any = document.querySelector('html');
    const marginLeftValue = Math.ceil(
      Number(window.getComputedStyle(menuNav).marginLeft.split('px')[0])
    );
    const marginRightValue = Math.ceil(
      Number(window.getComputedStyle(menuNav).marginRight.split('px')[0])
    );
    const check = menuNav.scrollWidth - mainContainer1.offsetWidth;
    // Show/Hide the arrows
    if (menuNav.scrollWidth > mainContainer1.offsetWidth) {
      slideRight.classList.remove('hidden');
      slideLeft.classList.add('hidden');
    } else {
      slideRight.classList.add('hidden');
      slideLeft.classList.add('hidden');
      menuNav.style.marginLeft = '0px';
      menuNav.style.marginRight = '0px';
    }
    if (!(Html.getAttribute('dir') === 'rtl')) {
      // LTR check the width and adjust the menu in screen
      if (menuNav.scrollWidth > mainContainer1.offsetWidth) {
        if (Math.abs(check) < Math.abs(marginLeftValue)) {
          menuNav.style.marginLeft = -check + 'px';
          slideLeft.classList.remove('hidden');
          slideRight.classList.add('hidden');
        }
      }
      if (marginLeftValue == 0) {
        slideLeft.classList.add('hidden');
        slideRight.classList.remove('hidden');
      } else {
        slideLeft.classList.remove('hidden');
      }
    } else {
      // RTL check the width and adjust the menu in screen
      if (menuNav.scrollWidth > mainContainer1.offsetWidth) {
        if (Math.abs(check) < Math.abs(marginRightValue)) {
          menuNav.style.marginRight = -check + 'px';
          slideLeft.classList.remove('hidden');
          slideRight.classList.add('hidden');
        }
      }
      if (marginRightValue == 0) {
        slideLeft.classList.add('hidden');
      } else {
        slideLeft.classList.remove('hidden');
      }
    }
    if (marginLeftValue != 0 || marginRightValue != 0) {
      slideLeft.classList.remove('hidden');
    }
  }
}