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

Show last authors
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 = '/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 this.offset = 0;
34 }
35
36 connectedCallback() {
37 // this.attachShadow({ mode: 'open' });
38 super.connectedCallback();
39 this.render();
40 this.$('#search').addEventListener('click', () => this.handleSearch());
41 this.$('#prev').addEventListener('click', () => {
42 if(this.offset > 0) {
43 this.offset--;
44 this.handleSearch();
45 }
46 });
47 this.$('#next').addEventListener('click', () => {
48 this.offset++;
49 this.handleSearch();
50 });
51 }
52
53
54 handleSearch = () => {
55 const value = this.$('input[type="text"]').value;
56 fetch(`${this.searchBaseUrl}${encodeURIComponent(value)}&offset=${this.offset}&nb=10`)
57 .then(res => res.json())
58 .then(this.renderResults);
59 }
60
61
62 get searchInputTemplate() {
63 return this.createTemplate(`
64 <div>
65 <input type="text"><button id="search">Search</button>
66 <div id="results"></div>
67 <button id="prev">prev</button>
68 <button id="next">next</button>
69 </div>
70 `);
71 }
72
73 renderResults = (results) => {
74 const resultsContainer = this.$('#results');
75 resultsContainer.replaceChildren();
76 for(let i = 0, j = results.length; i < j; i++) {
77 const { title_, spaces, doccontent_en } = results[i];
78 const elt = document.createElement('clb-search-result');
79 elt.setAttribute('data-title', title_);
80 elt.setAttribute('data-href', spaces.join('/'));
81 elt.innerHTML = doccontent_en;
82 resultsContainer.appendChild(elt);
83 }
84 }
85
86 render() {
87 this.shadowRoot.appendChild(this.searchInputTemplate.content.cloneNode(true));
88 }
89 }
90 );
91
92 customElements.define('clb-search-result',
93 class extends ClbComponent {
94 constructor() {
95 super();
96 }
97
98 get titleAttr() {
99 return this.getAttribute('data-title');
100 }
101
102 get hrefAttr() {
103 return this.getAttribute('data-href');
104 }
105
106 get resultTemplate() {
107 return this.createTemplate(`
108 <div>
109 <a href=${this.hrefAttr}>${this.titleAttr}</a>
110 <div>
111 <slot></slot>
112 </div>
113 </div>
114 `);
115 }
116 connectedCallback() {
117 this.attachShadow({ mode: 'open' });
118 this.shadowRoot.appendChild(this.resultTemplate.content.cloneNode(true));
119 }
120 }
121 );
122 </script>
123 <clb-simplified-search></clb-simplified-search>
124 {{/html}}