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.65 by bougault on 2022/11/17 09:59

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