Welcome to mirror list, hosted at ThFree Co, Russian Federation.

super_sidebar_toggle.vue « components « super_sidebar « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 71c1460423e50616fcd9152a2b8c493a17c21cf3 (plain)
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
<script>
import { GlButton, GlTooltipDirective } from '@gitlab/ui';
import { __ } from '~/locale';
import Tracking from '~/tracking';
import { JS_TOGGLE_COLLAPSE_CLASS, JS_TOGGLE_EXPAND_CLASS, sidebarState } from '../constants';
import { toggleSuperSidebarCollapsed } from '../super_sidebar_collapsed_state_manager';

export default {
  components: {
    GlButton,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  mixins: [Tracking.mixin()],
  props: {
    type: {
      type: String,
      required: false,
      default: 'expand',
    },
  },
  i18n: {
    primaryNavigationSidebar: __('Primary navigation sidebar'),
  },
  tooltipCollapse: {
    placement: 'bottom',
    container: 'super-sidebar',
    title: __('Hide sidebar'),
  },
  tooltipExpand: {
    placement: 'right',
    title: __('Keep sidebar visible'),
  },
  data() {
    return sidebarState;
  },
  computed: {
    isTypeCollapse() {
      return this.type === 'collapse';
    },
    isTypeExpand() {
      return this.type === 'expand';
    },
    tooltip() {
      return this.isTypeExpand ? this.$options.tooltipExpand : this.$options.tooltipCollapse;
    },
    ariaExpanded() {
      return String(this.isTypeCollapse);
    },
  },
  mounted() {
    this.$root.$on('bv::tooltip::show', this.onTooltipShow);
  },
  beforeUnmount() {
    this.$root.$off('bv::tooltip::show', this.onTooltipShow);
  },
  methods: {
    toggle() {
      this.track(this.isTypeExpand ? 'nav_show' : 'nav_hide', {
        label: 'nav_toggle',
        property: 'nav_sidebar',
      });
      toggleSuperSidebarCollapsed(!this.isTypeExpand, true);
      this.focusOtherToggle();
    },
    focusOtherToggle() {
      this.$nextTick(() => {
        const classSelector = this.isTypeExpand ? JS_TOGGLE_COLLAPSE_CLASS : JS_TOGGLE_EXPAND_CLASS;
        const otherToggle = document.querySelector(`.${classSelector}`);
        otherToggle?.focus();
      });
    },
    onTooltipShow(bvEvent) {
      if (
        bvEvent.target !== this.$el ||
        (this.isTypeCollapse && !this.isCollapsed) ||
        (this.isTypeExpand && this.isCollapsed) ||
        this.isPeek ||
        this.isHoverPeek
      )
        return;

      bvEvent.preventDefault();
    },
  },
};
</script>

<template>
  <gl-button
    v-gl-tooltip="tooltip"
    aria-controls="super-sidebar"
    :aria-expanded="ariaExpanded"
    :aria-label="$options.i18n.primaryNavigationSidebar"
    icon="sidebar"
    category="tertiary"
    @click="toggle"
  />
</template>