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 26.87 by bougault on 2022/11/17 10:51

Hide last authors
bougault 1.1 1 {{html clean="false"}}
bougault 26.64 2 <!--
bougault 26.65 3 !!! POC. Code would 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 26.69 43 this.filterUrl = new URL(`${document.location.origin}/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&favorite=true&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 26.68 54 }
bougault 26.47 55 setSearchText(e) {
56 this.searchText = e.target.value;
57 }
58 handleSearch() {
59 console.log(this.searchText);
bougault 26.72 60 if(this.searchText !== "") {
bougault 26.73 61 this.fetchWikiSearch();
bougault 26.72 62 }
bougault 26.47 63 }
bougault 26.71 64 fetchWikiSearch() {
65 // request should be built from component, not from "external" dependency. Leave it here like this for POC.
bougault 26.75 66 handleXWikiSearch(this.searchText).then(results => this.results = results);
bougault 26.71 67 }
bougault 26.64 68 buildFilter() {
69 //https://wiki-dev.ebrains.eu/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&favorite=true&roles=
70 }
bougault 26.46 71 render() {
bougault 26.49 72 return html`
73 <div>
74 <div>
75 <input @change="${this.setSearchText}" type="text" placeholder="Search..." />
76 <button @click="${this.handleSearch}">Search</button>
77 </div>
bougault 26.61 78 <div>
bougault 26.68 79 <label><input type="checkbox" @change="${() => this.toggleRoleFilter('administrator')}" .checked="${this.administratorFilter}"/> Administrator</label>
80 <label><input type="checkbox" @change="${() => this.toggleRoleFilter('editor')}" .checked="${this.editorFilter}"/> Editor</label>
81 <label><input type="checkbox" @change="${() => this.toggleRoleFilter('viewer')}" .checked="${this.viewerFilter}"/> Viewer</label>
bougault 26.61 82 </div>
bougault 26.76 83 <div>
84 ${this.results.map(result => html`
bougault 26.80 85 <div class="result">
bougault 26.78 86 <a href="#">${result.title_}</a>
bougault 26.83 87 <div class="light">${result.space}</div>
bougault 26.76 88 <div>${result.doccontent_.substring(0, 150)}
89 </div>
90 `)}
91 </div>
bougault 26.49 92 </div>
93 `
bougault 26.46 94 }
95 }
96 customElements.define('clb-unified-search', UnifiedSearch);
bougault 26.70 97
bougault 26.71 98 function handleXWikiSearch(searchString) {
99 return new Promise((resolve) => {
100 require(['jquery'], function($) {
bougault 26.74 101 const solrServiceURL = new XWiki.Document('SuggestSolrService', 'XWiki').getURL('get');
102 //contextSearchButton.addEventListener('click', function() {
bougault 26.71 103 $.post(solrServiceURL, {
104 outputSyntax: 'plain',
105 media: 'json',
106 query: [
107 'q="__INPUT__"~100',
108 'fq=type:DOCUMENT',
109 `fq=space:Collabs.*`
110 ].join('\n'),
111 input: searchString
112 }).then(resolve);
bougault 26.74 113 //});
bougault 26.70 114 });
115 });
116 }
117 });
bougault 26.45 118 </script>
bougault 1.1 119
bougault 26.46 120 <clb-unified-search></clb-unified-search>
bougault 26.62 121 <!--
bougault 1.1 122 <select id="context-search-type">
123 <option value="all">All</option>
124 <option value="collabs">Collabs</option>
125 <option value="current-collab">Current Collab</option>
126 </select>
bougault 26.37 127 <input type="text" id="context-search-text" />
128 <button id="context-search-button" class="btn btn-primary">Search</button>
bougault 26.36 129 <div class="form-group">
bougault 26.37 130 <label>
131 <input type="checkbox" data-role-filter="administrator"> Administrator
132 </label>
133 <label>
134 <input type="checkbox" data-role-filter="editor"> Editor
135 </label>
bougault 26.36 136 </div>
bougault 1.1 137
bougault 26.37 138
bougault 2.2 139 <pre id="context-search-response"></pre>
140 <script>
141 window.addEventListener('DOMContentLoaded', function() {
bougault 26.30 142 const filters = {
143 search: '',
144 offset: 0,
145 orderField: 'title',
146 order: 'asc',
147 favorite: false,
148 roles: [] // to be joined as administrator+editor+viewer
149 };
150
151 function addRole(role) {
152 if(filters.roles.indexOf(role) == -1) {
153 filters.roles.push(role);
154 }
155 }
156
157 function removeRole(role) {
bougault 26.39 158 filters.roles = filters.roles.filter(r => r !== role)
bougault 26.30 159 }
160
bougault 26.34 161 const filterInputs = document.querySelectorAll('input[data-role-filter]');
bougault 26.40 162
bougault 26.32 163 filterInputs.forEach(function(filterIpt) {
bougault 26.35 164 filterIpt.addEventListener('click', function() {
bougault 26.30 165 const role = this.getAttribute('data-role-filter');
166 if(this.checked) {
167 addRole(role);
168 } else {
169 removeRole(role);
170 }
bougault 26.42 171 runFilteredRequest();
bougault 26.30 172 })
173 });
174
bougault 26.42 175 function runFilteredRequest() {
bougault 26.43 176 const urlParams = new URLSearchParams();
bougault 26.44 177 const tmpFilters = filters;
178 tmpFilters.roles = tmpFilters.roles.join("+");
179 for(filter in tmpFilters) {
180 urlParams.set(filter, tmpFilters[filter]);
bougault 26.41 181 }
bougault 26.43 182 console.log(urlParams);
bougault 26.41 183 }
bougault 26.30 184 // https://wiki-dev.ebrains.eu/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&favorite=false&roles=administrator
bougault 2.2 185 const contextSearchType = document.getElementById("context-search-type");
186 const contextSearchText = document.getElementById("context-search-text");
187 const contextSearchResponse = document.getElementById("context-search-response");
188 const contextSearchButton = document.getElementById("context-search-button");
189 contextSearchButton.addEventListener('click', function() {
190 const context = contextSearchType.options[contextSearchType.selectedIndex].value;
bougault 2.3 191 const term = contextSearchText.value;
192 switch(context) {
193 case 'collabs':
194 document.location.href = `/bin/view/Collabs/#search=${term}`;
195 break;
196 case 'current-collab':
bougault 19.1 197 handleXWikiSearch('bougaultx');
bougault 2.3 198 break
199 default:
bougault 26.40 200 handleXWikiSearch('')
bougault 2.3 201 }
202
bougault 2.2 203 });
bougault 26.12 204 function handleXWikiSearch(space, callback) {
bougault 26.2 205 require(['jquery'], function($) {
206 var solrServiceURL = new XWiki.Document('SuggestSolrService', 'XWiki').getURL('get');
207 const contextSearchButton = document.getElementById("context-search-button");
208 const xwikiSpace = space ? `.${space}` : '';
209 contextSearchButton.addEventListener('click', function() {
210 $.post(solrServiceURL, {
211 outputSyntax: 'plain',
212 media: 'json',
213 query: [
214 'q="__INPUT__"~100',
215 'fq=type:DOCUMENT',
216 `fq=space:Collabs${xwikiSpace}.*`
217 ].join('\n'),
218 input: $('#context-search-text').val()
219 }).then(res => {
bougault 26.12 220 if(callback) {
221 callback(res).then(results => {
222 contextSearchResponse.innerText = JSON.stringify(results, null, 2);
223 })
224 } else {
225 contextSearchResponse.innerText = JSON.stringify(res, null, 2);
226 }
bougault 26.2 227 });
228 });
bougault 23.1 229 });
bougault 26.2 230 }
bougault 26.11 231 function applyAdministratorFilter(results) {
bougault 26.12 232 return new Promise((resolve, reject) => {
bougault 26.22 233 //fetch("/rest/v1/collabs?roles=administrator")
234 fetch("/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&favorite=false&roles=administrator")
bougault 26.12 235 .then(res => res.json())
bougault 26.14 236 .then(collabs => {
237 const spaces = collabs.map(collab => `Collabs.${collab.name}`);
bougault 26.25 238 const filtered = results.filter(result => spaces.some(space => result.space.startsWith(space)));
bougault 26.18 239 resolve(filtered);
bougault 26.12 240 })
241
242 })
bougault 26.2 243 }
244 });
bougault 2.2 245 </script>
bougault 26.62 246 -->
bougault 1.1 247 {{/html}}