Wiki source code of CLB Search Component
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{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 | createTemplate(htmlString) { | ||
| 13 | const template = document.createElement('template'); | ||
| 14 | template.innerHTML = htmlString; | ||
| 15 | return template; | ||
| 16 | } | ||
| 17 | |||
| 18 | $(selector) { | ||
| 19 | return this.shadowRoot.querySelector(selector); | ||
| 20 | } | ||
| 21 | |||
| 22 | $$(selector) { | ||
| 23 | return this.shadowRoot.querySelectorAll(selector); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | customElements.define('clb-simplified-search', | ||
| 28 | class extends ClbComponent { | ||
| 29 | searchBaseUrl = 'https://wiki-dev.ebrains.eu/bin/get/XWiki/SuggestSolrService?outputSyntax=plain&media=json&nb=10&offset=0&query=q%3D%22__INPUT__%22~100%0Afq%3Dtype%3ADOCUMENT%0Afq%3Dspace%3ACollabs.*&input='; | ||
| 30 | constructor() { | ||
| 31 | super(); | ||
| 32 | this.results = []; | ||
| 33 | } | ||
| 34 | |||
| 35 | connectedCallback() { | ||
| 36 | // this.attachShadow({ mode: 'open' }); | ||
| 37 | super.connectedCallback(); | ||
| 38 | this.render(); | ||
| 39 | this.searchButton.addEventListener('click', () => this.handleSearch()); | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | handleSearch = () => { | ||
| 44 | fetch(`${this.searchBaseUrl}${encodeURIComponent(this.searchInput.value)}`) | ||
| 45 | .then(res => res.json()) | ||
| 46 | .then(this.renderResults); | ||
| 47 | |||
| 48 | } | ||
| 49 | |||
| 50 | get searchInput() { | ||
| 51 | return this.$('input[type="text"]'); | ||
| 52 | } | ||
| 53 | |||
| 54 | get searchButton() { | ||
| 55 | return this.$('#search'); | ||
| 56 | } | ||
| 57 | |||
| 58 | |||
| 59 | get searchInputTemplate() { | ||
| 60 | return this.createTemplate(` | ||
| 61 | <div> | ||
| 62 | <input type="text"><button id="search">Search</button> | ||
| 63 | <div id="results"></div> | ||
| 64 | <button id="prev">prev</button> | ||
| 65 | <button id="next">next</button> | ||
| 66 | </div> | ||
| 67 | `); | ||
| 68 | } | ||
| 69 | |||
| 70 | renderResults(results) { | ||
| 71 | const resultsContainer = this.$('#results'); | ||
| 72 | resultsContainer.replaceChildren(); | ||
| 73 | for(let i = 0, j = results.length; i < j; i++) { | ||
| 74 | const { title_, spaces, doccontent_en } = results[i]; | ||
| 75 | const elt = document.createElement('clb-search-result'); | ||
| 76 | elt.setAttribute('data-title', title_); | ||
| 77 | elt.setAttribute('data-href', spaces.join('/')); | ||
| 78 | elt.innerHTML = doccontent_en; | ||
| 79 | resultsContainer.appendChild(elt); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | render() { | ||
| 84 | this.shadowRoot.appendChild(this.searchInputTemplate.content.cloneNode(true)); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | ); | ||
| 88 | |||
| 89 | customElements.define('clb-search-result', | ||
| 90 | class extends ClbComponent { | ||
| 91 | constructor() { | ||
| 92 | super(); | ||
| 93 | } | ||
| 94 | |||
| 95 | get titleAttr() { | ||
| 96 | return this.getAttribute('data-title'); | ||
| 97 | } | ||
| 98 | |||
| 99 | get hrefAttr() { | ||
| 100 | return this.getAttribute('data-href'); | ||
| 101 | } | ||
| 102 | |||
| 103 | get resultTemplate() { | ||
| 104 | return this.createTemplate(` | ||
| 105 | <div> | ||
| 106 | <a href=${this.hrefAttr}>${this.titleAttr}</a> | ||
| 107 | <div> | ||
| 108 | <slot></slot> | ||
| 109 | </div> | ||
| 110 | </div> | ||
| 111 | `); | ||
| 112 | } | ||
| 113 | connectedCallback() { | ||
| 114 | this.attachShadow({ mode: 'open' }); | ||
| 115 | this.shadowRoot.appendChild(this.resultTemplate.content.cloneNode(true)); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | ); | ||
| 119 | </script> | ||
| 120 | <clb-simplified-search></clb-simplified-search> | ||
| 121 | {{/html}} |