@gudhub/ssg-web-components-library 1.0.27 → 1.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gudhub/ssg-web-components-library",
3
- "version": "1.0.27",
3
+ "version": "1.0.29",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -14,13 +14,14 @@ class BreadcrumbsComponent extends GHComponent {
14
14
  } catch (error) {
15
15
  console.error(error);
16
16
  }
17
- }
18
- else {
17
+ } else {
19
18
  this.breadcrumbsConfig = window.getConfig().componentsConfigs.breadcrumbsConfig;
20
19
  this.initialRoute = this.breadcrumbsConfig[0].routesTree;
21
20
 
22
21
  let currentUrl = new URL(window.location.href);
23
22
  currentUrl = currentUrl.searchParams.get('path');
23
+
24
+ this.items = this.generateBreadcrumbs(this.initialRoute, currentUrl);
24
25
  }
25
26
 
26
27
  this.items === null ? console.error(`Didn't find current route in config, current URL: ${currentUrl}`) : null;
@@ -1,6 +1,26 @@
1
+ # Work types
2
+
3
+ We have two different ways to use this component:
4
+
5
+ 1. Automatically: looks on URL of the page, finds path in three of routes (got from "breadcrumbsConfig") and builds breadcrumbs chain
6
+ 2. Manually: set attribute "data-items" which will display
7
+ Manually example:
8
+ ```
9
+ js: this.breadcrumbs = JSON.stringify([
10
+ {
11
+ title: 'string',
12
+ link: '/'
13
+ },
14
+ {
15
+ title: "string"
16
+ }
17
+ ]);
18
+ ```
19
+ html: <breadcrumbs-component data-items='${breadcrumbs}'></breadcrumbs-component>
20
+
1
21
  # Attributes:
2
22
 
3
- None
23
+ data-items: allows for manual mode activation, parsing JSON-formatted data passed to it, and assigning the resulting JavaScript object to the items property of the current element.
4
24
 
5
25
  # Component data-object:
6
26
 
@@ -1,7 +1,7 @@
1
1
  <section>
2
2
  <div class="container">
3
3
  ${json.title ? `
4
- <h2 class="h2" gh-id="${ghId}.title"></h2>
4
+ <h${json?.hLevel ?? 2} class="h1" gh-id="${ghId}.title"></h${json?.hLevel ?? 2}>
5
5
  ` : ''}
