From 05f403cf046912b53c4b4f4dae48d4b8cc375812 Mon Sep 17 00:00:00 2001 From: Mike Greiling Date: Thu, 13 Sep 2018 16:59:09 -0500 Subject: [PATCH] Move search autocomplete from dispatcher to main.js --- app/assets/javascripts/dispatcher.js | 10 ---------- app/assets/javascripts/main.js | 3 +++ app/assets/javascripts/search_autocomplete.js | 6 +++++- spec/javascripts/search_autocomplete_spec.js | 4 ++-- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/dispatcher.js b/app/assets/javascripts/dispatcher.js index 192d8ca60fe..1186e9e4d95 100644 --- a/app/assets/javascripts/dispatcher.js +++ b/app/assets/javascripts/dispatcher.js @@ -4,16 +4,8 @@ import $ from 'jquery'; import GfmAutoComplete from './gfm_auto_complete'; import { convertPermissionToBoolean } from './lib/utils/common_utils'; import Shortcuts from './shortcuts'; -import SearchAutocomplete from './search_autocomplete'; import performanceBar from './performance_bar'; -function initSearch() { - // Only when search form is present - if ($('.search').length) { - return new SearchAutocomplete(); - } -} - function initPageShortcuts(page) { const pagesWithCustomShortcuts = [ 'projects:activity', @@ -70,8 +62,6 @@ function initPerformanceBar() { } export default () => { - initSearch(); - const page = $('body').attr('data-page'); if (page) { initPageShortcuts(page); diff --git a/app/assets/javascripts/main.js b/app/assets/javascripts/main.js index 393d195549f..a7179686aef 100644 --- a/app/assets/javascripts/main.js +++ b/app/assets/javascripts/main.js @@ -28,6 +28,7 @@ import './frequent_items'; import initBreadcrumbs from './breadcrumb'; import initDispatcher from './dispatcher'; import initUsagePingConsent from './usage_ping_consent'; +import initSearchAutocomplete from './search_autocomplete'; import GlFieldErrors from './gl_field_errors'; // expose jQuery as global (TODO: remove these) @@ -78,6 +79,8 @@ document.addEventListener('DOMContentLoaded', () => { initLogoAnimation(); initUsagePingConsent(); + if (document.querySelector('.search')) initSearchAutocomplete(); + // Set the default path for all cookies to GitLab's root directory Cookies.defaults.path = gon.relative_url_root || '/'; diff --git a/app/assets/javascripts/search_autocomplete.js b/app/assets/javascripts/search_autocomplete.js index aec09b8bc0a..50dd3c12382 100644 --- a/app/assets/javascripts/search_autocomplete.js +++ b/app/assets/javascripts/search_autocomplete.js @@ -68,7 +68,7 @@ function setSearchOptions() { } } -export default class SearchAutocomplete { +export class SearchAutocomplete { constructor({ wrap, optsEl, autocompletePath, projectId, projectRef } = {}) { setSearchOptions(); this.bindEventContext(); @@ -499,3 +499,7 @@ export default class SearchAutocomplete { this.dropdownMenu.toggleClass('fade-out', !this.isScrolledUp()); } } + +export default function initSearchAutocomplete(opts) { + return new SearchAutocomplete(opts); +} diff --git a/spec/javascripts/search_autocomplete_spec.js b/spec/javascripts/search_autocomplete_spec.js index 86c001678c5..646d843162c 100644 --- a/spec/javascripts/search_autocomplete_spec.js +++ b/spec/javascripts/search_autocomplete_spec.js @@ -2,7 +2,7 @@ import $ from 'jquery'; import '~/gl_dropdown'; -import SearchAutocomplete from '~/search_autocomplete'; +import initSearchAutocomplete from '~/search_autocomplete'; import '~/lib/utils/common_utils'; describe('Search autocomplete dropdown', () => { @@ -132,7 +132,7 @@ describe('Search autocomplete dropdown', () => { window.gon.current_user_id = userId; window.gon.current_username = userName; - return (widget = new SearchAutocomplete()); + return (widget = initSearchAutocomplete()); }); afterEach(function() {