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.81 by bougault on 2022/11/17 10:47

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