Changes for page Context Search
Last modified by bougault on 2023/02/20 16:29
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -1,77 +1,186 @@ 1 1 {{html clean="false"}} 2 +<!-- 3 + !!! POC. Code must be reviewed / optimized and packaged for use in production !!! 4 + !!! Many optimisations can be done !!! 5 +--> 6 +<script type="module"> 7 +import {LitElement, html, css} from 'https://cdn.jsdelivr.net/gh/lit/dist@2/core/lit-core.min.js'; 8 +window.addEventListener('DOMContentLoaded', function() { 2 2 3 -<select id="context-search-type"> 4 - <option value="all">All</option> 5 - <option value="collabs">Collabs</option> 6 - <option value="current-collab">Current Collab</option> 7 -</select> 8 -<input type="text" id="context-search-text" /> 9 -<button id="context-search-button" class="btn btn-primary">Search</button> 10 - 11 -<pre id="context-search-response"></pre> 12 -<script> 13 - window.addEventListener('DOMContentLoaded', function() { 14 - const contextSearchType = document.getElementById("context-search-type"); 15 - const contextSearchText = document.getElementById("context-search-text"); 16 - const contextSearchResponse = document.getElementById("context-search-response"); 17 - const contextSearchButton = document.getElementById("context-search-button"); 18 - contextSearchButton.addEventListener('click', function() { 19 - const context = contextSearchType.options[contextSearchType.selectedIndex].value; 20 - const term = contextSearchText.value; 21 - switch(context) { 22 - case 'collabs': 23 - document.location.href = `/bin/view/Collabs/#search=${term}`; 24 - break; 25 - case 'current-collab': 26 - handleXWikiSearch('bougaultx'); 27 - break 28 - default: 29 - handleXWikiSearch('', applyAdministratorFilter) 10 + class UnifiedSearch extends LitElement { 11 + static properties = { 12 + results: { type: Array }, 13 + resultsCache: { type: Array }, 14 + collabFiltersResults: { type: Array} 15 + } 16 + static styles = css` 17 + :host > div { 18 + padding-bottom: 4em; 30 30 } 20 + .result { 21 + margin-top: 1em; 22 + } 23 + .light { 24 + color: var(--color-gray-500, #CCC); 25 + font-size: 14px; 26 + } 27 + a { 28 + color: var(--color-brand-primary, blue); 29 + } 30 + .rawResults { 31 + width: 100%; 32 + display: flex; 33 + font-size: 12px; 34 + margin-top: 2em; 35 + } 36 + `; 37 + constructor() { 38 + super(); 39 + this.searchText = ""; 40 + this.prevSearchText = ""; 41 + this.administrator = false; 42 + this.editor = false; 43 + this.viewer = false; 44 + this.rolesFilter = []; 45 + this.filterUrl = new URL(`${document.location.origin}/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&limit=10000&favorite=false&roles=`); 46 + this.results = []; 47 + this.resultsCache = []; 48 + } 49 + firstUpdated() { 50 + this.renderRoot.getElementById('searchInput').focus(); 51 + } 52 + toggleRoleFilter(roleName) { 53 + if(this.rolesFilter.indexOf(roleName) === -1) { 54 + this.rolesFilter.push(roleName); 55 + } else { 56 + this.rolesFilter = this.rolesFilter.filter(role => role !== roleName); 57 + } 58 + this[roleName] = !this[roleName]; 59 + this.filterUrl.searchParams.set('roles', this.rolesFilter.join("+")); 60 + this.handleSearch(); 61 + } 62 + setSearchText(e) { 63 + this.searchText = e.target.value; 64 + } 65 + handleSearch(textSearch) { 66 + if(this.searchText !== "" || textSearch) { 67 + if(this.searchText !== this.prevSearchText) { 68 + this.clearResults(); 69 + this.prevSearchText = this.searchText; 70 + this.fetchWikiSearch().then(results => { 71 + this.resultsCache = results; 72 + if(this.hasActiveFilters()) { 73 + this.fetchFilterSearch().then(this.filterResults); 74 + } else { 75 + this.results = results; 76 + } 77 + }) 78 + } else if (this.searchText === this.prevSearchText && this.hasActiveFilters()) { 79 + this.fetchFilterSearch().then(this.filterResults) 80 + } else { 81 + this.results = this.resultsCache; 82 + } 83 + } else { 84 + this.clearResults(); 85 + console.log("no search text, should return filter search results if any"); 86 + this.fetchFilterSearch().then(collabs => { 87 + console.log("Format and display collabs", collabs); 88 + }) 89 + } 90 + } 91 + hasActiveFilters() { 92 + // we only look at role filters for POC. 93 + return this.rolesFilter.length > 0; 94 + } 95 + fetchWikiSearch() { 96 + // request should be built from component, not from "external" dependency. Leave it here like this for POC. 97 + return handleXWikiSearch(this.searchText); 98 + } 99 + fetchFilterSearch() { 100 + return new Promise((resolve) => { 101 + if(this.hasActiveFilters()) { 102 + fetch(this.filterUrl).then(res => res.json()).then((res) => { 103 + this.collabFiltersResults = res; 104 + resolve(res); 105 + }); 106 + } else { 107 + resolve([]); 108 + } 109 + }); 110 + } 111 + filterResults = (filteredCollabs) => { 112 + const collabSpaces = filteredCollabs.map(collab => `Collabs.${collab.name}`); 113 + this.results = this.resultsCache.filter(result => collabSpaces.some(space => result.space.startsWith(space))); 114 + } 115 + clearResults() { 116 + this.results = []; 117 + this.resultsCache = []; 118 + } 119 + getLink(xwikiDocSpace) { 120 + return `/bin/view/${xwikiDocSpace.replaceAll('.', '/')}`; 121 + } 122 + handleKeyPress(e) { 123 + if (e.keyCode === 13) { 124 + this.handleSearch(true); 125 + } 126 + } 127 + render() { 128 + return html` 129 +<div> 130 + <div> 131 + <input id="searchInput" @change="${this.setSearchText}" type="text" placeholder="Search..." @keypress="${this.handleKeyPress}" /> 132 + <button @click="${() => this.handleSearch(true)}">Search</button> 133 + </div> 134 + <div> 135 + <label><input type="checkbox" @change="${() => this.toggleRoleFilter('administrator')}" .checked="${this.administratorFilter}"/> Administrator</label> 136 + <label><input type="checkbox" @change="${() => this.toggleRoleFilter('editor')}" .checked="${this.editorFilter}"/> Editor</label> 137 + <label><input type="checkbox" @change="${() => this.toggleRoleFilter('viewer')}" .checked="${this.viewerFilter}"/> Viewer</label> 138 + </div> 139 + <div> 140 + ${this.results.map(result => html` 141 + <div class="result"> 142 + <a href="${this.getLink(result.space)}">${result.title_}</a> 143 + <div>${result.doccontent_.substring(0, 150)} 144 + <div class="light">${result.space.replaceAll('.', ' / ')}</div> 145 + </div> 146 + `)} 147 + </div> 148 + <div class="rawResults"> 149 + <div> 150 + <strong>XWiki Search Results</strong> 151 + <pre>${JSON.stringify(this.resultsCache, null, 2)}</pre> 152 + </div> 153 + <div> 154 + <strong>Collab Search Results (Filters)</strong> 155 + <pre>${JSON.stringify(this.collabFiltersResults, null, 2)}</pre> 156 + </div> 157 + </div> 158 +</div> 159 +` 160 + } 161 + } 162 + customElements.define('clb-unified-search', UnifiedSearch); 31 31 32 - });33 - functionhandleXWikiSearch(space, callback) {164 + function handleXWikiSearch(searchString) { 165 + return new Promise((resolve) => { 34 34 require(['jquery'], function($) { 35 - var solrServiceURL = new XWiki.Document('SuggestSolrService', 'XWiki').getURL('get'); 36 - const contextSearchButton = document.getElementById("context-search-button"); 37 - const xwikiSpace = space ? `.${space}` : ''; 38 - contextSearchButton.addEventListener('click', function() { 167 + const solrServiceURL = new XWiki.Document('SuggestSolrService', 'XWiki').getURL('get'); 168 + //contextSearchButton.addEventListener('click', function() { 39 39 $.post(solrServiceURL, { 40 - outputSyntax: 'plain', 41 - media: 'json', 42 - query: [ 43 - 'q="__INPUT__"~100', 44 - 'fq=type:DOCUMENT', 45 - `fq=space:Collabs${xwikiSpace}.*` 46 - ].join('\n'), 47 - input: $('#context-search-text').val() 48 - }).then(res => { 49 - if(callback) { 50 - callback(res).then(results => { 51 - contextSearchResponse.innerText = JSON.stringify(results, null, 2); 52 - }) 53 - } else { 54 - contextSearchResponse.innerText = JSON.stringify(res, null, 2); 55 - } 56 - }); 57 - }); 170 + outputSyntax: 'plain', 171 + media: 'json', 172 + query: [ 173 + 'q="__INPUT__"~100', 174 + 'fq=type:DOCUMENT', 175 + `fq=space:Collabs.*` 176 + ].join('\n'), 177 + input: searchString 178 + }).then(resolve); 179 + //}); 180 + }); 58 58 }); 59 59 } 60 - function applyAdministratorFilter(results) { 61 - return new Promise((resolve, reject) => { 62 - //fetch("/rest/v1/collabs?roles=administrator") 63 - fetch("/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&favorite=false&roles=administrator") 64 - .then(res => res.json()) 65 - .then(collabs => { 66 - const spaces = collabs.map(collab => `Collabs.${collab.name}`); 67 - const filtered = results.filter(result => spaces.some(space => result.space.startsWith(space)); 68 - resolve(filtered); 69 - }) 70 - 71 - }) 72 - } 73 73 }); 74 - 75 - 76 76 </script> 185 +<clb-unified-search></clb-unified-search> 77 77 {{/html}}