Commit 5b81f9dc authored by Ezekiel Kigbo's avatar Ezekiel Kigbo

Merge branch 'main-js-first-round' into 'master'

Tidy up some main.js behaviors

See merge request gitlab-org/gitlab!34651
parents 7f3083f0 3492c375
import $ from 'jquery';
import { GlBreakpointInstance as bp } from '@gitlab/ui/dist/utils';
/**
* This behavior collapses the right sidebar
* if the window size changes
*
* @sentrify
*/
export default () => {
const $sidebarGutterToggle = $('.js-sidebar-toggle');
let bootstrapBreakpoint = bp.getBreakpointSize();
$(window).on('resize.app', () => {
const oldBootstrapBreakpoint = bootstrapBreakpoint;
bootstrapBreakpoint = bp.getBreakpointSize();
if (bootstrapBreakpoint !== oldBootstrapBreakpoint) {
const breakpointSizes = ['md', 'sm', 'xs'];
if (breakpointSizes.includes(bootstrapBreakpoint)) {
const $gutterIcon = $sidebarGutterToggle.find('i');
if ($gutterIcon.hasClass('fa-angle-double-right')) {
$sidebarGutterToggle.trigger('click');
}
const sidebarGutterVueToggleEl = document.querySelector('.js-sidebar-vue-toggle');
// Sidebar has an icon which corresponds to collapsing the sidebar
// only then trigger the click.
if (sidebarGutterVueToggleEl) {
const collapseIcon = sidebarGutterVueToggleEl.querySelector('i.fa-angle-double-right');
if (collapseIcon) {
collapseIcon.click();
}
}
}
}
});
};
...@@ -11,9 +11,13 @@ import './requires_input'; ...@@ -11,9 +11,13 @@ import './requires_input';
import initPageShortcuts from './shortcuts'; import initPageShortcuts from './shortcuts';
import './toggler_behavior'; import './toggler_behavior';
import './preview_markdown'; import './preview_markdown';
import initCollapseSidebarOnWindowResize from './collapse_sidebar_on_window_resize';
import initSelect2Dropdowns from './select2';
installGlEmojiElement(); installGlEmojiElement();
initGFMInput(); initGFMInput();
initCopyAsGFM(); initCopyAsGFM();
initCopyToClipboard(); initCopyToClipboard();
initPageShortcuts(); initPageShortcuts();
initCollapseSidebarOnWindowResize();
initSelect2Dropdowns();
import $ from 'jquery';
export default () => {
if ($('select.select2').length) {
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(() => {
$('select.select2').select2({
width: 'resolve',
minimumResultsForSearch: 10,
dropdownAutoWidth: true,
});
// Close select2 on escape
$('.js-select2').on('select2-close', () => {
setTimeout(() => {
$('.select2-container-active').removeClass('select2-container-active');
$(':focus').blur();
}, 1);
});
})
.catch(() => {});
}
};
...@@ -62,12 +62,12 @@ function disableJQueryAnimations() { ...@@ -62,12 +62,12 @@ function disableJQueryAnimations() {
} }
// Disable jQuery animations // Disable jQuery animations
if (gon && gon.disable_animations) { if (gon?.disable_animations) {
disableJQueryAnimations(); disableJQueryAnimations();
} }
// inject test utilities if necessary // inject test utilities if necessary
if (process.env.NODE_ENV !== 'production' && gon && gon.test_env) { if (process.env.NODE_ENV !== 'production' && gon?.test_env) {
disableJQueryAnimations(); disableJQueryAnimations();
import(/* webpackMode: "eager" */ './test_utils/'); // eslint-disable-line no-unused-expressions import(/* webpackMode: "eager" */ './test_utils/'); // eslint-disable-line no-unused-expressions
} }
...@@ -132,27 +132,6 @@ function deferredInitialisation() { ...@@ -132,27 +132,6 @@ function deferredInitialisation() {
.fadeOut(); .fadeOut();
}); });
// Initialize select2 selects
if ($('select.select2').length) {
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(() => {
$('select.select2').select2({
width: 'resolve',
minimumResultsForSearch: 10,
dropdownAutoWidth: true,
});
// Close select2 on escape
$('.js-select2').on('select2-close', () => {
setTimeout(() => {
$('.select2-container-active').removeClass('select2-container-active');
$(':focus').blur();
}, 1);
});
})
.catch(() => {});
}
const glTooltipDelay = localStorage.getItem('gl-tooltip-delay'); const glTooltipDelay = localStorage.getItem('gl-tooltip-delay');
const delay = glTooltipDelay ? JSON.parse(glTooltipDelay) : 0; const delay = glTooltipDelay ? JSON.parse(glTooltipDelay) : 0;
...@@ -179,9 +158,7 @@ function deferredInitialisation() { ...@@ -179,9 +158,7 @@ function deferredInitialisation() {
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
const $body = $('body'); const $body = $('body');
const $document = $(document); const $document = $(document);
const $window = $(window); const bootstrapBreakpoint = bp.getBreakpointSize();
const $sidebarGutterToggle = $('.js-sidebar-toggle');
let bootstrapBreakpoint = bp.getBreakpointSize();
if (document.querySelector('#js-peek')) initPerformanceBar({ container: '#js-peek' }); if (document.querySelector('#js-peek')) initPerformanceBar({ container: '#js-peek' });
...@@ -199,6 +176,15 @@ document.addEventListener('DOMContentLoaded', () => { ...@@ -199,6 +176,15 @@ document.addEventListener('DOMContentLoaded', () => {
} }
}); });
/**
* TODO: Apparently we are collapsing the right sidebar on certain screensizes per default
* except on issue board pages. Why can't we do it with CSS?
*
* Proposal: Expose a global sidebar API, which we could import wherever we are manipulating
* the visibility of the sidebar.
*
* Quick fix: Get rid of jQuery for this implementation
*/
const isBoardsPage = /(projects|groups):boards:show/.test(document.body.dataset.page); const isBoardsPage = /(projects|groups):boards:show/.test(document.body.dataset.page);
if (!isBoardsPage && (bootstrapBreakpoint === 'sm' || bootstrapBreakpoint === 'xs')) { if (!isBoardsPage && (bootstrapBreakpoint === 'sm' || bootstrapBreakpoint === 'xs')) {
const $rightSidebar = $('aside.right-sidebar'); const $rightSidebar = $('aside.right-sidebar');
...@@ -225,14 +211,12 @@ document.addEventListener('DOMContentLoaded', () => { ...@@ -225,14 +211,12 @@ document.addEventListener('DOMContentLoaded', () => {
localTimeAgo($('abbr.timeago, .js-timeago'), true); localTimeAgo($('abbr.timeago, .js-timeago'), true);
// Form submitter /**
$('.trigger-submit').on('change', function triggerSubmitCallback() { * This disables form buttons while a form is submitting
$(this) * We do not difinitively know all of the places where this is used
.parents('form') *
.submit(); * TODO: Defer execution, migrate to behaviors, and add sentry logging
}); */
// Disable form buttons while a form is submitting
$body.on('ajax:complete, ajax:beforeSend, submit', 'form', function ajaxCompleteCallback(e) { $body.on('ajax:complete, ajax:beforeSend, submit', 'form', function ajaxCompleteCallback(e) {
const $buttons = $('[type="submit"], .js-disable-on-submit', this).not('.js-no-auto-disable'); const $buttons = $('[type="submit"], .js-disable-on-submit', this).not('.js-no-auto-disable');
switch (e.type) { switch (e.type) {
...@@ -259,7 +243,11 @@ document.addEventListener('DOMContentLoaded', () => { ...@@ -259,7 +243,11 @@ document.addEventListener('DOMContentLoaded', () => {
$('.header-content').toggleClass('menu-expanded'); $('.header-content').toggleClass('menu-expanded');
}); });
// Commit show suppressed diff /**
* Show suppressed commit diff
*
* TODO: Move to commit diff pages
*/
$document.on('click', '.diff-content .js-show-suppressed-diff', function showDiffCallback() { $document.on('click', '.diff-content .js-show-suppressed-diff', function showDiffCallback() {
const $container = $(this).parent(); const $container = $(this).parent();
$container.next('table').show(); $container.next('table').show();
...@@ -290,39 +278,6 @@ document.addEventListener('DOMContentLoaded', () => { ...@@ -290,39 +278,6 @@ document.addEventListener('DOMContentLoaded', () => {
$(document).trigger('toggle.comments'); $(document).trigger('toggle.comments');
}); });
$document.on('breakpoint:change', (e, breakpoint) => {
const breakpointSizes = ['md', 'sm', 'xs'];
if (breakpointSizes.includes(breakpoint)) {
const $gutterIcon = $sidebarGutterToggle.find('i');
if ($gutterIcon.hasClass('fa-angle-double-right')) {
$sidebarGutterToggle.trigger('click');
}
const sidebarGutterVueToggleEl = document.querySelector('.js-sidebar-vue-toggle');
// Sidebar has an icon which corresponds to collapsing the sidebar
// only then trigger the click.
if (sidebarGutterVueToggleEl) {
const collapseIcon = sidebarGutterVueToggleEl.querySelector('i.fa-angle-double-right');
if (collapseIcon) {
collapseIcon.click();
}
}
}
});
function fitSidebarForSize() {
const oldBootstrapBreakpoint = bootstrapBreakpoint;
bootstrapBreakpoint = bp.getBreakpointSize();
if (bootstrapBreakpoint !== oldBootstrapBreakpoint) {
$document.trigger('breakpoint:change', [bootstrapBreakpoint]);
}
}
$window.on('resize.app', fitSidebarForSize);
$('form.filter-form').on('submit', function filterFormSubmitCallback(event) { $('form.filter-form').on('submit', function filterFormSubmitCallback(event) {
const link = document.createElement('a'); const link = document.createElement('a');
link.href = this.action; link.href = this.action;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment