Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 97 additions & 74 deletions app/components/learningmaterial-search.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ export default class LearningmaterialSearchComponent extends Component {
@tracked searchResultsPerPage = 50;
@tracked hasMoreSearchResults = false;
@tracked searchReturned = false;
@tracked showResults = false;

search = task({ restartable: true }, async (query) => {
this.showResults = true;
if (query.trim() === '') {
this.searchReturned = false;
this.searchPage = 1;
Expand Down Expand Up @@ -61,6 +63,20 @@ export default class LearningmaterialSearchComponent extends Component {
this.query = '';
}

@action
focus() {
this.showResults = true;
}

@action
focusOut(event) {
const newFocus = event.relatedTarget;

if (!event.currentTarget.contains(newFocus)) {
this.showResults = false;
}
}

searchMore = task({ drop: true }, async () => {
const results = await this.store.query('learning-material', {
q: this.query,
Expand Down Expand Up @@ -88,92 +104,99 @@ export default class LearningmaterialSearchComponent extends Component {
element.style.width = `${element.previousElementSibling.clientWidth}px`;
});
<template>
<div class="learningmaterial-search" data-test-learningmaterial-search>
<div
class="learningmaterial-search"
{{on "focusout" this.focusOut}}
data-test-learningmaterial-search
>
<SearchBox
placeholder={{t "general.searchPlaceholder"}}
@liveSearch={{true}}
@search={{perform this.search}}
@clear={{this.clear}}
{{on "focus" this.focus}}
/>
{{#if this.search.isRunning}}
<ul class="lm-search-results" {{this.resultsAppear}}>
<li>
{{t "general.currentlySearchingPrompt"}}
</li>
</ul>
{{else if this.searchResults.length}}
<ul class="lm-search-results" {{this.resultsAppear}}>
{{#each this.searchResults as |learningMaterial|}}
<li class={{if (includes learningMaterial.id @currentMaterialIds) "disabled"}}>
<button
class="result"
type="button"
{{on "click" (perform this.addLearningMaterial learningMaterial)}}
>
<div class="learning-material-title">
<LmTypeIcon
@type={{learningMaterial.type}}
@mimetype={{learningMaterial.mimetype}}
/>
<span data-test-title>
{{learningMaterial.title}}
</span>
</div>
<span class="learning-material-description">
{{! template-lint-disable no-triple-curlies }}
{{{learningMaterial.description}}}
</span>
{{#if learningMaterial.status.title}}
<span class="learning-material-status">
{{learningMaterial.status.title}}
{{#if this.showResults}}
{{#if this.search.isRunning}}
<ul class="lm-search-results" {{this.resultsAppear}}>
<li>
{{t "general.currentlySearchingPrompt"}}
</li>
</ul>
{{else if this.searchResults.length}}
<ul class="lm-search-results" {{this.resultsAppear}}>
{{#each this.searchResults as |learningMaterial|}}
<li class={{if (includes learningMaterial.id @currentMaterialIds) "disabled"}}>
<button
class="result"
type="button"
{{on "click" (perform this.addLearningMaterial learningMaterial)}}
>
<div class="learning-material-title">
<LmTypeIcon
@type={{learningMaterial.type}}
@mimetype={{learningMaterial.mimetype}}
/>
<span data-test-title>
{{learningMaterial.title}}
</span>
</div>
<span class="learning-material-description">
{{! template-lint-disable no-triple-curlies }}
{{{learningMaterial.description}}}
</span>
{{/if}}
<ul class="learning-material-properties">
<li>
{{t "general.owner"}}:
{{learningMaterial.owningUser.fullName}}
</li>
{{#if learningMaterial.originalAuthor}}
{{#if learningMaterial.status.title}}
<span class="learning-material-status">
{{learningMaterial.status.title}}
</span>
{{/if}}
<ul class="learning-material-properties">
<li>
{{t "general.contentAuthor"}}:
{{learningMaterial.originalAuthor}}
{{t "general.owner"}}:
{{learningMaterial.owningUser.fullName}}
</li>
{{#if learningMaterial.originalAuthor}}
<li>
{{t "general.contentAuthor"}}:
{{learningMaterial.originalAuthor}}
</li>
{{/if}}
<li>
{{t "general.uploadDate"}}:
{{formatDate
learningMaterial.uploadDate
day="2-digit"
month="2-digit"
year="numeric"
}}
</li>
</ul>
</button>
</li>
{{/each}}
{{#if this.hasMoreSearchResults}}
<li>
<button
disabled={{if this.searchMore.isRunning true}}
type="button"
data-test-show-more
{{on "click" (perform this.searchMore)}}
>
{{#if this.searchMore.isRunning}}
<LoadingSpinner />
{{/if}}
<li>
{{t "general.uploadDate"}}:
{{formatDate
learningMaterial.uploadDate
day="2-digit"
month="2-digit"
year="numeric"
}}
</li>
</ul>
</button>
</li>
{{/each}}
{{#if this.hasMoreSearchResults}}
{{t "general.showMore"}}
</button>
</li>
{{/if}}
</ul>
{{else if this.searchReturned}}
<ul class="lm-search-results" {{this.resultsAppear}}>
<li>
<button
disabled={{if this.searchMore.isRunning true}}
type="button"
data-test-show-more
{{on "click" (perform this.searchMore)}}
>
{{#if this.searchMore.isRunning}}
<LoadingSpinner />
{{/if}}
{{t "general.showMore"}}
</button>
{{t "general.noSearchResultsPrompt"}}
</li>
{{/if}}
</ul>
{{else if this.searchReturned}}
<ul class="lm-search-results" {{this.resultsAppear}}>
<li>
{{t "general.noSearchResultsPrompt"}}
</li>
</ul>
</ul>
{{/if}}
{{/if}}
</div>
</template>
Expand Down
1 change: 1 addition & 0 deletions app/components/search-box.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export default class SearchBoxComponent extends Component {
maxlength={{this.maxlength}}
{{on "input" this.update}}
{{on "keyup" this.keyUp}}
...attributes
/>
<button
aria-label={{t "general.search"}}
Expand Down
Loading