Wiki source code of Context Search
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
| |
1.1 | 1 | {{html clean="false"}} |
| |
26.64 | 2 | <!-- |
| |
26.65 | 3 | !!! POC. Code would must be reviewed / optimized and packaged for use in production !!! |
| |
26.64 | 4 | --> |
| |
26.45 | 5 | <script type="module"> |
| |
26.81 | 6 | import {LitElement, html, css} from 'https://cdn.jsdelivr.net/gh/lit/dist@2/core/lit-core.min.js'; |
| |
26.70 | 7 | window.addEventListener('DOMContentLoaded', function() { |
| 8 | |||
| |
26.46 | 9 | class UnifiedSearch extends LitElement { |
| |
26.65 | 10 | /* |
| |
26.47 | 11 | static properties = { |
| |
26.49 | 12 | searchText: { type: String }, |
| |
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 }, | ||
| |
26.47 | 19 | } |
| |
26.65 | 20 | */ |
| |
26.77 | 21 | static properties = { |
| 22 | results: { type: Array } | ||
| 23 | } | ||
| |
26.80 | 24 | static styles = css` |
| 25 | .result { | ||
| 26 | margin-top: 1em; | ||
| 27 | } | ||
| |
26.82 | 28 | .light { |
| |
26.86 | 29 | color: var(--color-gray-500, #CCC); |
| |
26.82 | 30 | font-size: 14px; |
| 31 | } | ||
| |
26.87 | 32 | a { |
| 33 | color: var(--color-brand-primary, blue); | ||
| 34 | } | ||
| |
26.80 | 35 | `; |
| |
26.49 | 36 | constructor() { |
| 37 | super(); | ||
| 38 | this.searchText = ""; | ||
| |
26.68 | 39 | this.administrator = false; |
| 40 | this.editor = false; | ||
| 41 | this.viewer = false; | ||
| 42 | this.rolesFilter = []; | ||
| |
26.69 | 43 | this.filterUrl = new URL(`${document.location.origin}/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&favorite=true&roles=`); |
| |
26.75 | 44 | this.results = []; |
| |
26.49 | 45 | } |
| |
26.68 | 46 | toggleRoleFilter(roleName) { |
| 47 | if(this.rolesFilter.indexOf(roleName) == -1) { | ||
| 48 | this.rolesFilter.push(roleName); | ||
| 49 | } else { | ||
| 50 | this.rolesFilter.filter(role => role !== roleName); | ||
| 51 | } | ||
| |
26.69 | 52 | this[roleName] = !this[roleName]; |
| 53 | this.filterUrl.searchParams.set('roles', this.rolesFilter.join("+")); | ||
| |
26.68 | 54 | } |
| |
26.47 | 55 | setSearchText(e) { |
| 56 | this.searchText = e.target.value; | ||
| 57 | } | ||
| 58 | handleSearch() { | ||
| 59 | console.log(this.searchText); | ||
| |
26.72 | 60 | if(this.searchText !== "") { |
| |
26.73 | 61 | this.fetchWikiSearch(); |
| |
26.72 | 62 | } |
| |
26.47 | 63 | } |
| |
26.71 | 64 | fetchWikiSearch() { |
| 65 | // request should be built from component, not from "external" dependency. Leave it here like this for POC. | ||
| |
26.75 | 66 | handleXWikiSearch(this.searchText).then(results => this.results = results); |
| |
26.71 | 67 | } |
| |
26.64 | 68 | buildFilter() { |
| 69 | //https://wiki-dev.ebrains.eu/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&favorite=true&roles= | ||
| 70 | } | ||
| |
26.88 | 71 | getLink(xwikiDocId) { |
| 72 | return `/bin/view/${xwikiDocId.replace('.', '/')}`; | ||
| 73 | } | ||
| |
26.46 | 74 | render() { |
| |
26.49 | 75 | return html` |
| 76 | <div> | ||
| 77 | <div> | ||
| 78 | <input @change="${this.setSearchText}" type="text" placeholder="Search..." /> | ||
| 79 | <button @click="${this.handleSearch}">Search</button> | ||
| 80 | </div> | ||
| |
26.61 | 81 | <div> |
| |
26.68 | 82 | <label><input type="checkbox" @change="${() => this.toggleRoleFilter('administrator')}" .checked="${this.administratorFilter}"/> Administrator</label> |
| 83 | <label><input type="checkbox" @change="${() => this.toggleRoleFilter('editor')}" .checked="${this.editorFilter}"/> Editor</label> | ||
| 84 | <label><input type="checkbox" @change="${() => this.toggleRoleFilter('viewer')}" .checked="${this.viewerFilter}"/> Viewer</label> | ||
| |
26.61 | 85 | </div> |
| |
26.76 | 86 | <div> |
| 87 | ${this.results.map(result => html` | ||
| |
26.80 | 88 | <div class="result"> |
| |
26.88 | 89 | <a href="${this.getLink(result.id)}">${result.title_}</a> |
| |
26.83 | 90 | <div class="light">${result.space}</div> |
| |
26.76 | 91 | <div>${result.doccontent_.substring(0, 150)} |
| 92 | </div> | ||
| 93 | `)} | ||
| 94 | </div> | ||
| |
26.49 | 95 | </div> |
| 96 | ` | ||
| |
26.46 | 97 | } |
| 98 | } | ||
| 99 | customElements.define('clb-unified-search', UnifiedSearch); | ||
| |
26.70 | 100 | |
| |
26.71 | 101 | function handleXWikiSearch(searchString) { |
| 102 | return new Promise((resolve) => { | ||
| 103 | require(['jquery'], function($) { | ||
| |
26.74 | 104 | const solrServiceURL = new XWiki.Document('SuggestSolrService', 'XWiki').getURL('get'); |
| 105 | //contextSearchButton.addEventListener('click', function() { | ||
| |
26.71 | 106 | $.post(solrServiceURL, { |
| 107 | outputSyntax: 'plain', | ||
| 108 | media: 'json', | ||
| 109 | query: [ | ||
| 110 | 'q="__INPUT__"~100', | ||
| 111 | 'fq=type:DOCUMENT', | ||
| 112 | `fq=space:Collabs.*` | ||
| 113 | ].join('\n'), | ||
| 114 | input: searchString | ||
| 115 | }).then(resolve); | ||
| |
26.74 | 116 | //}); |
| |
26.70 | 117 | }); |
| 118 | }); | ||
| 119 | } | ||
| 120 | }); | ||
| |
26.45 | 121 | </script> |
| |
1.1 | 122 | |
| |
26.46 | 123 | <clb-unified-search></clb-unified-search> |
| |
26.62 | 124 | <!-- |
| |
1.1 | 125 | <select id="context-search-type"> |
| 126 | <option value="all">All</option> | ||
| 127 | <option value="collabs">Collabs</option> | ||
| 128 | <option value="current-collab">Current Collab</option> | ||
| 129 | </select> | ||
| |
26.37 | 130 | <input type="text" id="context-search-text" /> |
| 131 | <button id="context-search-button" class="btn btn-primary">Search</button> | ||
| |
26.36 | 132 | <div class="form-group"> |
| |
26.37 | 133 | <label> |
| 134 | <input type="checkbox" data-role-filter="administrator"> Administrator | ||
| 135 | </label> | ||
| 136 | <label> | ||
| 137 | <input type="checkbox" data-role-filter="editor"> Editor | ||
| 138 | </label> | ||
| |
26.36 | 139 | </div> |
| |
1.1 | 140 | |
| |
26.37 | 141 | |
| |
2.2 | 142 | <pre id="context-search-response"></pre> |
| 143 | <script> | ||
| 144 | window.addEventListener('DOMContentLoaded', function() { | ||
| |
26.30 | 145 | const filters = { |
| 146 | search: '', | ||
| 147 | offset: 0, | ||
| 148 | orderField: 'title', | ||
| 149 | order: 'asc', | ||
| 150 | favorite: false, | ||
| 151 | roles: [] // to be joined as administrator+editor+viewer | ||
| 152 | }; | ||
| 153 | |||
| 154 | function addRole(role) { | ||
| 155 | if(filters.roles.indexOf(role) == -1) { | ||
| 156 | filters.roles.push(role); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | function removeRole(role) { | ||
| |
26.39 | 161 | filters.roles = filters.roles.filter(r => r !== role) |
| |
26.30 | 162 | } |
| 163 | |||
| |
26.34 | 164 | const filterInputs = document.querySelectorAll('input[data-role-filter]'); |
| |
26.40 | 165 | |
| |
26.32 | 166 | filterInputs.forEach(function(filterIpt) { |
| |
26.35 | 167 | filterIpt.addEventListener('click', function() { |
| |
26.30 | 168 | const role = this.getAttribute('data-role-filter'); |
| 169 | if(this.checked) { | ||
| 170 | addRole(role); | ||
| 171 | } else { | ||
| 172 | removeRole(role); | ||
| 173 | } | ||
| |
26.42 | 174 | runFilteredRequest(); |
| |
26.30 | 175 | }) |
| 176 | }); | ||
| 177 | |||
| |
26.42 | 178 | function runFilteredRequest() { |
| |
26.43 | 179 | const urlParams = new URLSearchParams(); |
| |
26.44 | 180 | const tmpFilters = filters; |
| 181 | tmpFilters.roles = tmpFilters.roles.join("+"); | ||
| 182 | for(filter in tmpFilters) { | ||
| 183 | urlParams.set(filter, tmpFilters[filter]); | ||
| |
26.41 | 184 | } |
| |
26.43 | 185 | console.log(urlParams); |
| |
26.41 | 186 | } |
| |
26.30 | 187 | // https://wiki-dev.ebrains.eu/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&favorite=false&roles=administrator |
| |
2.2 | 188 | const contextSearchType = document.getElementById("context-search-type"); |
| 189 | const contextSearchText = document.getElementById("context-search-text"); | ||
| 190 | const contextSearchResponse = document.getElementById("context-search-response"); | ||
| 191 | const contextSearchButton = document.getElementById("context-search-button"); | ||
| 192 | contextSearchButton.addEventListener('click', function() { | ||
| 193 | const context = contextSearchType.options[contextSearchType.selectedIndex].value; | ||
| |
2.3 | 194 | const term = contextSearchText.value; |
| 195 | switch(context) { | ||
| 196 | case 'collabs': | ||
| 197 | document.location.href = `/bin/view/Collabs/#search=${term}`; | ||
| 198 | break; | ||
| 199 | case 'current-collab': | ||
| |
19.1 | 200 | handleXWikiSearch('bougaultx'); |
| |
2.3 | 201 | break |
| 202 | default: | ||
| |
26.40 | 203 | handleXWikiSearch('') |
| |
2.3 | 204 | } |
| 205 | |||
| |
2.2 | 206 | }); |
| |
26.12 | 207 | function handleXWikiSearch(space, callback) { |
| |
26.2 | 208 | require(['jquery'], function($) { |
| 209 | var solrServiceURL = new XWiki.Document('SuggestSolrService', 'XWiki').getURL('get'); | ||
| 210 | const contextSearchButton = document.getElementById("context-search-button"); | ||
| 211 | const xwikiSpace = space ? `.${space}` : ''; | ||
| 212 | contextSearchButton.addEventListener('click', function() { | ||
| 213 | $.post(solrServiceURL, { | ||
| 214 | outputSyntax: 'plain', | ||
| 215 | media: 'json', | ||
| 216 | query: [ | ||
| 217 | 'q="__INPUT__"~100', | ||
| 218 | 'fq=type:DOCUMENT', | ||
| 219 | `fq=space:Collabs${xwikiSpace}.*` | ||
| 220 | ].join('\n'), | ||
| 221 | input: $('#context-search-text').val() | ||
| 222 | }).then(res => { | ||
| |
26.12 | 223 | if(callback) { |
| 224 | callback(res).then(results => { | ||
| 225 | contextSearchResponse.innerText = JSON.stringify(results, null, 2); | ||
| 226 | }) | ||
| 227 | } else { | ||
| 228 | contextSearchResponse.innerText = JSON.stringify(res, null, 2); | ||
| 229 | } | ||
| |
26.2 | 230 | }); |
| 231 | }); | ||
| |
23.1 | 232 | }); |
| |
26.2 | 233 | } |
| |
26.11 | 234 | function applyAdministratorFilter(results) { |
| |
26.12 | 235 | return new Promise((resolve, reject) => { |
| |
26.22 | 236 | //fetch("/rest/v1/collabs?roles=administrator") |
| 237 | fetch("/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&favorite=false&roles=administrator") | ||
| |
26.12 | 238 | .then(res => res.json()) |
| |
26.14 | 239 | .then(collabs => { |
| 240 | const spaces = collabs.map(collab => `Collabs.${collab.name}`); | ||
| |
26.25 | 241 | const filtered = results.filter(result => spaces.some(space => result.space.startsWith(space))); |
| |
26.18 | 242 | resolve(filtered); |
| |
26.12 | 243 | }) |
| 244 | |||
| 245 | }) | ||
| |
26.2 | 246 | } |
| 247 | }); | ||
| |
2.2 | 248 | </script> |
| |
26.62 | 249 | --> |
| |
1.1 | 250 | {{/html}} |