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