6
6
  ${json.subtitle ? `
7
7
  <p class="subtitle" gh-id="${ghId}.subtitle"></p>
@@ -20,11 +20,25 @@ class MasonryGallery extends GHComponent {
20
20
  this.ghId = this.getAttribute('data-gh-id') || null;
21
21
  this.json = await super.getGhData(this.ghId);
22
22
 
23
- const isMoreItems = this.json.moreItems ? this.json.moreItems : null;
24
-
25
- // Passing second array to client
26
- this.setAttribute('init-images', JSON.stringify(this.json.items));
27
- this.setAttribute('add-array', JSON.stringify(isMoreItems));
23
+ if (Array.isArray(this.json.items)) {
24
+ const isMoreItems = this.json.moreItems ? this.json.moreItems : null;
25
+
26
+ // Passing second array to client
27
+ this.setAttribute('init-images', JSON.stringify(this.json.items));
28
+ this.setAttribute('add-array', JSON.stringify(isMoreItems));
29
+ } else if (typeof this.json.items === 'object' && this.json.items !== null) {
30
+ const {
31
+ initCount = null,
32
+ moreCount = null,
33
+ url
34
+ } = this.json.items;
35
+
36
+ this.setAttribute('images-url', url);
37
+ this.setAttribute('init-count', initCount);
38
+ this.setAttribute('more-count', moreCount);
39
+ } else {
40
+ console.error('const "images" is neither an array nor an object');
41
+ }
28
42
 
29
43
  super.render(html);
30
44
  }
@@ -32,8 +46,34 @@ class MasonryGallery extends GHComponent {
32
46
  async onClientReady() {
33
47
  const modal = document.getElementById('modal');
34
48
 
35
- const initImages = JSON.parse(this.getAttribute('init-images'));
36
- this.moreImages = JSON.parse(this.getAttribute('add-array'));
49
+ if (this.hasAttribute('images-url')) {
50
+ try {
51
+ const url = this.getAttribute('images-url');
52
+
53
+ const response = await fetch(url);
54
+ const data = await response.json();
55
+
56
+ const { images } = data;
57
+
58
+ const isInitImagesEqualNull = this.getAttribute('init-count') === 'null';
59
+
60
+ const initCountImages = this.hasAttribute('init-count') && !isInitImagesEqualNull ? this.getAttribute('init-count') : images.length;
61
+
62
+ const initImages = images.slice(0, initCountImages);
63
+ const initMoreImages = images.slice(initCountImages);
64
+
65
+ this.allImagesArrayLength = images.length;
66
+ this.initCountImages = initCountImages;
67
+
68
+ this.initImages = initImages;
69
+ this.moreImages = initMoreImages;
70
+ } catch (error) {
71
+ console.error(error);
72
+ }
73
+ } else {
74
+ this.initImages = JSON.parse(this.getAttribute('init-images'));
75
+ this.moreImages = JSON.parse(this.getAttribute('add-array'));
76
+ }
37
77
 
38
78
  const grid = this.imagesContainer;
39
79
 
@@ -45,11 +85,12 @@ class MasonryGallery extends GHComponent {
45
85
  });
46
86
 
47
87
  // Add initial images to the grid
48
- this.addImages(initImages);
88
+ this.addImages(this.initImages);
49
89
 
50
90
  // Add more images to the grid
51
91
  this.buttonMoreInit();
52
92
 
93
+ // Button for modal window which open form
53
94
  if (this.contactUsButton && this.contactUsButtonId) {
54
95
  const contactUsHTML = `
55
96
  <div class='contact-us-wrapper'>
@@ -112,14 +153,19 @@ class MasonryGallery extends GHComponent {
112
153
 
113
154
  addImages = (imagesSrcArray) => {
114
155
  // Iterate through each image source and add it to the grid
115
- imagesSrcArray.forEach(({ image }) => {
116
- const { src, alt, title, fullImage } = image;
117
-
118
- this.addImage(src, alt, title, fullImage);
119
- });
156
+ if (this.hasAttribute('images-url')) {
157
+ // TODO: need to add alt and title for images in case when we fetch them from endpoint
158
+ imagesSrcArray.forEach(({ src, fullImage }) => this.addImage(src, null, null, fullImage));
159
+ } else {
160
+ imagesSrcArray.forEach(({ image }) => {
161
+ const { src, alt, title, fullImage } = image;
162
+
163
+ this.addImage(src, alt, title, fullImage);
164
+ });
165
+ }
120
166
  }
121
167
 
122
- addImage(imageSrc, imageAlt, imageTitle, fullImageSrc = null) {
168
+ addImage(imageSrc, imageAlt = '', imageTitle = '', fullImageSrc = null) {
123
169
  const msnry = this.msnry;
124
170
 
125
171
  const promise = new Promise((res, rej) => {
@@ -171,28 +217,45 @@ class MasonryGallery extends GHComponent {
171
217
  const buttonWrapper = document.querySelector('.button-wrapper');
172
218
  const button = buttonWrapper.querySelector('#grid-add-items');
173
219
  const addImages = this.addImages;
174
- const images = this.moreImages;
175
220
 
176
- if (!masonryGrid || !button || !buttonWrapper) return;
221
+ const isInitCountEqualAllLength = this.initCountImages === this.allImagesArrayLength;
222
+
223
+ if (!masonryGrid || !button || !buttonWrapper || !this.moreImages || isInitCountEqualAllLength) {
224
+ if (button) button.remove();
225
+ return;
226
+ };
227
+
228
+ // Make a copy of moreImages to avoid modifying the original array
229
+ let images = [...this.moreImages];
230
+
231
+ // Get more-count attribute value
232
+ const moreCountImages = parseInt(this.getAttribute('more-count')) || images.length;
177
233
 
178
- // Add additional images to the grid
179
234
  button.addEventListener('click', async () => {
180
- // If we set max-height for block, this code remove styles which hide content, when we clicked show more
235
+ // If we set max-height for block, this code removes styles which hide content, when we click show more
181
236
  masonryGrid.style.maxHeight = 'none';
182
237
  masonryGrid.style.overflowY = 'visible';
183
- masonryGrid.style.scrollbarWidth = 'auto';
184
- masonryGrid.style.msOverflowStyle = 'auto';
185
-
186
- const styleSheet = document.styleSheets[0];
238
+ masonryGrid.style.scrollbarWidth = 'auto';
239
+ masonryGrid.style.msOverflowStyle = 'auto';
240
+
241
+ const styleSheet = document.styleSheets[0];
187
242
  styleSheet.insertRule('.masonry-grid::-webkit-scrollbar { display: auto; }', styleSheet.cssRules.length);
188
243
 
189
- // For animation, when we open full block of images
244
+ // Get the next batch of images
245
+ const imagesToAdd = images.slice(0, moreCountImages);
246
+
247
+ // Delay use to prevent masonry async bugs
190
248
  setTimeout(() => {
191
- addImages(images);
249
+ addImages(imagesToAdd);
250
+
251
+ // Update the images array to remove added images
252
+ images = images.slice(moreCountImages);
192
253
 
193
- button.disabled = true;
194
- button.style.display = 'none';
195
- }, 100)
254
+ if (images.length === 0) {
255
+ button.disabled = true;
256
+ button.style.display = 'none';
257
+ }
258
+ }, 100);
196
259
  });
197
260
  }
198
261
  }
@@ -1,3 +1,19 @@
1
+ # Description
2
+
3
+ To implement the functionality of the "Load more" button, we need to pass two values ​​to the JSON: `initCount` and `moreCount`, or just `initCount`.
4
+ 1. If only `initCount` is passed in the JSON, clicking the "Load more" button will open all photos at once.
5
+
6
+ 2. If only `moreCount` without `initCount` is passed in the JSON, the Load More button will not appear and all images will be displayed at once.
7
+
8
+ 3. If both values ​​are passed in JSON, then the number of images specified in `initCount` will be displayed first.
9
+ Clicking the "Upload more" button will load additional images, the number of which corresponds to the `moreCount' value.
10
+
11
+ # Variants
12
+
13
+ We have two options how you can use this component:
14
+ 1. When items it's array with objects where we can have as a key image
15
+ 2. When items it's object with endpoint for fetching data with images
16
+
1
17
  # Attributes:
2
18
 
3
19
  data-column-width: we need to pass column width value (number), for gap between grid items
@@ -9,32 +25,49 @@
9
25
  ("?" means "unnecessary")
10
26
 
11
27
  ```json
12
- {
13
- "title?": "Item Title",
14
- "subtitle?": "Item Subtitle",
15
- "button?": "Button text",
16
- "items": [ // main set of images for first render
17
- {
18
- // basicly grid was created for 3 columns, it can be changed by changing .grid-item width(it accepts value in percents)
19
- "image": {
20
- "src": "Path To File", // path should be like '/assets/...../photo1.png' and changing only the numbers for the others images in the end of name of the file
21
- "alt": "Item Alt",
22
- "title": "Item Title",
23
- "fullImage?": "Path To File" // it must be image for modal window, when we click on image from grid
28
+
29
+ #Variant-1
30
+
31
+ {
32
+ "title?": "Item Title",
33
+ "subtitle?": "Item Subtitle",
34
+ "button?": "Button text",
35
+ "items": [ // main set of images for first render
36
+ {
37
+ // basicly grid was created for 3 columns, it can be changed by changing .grid-item width(it accepts value in percents)
38
+ "image": {
39
+ "src": "Path To File", // path should be like '/assets/...../photo1.png' and changing only the numbers for the others images in the end of name of the file
40
+ "alt": "Item Alt",
41
+ "title": "Item Title",
42
+ "fullImage?": "Path To File" // it must be image for modal window, when we click on image from grid
43
+ }
24
44
  }
25
- }
26
- ],
27
- "moreItems?": [ // images which will be added to layout when you press the button
28
- {
29
- "image": {
30
- "src": "Path To File", // path should be like '/assets/...../photo1.png' and changing only the numbers for the others images in the end of name of the file
31
- "alt": "Item Alt",
32
- "title": "Item Title",
33
- "fullImage?": "Path To File" // it must be image for modal window, when we click on image from grid
45
+ ],
46
+ "moreItems?": [ // images which will be added to layout when you press the button
47
+ {
48
+ "image": {
49
+ "src": "Path To File", // path should be like '/assets/...../photo1.png' and changing only the numbers for the others images in the end of name of the file
50
+ "alt": "Item Alt",
51
+ "title": "Item Title",
52
+ "fullImage?": "Path To File" // it must be image for modal window, when we click on image from grid
53
+ }
34
54
  }
55
+ ]
56
+ }
57
+
58
+ #Variant-2
59
+
60
+ {
61
+ "title?": "Item Title",
62
+ "subtitle?": "Item Subtitle",
63
+ "button?": "Button text",
64
+ "items": {
65
+ "url": "Endpoint for fetching",
66
+ "initCount": "number",
67
+ "moreCount": "number"
35
68
  }
36
- ]
37
- }
69
+ }
70
+
38
71
  ```
39
72
 
40
73
  ## Button settings