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