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

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