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