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.18 by bougault on 2023/01/13 14:17

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.18 18 ${this.template}
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 // this.attachShadow({ mode: 'open' });
47 super.connectedCallback();
48 this.render();
bougault 7.7 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 });
bougault 1.1 60 }
61
bougault 7.15 62 get styles() {
63 return `
bougault 7.16 64 input[type="text"] {
65 border: 1px solid red;
bougault 7.15 66 }
67 `
68 }
bougault 1.1 69
bougault 7.4 70 handleSearch = () => {
bougault 7.7 71 const value = this.$('input[type="text"]').value;
bougault 7.12 72 fetch(`${this.searchBaseUrl}${encodeURIComponent(value)}&offset=${this.offset}`)
bougault 7.4 73 .then(res => res.json())
74 .then(this.renderResults);
bougault 1.1 75 }
76
77
bougault 7.18 78 get html() {
79 return `
bougault 7.4 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>
bougault 7.18 86 `;
bougault 1.1 87 }
88
bougault 7.6 89 renderResults = (results) => {
bougault 7.4 90 const resultsContainer = this.$('#results');
bougault 1.1 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() {
bougault 7.18 103 this.shadowRoot.appendChild(this.template.content.cloneNode(true));
bougault 1.1 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
bougault 7.17 122 get styles() {
123 return `
124 a {
125 color: var(--color-brand-primary);
126 }
127 `;
128 }
bougault 7.18 129 get html() {
130 return `
bougault 1.1 131 <div>
132 <a href=${this.hrefAttr}>${this.titleAttr}</a>
133 <div>
134 <slot></slot>
135 </div>
bougault 7.18 136 </div> `
bougault 1.1 137 }
bougault 7.18 138
bougault 1.1 139 connectedCallback() {
140 this.attachShadow({ mode: 'open' });
bougault 7.18 141 this.shadowRoot.appendChild(this.template.content.cloneNode(true));
bougault 1.1 142 }
143 }
144 );
bougault 3.1 145 </script>
bougault 7.2 146 <clb-simplified-search></clb-simplified-search>
bougault 3.1 147 {{/html}}