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.70 by bougault on 2022/11/17 14:36

Hide last authors
bougault 1.1 1 {{html clean="false"}}
bougault 26.64 2 <!--
bougault 27.56 3 !!! POC. Code must be reviewed / optimized and packaged for use in production !!!
4 !!! Many optimisations can be done !!!
bougault 26.64 5 -->
bougault 26.45 6 <script type="module">
bougault 26.81 7 import {LitElement, html, css} from 'https://cdn.jsdelivr.net/gh/lit/dist@2/core/lit-core.min.js';
bougault 26.70 8 window.addEventListener('DOMContentLoaded', function() {
9
bougault 26.46 10 class UnifiedSearch extends LitElement {
bougault 26.47 11 static properties = {
bougault 26.77 12 results: { type: Array }
13 }
bougault 26.80 14 static styles = css`
bougault 27.58 15 :host > div {
bougault 27.57 16 padding-bottom: 4em;
17 }
bougault 26.80 18 .result {
19 margin-top: 1em;
20 }
bougault 26.82 21 .light {
bougault 26.86 22 color: var(--color-gray-500, #CCC);
bougault 26.82 23 font-size: 14px;
24 }
bougault 26.87 25 a {
26 color: var(--color-brand-primary, blue);
27 }
bougault 26.80 28 `;
bougault 26.49 29 constructor() {
30 super();
31 this.searchText = "";
bougault 27.7 32 this.prevSearchText = "";
bougault 26.68 33 this.administrator = false;
34 this.editor = false;
35 this.viewer = false;
36 this.rolesFilter = [];
bougault 27.24 37 this.filterUrl = new URL(`${document.location.origin}/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&limit=10000&favorite=false&roles=`);
bougault 26.75 38 this.results = [];
bougault 27.38 39 this.resultsCache = [];
bougault 26.49 40 }
bougault 27.68 41 firstUpdated() {
42 this.renderRoot.getElementById('searchInput').focus();
43 }
bougault 26.68 44 toggleRoleFilter(roleName) {
bougault 27.28 45 if(this.rolesFilter.indexOf(roleName) === -1) {
bougault 26.68 46 this.rolesFilter.push(roleName);
47 } else {
bougault 27.32 48 this.rolesFilter = this.rolesFilter.filter(role => role !== roleName);
bougault 26.68 49 }
bougault 26.69 50 this[roleName] = !this[roleName];
51 this.filterUrl.searchParams.set('roles', this.rolesFilter.join("+"));
bougault 27.8 52 this.handleSearch();
bougault 26.68 53 }
bougault 26.47 54 setSearchText(e) {
55 this.searchText = e.target.value;
56 }
bougault 27.45 57 handleSearch(textSearch) {
58 if(this.searchText !== "" || textSearch) {
bougault 27.27 59 if(this.searchText !== this.prevSearchText) {
bougault 27.53 60 this.clearResults();
bougault 27.27 61 this.prevSearchText = this.searchText;
bougault 27.47 62 this.fetchWikiSearch().then(results => {
bougault 27.48 63 this.resultsCache = results;
bougault 27.51 64 if(this.hasActiveFilters()) {
65 this.fetchFilterSearch().then(this.filterResults);
66 } else {
67 this.results = results;
68 }
bougault 27.47 69 })
bougault 27.37 70 } else if (this.searchText === this.prevSearchText && this.hasActiveFilters()) {
bougault 27.27 71 this.fetchFilterSearch().then(this.filterResults)
bougault 27.42 72 } else {
73 this.results = this.resultsCache;
bougault 27.27 74 }
bougault 27.5 75 } else {
bougault 27.43 76 this.clearResults();
bougault 27.5 77 console.log("no search text, should return filter search results if any");
bougault 27.10 78 this.fetchFilterSearch().then(collabs => {
bougault 27.7 79 console.log("Format and display collabs", collabs);
80 })
bougault 26.72 81 }
bougault 26.47 82 }
bougault 27.5 83 hasActiveFilters() {
84 // we only look at role filters for POC.
bougault 27.16 85 return this.rolesFilter.length > 0;
bougault 27.5 86 }
bougault 26.71 87 fetchWikiSearch() {
88 // request should be built from component, not from "external" dependency. Leave it here like this for POC.
bougault 27.5 89 return handleXWikiSearch(this.searchText);
bougault 26.71 90 }
bougault 27.5 91 fetchFilterSearch() {
92 return new Promise((resolve) => {
bougault 27.20 93 if(this.hasActiveFilters()) {
bougault 27.9 94 fetch(this.filterUrl).then(res => res.json()).then(resolve);
bougault 27.5 95 } else {
bougault 27.33 96 resolve([]);
bougault 27.5 97 }
98 });
99 }
bougault 27.11 100 filterResults = (filteredCollabs) => {
bougault 27.14 101 const collabSpaces = filteredCollabs.map(collab => `Collabs.${collab.name}`);
bougault 27.39 102 this.results = this.resultsCache.filter(result => collabSpaces.some(space => result.space.startsWith(space)));
bougault 27.7 103 }
bougault 27.43 104 clearResults() {
105 this.results = [];
106 this.resultsCache = [];
107 }
bougault 26.89 108 getLink(xwikiDocSpace) {
bougault 26.90 109 return `/bin/view/${xwikiDocSpace.replaceAll('.', '/')}`;
bougault 26.88 110 }
bougault 27.59 111 handleKeyPress(e) {
bougault 27.62 112 if (e.keyCode === 13) {
bougault 27.59 113 this.handleSearch(true);
114 }
115 }
bougault 26.46 116 render() {
bougault 26.49 117 return html`
118 <div>
119 <div>
bougault 27.68 120 <input id="searchInput" @change="${this.setSearchText}" type="text" placeholder="Search..." @keypress="${this.handleKeyPress}" />
bougault 27.46 121 <button @click="${() => this.handleSearch(true)}">Search</button>
bougault 26.49 122 </div>
bougault 26.61 123 <div>
bougault 26.68 124 <label><input type="checkbox" @change="${() => this.toggleRoleFilter('administrator')}" .checked="${this.administratorFilter}"/> Administrator</label>
125 <label><input type="checkbox" @change="${() => this.toggleRoleFilter('editor')}" .checked="${this.editorFilter}"/> Editor</label>
126 <label><input type="checkbox" @change="${() => this.toggleRoleFilter('viewer')}" .checked="${this.viewerFilter}"/> Viewer</label>
bougault 26.61 127 </div>
bougault 26.76 128 <div>
129 ${this.results.map(result => html`
bougault 26.80 130 <div class="result">
bougault 26.89 131 <a href="${this.getLink(result.space)}">${result.title_}</a>
bougault 26.76 132 <div>${result.doccontent_.substring(0, 150)}
bougault 26.92 133 <div class="light">${result.space.replaceAll('.', ' / ')}</div>
bougault 26.76 134 </div>
135 `)}
136 </div>
bougault 26.49 137 </div>
138 `
bougault 26.46 139 }
140 }
141 customElements.define('clb-unified-search', UnifiedSearch);
bougault 26.70 142
bougault 26.71 143 function handleXWikiSearch(searchString) {
144 return new Promise((resolve) => {
145 require(['jquery'], function($) {
bougault 26.74 146 const solrServiceURL = new XWiki.Document('SuggestSolrService', 'XWiki').getURL('get');
147 //contextSearchButton.addEventListener('click', function() {
bougault 26.71 148 $.post(solrServiceURL, {
149 outputSyntax: 'plain',
150 media: 'json',
151 query: [
152 'q="__INPUT__"~100',
153 'fq=type:DOCUMENT',
154 `fq=space:Collabs.*`
155 ].join('\n'),
156 input: searchString
157 }).then(resolve);
bougault 26.74 158 //});
bougault 26.70 159 });
160 });
161 }
162 });
bougault 26.45 163 </script>
bougault 26.46 164 <clb-unified-search></clb-unified-search>
bougault 1.1 165 {{/html}}