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,139 +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 - get styles() { 62 - return ` 63 - input[type="text"] { 64 - border: 1px solid red; 65 - } 66 - ` 67 - } 68 - 69 - handleSearch = () => { 70 - const value = this.$('input[type="text"]').value; 71 - fetch(`${this.searchBaseUrl}${encodeURIComponent(value)}&offset=${this.offset}`) 72 - .then(res => res.json()) 73 - .then(this.renderResults); 74 - } 75 - 76 - 77 - get searchInputTemplate() { 78 - return this.createTemplate(` 79 - <div> 80 - <input type="text"><button id="search">Search</button> 81 - <div id="results"></div> 82 - <button id="prev">prev</button> 83 - <button id="next">next</button> 84 - </div> 85 - `); 86 - } 87 - 88 - renderResults = (results) => { 89 - const resultsContainer = this.$('#results'); 90 - resultsContainer.replaceChildren(); 91 - for(let i = 0, j = results.length; i < j; i++) { 92 - const { title_, spaces, doccontent_en } = results[i]; 93 - const elt = document.createElement('clb-search-result'); 94 - elt.setAttribute('data-title', title_); 95 - elt.setAttribute('data-href', spaces.join('/')); 96 - elt.innerHTML = doccontent_en; 97 - resultsContainer.appendChild(elt); 98 - } 99 - } 100 - 101 - render() { 102 - this.shadowRoot.appendChild(this.searchInputTemplate.content.cloneNode(true)); 103 - } 104 - } 105 -); 106 - 107 -customElements.define('clb-search-result', 108 - class extends ClbComponent { 109 - constructor() { 110 - super(); 111 - } 112 - 113 - get titleAttr() { 114 - return this.getAttribute('data-title'); 115 - } 116 - 117 - get hrefAttr() { 118 - return this.getAttribute('data-href'); 119 - } 120 - 121 - get resultTemplate() { 122 - return this.createTemplate(` 123 - <div> 124 - <a href=${this.hrefAttr}>${this.titleAttr}</a> 125 - <div> 126 - <slot></slot> 127 - </div> 128 - </div> 129 - `); 130 - } 131 - connectedCallback() { 132 - this.attachShadow({ mode: 'open' }); 133 - this.shadowRoot.appendChild(this.resultTemplate.content.cloneNode(true)); 134 - } 135 - } 136 -); 137 -</script> 2 +{{html clean="false"}} 138 138 <clb-simplified-search></clb-simplified-search> 139 139 {{/html}}