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

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