diff --git a/src/addons/block/globalsearch/services/block-handler.ts b/src/addons/block/globalsearch/services/block-handler.ts index 0fae9d28a..609befb5f 100644 --- a/src/addons/block/globalsearch/services/block-handler.ts +++ b/src/addons/block/globalsearch/services/block-handler.ts @@ -18,8 +18,8 @@ import { CoreBlockOnlyTitleComponent } from '@features/block/components/only-tit import { CoreBlockBaseHandler } from '@features/block/classes/base-block-handler'; import { makeSingleton } from '@singletons'; import { CoreCourseBlock } from '@features/course/services/course'; -import { CoreSites } from '@services/sites'; import { CORE_SEARCH_PAGE_NAME } from '@features/search/services/handlers/mainmenu'; +import { CoreSearchGlobalSearch } from '@features/search/services/global-search'; /** * Block handler. @@ -34,11 +34,7 @@ export class AddonBlockGlobalSearchHandlerService extends CoreBlockBaseHandler { * @inheritdoc */ async isEnabled(): Promise { - const site = CoreSites.getRequiredCurrentSite(); - - return !site.isFeatureDisabled('CoreNoDelegate_GlobalSearch') - && site.wsAvailable('core_search_get_results') - && site.canUseAdvancedFeature('enableglobalsearch'); + return CoreSearchGlobalSearch.isEnabled(); } /** diff --git a/src/core/features/search/pages/global-search/global-search.ts b/src/core/features/search/pages/global-search/global-search.ts index 100b8d1bb..9f6b01f98 100644 --- a/src/core/features/search/pages/global-search/global-search.ts +++ b/src/core/features/search/pages/global-search/global-search.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, OnInit, OnDestroy } from '@angular/core'; +import { Component, OnInit, OnDestroy, AfterViewInit, ViewChild } from '@angular/core'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreSearchGlobalSearchResultsSource } from '@features/search/classes/global-search-results-source'; import { CoreSites } from '@services/sites'; @@ -28,18 +28,21 @@ import { CoreSearchGlobalSearch, } from '@features/search/services/global-search'; import { CoreNavigator } from '@services/navigator'; +import { CoreSearchBoxComponent } from '@features/search/components/search-box/search-box'; @Component({ selector: 'page-core-search-global-search', templateUrl: 'global-search.html', }) -export class CoreSearchGlobalSearchPage implements OnInit, OnDestroy { +export class CoreSearchGlobalSearchPage implements OnInit, OnDestroy, AfterViewInit { loadMoreError: string | null = null; searchBanner: string | null = null; resultsSource = new CoreSearchGlobalSearchResultsSource('', {}); private filtersObserver?: CoreEventObserver; + @ViewChild(CoreSearchBoxComponent) searchBox?: CoreSearchBoxComponent; + /** * @inheritdoc */ @@ -62,6 +65,21 @@ export class CoreSearchGlobalSearchPage implements OnInit, OnDestroy { ); } + /** + * @inheritdoc + */ + ngAfterViewInit(): void { + const query = CoreNavigator.getRouteParam('query'); + + if (query) { + if (this.searchBox) { + this.searchBox.searchText = query; + } + + this.search(query); + } + } + /** * @inheritdoc */ diff --git a/src/core/features/search/search.module.ts b/src/core/features/search/search.module.ts index 0c19dc2d1..d5c7510cc 100644 --- a/src/core/features/search/search.module.ts +++ b/src/core/features/search/search.module.ts @@ -25,6 +25,8 @@ import { CORE_SITE_SCHEMAS } from '@services/sites'; import { CoreSearchComponentsModule } from './components/components.module'; import { SITE_SCHEMA } from './services/search-history-db'; import { CoreSearchHistoryProvider } from './services/search-history.service'; +import { CoreContentLinksDelegate } from '@features/contentlinks/services/contentlinks-delegate'; +import { CoreSearchGlobalSearchLinkHandler } from '@features/search/services/handlers/global-search-link'; export const CORE_SEARCH_SERVICES: Type[] = [ CoreSearchHistoryProvider, @@ -51,6 +53,7 @@ const mainMenuChildrenRoutes: Routes = [ multi: true, useValue() { CoreMainMenuDelegate.registerHandler(CoreSearchMainMenuHandler.instance); + CoreContentLinksDelegate.registerHandler(CoreSearchGlobalSearchLinkHandler.instance); }, }, ], diff --git a/src/core/features/search/services/global-search.ts b/src/core/features/search/services/global-search.ts index a661050aa..f423e8626 100644 --- a/src/core/features/search/services/global-search.ts +++ b/src/core/features/search/services/global-search.ts @@ -83,6 +83,21 @@ export class CoreSearchGlobalSearchService { private static readonly SEARCH_AREAS_CACHE_KEY = 'CoreSearchGlobalSearch:SearchAreas'; + /** + * Check whether global search is enabled or not. + * + * @returns Whether global search is enabled or not. + */ + async isEnabled(siteId?: string): Promise { + const site = siteId + ? await CoreSites.getSite(siteId) + : CoreSites.getRequiredCurrentSite(); + + return !site?.isFeatureDisabled('CoreNoDelegate_GlobalSearch') + && site?.wsAvailable('core_search_get_results') // @since 4.3 + && site?.canUseAdvancedFeature('enableglobalsearch'); + } + /** * Get results. * diff --git a/src/core/features/search/services/handlers/global-search-link.ts b/src/core/features/search/services/handlers/global-search-link.ts new file mode 100644 index 000000000..fb5d887e7 --- /dev/null +++ b/src/core/features/search/services/handlers/global-search-link.ts @@ -0,0 +1,56 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { Injectable } from '@angular/core'; +import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler'; +import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate'; +import { CoreSearchGlobalSearch } from '@features/search/services/global-search'; +import { CORE_SEARCH_PAGE_NAME } from '@features/search/services/handlers/mainmenu'; +import { CoreNavigator } from '@services/navigator'; +import { makeSingleton } from '@singletons'; + +/** + * Handler to treat links to search page. + */ +@Injectable( { providedIn: 'root' }) +export class CoreSearchGlobalSearchLinkHandlerService extends CoreContentLinksHandlerBase { + + name = 'CoreSearchSearchLinkHandler'; + pattern = /\/search\/index\.php.*/; + + /** + * @inheritdoc + */ + async isEnabled(siteId: string): Promise { + return CoreSearchGlobalSearch.isEnabled(siteId); + } + + /** + * @inheritdoc + */ + getActions(siteIds: string[], url: string, params: Record): CoreContentLinksAction[] { + return [{ + action: (siteId: string): void => { + CoreNavigator.navigateToSitePath(CORE_SEARCH_PAGE_NAME, { + siteId, + params: { + query: params.q, + }, + }); + }, + }]; + } + +} +export const CoreSearchGlobalSearchLinkHandler = makeSingleton(CoreSearchGlobalSearchLinkHandlerService); diff --git a/src/core/features/search/services/handlers/mainmenu.ts b/src/core/features/search/services/handlers/mainmenu.ts index 926263720..e97531ae8 100644 --- a/src/core/features/search/services/handlers/mainmenu.ts +++ b/src/core/features/search/services/handlers/mainmenu.ts @@ -15,7 +15,7 @@ import { Injectable } from '@angular/core'; import { makeSingleton } from '@singletons'; import { CoreMainMenuHandler, CoreMainMenuHandlerData } from '@features/mainmenu/services/mainmenu-delegate'; -import { CoreSites } from '@services/sites'; +import { CoreSearchGlobalSearch } from '@features/search/services/global-search'; export const CORE_SEARCH_PAGE_NAME = 'search'; @@ -32,11 +32,7 @@ export class CoreSearchMainMenuHandlerService implements CoreMainMenuHandler { * @inheritdoc */ async isEnabled(): Promise { - const site = CoreSites.getRequiredCurrentSite(); - - return !site.isFeatureDisabled('CoreNoDelegate_GlobalSearch') - && site.wsAvailable('core_search_get_results') - && site.canUseAdvancedFeature('enableglobalsearch'); + return CoreSearchGlobalSearch.isEnabled(); } /**