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