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