From 0f8ceee0f3d8767e62e6e800d801331386eaa03e Mon Sep 17 00:00:00 2001 From: Stanislav Lashmanov Date: Thu, 15 Dec 2022 18:28:02 +0400 Subject: [PATCH] Remove excessive jQuery usage from Vue components --- app/assets/javascripts/layout_nav.js | 77 +++++++++---------- .../labels_select_vue/labels_select_root.vue | 5 +- .../components/states/work_in_progress.vue | 5 +- .../vue_shared/components/navigation_tabs.vue | 4 +- 4 files changed, 45 insertions(+), 46 deletions(-) diff --git a/app/assets/javascripts/layout_nav.js b/app/assets/javascripts/layout_nav.js index ab83f1ecc147bc..90c1b31286ab62 100644 --- a/app/assets/javascripts/layout_nav.js +++ b/app/assets/javascripts/layout_nav.js @@ -12,8 +12,45 @@ function hideEndFade($scrollingTabs) { }); } +export function initScrollingTabs() { + const $scrollingTabs = $('.scrolling-tabs').not('.is-initialized'); + $scrollingTabs.addClass('is-initialized'); + + $(window) + .on('resize.nav', () => { + hideEndFade($scrollingTabs); + }) + .trigger('resize.nav'); + + $scrollingTabs.on('scroll', function tabsScrollEvent() { + const $this = $(this); + const currentPosition = $this.scrollLeft(); + const maxPosition = $this.prop('scrollWidth') - $this.outerWidth(); + + $this.siblings('.fade-left').toggleClass('scrolling', currentPosition > 0); + $this.siblings('.fade-right').toggleClass('scrolling', currentPosition < maxPosition - 1); + }); + + $scrollingTabs.each(function scrollTabsEachLoop() { + const $this = $(this); + const scrollingTabWidth = $this.width(); + const $active = $this.find('.active'); + const activeWidth = $active.width(); + + if ($active.length) { + const offset = $active.offset().left + activeWidth; + + if (offset > scrollingTabWidth - 30) { + const scrollLeft = offset - scrollingTabWidth / 2 - activeWidth / 2; + + $this.scrollLeft(scrollLeft); + } + } + }); +} + function initDeferred() { - $(document).trigger('init.scrolling-tabs'); + initScrollingTabs(); const appEl = document.getElementById('whats-new-app'); if (!appEl) return; @@ -34,43 +71,5 @@ export default function initLayoutNav() { initFlyOutNav(); - // We need to init it on DomContentLoaded as others could also call it - $(document).on('init.scrolling-tabs', () => { - const $scrollingTabs = $('.scrolling-tabs').not('.is-initialized'); - $scrollingTabs.addClass('is-initialized'); - - $(window) - .on('resize.nav', () => { - hideEndFade($scrollingTabs); - }) - .trigger('resize.nav'); - - $scrollingTabs.on('scroll', function tabsScrollEvent() { - const $this = $(this); - const currentPosition = $this.scrollLeft(); - const maxPosition = $this.prop('scrollWidth') - $this.outerWidth(); - - $this.siblings('.fade-left').toggleClass('scrolling', currentPosition > 0); - $this.siblings('.fade-right').toggleClass('scrolling', currentPosition < maxPosition - 1); - }); - - $scrollingTabs.each(function scrollTabsEachLoop() { - const $this = $(this); - const scrollingTabWidth = $this.width(); - const $active = $this.find('.active'); - const activeWidth = $active.width(); - - if ($active.length) { - const offset = $active.offset().left + activeWidth; - - if (offset > scrollingTabWidth - 30) { - const scrollLeft = offset - scrollingTabWidth / 2 - activeWidth / 2; - - $this.scrollLeft(scrollLeft); - } - } - }); - }); - requestIdleCallback(initDeferred); } diff --git a/app/assets/javascripts/sidebar/components/labels/labels_select_vue/labels_select_root.vue b/app/assets/javascripts/sidebar/components/labels/labels_select_vue/labels_select_root.vue index 2a78db352d76eb..04c62c99a1148a 100644 --- a/app/assets/javascripts/sidebar/components/labels/labels_select_vue/labels_select_root.vue +++ b/app/assets/javascripts/sidebar/components/labels/labels_select_vue/labels_select_root.vue @@ -1,5 +1,4 @@