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