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.92 by bougault on 2022/11/17 11:01

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.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.62 124 <!--
bougault 1.1 125 <select id="context-search-type">
126 <option value="all">All</option>
127 <option value="collabs">Collabs</option>
128 <option value="current-collab">Current Collab</option>
129 </select>
bougault 26.37 130 <input type="text" id="context-search-text" />
131 <button id="context-search-button" class="btn btn-primary">Search</button>
bougault 26.36 132 <div class="form-group">
bougault 26.37 133 <label>
134 <input type="checkbox" data-role-filter="administrator"> Administrator
135 </label>
136 <label>
137 <input type="checkbox" data-role-filter="editor"> Editor
138 </label>
bougault 26.36 139 </div>
bougault 1.1 140
bougault 26.37 141
bougault 2.2 142 <pre id="context-search-response"></pre>
143 <script>
144 window.addEventListener('DOMContentLoaded', function() {
bougault 26.30 145 const filters = {
146 search: '',
147 offset: 0,
148 orderField: 'title',
149 order: 'asc',
150 favorite: false,
151 roles: [] // to be joined as administrator+editor+viewer
152 };
153
154 function addRole(role) {
155 if(filters.roles.indexOf(role) == -1) {
156 filters.roles.push(role);
157 }
158 }
159
160 function removeRole(role) {
bougault 26.39 161 filters.roles = filters.roles.filter(r => r !== role)
bougault 26.30 162 }
163
bougault 26.34 164 const filterInputs = document.querySelectorAll('input[data-role-filter]');
bougault 26.40 165
bougault 26.32 166 filterInputs.forEach(function(filterIpt) {
bougault 26.35 167 filterIpt.addEventListener('click', function() {
bougault 26.30 168 const role = this.getAttribute('data-role-filter');
169 if(this.checked) {
170 addRole(role);
171 } else {
172 removeRole(role);
173 }
bougault 26.42 174 runFilteredRequest();
bougault 26.30 175 })
176 });
177
bougault 26.42 178 function runFilteredRequest() {
bougault 26.43 179 const urlParams = new URLSearchParams();
bougault 26.44 180 const tmpFilters = filters;
181 tmpFilters.roles = tmpFilters.roles.join("+");
182 for(filter in tmpFilters) {
183 urlParams.set(filter, tmpFilters[filter]);
bougault 26.41 184 }
bougault 26.43 185 console.log(urlParams);
bougault 26.41 186 }
bougault 26.30 187 // https://wiki-dev.ebrains.eu/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&favorite=false&roles=administrator
bougault 2.2 188 const contextSearchType = document.getElementById("context-search-type");
189 const contextSearchText = document.getElementById("context-search-text");
190 const contextSearchResponse = document.getElementById("context-search-response");
191 const contextSearchButton = document.getElementById("context-search-button");
192 contextSearchButton.addEventListener('click', function() {
193 const context = contextSearchType.options[contextSearchType.selectedIndex].value;
bougault 2.3 194 const term = contextSearchText.value;
195 switch(context) {
196 case 'collabs':
197 document.location.href = `/bin/view/Collabs/#search=${term}`;
198 break;
199 case 'current-collab':
bougault 19.1 200 handleXWikiSearch('bougaultx');
bougault 2.3 201 break
202 default:
bougault 26.40 203 handleXWikiSearch('')
bougault 2.3 204 }
205
bougault 2.2 206 });
bougault 26.12 207 function handleXWikiSearch(space, callback) {
bougault 26.2 208 require(['jquery'], function($) {
209 var solrServiceURL = new XWiki.Document('SuggestSolrService', 'XWiki').getURL('get');
210 const contextSearchButton = document.getElementById("context-search-button");
211 const xwikiSpace = space ? `.${space}` : '';
212 contextSearchButton.addEventListener('click', function() {
213 $.post(solrServiceURL, {
214 outputSyntax: 'plain',
215 media: 'json',
216 query: [
217 'q="__INPUT__"~100',
218 'fq=type:DOCUMENT',
219 `fq=space:Collabs${xwikiSpace}.*`
220 ].join('\n'),
221 input: $('#context-search-text').val()
222 }).then(res => {
bougault 26.12 223 if(callback) {
224 callback(res).then(results => {
225 contextSearchResponse.innerText = JSON.stringify(results, null, 2);
226 })
227 } else {
228 contextSearchResponse.innerText = JSON.stringify(res, null, 2);
229 }
bougault 26.2 230 });
231 });
bougault 23.1 232 });
bougault 26.2 233 }
bougault 26.11 234 function applyAdministratorFilter(results) {
bougault 26.12 235 return new Promise((resolve, reject) => {
bougault 26.22 236 //fetch("/rest/v1/collabs?roles=administrator")
237 fetch("/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&favorite=false&roles=administrator")
bougault 26.12 238 .then(res => res.json())
bougault 26.14 239 .then(collabs => {
240 const spaces = collabs.map(collab => `Collabs.${collab.name}`);
bougault 26.25 241 const filtered = results.filter(result => spaces.some(space => result.space.startsWith(space)));
bougault 26.18 242 resolve(filtered);
bougault 26.12 243 })
244
245 })
bougault 26.2 246 }
247 });
bougault 2.2 248 </script>
bougault 26.62 249 -->
bougault 1.1 250 {{/html}}