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,7 +1,6 @@ 1 1 {{html clean="false"}} 2 2 <!-- 3 - !!! POC. Code must be reviewed / optimized and packaged for use in production !!! 4 - !!! Many optimisations can be done !!! 3 + !!! POC. Code must be reviewed / optimized and packaged for use in production !!! 5 5 --> 6 6 <script type="module"> 7 7 import {LitElement, html, css} from 'https://cdn.jsdelivr.net/gh/lit/dist@2/core/lit-core.min.js'; ... ... @@ -8,15 +8,21 @@ 8 8 window.addEventListener('DOMContentLoaded', function() { 9 9 10 10 class UnifiedSearch extends LitElement { 10 + /* 11 11 static properties = { 12 - results: { type: Array }, 13 - resultsCache: { type: Array }, 14 - collabFiltersResults: { type: Array} 12 + searchText: { type: String }, 13 + favoritesFilter: { type: Boolean }, 14 + administratorFilter: { type: Boolean }, 15 + editorFilter: { type: Boolean }, 16 + viewerFilter: { type: Boolean }, 17 + publicFilter: { type: Boolean }, 18 + privateFilter: { type: Boolean }, 15 15 } 20 + */ 21 + static properties = { 22 + results: { type: Array } 23 + } 16 16 static styles = css` 17 - :host > div { 18 - padding-bottom: 4em; 19 - } 20 20 .result { 21 21 margin-top: 1em; 22 22 } ... ... @@ -27,15 +27,6 @@ 27 27 a { 28 28 color: var(--color-brand-primary, blue); 29 29 } 30 - .rawResults { 31 - width: 100%; 32 - display: flex; 33 - font-size: 12px; 34 - margin-top: 2em; 35 - } 36 - .rawResults pre { 37 - overflow: scroll; 38 - } 39 39 `; 40 40 constructor() { 41 41 super(); ... ... @@ -47,16 +47,12 @@ 47 47 this.rolesFilter = []; 48 48 this.filterUrl = new URL(`${document.location.origin}/rest/v1/collabs?search=&offset=0&orderField=title&order=asc&limit=10000&favorite=false&roles=`); 49 49 this.results = []; 50 - this.resultsCache = []; 51 51 } 52 - firstUpdated() { 53 - this.renderRoot.getElementById('searchInput').focus(); 54 - } 55 55 toggleRoleFilter(roleName) { 56 - if(this.rolesFilter.indexOf(roleName) == =-1) {48 + if(this.rolesFilter.indexOf(roleName) == -1) { 57 57 this.rolesFilter.push(roleName); 58 58 } else { 59 - this.rolesFilter = this.rolesFilter.filter(role => role !== roleName);51 + this.rolesFilter.filter(role => role !== roleName); 60 60 } 61 61 this[roleName] = !this[roleName]; 62 62 this.filterUrl.searchParams.set('roles', this.rolesFilter.join("+")); ... ... @@ -65,26 +65,24 @@ 65 65 setSearchText(e) { 66 66 this.searchText = e.target.value; 67 67 } 68 - handleSearch(textSearch) { 69 - if(this.searchText !== "" || textSearch) { 60 + handleSearch() { 61 + console.log(this.rolesFilter) 62 + if(this.searchText !== "") { 70 70 if(this.searchText !== this.prevSearchText) { 71 - this. clearResults();64 + console.log(this.searchText, this.prevSearchText); 72 72 this.prevSearchText = this.searchText; 73 - this.fetchWikiSearch().then(results => {74 - this. resultsCache=results;75 - if(this.hasActiveFilters()) {76 - this.fetchFilterSearch().then(this.filterResults);77 - }else{78 - this.results=results;79 - }80 - })81 - }elseif(this.searchText===this.prevSearchText && this.hasActiveFilters()) {66 + Promise.all([ 67 + this.fetchWikiSearch(), 68 + this.fetchFilterSearch() 69 + ]).then(([wikiSearchResults, collabSearchResults]) => { 70 + this.results = wikiSearchResults; 71 + console.log(wikiSearchResults, collabSearchResults); 72 + }); 73 + } else if (this.searchText === this.prevSearchText) { 74 + console.log("Only apply filter on results"); 82 82 this.fetchFilterSearch().then(this.filterResults) 83 - } else { 84 - this.results = this.resultsCache; 85 85 } 86 86 } else { 87 - this.clearResults(); 88 88 console.log("no search text, should return filter search results if any"); 89 89 this.fetchFilterSearch().then(collabs => { 90 90 console.log("Format and display collabs", collabs); ... ... @@ -93,6 +93,7 @@ 93 93 } 94 94 hasActiveFilters() { 95 95 // we only look at role filters for POC. 86 + console.log('rolesfilter', this.rolesFilter); 96 96 return this.rolesFilter.length > 0; 97 97 } 98 98 fetchWikiSearch() { ... ... @@ -102,37 +102,27 @@ 102 102 fetchFilterSearch() { 103 103 return new Promise((resolve) => { 104 104 if(this.hasActiveFilters()) { 105 - fetch(this.filterUrl).then(res => res.json()).then((res) => { 106 - this.collabFiltersResults = res; 107 - resolve(res); 108 - }); 96 + fetch(this.filterUrl).then(res => res.json()).then(resolve); 109 109 } else { 110 - resolve( []);98 + resolve(null); 111 111 } 112 112 }); 113 113 } 114 114 filterResults = (filteredCollabs) => { 103 + console.log("filteredCollabs:", filteredCollabs, "search results:", this.results); 115 115 const collabSpaces = filteredCollabs.map(collab => `Collabs.${collab.name}`); 116 - this.results = this.resultsCache.filter(result => collabSpaces.some(space => result.space.startsWith(space))); 105 + const filteredResults = this.results.filter(result => collabSpaces.some(space => result.space.startsWith(space))); 106 + console.log(filteredResults); 117 117 } 118 - clearResults() { 119 - this.results = []; 120 - this.resultsCache = []; 121 - } 122 122 getLink(xwikiDocSpace) { 123 123 return `/bin/view/${xwikiDocSpace.replaceAll('.', '/')}`; 124 124 } 125 - handleKeyPress(e) { 126 - if (e.keyCode === 13) { 127 - this.handleSearch(true); 128 - } 129 - } 130 130 render() { 131 131 return html` 132 132 <div> 133 133 <div> 134 - <input id="searchInput"@change="${this.setSearchText}" type="text" placeholder="Search..."@keypress="${this.handleKeyPress}"/>135 - <button @click="${ () =>this.handleSearch(true)}">Search</button>115 + <input @change="${this.setSearchText}" type="text" placeholder="Search..." /> 116 + <button @click="${this.handleSearch}">Search</button> 136 136 </div> 137 137 <div> 138 138 <label><input type="checkbox" @change="${() => this.toggleRoleFilter('administrator')}" .checked="${this.administratorFilter}"/> Administrator</label> ... ... @@ -148,16 +148,6 @@ 148 148 </div> 149 149 `)} 150 150 </div> 151 - <div class="rawResults"> 152 - <div> 153 - <strong>XWiki Search Results</strong> 154 - <pre>${JSON.stringify(this.resultsCache, null, 2)}</pre> 155 - </div> 156 - <div> 157 - <strong>Collab Search Results (Filters)</strong> 158 - <pre>${JSON.stringify(this.collabFiltersResults, null, 2)}</pre> 159 - </div> 160 - </div> 161 161 </div> 162 162 ` 163 163 } ... ... @@ -185,5 +185,7 @@ 185 185 } 186 186 }); 187 187 </script> 159 + 188 188 <clb-unified-search></clb-unified-search> 161 + 189 189 {{/html}}