import {LitElement, html, css} from 'https://cdn.jsdelivr.net/gh/lit/dist@2/core/lit-core.min.js';
window.addEventListener('DOMContentLoaded', function() {
class UnifiedSearch extends LitElement {
/*
static properties = {
searchText: { type: String },
favoritesFilter: { type: Boolean },
administratorFilter: { type: Boolean },
editorFilter: { type: Boolean },
viewerFilter: { type: Boolean },
publicFilter: { type: Boolean },
privateFilter: { type: Boolean },
}
*/
static properties = {
results: { type: Array }
}
static styles = css`
.result {
margin-top: 1em;
}
.light {
color: var(--color-gray-500, #CCC);
font-size: 14px;
}
a {
color: var(--color-brand-primary, blue);
}
`;
constructor() {
super();
this.searchText = "";
this.administrator = false;
this.editor = false;
this.viewer = false;
this.rolesFilter = [];
this.filterUrl = new URL(`${document.location.origin}/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&favorite=&roles=`);
this.results = [];
}
toggleRoleFilter(roleName) {
if(this.rolesFilter.indexOf(roleName) == -1) {
this.rolesFilter.push(roleName);
} else {
this.rolesFilter.filter(role => role !== roleName);
}
this[roleName] = !this[roleName];
this.filterUrl.searchParams.set('roles', this.rolesFilter.join("+"));
console.log(this.filterUrl.toString());
}
setSearchText(e) {
this.searchText = e.target.value;
}
handleSearch() {
console.log(this.searchText);
if(this.searchText !== "") {
this.fetchWikiSearch();
}
}
fetchWikiSearch() {
// request should be built from component, not from "external" dependency. Leave it here like this for POC.
handleXWikiSearch(this.searchText).then(results => this.results = results);
}
getLink(xwikiDocSpace) {
return `/bin/view/${xwikiDocSpace.replaceAll('.', '/')}`;
}
render() {
return html`
${this.results.map(result => html`
${result.title_}
${result.doccontent_.substring(0, 150)}
${result.space.replaceAll('.', ' / ')}
`)}
`
}
}
customElements.define('clb-unified-search', UnifiedSearch);
function handleXWikiSearch(searchString) {
return new Promise((resolve) => {
require(['jquery'], function($) {
const solrServiceURL = new XWiki.Document('SuggestSolrService', 'XWiki').getURL('get');
//contextSearchButton.addEventListener('click', function() {
$.post(solrServiceURL, {
outputSyntax: 'plain',
media: 'json',
query: [
'q="__INPUT__"~100',
'fq=type:DOCUMENT',
`fq=space:Collabs.*`
].join('\n'),
input: searchString
}).then(resolve);
//});
});
});
}
});