Changes for page CLB Search Component
Last modified by bougault on 2023/01/17 11:42
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -11,31 +11,17 @@ 11 11 12 12 createTemplate(htmlString) { 13 13 const template = document.createElement('template'); 14 - template.innerHTML = ` 15 - <style> 16 - ${this.styles} 17 - </style> 18 - ${htmlString} 19 - `; 14 + template.innerHTML = htmlString; 20 20 return template; 21 21 } 22 - 23 - $(selector) { 24 - return this.shadowRoot.querySelector(selector); 25 - } 26 - 27 - $$(selector) { 28 - return this.shadowRoot.querySelectorAll(selector); 29 - } 30 30 } 31 31 32 32 customElements.define('clb-simplified-search', 33 33 class extends ClbComponent { 34 - searchBaseUrl = '/bin/get/XWiki/SuggestSolrService?outputSyntax=plain&media=json&nb=10&query=q%3D%22__INPUT__%22~100%0Afq%3Dtype%3ADOCUMENT%0Afq%3Dspace%3ACollabs.*&input='; 21 + searchBaseUrl = 'https://wiki-dev.ebrains.eu/bin/get/XWiki/SuggestSolrService?outputSyntax=plain&media=json&nb=10&offset=0&query=q%3D%22__INPUT__%22~100%0Afq%3Dtype%3ADOCUMENT%0Afq%3Dspace%3ACollabs.*&input='; 35 35 constructor() { 36 36 super(); 37 37 this.results = []; 38 - this.offset = 0; 39 39 } 40 40 41 41 connectedCallback() { ... ... @@ -42,41 +42,34 @@ 42 42 // this.attachShadow({ mode: 'open' }); 43 43 super.connectedCallback(); 44 44 this.render(); 45 - this.$('#search').addEventListener('click', () => this.handleSearch()); 46 - this.$('#prev').addEventListener('click', () => { 47 - if(this.offset > 0) { 48 - this.offset--; 49 - this.handleSearch(); 50 - } 51 - }); 52 - this.$('#next').addEventListener('click', () => { 53 - this.offset++; 54 - this.handleSearch(); 55 - }); 31 + this.searchButton.addEventListener('click', () => this.handleSearch()); 56 56 } 57 57 58 58 59 - handleSearch = () => { 60 - const value = this.$('input[type="text"]').value; 61 - fetch(`${this.searchBaseUrl}${encodeURIComponent(value)}&offset=${this.offset}`) 62 - .then(res => res.json()) 63 - .then(this.renderResults); 35 + handleSearch() { 36 + fetch(`${this.searchBaseUrl}${encodeURIComponent(this.searchInput.value)}`) 37 + .then(res => res.json()) 38 + .then(this.renderResults); 64 64 } 65 65 41 + get searchInput() { 42 + return this.shadowRoot.querySelector('input[type="text"]'); 43 + } 66 66 45 + get searchButton() { 46 + return this.shadowRoot.querySelector('button'); 47 + } 48 + 67 67 get searchInputTemplate() { 68 - return this.createTemplate(` 69 - <div> 70 - <input type="text"><button id="search">Search</button> 71 - <div id="results"></div> 72 - <button id="prev">prev</button> 73 - <button id="next">next</button> 74 - </div> 75 - `); 50 + return this.createTemplate('<div><input type="text"><button>Search</button><div id="results"></div></div>'); 76 76 } 77 77 53 + resultsContainer = () => { 54 + return this.shadowRoot.querySelector('#results'); 55 + } 56 + 78 78 renderResults = (results) => { 79 - const resultsContainer = this. $('#results');58 + const resultsContainer = this.shadowRoot.querySelector('#results'); 80 80 resultsContainer.replaceChildren(); 81 81 for(let i = 0, j = results.length; i < j; i++) { 82 82 const { title_, spaces, doccontent_en } = results[i];