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