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