#4303: add search function.
This commit is contained in:
147
Oqtane.Client/Modules/SearchResults/Index.razor
Normal file
147
Oqtane.Client/Modules/SearchResults/Index.razor
Normal file
@@ -0,0 +1,147 @@
|
||||
@using Oqtane.Modules.SearchResults.Services
|
||||
@namespace Oqtane.Modules.SearchResults
|
||||
@inherits ModuleBase
|
||||
@inject ISearchResultsService SearchResultsService
|
||||
@inject IStringLocalizer<Index> Localizer
|
||||
|
||||
<div class="search-result-container">
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<div class="input-group mb-3">
|
||||
<span class="input-group-text">@Localizer["SearchPrefix"]</span>
|
||||
<input type="text" class="form-control shadow-none" maxlength="50"
|
||||
aria-label="Keywords"
|
||||
placeholder="@Localizer["SearchPlaceholder"]"
|
||||
@bind="_keywords"
|
||||
@bind:event="oninput"
|
||||
@onkeypress="KeywordsChanged">
|
||||
<button class="btn btn-primary shadow-none" type="button" @onclick="@(async () => await Search())">@Localizer["Search"]</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-12 mb-3">
|
||||
@if (_loading)
|
||||
{
|
||||
<div class="app-progress-indicator"></div>
|
||||
}
|
||||
else
|
||||
{
|
||||
@if (_searchResults != null && _searchResults.Results != null)
|
||||
{
|
||||
if (_searchResults.Results.Any())
|
||||
{
|
||||
<Pager Items="@_searchResults?.Results"
|
||||
Format="Grid"
|
||||
PageSize="@_pageSize.ToString()"
|
||||
DisplayPages="@_displayPages.ToString()"
|
||||
CurrentPage="@_currentPage.ToString()" Columns="1"
|
||||
Toolbar="Bottom">
|
||||
<Row>
|
||||
<div class="search-item">
|
||||
<h4 class="mb-1"><a href="@context.Url">@context.Title</a></h4>
|
||||
<div class="font-13 text-success mb-3">@context.Url</div>
|
||||
<p class="mb-0 text-muted">@((MarkupString)context.Snippet)</p>
|
||||
</div>
|
||||
</Row>
|
||||
</Pager>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="alert alert-info show mt-3" role="alert">
|
||||
@Localizer["NoResult"]
|
||||
</div>
|
||||
}
|
||||
}
|
||||
<div class="clearfix"></div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@code {
|
||||
private SearchSortDirections _searchSortDirection = SearchSortDirections.Descending; //default sort by
|
||||
private SearchSortFields _searchSortField = SearchSortFields.Relevance;
|
||||
private string _keywords;
|
||||
private bool _loading;
|
||||
private SearchResults _searchResults;
|
||||
private int _currentPage = 0;
|
||||
private int _pageSize = Constants.SearchDefaultPageSize;
|
||||
private int _displayPages = 7;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
if (ModuleState.Settings.ContainsKey("PageSize"))
|
||||
{
|
||||
_pageSize = int.Parse(ModuleState.Settings["PageSize"]);
|
||||
}
|
||||
|
||||
if (PageState.QueryString.ContainsKey("s"))
|
||||
{
|
||||
_keywords = PageState.QueryString["s"];
|
||||
}
|
||||
|
||||
if (PageState.QueryString.ContainsKey("p"))
|
||||
{
|
||||
_currentPage = Convert.ToInt32(PageState.QueryString["p"]);
|
||||
if (_currentPage < 1)
|
||||
{
|
||||
_currentPage = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(_keywords))
|
||||
{
|
||||
await PerformSearch();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task KeywordsChanged(KeyboardEventArgs e)
|
||||
{
|
||||
if (e.Code == "Enter" || e.Code == "NumpadEnter")
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_keywords))
|
||||
{
|
||||
await Search();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Search()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_keywords))
|
||||
{
|
||||
AddModuleMessage(Localizer["MissingKeywords"], MessageType.Warning);
|
||||
}
|
||||
else
|
||||
{
|
||||
ClearModuleMessage();
|
||||
|
||||
|
||||
_currentPage = 0;
|
||||
await PerformSearch();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PerformSearch()
|
||||
{
|
||||
_loading = true;
|
||||
StateHasChanged();
|
||||
|
||||
var searchQuery = new SearchQuery
|
||||
{
|
||||
SiteId = PageState.Site.SiteId,
|
||||
Alias = PageState.Alias,
|
||||
User = PageState.User,
|
||||
Keywords = _keywords,
|
||||
SortDirection = _searchSortDirection,
|
||||
SortField = _searchSortField,
|
||||
PageIndex = 0,
|
||||
PageSize = int.MaxValue
|
||||
};
|
||||
|
||||
_searchResults = await SearchResultsService.SearchAsync(PageState.ModuleId, searchQuery);
|
||||
|
||||
_loading = false;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user