Failed to execute template. Cause: [Access denied when checking [script] access to [xwiki:Collaboratory.UX.HbpSkin.WebHome] for user [xwiki:XWiki.Admin]]. Click on this message for details.

CLB Search Component - HBP Wiki

IAM21 instance, do not create collab nor modify a team, your changes will be lost


Wiki source code of CLB Search Component

Version 7.21 by bougault on 2023/01/13 14:33

Hide last authors
bougault 4.1 1 {{html clean="false"}}
bougault 3.1 2 <script>
bougault 1.1 3 class ClbComponent extends HTMLElement {
4 constructor() {
5 super();
6 }
7
8 connectedCallback() {
9 this.attachShadow({ mode: 'open'});
10 }
11
bougault 7.18 12 get template() {
bougault 1.1 13 const template = document.createElement('template');
bougault 7.13 14 template.innerHTML = `
15 <style>
16 ${this.styles}
17 </style>
bougault 7.19 18 ${this.html}
bougault 7.13 19 `;
bougault 1.1 20 return template;
21 }
bougault 7.4 22
bougault 7.18 23
bougault 7.14 24 get styles() {
25 return ``;
26 }
bougault 7.4 27 $(selector) {
28 return this.shadowRoot.querySelector(selector);
29 }
30
31 $$(selector) {
32 return this.shadowRoot.querySelectorAll(selector);
33 }
bougault 1.1 34 }
35
36 customElements.define('clb-simplified-search',
37 class extends ClbComponent {
bougault 7.12 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=';
bougault 1.1 39 constructor() {
40 super();
41 this.results = [];
bougault 7.9 42 this.offset = 0;
bougault 1.1 43 }
44
45 connectedCallback() {
46 super.connectedCallback();
47 this.render();
bougault 7.20 48 this.$('#search').addEventListener('click', this.handleSearch);
49 this.$('#prev').addEventListener('click', this.handlePrevPage);
50 this.$('#next').addEventListener('click', this.handleNextPage;
bougault 1.1 51 }
52
bougault 7.20 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
bougault 7.15 59 get styles() {
60 return `
bougault 7.16 61 input[type="text"] {
62 border: 1px solid red;
bougault 7.15 63 }
64 `
65 }
bougault 7.20 66 handlePrevPage = () => {
67 if(this.offset > 0) {
68 this.offset--;
69 this.handleSearch();
70 }
71 }
72 handleNextPage = () => {
73 this.offset++;
74 this.handleSearch();
75 }
bougault 1.1 76
bougault 7.4 77 handleSearch = () => {
bougault 7.7 78 const value = this.$('input[type="text"]').value;
bougault 7.12 79 fetch(`${this.searchBaseUrl}${encodeURIComponent(value)}&offset=${this.offset}`)
bougault 7.4 80 .then(res => res.json())
81 .then(this.renderResults);
bougault 1.1 82 }
83
84
bougault 7.18 85 get html() {
86 return `
bougault 7.4 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>
bougault 7.18 93 `;
bougault 1.1 94 }
95
bougault 7.6 96 renderResults = (results) => {
bougault 7.4 97 const resultsContainer = this.$('#results');
bougault 1.1 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() {
bougault 7.18 110 this.shadowRoot.appendChild(this.template.content.cloneNode(true));
bougault 1.1 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
bougault 7.17 129 get styles() {
130 return `
131 a {
132 color: var(--color-brand-primary);
133 }
134 `;
135 }
bougault 7.18 136 get html() {
137 return `
bougault 1.1 138 <div>
139 <a href=${this.hrefAttr}>${this.titleAttr}</a>
140 <div>
141 <slot></slot>
142 </div>
bougault 7.18 143 </div> `
bougault 1.1 144 }
bougault 7.18 145
bougault 1.1 146 connectedCallback() {
147 this.attachShadow({ mode: 'open' });
bougault 7.18 148 this.shadowRoot.appendChild(this.template.content.cloneNode(true));
bougault 1.1 149 }
150 }
151 );
bougault 3.1 152 </script>
bougault 7.2 153 <clb-simplified-search></clb-simplified-search>
bougault 3.1 154 {{/html}}