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

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