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