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


Changes for page CLB Search Component

Last modified by bougault on 2023/01/17 11:42

From version 7.13
edited by bougault
on 2023/01/13 14:10
Change comment: There is no comment for this version
To version 7.30
edited by bougault
on 2023/01/13 14:52
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -9,17 +9,24 @@
9 9   this.attachShadow({ mode: 'open'});
10 10   }
11 11  
12 - createTemplate(htmlString) {
12 + get template() {
13 13   const template = document.createElement('template');
14 14   template.innerHTML = `
15 15   <style>
16 16   ${this.styles}
17 17   </style>
18 - ${htmlString}
18 + ${this.html}
19 19   `;
20 20   return template;
21 21   }
22 22  
23 + get styles() {
24 + return '';
25 + }
26 +
27 + get html() {
28 + return '';
29 + }
23 23   $(selector) {
24 24   return this.shadowRoot.querySelector(selector);
25 25   }
... ... @@ -39,23 +39,41 @@
39 39   }
40 40  
41 41   connectedCallback() {
42 - // this.attachShadow({ mode: 'open' });
43 43   super.connectedCallback();
44 44   this.render();
45 - this.$('#search').addEventListener('click', () => this.handleSearch());
46 - this.$('#prev').addEventListener('click', () => {
47 - if(this.offset > 0) {
48 - this.offset--;
49 - this.handleSearch();
50 - }
51 - });
52 - this.$('#next').addEventListener('click', () => {
53 - this.offset++;
54 - this.handleSearch();
55 - });
51 + const searchInput = this.$('input[type="text"]');
52 + this.$('#search').addEventListener('click', this.handleSearch);
53 + this.$('#prev').addEventListener('click', this.handlePrevPage);
54 + this.$('#next').addEventListener('click', this.handleNextPage);
55 + searchInput.addEventListener('keypress', this.handleKeyPress);
56 + searchInput.focus();
56 56   }
57 57  
59 + disconnectedCallback() {
60 + this.$('#search').removeEventListener('click', this.handleSearch);
61 + this.$('#prev').removeEventListener('click', this.handlePrevPage);
62 + this.$('#next').removeEventListener('click', this.handleNextPage);
63 + this.$('input[type="text"]').addEventListener('keypress', this.handleKeyPress);
64 + }
58 58  
66 + get styles() {
67 + return `
68 + input[type="text"] {
69 + border: 1px solid red;
70 + }
71 + `
72 + }
73 + handlePrevPage = () => {
74 + if(this.offset > 0) {
75 + this.offset--;
76 + this.handleSearch();
77 + }
78 + }
79 + handleNextPage = () => {
80 + this.offset++;
81 + this.handleSearch();
82 + }
83 +
59 59   handleSearch = () => {
60 60   const value = this.$('input[type="text"]').value;
61 61   fetch(`${this.searchBaseUrl}${encodeURIComponent(value)}&offset=${this.offset}`)
... ... @@ -63,9 +63,14 @@
63 63   .then(this.renderResults);
64 64   }
65 65  
91 + handleKeyPress = (e) => {
92 + if(e.code === 'Enter') {
93 + this.handleSearch();
94 + }
95 + }
66 66  
67 - get searchInputTemplate() {
68 - return this.createTemplate(`
97 + get html() {
98 + return `
69 69   <div>
70 70   <input type="text"><button id="search">Search</button>
71 71   <div id="results"></div>
... ... @@ -72,7 +72,7 @@
72 72   <button id="prev">prev</button>
73 73   <button id="next">next</button>
74 74   </div>
75 - `);
105 + `;
76 76   }
77 77  
78 78   renderResults = (results) => {
... ... @@ -82,7 +82,7 @@
82 82   const { title_, spaces, doccontent_en } = results[i];
83 83   const elt = document.createElement('clb-search-result');
84 84   elt.setAttribute('data-title', title_);
85 - elt.setAttribute('data-href', spaces.join('/'));
115 + elt.setAttribute('data-spaces', spaces);
86 86   elt.innerHTML = doccontent_en;
87 87   resultsContainer.appendChild(elt);
88 88   }
... ... @@ -89,7 +89,7 @@
89 89   }
90 90  
91 91   render() {
92 - this.shadowRoot.appendChild(this.searchInputTemplate.content.cloneNode(true));
122 + this.shadowRoot.appendChild(this.template.content.cloneNode(true));
93 93   }
94 94   }
95 95  );
... ... @@ -104,23 +104,30 @@
104 104   return this.getAttribute('data-title');
105 105   }
106 106  
107 - get hrefAttr() {
108 - return this.getAttribute('data-href');
137 + get spacesAttr() {
138 + return this.getAttribute('data-spaces');
109 109   }
110 110  
111 - get resultTemplate() {
112 - return this.createTemplate(`
141 + get styles() {
142 + return `
143 + a {
144 + color: var(--color-brand-primary);
145 + }
146 + `;
147 + }
148 + get html() {
149 + return `
113 113   <div>
114 - <a href=${this.hrefAttr}>${this.titleAttr}</a>
151 + <a href="/bin/view/${this.spacesAttr.join("/")}>${this.titleAttr}</a>
115 115   <div>
116 116   <slot></slot>
117 117   </div>
118 - </div>
119 - `);
155 + </div> `
120 120   }
157 +
121 121   connectedCallback() {
122 122   this.attachShadow({ mode: 'open' });
123 - this.shadowRoot.appendChild(this.resultTemplate.content.cloneNode(true));
160 + this.shadowRoot.appendChild(this.template.content.cloneNode(true));
124 124   }
125 125   }
126 126  );