All files / components/dropdown dropdown-menu.vue

0% Statements 0/51
0% Branches 0/36
0% Functions 0/12
0% Lines 0/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
<template>
 
  <li class="dropdown specific-breakpoint mw-100 m-lg-0 me-4 mb-2 position-static" ref='menu-dropdown'>
    <dropdown-button
      ref="dropdown-btn"
      :btnSpecs="btnSpecs"
      :class="{'show': isDropActive}"
      :aria-expanded="isDropActive"
    ></dropdown-button>
    <dropdown-list
      :listSpecs="listSpecs"
      :class="{
        'show': isDropActive,
        'fast-transition': listSpecs['activeDropdown'] !== listSpecs['index'] && listSpecs['activeDropdown'] !== -1,
        'slow-transition': listSpecs['activeDropdown'] === listSpecs['index'] || listSpecs['activeDropdown'] === -1
      }"
      :data-bs-popper="isDropActive ? 'none': ''"
      ></dropdown-list>
  </li>
 
</template>
 
<script>
import dropdownButton from './dropdown-button.vue';
import dropdownList from './dropdown-list.vue';
 
export default {
  props: ['btnSpecs', 'listSpecs'],
  emits: ['dropActive'],
  data() {
    return {
      isDropActive: false,
      removeEvents: false,
      media: '',
      mediaIpadProLandscape: '',
      mediaIpadProPortrait: '',
      mediaIpadLandscape: '',
      dropdown: null
    }
  },
  // watch: {
  //   listSpecs(val) {
  //     console.log(this.listSpecs['index'])
  //   }
  // },
  components: {dropdownButton, dropdownList},
  methods: {
    /**
     * Show dropdown
     * @see test
     * @returns void
     */
    test() {
      this.isDropActive = false;
      this.$emit('dropActive', -1);
    },
    /**
     * Show dropdown
     * @see showDrop
     * @returns void
     */
    showDrop() {
      this.isDropActive = true;
      this.$emit('dropActive', this.listSpecs['index']);
    },
    /**
     * Close dropdown
     * @see removeDrop
     * @returns void
     */
    removeDrop() {
      this.isDropActive = false;
      this.$emit('dropActive', -1);
    },
    /**
     * Toggle dropdown button for mobile
     * @see toggleButtonMobile
     * @returns void
     */
    // toggleButtonMobile() {
    //     this.isDropActive = true;
    //     this.$emit('dropActive', this.listSpecs['index']);
    // },
 
    /**
     * Toggles hover event listeners
     * @see toggleEventListeners
     * @param {string} task
     * @returns void
     */
    toggleEventListeners(task) {
      let dropdown = this.$refs["menu-dropdown"];
 
      if (task === 'enable') {
        this.removeEvents = true;
        dropdown.addEventListener('mouseenter', this.showDrop);
        dropdown.addEventListener('mouseleave', this.removeDrop);
        //dropdown.addEventListener('touchstart', this.toggleButtonMobile, {passive: true});
      }
 
      if (task === 'disable') {
        this.removeEvents = false;
        dropdown.removeEventListener('mouseenter', this.showDrop);
        dropdown.removeEventListener('mouseleave', this.removeDrop);
        //dropdown.removeEventListener('touchstart', this.toggleButtonMobile, {passive: true});
      }
    },
    /**
     * Enable or disable events based on the viewport (on ipad)
     * @see toggleEventsIpad
     * @param {object} e
     * @returns void
     */
    toggleEventsIpad(e) {
      if (e.matches) {
        this.toggleEventListeners('disable');
      }
 
      if (!e.matches && this.media.matches) {
        this.toggleEventListeners('enable');
      }
    },
 
    /**
     * Toggles hover event listeners based on viewport
     * @see changeDropdownEvents
     * @returns void
     */
 
    changeDropdownEvents(e) {
      if (e.matches && !this.mediaIpadProPortrait.matches && !this.mediaIpadProLandscape.matches && !this.mediaIpadLandscape.matches) {
        this.toggleEventListeners('enable');
      }
 
      if (!e.matches) {
        this.toggleEventListeners('disable');
      }
    }
 
  },
 
  /**
   * Adds or removes event listeners based on viewport
   * @see mounted
   * @returns void
   */
  async mounted() {
 
    let {Dropdown} = await import('bootstrap');
 
    let myDropdown = this.$refs['dropdown-btn'];
    this.dropdown = new Dropdown(myDropdown.$el);
 
    this.media = window.matchMedia('(min-width: 993px) and (hover: hover)');
    this.mediaIpadProLandscape = window.matchMedia('screen and (min-device-width: 1366px) and (max-device-width: 1366px) and (min-device-height: 1024px) and (max-device-height: 1024px) and (-webkit-min-device-pixel-ratio: 2)  and (orientation: landscape) and (hover: none)');
    this.mediaIpadProPortrait = window.matchMedia('screen and (min-device-width: 1024px) and (max-device-width: 1024px) and (min-device-height: 1366px) and (max-device-height: 1366px) and (-webkit-min-device-pixel-ratio: 2)  and (orientation: portrait) and (hover: none)');
    this.mediaIpadLandscape = window.matchMedia('screen and (min-device-width: 1024px) and (max-device-width: 1024px) and (min-device-height: 768px) and (max-device-height: 768px) and (-webkit-min-device-pixel-ratio: 2)  and (orientation: landscape) and (hover: none)');
 
    // Override bootstrap show method in order to disable click on desktop
    const oldShow = this.dropdown.show.bind(this.dropdown);
    this.dropdown.show = () => {
      if (!this.media.matches || this.mediaIpadProPortrait.matches || this.mediaIpadProLandscape.matches || this.mediaIpadLandscape.matches) {
        oldShow();
      }
    }
 
    //Override bootstrap hide method in order to disable click on desktop
    const oldHide = this.dropdown._completeHide.bind(this.dropdown);
    this.dropdown._completeHide = () => {
      if (!this.media.matches || this.mediaIpadProPortrait.matches || this.mediaIpadProLandscape.matches || this.mediaIpadLandscape.matches) {
        oldHide({relatedTarget: myDropdown});
      }
    }
 
    // add hover events on desktop
    if (this.media.matches && !this.mediaIpadProPortrait.matches && !this.mediaIpadProLandscape.matches && !this.mediaIpadLandscape.matches) {
      this.toggleEventListeners('enable');
    }
 
    this.media.addEventListener('change', this.changeDropdownEvents)
 
    this.mediaIpadProPortrait.addEventListener('change', this.toggleEventsIpad);
    this.mediaIpadProLandscape.addEventListener('change', this.toggleEventsIpad);
    this.mediaIpadLandscape.addEventListener('change', this.toggleEventsIpad);
  },
  /**
   * Removes all event listeners
   * @see beforeDestroy
   * @returns void
   */
  beforeDestroy() {
    this.media.removeEventListener('change', this.changeDropdownEvents);
    this.mediaIpadProPortrait.removeEventListener('change', this.toggleEventsIpad);
    this.mediaIpadProLandscape.removeEventListener('change', this.toggleEventsIpad);
    this.mediaIpadLandscape.removeEventListener('change', this.toggleEventsIpad);
 
    // if (this.removeEvents) {
    //   let dropdown = this.$refs["menu-dropdown"];
    //   dropdown.removeEventListener('mouseenter', this.showDrop);
    //   dropdown.removeEventListener('mouseleave', this.removeDrop);
    //   dropdown.removeEventListener('touchstart', this.toggleButtonMobile, {passive: true});
    // }
  }
}
</script>
 
