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