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