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

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