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