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