Skip to content

Commit 0c0db52

Browse files
authored
Merge pull request #533 from vpetrusevici/fix-ssr-error
Fix error on Angular SSR
2 parents 6bbd76e + 0303fa2 commit 0c0db52

File tree

6 files changed

+143
-136
lines changed

6 files changed

+143
-136
lines changed

package-lock.json

Lines changed: 38 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"@angular/common": "^15.1.0",
1818
"@angular/compiler": "^15.1.0",
1919
"@angular/core": "^15.1.0",
20-
"@angular/flex-layout": "^14.0.0-beta.41",
20+
"@angular/flex-layout": "^15.0.0-beta.42",
2121
"@angular/forms": "^15.1.0",
2222
"@angular/material": "^15.1.0",
2323
"@angular/platform-browser": "^15.1.0",

projects/ng-gallery/src/lib/components/gallery-item.component.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import {
77
AfterViewChecked,
88
ElementRef,
99
ChangeDetectorRef,
10-
ChangeDetectionStrategy
10+
ChangeDetectionStrategy,
1111
} from '@angular/core';
12+
import { Platform } from '@angular/cdk/platform';
1213
import { GalleryConfig } from '../models/config.model';
1314
import { LoadingStrategy, GalleryItemType } from '../models/constants';
1415
import { GalleryItemData, ImageItemData, VideoItemData, YoutubeItemData } from './templates/items.model';
@@ -149,13 +150,15 @@ export class GalleryItemComponent implements AfterViewChecked {
149150
return this.data;
150151
}
151152

