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
-
... ... @@ -1,4 +2,108 @@ 1 - 2 2 {{html clean="false"}} 2 +<script> 3 +class ClbComponent extends HTMLElement { 4 + constructor() { 5 + super(); 6 + } 7 + 8 + connectedCallback() { 9 + this.attachShadow({ mode: 'open'}); 10 + } 11 + 12 + createTemplate(htmlString) { 13 + const template = document.createElement('template'); 14 + template.innerHTML = htmlString; 15 + return template; 16 + } 17 +} 18 + 19 +customElements.define('clb-simplified-search', 20 + class extends ClbComponent { 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='; 22 + constructor() { 23 + super(); 24 + this.results = []; 25 + } 26 + 27 + connectedCallback() { 28 + // this.attachShadow({ mode: 'open' }); 29 + super.connectedCallback(); 30 + this.render(); 31 + this.searchButton.addEventListener('click', () => this.handleSearch()); 32 + } 33 + 34 + 35 + handleSearch() { 36 + fetch(`${this.searchBaseUrl}${encodeURIComponent(this.searchInput.value)}`) 37 + .then(res => res.json()) 38 + .then(this.renderResults); 39 + } 40 + 41 + get searchInput() { 42 + return this.shadowRoot.querySelector('input[type="text"]'); 43 + } 44 + 45 + get searchButton() { 46 + return this.shadowRoot.querySelector('button'); 47 + } 48 + 49 + get searchInputTemplate() { 50 + return this.createTemplate('<div><input type="text"><button>Search</button><div id="results"></div></div>'); 51 + } 52 + 53 + resultsContainer = () => { 54 + return this.shadowRoot.querySelector('#results'); 55 + } 56 + 57 + renderResults(results) { 58 + const resultsContainer = this.shadowRoot.querySelector('#results'); 59 + resultsContainer.replaceChildren(); 60 + for(let i = 0, j = results.length; i < j; i++) { 61 + const { title_, spaces, doccontent_en } = results[i]; 62 + const elt = document.createElement('clb-search-result'); 63 + elt.setAttribute('data-title', title_); 64 + elt.setAttribute('data-href', spaces.join('/')); 65 + elt.innerHTML = doccontent_en; 66 + resultsContainer.appendChild(elt); 67 + } 68 + } 69 + 70 + render() { 71 + this.shadowRoot.appendChild(this.searchInputTemplate.content.cloneNode(true)); 72 + } 73 + } 74 +); 75 + 76 +customElements.define('clb-search-result', 77 + class extends ClbComponent { 78 + constructor() { 79 + super(); 80 + } 81 + 82 + get titleAttr() { 83 + return this.getAttribute('data-title'); 84 + } 85 + 86 + get hrefAttr() { 87 + return this.getAttribute('data-href'); 88 + } 89 + 90 + get resultTemplate() { 91 + return this.createTemplate(` 92 + <div> 93 + <a href=${this.hrefAttr}>${this.titleAttr}</a> 94 + <div> 95 + <slot></slot> 96 + </div> 97 + </div> 98 + `); 99 + } 100 + connectedCallback() { 101 + this.attachShadow({ mode: 'open' }); 102 + this.shadowRoot.appendChild(this.resultTemplate.content.cloneNode(true)); 103 + } 104 + } 105 +); 106 +</script> 3 3 <clb-simplified-search></clb-simplified-search> 4 4 {{/html}}