Wiki source code of Context Search
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
| |
1.1 | 1 | {{html clean="false"}} |
| |
26.64 | 2 | <!-- |
| |
27.56 | 3 | !!! POC. Code must be reviewed / optimized and packaged for use in production !!! |
| 4 | !!! Many optimisations can be done !!! | ||
| |
26.64 | 5 | --> |
| |
26.45 | 6 | <script type="module"> |
| |
26.81 | 7 | import {LitElement, html, css} from 'https://cdn.jsdelivr.net/gh/lit/dist@2/core/lit-core.min.js'; |
| |
26.70 | 8 | window.addEventListener('DOMContentLoaded', function() { |
| 9 | |||
| |
26.46 | 10 | class UnifiedSearch extends LitElement { |
| |
26.47 | 11 | static properties = { |
| |
27.71 | 12 | results: { type: Array }, |
| 13 | resultsCache: { type: Array }, | ||
| 14 | collabFiltersResults: { type: Array} | ||
| |
26.77 | 15 | } |
| |
26.80 | 16 | static styles = css` |
| |
27.58 | 17 | :host > div { |
| |
27.57 | 18 | padding-bottom: 4em; |
| 19 | } | ||
| |
26.80 | 20 | .result { |
| 21 | margin-top: 1em; | ||
| 22 | } | ||
| |
26.82 | 23 | .light { |
| |
26.86 | 24 | color: var(--color-gray-500, #CCC); |
| |
26.82 | 25 | font-size: 14px; |
| 26 | } | ||
| |
26.87 | 27 | a { |
| 28 | color: var(--color-brand-primary, blue); | ||
| 29 | } | ||
| |
26.80 | 30 | `; |
| |
26.49 | 31 | constructor() { |
| 32 | super(); | ||
| 33 | this.searchText = ""; | ||
| |
27.7 | 34 | this.prevSearchText = ""; |
| |
26.68 | 35 | this.administrator = false; |
| 36 | this.editor = false; | ||
| 37 | this.viewer = false; | ||
| 38 | this.rolesFilter = []; | ||
| |
27.24 | 39 | this.filterUrl = new URL(`${document.location.origin}/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&limit=10000&favorite=false&roles=`); |
| |
26.75 | 40 | this.results = []; |
| |
27.38 | 41 | this.resultsCache = []; |
| |
26.49 | 42 | } |
| |
27.68 | 43 | firstUpdated() { |
| 44 | this.renderRoot.getElementById('searchInput').focus(); | ||
| 45 | } | ||
| |
26.68 | 46 | toggleRoleFilter(roleName) { |
| |
27.28 | 47 | if(this.rolesFilter.indexOf(roleName) === -1) { |
| |
26.68 | 48 | this.rolesFilter.push(roleName); |
| 49 | } else { | ||
| |
27.32 | 50 | this.rolesFilter = this.rolesFilter.filter(role => role !== roleName); |
| |
26.68 | 51 | } |
| |
26.69 | 52 | this[roleName] = !this[roleName]; |
| 53 | this.filterUrl.searchParams.set('roles', this.rolesFilter.join("+")); | ||
| |
27.8 | 54 | this.handleSearch(); |
| |
26.68 | 55 | } |
| |
26.47 | 56 | setSearchText(e) { |
| 57 | this.searchText = e.target.value; | ||
| 58 | } | ||
| |
27.45 | 59 | handleSearch(textSearch) { |
| 60 | if(this.searchText !== "" || textSearch) { | ||
| |
27.27 | 61 | if(this.searchText !== this.prevSearchText) { |
| |
27.53 | 62 | this.clearResults(); |
| |
27.27 | 63 | this.prevSearchText = this.searchText; |
| |
27.47 | 64 | this.fetchWikiSearch().then(results => { |
| |
27.48 | 65 | this.resultsCache = results; |
| |
27.51 | 66 | if(this.hasActiveFilters()) { |
| 67 | this.fetchFilterSearch().then(this.filterResults); | ||
| 68 | } else { | ||
| 69 | this.results = results; | ||
| 70 | } | ||
| |
27.47 | 71 | }) |
| |
27.37 | 72 | } else if (this.searchText === this.prevSearchText && this.hasActiveFilters()) { |
| |
27.27 | 73 | this.fetchFilterSearch().then(this.filterResults) |
| |
27.42 | 74 | } else { |
| 75 | this.results = this.resultsCache; | ||
| |
27.27 | 76 | } |
| |
27.5 | 77 | } else { |
| |
27.43 | 78 | this.clearResults(); |
| |
27.5 | 79 | console.log("no search text, should return filter search results if any"); |
| |
27.10 | 80 | this.fetchFilterSearch().then(collabs => { |
| |
27.7 | 81 | console.log("Format and display collabs", collabs); |
| 82 | }) | ||
| |
26.72 | 83 | } |
| |
26.47 | 84 | } |
| |
27.5 | 85 | hasActiveFilters() { |
| 86 | // we only look at role filters for POC. | ||
| |
27.16 | 87 | return this.rolesFilter.length > 0; |
| |
27.5 | 88 | } |
| |
26.71 | 89 | fetchWikiSearch() { |
| 90 | // request should be built from component, not from "external" dependency. Leave it here like this for POC. | ||
| |
27.5 | 91 | return handleXWikiSearch(this.searchText); |
| |
26.71 | 92 | } |
| |
27.5 | 93 | fetchFilterSearch() { |
| 94 | return new Promise((resolve) => { | ||
| |
27.20 | 95 | if(this.hasActiveFilters()) { |
| |
27.82 | 96 | fetch(this.filterUrl).then(res => res.json()).then((res) => resolve(res)); |
| |
27.5 | 97 | } else { |
| |
27.33 | 98 | resolve([]); |
| |
27.5 | 99 | } |
| 100 | }); | ||
| 101 | } | ||
| |
27.11 | 102 | filterResults = (filteredCollabs) => { |
| |
27.14 | 103 | const collabSpaces = filteredCollabs.map(collab => `Collabs.${collab.name}`); |
| |
27.39 | 104 | this.results = this.resultsCache.filter(result => collabSpaces.some(space => result.space.startsWith(space))); |
| |
27.7 | 105 | } |
| |
27.43 | 106 | clearResults() { |
| 107 | this.results = []; | ||
| 108 | this.resultsCache = []; | ||
| 109 | } | ||
| |
26.89 | 110 | getLink(xwikiDocSpace) { |
| |
26.90 | 111 | return `/bin/view/${xwikiDocSpace.replaceAll('.', '/')}`; |
| |
26.88 | 112 | } |
| |
27.59 | 113 | handleKeyPress(e) { |
| |
27.62 | 114 | if (e.keyCode === 13) { |
| |
27.59 | 115 | this.handleSearch(true); |
| 116 | } | ||
| 117 | } | ||
| |
26.46 | 118 | render() { |
| |
26.49 | 119 | return html` |
| 120 | <div> | ||
| 121 | <div> | ||
| |
27.68 | 122 | <input id="searchInput" @change="${this.setSearchText}" type="text" placeholder="Search..." @keypress="${this.handleKeyPress}" /> |
| |
27.46 | 123 | <button @click="${() => this.handleSearch(true)}">Search</button> |
| |
26.49 | 124 | </div> |
| |
26.61 | 125 | <div> |
| |
26.68 | 126 | <label><input type="checkbox" @change="${() => this.toggleRoleFilter('administrator')}" .checked="${this.administratorFilter}"/> Administrator</label> |
| 127 | <label><input type="checkbox" @change="${() => this.toggleRoleFilter('editor')}" .checked="${this.editorFilter}"/> Editor</label> | ||
| 128 | <label><input type="checkbox" @change="${() => this.toggleRoleFilter('viewer')}" .checked="${this.viewerFilter}"/> Viewer</label> | ||
| |
26.61 | 129 | </div> |
| |
26.76 | 130 | <div> |
| 131 | ${this.results.map(result => html` | ||
| |
26.80 | 132 | <div class="result"> |
| |
26.89 | 133 | <a href="${this.getLink(result.space)}">${result.title_}</a> |
| |
26.76 | 134 | <div>${result.doccontent_.substring(0, 150)} |
| |
26.92 | 135 | <div class="light">${result.space.replaceAll('.', ' / ')}</div> |
| |
26.76 | 136 | </div> |
| 137 | `)} | ||
| 138 | </div> | ||
| |
26.49 | 139 | ` |
| |
26.46 | 140 | } |
| 141 | } | ||
| 142 | customElements.define('clb-unified-search', UnifiedSearch); | ||
| |
26.70 | 143 | |
| |
26.71 | 144 | function handleXWikiSearch(searchString) { |
| 145 | return new Promise((resolve) => { | ||
| 146 | require(['jquery'], function($) { | ||
| |
26.74 | 147 | const solrServiceURL = new XWiki.Document('SuggestSolrService', 'XWiki').getURL('get'); |
| 148 | //contextSearchButton.addEventListener('click', function() { | ||
| |
29.1 | 149 | $.get(solrServiceURL, { |
| |
26.71 | 150 | outputSyntax: 'plain', |
| 151 | media: 'json', | ||
| 152 | query: [ | ||
| 153 | 'q="__INPUT__"~100', | ||
| 154 | 'fq=type:DOCUMENT', | ||
| 155 | `fq=space:Collabs.*` | ||
| 156 | ].join('\n'), | ||
| 157 | input: searchString | ||
| 158 | }).then(resolve); | ||
| |
26.74 | 159 | //}); |
| |
26.70 | 160 | }); |
| 161 | }); | ||
| 162 | } | ||
| 163 | }); | ||
| |
26.45 | 164 | </script> |
| |
26.46 | 165 | <clb-unified-search></clb-unified-search> |
| |
1.1 | 166 | {{/html}} |