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

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