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
|
<script>
import { GlButton } from '@gitlab/ui';
import { GlBreakpointInstance, breakpoints } from '@gitlab/ui/dist/utils';
import ExtraInfo from 'jh_else_ce/super_sidebar/components/extra_info.vue';
import { Mousetrap } from '~/lib/mousetrap';
import { TAB_KEY_CODE } from '~/lib/utils/keycodes';
import { keysFor, TOGGLE_SUPER_SIDEBAR } from '~/behaviors/shortcuts/keybindings';
import { __, s__ } from '~/locale';
import Tracking from '~/tracking';
import {
sidebarState,
SUPER_SIDEBAR_PEEK_STATE_CLOSED as STATE_CLOSED,
SUPER_SIDEBAR_PEEK_STATE_WILL_OPEN as STATE_WILL_OPEN,
SUPER_SIDEBAR_PEEK_STATE_OPEN as STATE_OPEN,
} from '../constants';
import { isCollapsed, toggleSuperSidebarCollapsed } from '../super_sidebar_collapsed_state_manager';
import { trackContextAccess } from '../utils';
import UserBar from './user_bar.vue';
import SidebarPortalTarget from './sidebar_portal_target.vue';
import HelpCenter from './help_center.vue';
import SidebarMenu from './sidebar_menu.vue';
import SidebarPeekBehavior from './sidebar_peek_behavior.vue';
import SidebarHoverPeekBehavior from './sidebar_hover_peek_behavior.vue';
import ScrollScrim from './scroll_scrim.vue';
export default {
components: {
GlButton,
UserBar,
HelpCenter,
ExtraInfo,
SidebarMenu,
SidebarPeekBehavior,
SidebarHoverPeekBehavior,
SidebarPortalTarget,
ScrollScrim,
TrialStatusWidget: () =>
import('ee_component/contextual_sidebar/components/trial_status_widget.vue'),
TrialStatusPopover: () =>
import('ee_component/contextual_sidebar/components/trial_status_popover.vue'),
},
mixins: [Tracking.mixin()],
i18n: {
skipToMainContent: __('Skip to main content'),
primaryNavigation: s__('Navigation|Primary navigation'),
adminArea: s__('Navigation|Admin Area'),
},
inject: ['showTrialStatusWidget'],
props: {
sidebarData: {
type: Object,
required: true,
},
},
data() {
return {
sidebarState,
showPeekHint: false,
isMouseover: false,
breakpoint: null,
};
},
computed: {
showOverlay() {
return this.sidebarState.isPeek || this.sidebarState.isHoverPeek;
},
menuItems() {
return this.sidebarData.current_menu_items || [];
},
peekClasses() {
return {
'super-sidebar-peek-hint': this.showPeekHint,
'super-sidebar-peek': this.showOverlay,
'super-sidebar-has-peeked': this.sidebarState.hasPeeked,
};
},
},
watch: {
'sidebarState.isCollapsed': {
handler() {
this.setupFocusTrapListener();
},
},
},
created() {
const {
is_logged_in: isLoggedIn,
current_context: currentContext,
username,
track_visits_path: trackVisitsPath,
} = this.sidebarData;
if (isLoggedIn && currentContext.namespace) {
trackContextAccess(username, currentContext, trackVisitsPath);
}
},
mounted() {
this.setupFocusTrapListener();
Mousetrap.bind(keysFor(TOGGLE_SUPER_SIDEBAR), this.toggleSidebar);
},
beforeDestroy() {
document.removeEventListener('keydown', this.focusTrap);
Mousetrap.unbind(keysFor(TOGGLE_SUPER_SIDEBAR));
},
methods: {
toggleSidebar() {
this.track(isCollapsed() ? 'nav_show' : 'nav_hide', {
label: 'nav_toggle_keyboard_shortcut',
property: 'nav_sidebar',
});
toggleSuperSidebarCollapsed(!isCollapsed(), true);
},
setupFocusTrapListener() {
/**
* Only trap focus when sidebar displays over page content to avoid
* focus moving to page content and being obscured by the sidebar
*/
if (GlBreakpointInstance.windowWidth() < breakpoints.xl && !this.sidebarState.isCollapsed) {
document.addEventListener('keydown', this.focusTrap);
} else {
document.removeEventListener('keydown', this.focusTrap);
}
},
collapseSidebar() {
toggleSuperSidebarCollapsed(true, false);
},
onPeekChange(state) {
if (state === STATE_CLOSED) {
this.sidebarState.isPeek = false;
this.sidebarState.isCollapsed = true;
this.showPeekHint = false;
} else if (state === STATE_WILL_OPEN) {
this.sidebarState.hasPeeked = true;
this.sidebarState.isPeek = false;
this.sidebarState.isCollapsed = true;
this.showPeekHint = true;
} else {
this.sidebarState.isPeek = true;
this.sidebarState.isCollapsed = false;
this.showPeekHint = false;
}
},
onHoverPeekChange(state) {
if (state === STATE_OPEN) {
this.sidebarState.hasPeeked = true;
this.sidebarState.isHoverPeek = true;
this.sidebarState.isCollapsed = false;
} else if (state === STATE_CLOSED) {
this.sidebarState.isHoverPeek = false;
this.sidebarState.isCollapsed = true;
}
},
focusTrap(event) {
const { keyCode, shiftKey } = event;
const firstFocusableElement = this.$refs.userBar.$el.querySelector('a');
const lastFocusableElement = this.$refs.helpCenter.$el.querySelector('button');
if (keyCode !== TAB_KEY_CODE) return;
if (shiftKey) {
if (document.activeElement === firstFocusableElement) {
lastFocusableElement.focus();
event.preventDefault();
}
} else if (document.activeElement === lastFocusableElement) {
firstFocusableElement.focus();
event.preventDefault();
}
},
},
};
</script>
<template>
<div>
<div class="super-sidebar-overlay" @click="collapseSidebar"></div>
<gl-button
v-if="sidebarData.is_logged_in"
class="super-sidebar-skip-to gl-sr-only-focusable gl-fixed gl-left-0 gl-m-3"
data-testid="super-sidebar-skip-to"
href="#content-body"
variant="confirm"
>
{{ $options.i18n.skipToMainContent }}
</gl-button>
<nav
id="super-sidebar"
aria-labelledby="super-sidebar-heading"
class="super-sidebar"
:class="peekClasses"
data-testid="super-sidebar"
data-qa-selector="navbar"
:inert="sidebarState.isCollapsed"
@mouseenter="isMouseover = true"
@mouseleave="isMouseover = false"
>
<h2 id="super-sidebar-heading" class="gl-sr-only">
{{ $options.i18n.primaryNavigation }}
</h2>
<user-bar ref="userBar" :has-collapse-button="!showOverlay" :sidebar-data="sidebarData" />
<div v-if="showTrialStatusWidget" class="gl-px-2 gl-py-2">
<trial-status-widget
class="gl-rounded-base gl-relative gl-display-flex gl-align-items-center gl-mb-1 gl-px-3 gl-line-height-normal gl-text-black-normal! gl-hover-bg-t-gray-a-08 gl-focus-bg-t-gray-a-08 gl-text-decoration-none! gl-py-3"
/>
<trial-status-popover />
</div>
<div
class="contextual-nav gl-display-flex gl-flex-direction-column gl-flex-grow-1 gl-overflow-hidden"
>
<scroll-scrim class="gl-flex-grow-1" data-testid="nav-container">
<div
id="super-sidebar-context-header"
class="gl-px-5 gl-pt-3 gl-pb-2 gl-m-0 gl-reset-line-height gl-font-weight-bold gl-font-sm super-sidebar-context-header"
>
{{ sidebarData.current_context_header }}
</div>
<sidebar-menu
v-if="menuItems.length"
:items="menuItems"
:is-logged-in="sidebarData.is_logged_in"
:panel-type="sidebarData.panel_type"
:pinned-item-ids="sidebarData.pinned_items"
:update-pins-url="sidebarData.update_pins_url"
/>
<sidebar-portal-target />
</scroll-scrim>
<div class="gl-p-2">
<help-center ref="helpCenter" :sidebar-data="sidebarData" />
<gl-button
v-if="sidebarData.is_admin"
class="gl-fixed gl-right-0 gl-mr-3 gl-mt-2"
data-testid="sidebar-admin-link"
:href="sidebarData.admin_url"
icon="admin"
size="small"
>
{{ $options.i18n.adminArea }}
</gl-button>
<extra-info />
</div>
</div>
</nav>
<a
v-for="shortcutLink in sidebarData.shortcut_links"
:key="shortcutLink.href"
:href="shortcutLink.href"
:class="shortcutLink.css_class"
class="gl-display-none"
>
{{ shortcutLink.title }}
</a>
<!--
Only mount peek behavior components if the sidebar is peekable, to avoid
setting up event listeners unnecessarily.
-->
<sidebar-peek-behavior
v-if="sidebarState.isPeekable && !sidebarState.isHoverPeek"
:is-mouse-over-sidebar="isMouseover"
@change="onPeekChange"
/>
<sidebar-hover-peek-behavior
v-if="sidebarState.isPeekable && !sidebarState.isPeek"
:is-mouse-over-sidebar="isMouseover"
@change="onHoverPeekChange"
/>
</div>
</template>
|