2019-09-07 21:54:44 +02:00
|
|
|
import html from 'choo/html';
|
|
|
|
|
|
|
|
import { SearchController } from './controller'; // The controller for this view, where processing should happen.
|
2019-09-12 01:34:11 +02:00
|
|
|
import { resultDetails } from './resultDetails';
|
2019-09-26 18:39:08 +02:00
|
|
|
import { modal } from '../partials/modal';
|
2019-09-07 21:54:44 +02:00
|
|
|
|
|
|
|
// This is the view function that is exported and used in the view manager.
|
2019-09-16 20:32:53 +02:00
|
|
|
export const searchView = (state, emit, i18n) => {
|
2019-09-25 22:41:56 +02:00
|
|
|
const controller = new SearchController(state, emit, i18n);
|
2019-09-16 20:32:53 +02:00
|
|
|
const { __ } = controller.i18n;
|
2019-09-07 21:54:44 +02:00
|
|
|
|
2019-09-26 00:41:45 +02:00
|
|
|
if (controller.hasQuery && controller.queryIsNew) {
|
2019-09-25 22:41:56 +02:00
|
|
|
controller.search();
|
2019-09-26 01:02:14 +02:00
|
|
|
} else if (controller.state.lastSearch !== '') {
|
|
|
|
controller.appState.query.for = controller.state.lastSearch;
|
2019-09-10 01:09:25 +02:00
|
|
|
}
|
2019-09-07 21:54:44 +02:00
|
|
|
|
|
|
|
// Returning an array in a view allows non-shared parent HTML elements.
|
|
|
|
// This one doesn't have the problem right now, but it's good to remember.
|
|
|
|
return [
|
2019-09-26 00:41:45 +02:00
|
|
|
html`<h1 class="title">${__('search.header')}</h1>`,
|
|
|
|
|
|
|
|
html`<section class="flex">
|
|
|
|
<label class="three-fourth">
|
|
|
|
<input type="text" name="search"
|
|
|
|
aria-label=${i18n.__('search.placeholder')}
|
|
|
|
placeholder=${i18n.__('search.placeholder')}
|
|
|
|
value=${controller.state.lastSearch}
|
|
|
|
onchange=${e => {
|
|
|
|
emit('pushState', '/search?for=' + encodeURIComponent(e.target.value.trim()));
|
|
|
|
}}
|
|
|
|
${!controller.doneSearching ? 'disabled' : null}
|
|
|
|
>
|
|
|
|
</label>
|
2019-09-26 01:02:14 +02:00
|
|
|
<button class="fourth" style="margin-top:0;height:2.1em;"
|
|
|
|
onclick=${() => emit('pushState', '/search?for=' + encodeURIComponent(controller.appState.query.for))}
|
|
|
|
${!controller.doneSearching ? 'disabled' : null}
|
|
|
|
>
|
2019-09-26 00:41:45 +02:00
|
|
|
${!controller.doneSearching
|
|
|
|
? html`<i class="icon-loading animate-spin"></i>`
|
|
|
|
: __('search.button_text')
|
2019-09-16 20:47:47 +02:00
|
|
|
}
|
2019-09-26 00:41:45 +02:00
|
|
|
</button>
|
|
|
|
</section>`,
|
2019-09-11 01:16:01 +02:00
|
|
|
|
2019-09-26 18:39:08 +02:00
|
|
|
// Search Options Section
|
2019-09-26 20:25:36 +02:00
|
|
|
html`<section class="flex one two-700">
|
|
|
|
<div>
|
|
|
|
${modal('searchSourceInfo', controller, [
|
|
|
|
html`<p>
|
2019-09-26 21:25:25 +02:00
|
|
|
${__('search.search_source_help_text')}
|
2019-09-26 20:25:36 +02:00
|
|
|
</p>`,
|
|
|
|
html`<ul>
|
|
|
|
<li>
|
|
|
|
<a href="https://inventaire.io" target="_blank">
|
2019-09-26 18:39:08 +02:00
|
|
|
Inventaire
|
2019-09-26 20:25:36 +02:00
|
|
|
</a>
|
2019-09-26 21:25:25 +02:00
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
${__('search.search_source_help_inventaire')}
|
|
|
|
</li>
|
|
|
|
</ul>
|
2019-09-26 20:25:36 +02:00
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<a href="https://openlibrary.org" target="_blank">
|
2019-09-26 18:39:08 +02:00
|
|
|
Open Library
|
2019-09-26 20:25:36 +02:00
|
|
|
</a>
|
2019-09-26 21:25:25 +02:00
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
${__('search.search_source_help_openLibrary')}
|
|
|
|
</li>
|
|
|
|
</ul>
|
2019-09-26 20:25:36 +02:00
|
|
|
</li>
|
|
|
|
</ul>`,
|
|
|
|
], {
|
2019-09-26 21:25:25 +02:00
|
|
|
buttonText: __('search.search_source_help_button'),
|
2019-09-26 20:25:36 +02:00
|
|
|
buttonClasses: 'small marginless pseudo button pull-right',
|
2019-09-26 21:25:25 +02:00
|
|
|
headerText: __('search.search_source_help_header'),
|
2019-09-26 20:25:36 +02:00
|
|
|
})}
|
|
|
|
<label>
|
2019-09-26 21:25:25 +02:00
|
|
|
${__('search.search_source_label')}
|
|
|
|
|
2019-09-26 20:25:36 +02:00
|
|
|
<select onchange=${event => {
|
|
|
|
controller.state.searchSource = event.target.value;
|
|
|
|
}}>
|
|
|
|
<option value="inventaire" ${controller.state.searchSource === 'inventaire' ? 'selected' : null}>
|
|
|
|
Inventaire
|
|
|
|
</option>
|
|
|
|
<option value="openLibrary" ${controller.state.searchSource === 'openLibrary' ? 'selected' : null}>
|
|
|
|
Open Library
|
|
|
|
</option>
|
|
|
|
</select>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
<div>
|
2019-09-26 21:25:25 +02:00
|
|
|
${__('search.search_by_label')}<br>
|
|
|
|
|
2019-09-26 20:25:36 +02:00
|
|
|
<label>
|
|
|
|
<input type="radio" name="searchBy" value="title"
|
|
|
|
${controller.state.searchBy === 'title' ? 'checked' : null}
|
|
|
|
onchange=${(event) => {
|
|
|
|
if (event.target.checked) {
|
|
|
|
controller.state.searchBy = event.target.value;
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
2019-09-26 21:25:25 +02:00
|
|
|
<span class="checkable">
|
|
|
|
${__('search.search_by_title')}
|
|
|
|
</span>
|
2019-09-26 20:25:36 +02:00
|
|
|
</label>
|
|
|
|
<label>
|
|
|
|
<input type="radio" name="searchBy" value="author"
|
|
|
|
${controller.state.searchBy === 'author' ? 'checked' : null}
|
|
|
|
onchange=${(event) => {
|
|
|
|
if (event.target.checked) {
|
|
|
|
controller.state.searchBy = event.target.value;
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
2019-09-26 21:25:25 +02:00
|
|
|
<span class="checkable">
|
|
|
|
${__('search.search_by_author')}
|
|
|
|
</span>
|
2019-09-26 01:19:35 +02:00
|
|
|
</label>
|
2019-09-26 20:25:36 +02:00
|
|
|
</div>
|
2019-09-26 00:41:45 +02:00
|
|
|
</section>`,
|
|
|
|
|
2019-09-26 18:51:52 +02:00
|
|
|
// Search Results section
|
2019-09-26 00:41:45 +02:00
|
|
|
html`<section>
|
|
|
|
<h2>
|
|
|
|
${controller.hasQuery && !controller.doneSearching
|
|
|
|
? html`<span>${__('search.loading')}</span> <i class="icon-loading animate-spin"></i>`
|
|
|
|
: null
|
2019-09-16 21:09:40 +02:00
|
|
|
}
|
2019-09-26 00:41:45 +02:00
|
|
|
</h2>
|
|
|
|
|
2019-09-26 18:51:52 +02:00
|
|
|
${controller.hasQuery && controller.doneSearching && controller.results < 1
|
2019-09-26 00:41:45 +02:00
|
|
|
? [
|
|
|
|
html`<h3>${__('search.no_results')}</h3>`,
|
|
|
|
html`<a class="button" href="https://wiki.inventaire.io/wiki/How-to-contribute" target="_blank">
|
|
|
|
${__('search.no_results_suggestion')}
|
|
|
|
</a>`
|
|
|
|
]
|
2019-09-26 18:51:52 +02:00
|
|
|
: controller.results.map(result => {
|
2019-09-26 00:41:45 +02:00
|
|
|
return html`<article class="flex search-result">
|
|
|
|
<header class="two-third-800 half-500">
|
|
|
|
<h3 class="title">${result.name}</h3>
|
|
|
|
${result.description ? html`<h4 class="subtitle">${result.description}</h4>` : null}
|
|
|
|
</header>
|
|
|
|
<footer class="third-800 half-500">
|
|
|
|
${resultDetails(controller, result, emit)}
|
|
|
|
</footer>
|
|
|
|
</article>`;
|
|
|
|
})
|
|
|
|
}
|
2019-09-07 21:54:44 +02:00
|
|
|
</section>`,
|
|
|
|
];
|
|
|
|
}
|