Failed to execute template. Cause: [Access denied when checking [script] access to [xwiki:Collaboratory.UX.HbpSkin.WebHome] for user [xwiki:XWiki.Admin]]. Click on this message for details.

Context Search - HBP Wiki

IAM21 instance, do not create collab nor modify a team, your changes will be lost


Wiki source code of Context Search

Version 27.3 by bougault on 2022/11/17 12:51

Hide last authors
bougault 1.1 1 {{html clean="false"}}
bougault 26.64 2 <!--
bougault 26.93 3 !!! POC. Code must be reviewed / optimized and packaged for use in production !!!
bougault 26.64 4 -->
bougault 26.45 5 <script type="module">
bougault 26.81 6 import {LitElement, html, css} from 'https://cdn.jsdelivr.net/gh/lit/dist@2/core/lit-core.min.js';
bougault 26.70 7 window.addEventListener('DOMContentLoaded', function() {
8
bougault 26.46 9 class UnifiedSearch extends LitElement {
bougault 26.65 10 /*
bougault 26.47 11 static properties = {
bougault 26.49 12 searchText: { type: String },
bougault 26.50 13 favoritesFilter: { type: Boolean },
14 administratorFilter: { type: Boolean },
15 editorFilter: { type: Boolean },
16 viewerFilter: { type: Boolean },
17 publicFilter: { type: Boolean },
18 privateFilter: { type: Boolean },
bougault 26.47 19 }
bougault 26.65 20 */
bougault 26.77 21 static properties = {
22 results: { type: Array }
23 }
bougault 26.80 24 static styles = css`
25 .result {
26 margin-top: 1em;
27 }
bougault 26.82 28 .light {
bougault 26.86 29 color: var(--color-gray-500, #CCC);
bougault 26.82 30 font-size: 14px;
31 }
bougault 26.87 32 a {
33 color: var(--color-brand-primary, blue);
34 }
bougault 26.80 35 `;
bougault 26.49 36 constructor() {
37 super();
38 this.searchText = "";
bougault 26.68 39 this.administrator = false;
40 this.editor = false;
41 this.viewer = false;
42 this.rolesFilter = [];
bougault 27.3 43 this.filterUrl = new URL(`${document.location.origin}/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&favorite=&roles=`);
bougault 26.75 44 this.results = [];
bougault 26.49 45 }
bougault 26.68 46 toggleRoleFilter(roleName) {
47 if(this.rolesFilter.indexOf(roleName) == -1) {
48 this.rolesFilter.push(roleName);
49 } else {
50 this.rolesFilter.filter(role => role !== roleName);
51 }
bougault 26.69 52 this[roleName] = !this[roleName];
53 this.filterUrl.searchParams.set('roles', this.rolesFilter.join("+"));
bougault 27.2 54 console.log(this.filterUrl.toString());
bougault 26.68 55 }
bougault 26.47 56 setSearchText(e) {
57 this.searchText = e.target.value;
58 }
59 handleSearch() {
60 console.log(this.searchText);
bougault 26.72 61 if(this.searchText !== "") {
bougault 26.73 62 this.fetchWikiSearch();
bougault 26.72 63 }
bougault 26.47 64 }
bougault 26.71 65 fetchWikiSearch() {
66 // request should be built from component, not from "external" dependency. Leave it here like this for POC.
bougault 26.75 67 handleXWikiSearch(this.searchText).then(results => this.results = results);
bougault 26.71 68 }
bougault 27.2 69
70
bougault 26.89 71 getLink(xwikiDocSpace) {
bougault 26.90 72 return `/bin/view/${xwikiDocSpace.replaceAll('.', '/')}`;
bougault 26.88 73 }
bougault 26.46 74 render() {
bougault 26.49 75 return html`
76 <div>
77 <div>
78 <input @change="${this.setSearchText}" type="text" placeholder="Search..." />
79 <button @click="${this.handleSearch}">Search</button>
80 </div>
bougault 26.61 81 <div>
bougault 26.68 82 <label><input type="checkbox" @change="${() => this.toggleRoleFilter('administrator')}" .checked="${this.administratorFilter}"/> Administrator</label>
83 <label><input type="checkbox" @change="${() => this.toggleRoleFilter('editor')}" .checked="${this.editorFilter}"/> Editor</label>
84 <label><input type="checkbox" @change="${() => this.toggleRoleFilter('viewer')}" .checked="${this.viewerFilter}"/> Viewer</label>
bougault 26.61 85 </div>
bougault 26.76 86 <div>
87 ${this.results.map(result => html`
bougault 26.80 88 <div class="result">
bougault 26.89 89 <a href="${this.getLink(result.space)}">${result.title_}</a>
bougault 26.76 90 <div>${result.doccontent_.substring(0, 150)}
bougault 26.92 91 <div class="light">${result.space.replaceAll('.', ' / ')}</div>
bougault 26.76 92 </div>
93 `)}
94 </div>
bougault 26.49 95 </div>
96 `
bougault 26.46 97 }
98 }
99 customElements.define('clb-unified-search', UnifiedSearch);
bougault 26.70 100
bougault 26.71 101 function handleXWikiSearch(searchString) {
102 return new Promise((resolve) => {
103 require(['jquery'], function($) {
bougault 26.74 104 const solrServiceURL = new XWiki.Document('SuggestSolrService', 'XWiki').getURL('get');
105 //contextSearchButton.addEventListener('click', function() {
bougault 26.71 106 $.post(solrServiceURL, {
107 outputSyntax: 'plain',
108 media: 'json',
109 query: [
110 'q="__INPUT__"~100',
111 'fq=type:DOCUMENT',
112 `fq=space:Collabs.*`
113 ].join('\n'),
114 input: searchString
115 }).then(resolve);
bougault 26.74 116 //});
bougault 26.70 117 });
118 });
119 }
120 });
bougault 26.45 121 </script>
bougault 1.1 122
bougault 26.46 123 <clb-unified-search></clb-unified-search>
bougault 26.94 124
bougault 1.1 125 {{/html}}