<style lang="scss" scoped>
 
.dropdown-menu{
  z-index: -1;
}
 
@include media-breakpoint-up(lg) {
 
  .dropdown:hover::v-deep .btn.show, .dropdown:active::v-deep .btn.show {
    color: rgb(0, 21, 44) !important;
    transform: scale(1.1);
  }
  .dropdown::v-deep>.dropdown-menu {
    display: inherit;
    left: 0;
    transform-origin: center top;
    transform: scaleY(0);
    &.slow-transition {
      transition: transform .201s ease-out .366s;
    }
  }
 
  .dropdown::v-deep>.btn.show+.dropdown-menu.slow-transition {
    transform: scaleY(1);
    transition: transform .3s ease-out .2s;
  }
 
}
 
@include media-breakpoint-down(lg) {
  .dropdown::v-deep .btn.show {
    color: rgb(0, 21, 44) !important;
  }
}
 
@media screen and #{$ipad-pro-portrait-breakpoint},
screen and #{$ipad-pro-landscape-breakpoint},
screen and #{$ipad-landscape-breakpoint} {
 
  .dropdown:hover::v-deep .btn.show, .dropdown:active::v-deep .btn.show {
    transform: none;
  }
  .dropdown::v-deep>.dropdown-menu {
    display: none;
    transform: none;
    transform-origin: unset;
    &.slow-transition {
      transition: none;
    }
  }
 
  .dropdown::v-deep .dropdown-menu.show {
    display: block;
  }
 
  .dropdown::v-deep>.btn.show+.dropdown-menu.slow-transition {
    transform: none;
    transition: none;
  }
 
  .dropdown::v-deep .btn.show {
    color: rgb(0, 21, 44) !important;
  }
 
  .specific-breakpoint.dropdown {
    margin: 0 !important;
    margin-bottom: $spacer * 2 !important;
    margin-right: $spacer * 1.5 !important;
  }
}
 
</style>