152-
constructor(private el: ElementRef, private cd: ChangeDetectorRef) {
153+
constructor(private el: ElementRef, private cd: ChangeDetectorRef, private _platform: Platform) {
153154
}
154155

155156
ngAfterViewChecked(): void {
156-
const height = this.getHeight();
157-
this.element.style.setProperty('--g-item-width', `${ this.getWidth() }px`);
158-
this.element.style.setProperty('--g-item-height', `${ height }px`);
157+
const height: number = this.getHeight();
158+
if (this._platform.isBrowser) {
159+
this.element.style.setProperty('--g-item-width', `${ this.getWidth() }px`);
160+
this.element.style.setProperty('--g-item-height', `${ height }px`);
161+
}
159162
if (this.currIndex === this.index) {
160163
// Auto-height feature, only allowed when sliding direction is horizontal
161164
const isThumbPositionHorizontal: boolean = this.config.thumbPosition === 'top' || this.config.thumbPosition === 'bottom';

projects/ng-gallery/src/lib/components/gallery-slider.component.ts

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { tap, debounceTime, filter, takeUntil, switchMap } from 'rxjs/operators'
3434
import { Gallery } from '../services/gallery.service';
3535
import { GalleryState, GalleryError } from '../models/gallery.model';
3636
import { GalleryConfig } from '../models/config.model';
37-
import { SlidingDirection, ThumbnailsView } from '../models/constants';
37+
import { SlidingDirection } from '../models/constants';
3838
import { SliderAdapter, HorizontalAdapter, VerticalAdapter } from './adapters';
3939
import { SmoothScrollManager } from '../smooth-scroll';
4040
import { resizeObservable } from '../utils/resize-observer';
@@ -162,89 +162,90 @@ export class GallerySliderComponent implements OnInit, OnChanges, AfterViewInit,
162162
this.adapter = new VerticalAdapter(this.slider, this.config);
163163
break;
164164
}
165-
166-
if (!changes.config.firstChange) {
167-
requestAnimationFrame(() => {
168-
// Keep the correct sliding position when direction changes
169-
this.scrollToIndex(this.state.currIndex, 'auto');
170-
});
165+
if (this._platform.isBrowser) {
166+
if (!changes.config.firstChange) {
167+
requestAnimationFrame(() => {
168+
// Keep the correct sliding position when direction changes
169+
this.scrollToIndex(this.state.currIndex, 'auto');
170+
});
171+
}
172+
// Reactivate gestures
173+
this.enableDisableGestures();
171174
}
172-
173-
// Reactivate gestures
174-
this.enableDisableGestures();
175175
}
176176

177-
if (!changes.config.firstChange) {
177+
if (this._platform.isBrowser && !changes.config.firstChange) {
178178
if (changes.config.currentValue?.mouseSlidingDisabled !== changes.config.previousValue?.mouseSlidingDisabled) {
179179
this.enableDisableGestures();
180180
}
181181
}
182182
}
183183

184184
// Scroll to current index
185-
if (changes.state && changes.state.currentValue?.currIndex !== changes.state.previousValue?.currIndex) {
185+
if (this._platform.isBrowser && changes.state && changes.state.currentValue?.currIndex !== changes.state.previousValue?.currIndex) {
186186
requestAnimationFrame(() => {
187187
this.scrollToIndex(this.state.currIndex, changes.state.firstChange ? 'auto' : this.state.behavior);
188188
});
189189
}
190190
}
191191

192192
ngOnInit(): void {
193-
this._zone.runOutsideAngular(() => {
194-
195-
// We need to set the visibleElements in the viewport using intersection observer
196-
this.createIntersectionObserver(this.slider).pipe(
197-
tap((entry: IntersectionObserverEntry) => {
198-
entry.target.classList.toggle('g-item-visible', entry.isIntersecting);
199-
if (entry.isIntersecting) {
200-
this.visibleElements.set(entry.target, entry);
201-
} else {
202-
this.visibleElements.delete(entry.target);
203-
}
204-
}),
205-
takeUntil(this._destroyed$)
206-
).subscribe();
193+
if (this._platform.isBrowser) {
194+
this._zone.runOutsideAngular(() => {
195+
// We need to set the visibleElements in the viewport using intersection observer
196+
this.createIntersectionObserver(this.slider).pipe(
197+
tap((entry: IntersectionObserverEntry) => {
198+
entry.target.classList.toggle('g-item-visible', entry.isIntersecting);
199+
if (entry.isIntersecting) {
200+
this.visibleElements.set(entry.target, entry);
201+
} else {
202+
this.visibleElements.delete(entry.target);
203+
}
204+
}),
205+
takeUntil(this._destroyed$)
206+
).subscribe();
207207

208-
// Subscribe to slider scroll event
209-
fromEvent(this.slider, 'scroll', { passive: true }).pipe(
210-
debounceTime(50),
211-
filter(() => !this._isPanning),
212-
tap(() => this.onViewportScroll()),
213-
takeUntil(this._destroyed$)
214-
).subscribe();
208+
// Subscribe to slider scroll event
209+
fromEvent(this.slider, 'scroll', { passive: true }).pipe(
210+
debounceTime(50),
211+
filter(() => !this._isPanning),
212+
tap(() => this.onViewportScroll()),
213+
takeUntil(this._destroyed$)
214+
).subscribe();
215215

216-
// Detect if the size of the slider has changed detecting current index on scroll
217-
if (this._platform.isBrowser) {
216+
// Detect if the size of the slider has changed detecting current index on scroll
218217
resizeObservable(this._el.nativeElement).pipe(
219218
debounceTime(this.config.resizeDebounceTime),
220219
tap(([entry]: ResizeObserverEntry[]) => this.onHostResize(entry)),
221220
takeUntil(this._destroyed$)
222221
).subscribe();
223-
}
224-
});
222+
});
223+
}
225224
}
226225

227226
ngAfterViewInit(): void {
228-
this.items.notifyOnChanges();
229-
this.items.changes.pipe(
230-
startWith(null),
231-
tap(() => {
232-
// Disconnect all and reconnect later
233-
this.visibleElements.forEach((item: IntersectionObserverEntry) => {
234-
this.intersectionObserver.unobserve(item.target);
235-
});
227+
if (this._platform.isBrowser) {
228+
this.items.notifyOnChanges();
229+
this.items.changes.pipe(
230+
startWith(null),
231+
tap(() => {
232+
// Disconnect all and reconnect later
233+
this.visibleElements.forEach((item: IntersectionObserverEntry) => {
234+
this.intersectionObserver.unobserve(item.target);
235+
});
236236

237-
// Connect with the new items
238-
this.items.toArray().map((item: GalleryItemComponent) => {
239-
this.intersectionObserver.observe(item.element);
240-
});
241-
}),
242-
takeUntil(this._destroyed$)
243-
).subscribe();
237+
// Connect with the new items
238+
this.items.toArray().map((item: GalleryItemComponent) => {
239+
this.intersectionObserver.observe(item.element);
240+
});
241+
}),
242+
takeUntil(this._destroyed$)
243+
).subscribe();
244+
}
244245
}
245246

246247
ngAfterViewChecked(): void {
247-
if (this.config.itemAutosize) {
248+
if (this.config.itemAutosize && this._platform.isBrowser) {
248249
this.slider.style.setProperty('--slider-centralize-start-size', this.adapter.getCentralizerStartSize() + 'px');
249250
this.slider.style.setProperty('--slider-centralize-end-size', this.adapter.getCentralizerEndSize() + 'px');
250251
}

0 commit comments

Comments
 (0)