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