All files / app/elements/preview-destination-modal preview-destination-modal.component.ts

87.95% Statements 168/191
72.41% Branches 21/29
93.75% Functions 15/16
87.95% Lines 168/191

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 1921x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 13x 13x 14x 14x 14x 14x 14x 1x 14x 14x 14x 14x 13x 13x       13x 13x 16x 17x 17x   17x   17x   17x   17x 17x 17x     17x 16x 16x 16x 1x 16x 16x 16x 16x 16x                 16x 16x 13x 13x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x             1x 1x 1x 13x 13x 1x 1x 1x 1x 13x 13x 2x 2x 2x 13x 13x 1x 1x 13x 13x 1x 1x 13x 13x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 1x 1x 1x 1x 13x 13x 1x 1x 13x 13x 1x 1x 13x  
import { NgStyle } from '@angular/common';
import { Component, HostListener, Input, OnInit, ViewChild, inject, output } from '@angular/core';
import { CityClass } from '@classes/city.class';
import { MarketingCityClass } from '@classes/marketing_city.class';
import { NetworkService } from '@services/network.service';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { CarouselModule, OwlOptions, SlidesOutputData } from 'ngx-owl-carousel-o';
 
@Component({
	selector: 'grt-preview-destination-modal',
	templateUrl: './preview-destination-modal.component.html',
	styleUrls: ['./preview-destination-modal.component.scss'],
	imports: [NgStyle, CarouselModule],
})
export class PreviewDestinationModalComponent implements OnInit {
	bsModalRef = inject(BsModalRef);
	private network = inject(NetworkService);
 
	@ViewChild('owlCar', { static: false }) owlCarousel: any;
	@Input() city!: CityClass;
	selectedCity!: CityClass;
	marketing: MarketingCityClass = new MarketingCityClass();
	galleryImages: any[] = [];
	imageUrl: string = '';
	imagesLoaded = false;
	animationState = 'initial';
	activeSlides!: SlidesOutputData;
	images: any = [];
	mobile = false;
	screenSize = 343;
	screenShow = 0;
	show_more = false;
	image_selected = 0;
	isLoading = true;
	summary = '';
	_album: any[] = [];
	imagesLoadedCount = 0;
	readonly addCityToTrip = output<any>();
	readonly removeCityFromTrip = output<any>();
	carouselData: any[] = [];
	showLessActive: boolean = false;
 
	customOptions: OwlOptions = {
		loop: true,
		autoplay: false,
		center: false,
		dots: false,
		autoHeight: false,
		autoWidth: true,
		margin: 200,
		items: 1,
		merge: false,
		lazyLoad: false,
	};
 
	@HostListener('window:resize', ['$event'])
	onResize(): void {
		this.getDetails(); // Call the getDetails function on window resize
	}
 
	ngOnInit() {
		this.selectedCity = this.city;
		this.images =
			this.city &&
			this.city.marketingCity &&
			this.city.marketingCity.cityGalleryImages &&
			this.city.marketingCity.cityGalleryImages?.length
				? this.city.marketingCity.cityGalleryImages
				: this.city && this.city.marketingCity?.images;
		this.getDetails();
	}
 
	onClose() {
		this.imagesLoadedCount = 0;
		this.bsModalRef.hide(); // Close the modal
	}
 
	getDetails() {
		const updateScreen = () => {
			this.screenSize = document.body.offsetWidth;
			if (this.screenSize >= 767) {
				this.screenShow = 720;
			} else if (this.screenSize <= 424 && this.screenSize >= 390) {
				this.screenShow = 330;
			} else if (this.screenSize <= 389 && this.screenSize >= 375) {
				this.screenShow = 310;
			} else if (this.screenSize <= 374 && this.screenSize > 320) {
				this.screenShow = 280;
			} else if (this.screenSize <= 320) {
				this.screenShow = 255;
			} else {
				this.screenShow = 350;
			}
			this.mobile = document.body.offsetWidth <= 1050;
		};
 
		window.onresize = () => {
			updateScreen();
		};
 
		updateScreen();
 
		this.network.getCity(this.city && this.city.slug).then((city) => {
			this.city = city;
			this.marketing = this.city.getMarketingCity();
			this.images =
				city.marketingCity.cityGalleryImages && city.marketingCity.cityGalleryImages?.length
					? city.marketingCity.cityGalleryImages
					: this.marketing.getImages();
			this.initializeSlides();
			this.showLess();
		});
	}
 
	initializeSlides() {
		this.carouselData = (
			this.city.marketingCity.cityGalleryImages && this.city.marketingCity.cityGalleryImages?.length
				? this.city.marketingCity.cityGalleryImages
				: this.marketing.getImages()
		).map((item, index) => {
			if (this.city.marketingCity.cityGalleryImages && this.city.marketingCity.cityGalleryImages?.length) {
				return {
					id: `slide-${index + 1}`,
					url: item.url,
					item: null,
				};
			} else {
				return {
					id: `slide-${index + 1}`,
					url: null,
					item: item,
				};
			}
		});
		this.isLoading = false;
	}
 
	moveToSlide(index: number) {
		const slideName = `slide-${index + 1}`;
		this.owlCarousel.to(slideName);
		this.image_selected = index;
	}
 
	toggleShow() {
		this.show_more = !this.show_more;
		this.showLessActive = !this.show_more;
	}
 
	onCityAdd() {
		this.addCityToTrip.emit(this.city);
	}
 
	onCityRemove() {
		this.removeCityFromTrip.emit(this.city);
	}
 
	showLess() {
		this.showLessActive = false;
		const textContent = this.city.marketingCity.summary.replace(/<[^>]+>/g, '');
		this.summary = this.city.marketingCity.summary.replace(/style="[^"]*"/g, '');
		this.summary = this.summary.replace(/<p[^>]*>/g, '<div>').replace(/<\/p>/g, '</div>');
 
		this.summary = this.summary.replace(/<h2[^>]*>/g, '<div>').replace(/<\/h2>/g, '</div>');
 
		const maxLengths = 300;
		let truncatedText = textContent.substring(0, maxLengths);
 
		// Check if the last character is a space and adjust the text accordingly
		if (textContent[maxLengths] !== ' ' && textContent[maxLengths - 1] !== ' ') {
			const lastSpaceIndex = truncatedText.lastIndexOf(' ');
			if (lastSpaceIndex !== -1) {
				truncatedText = truncatedText.substring(0, lastSpaceIndex);
			}
		}
		return truncatedText + '...';
	}
 
	details() {
		// this.bsModalRef.hide();
		const url = `/destinations/${this.city && this.city.slug}`;
		window.open(url, '_blank'); // This opens the URL in a new tab/window
	}
 
	allImagesLoaded() {
		this.imagesLoaded = true;
	}
 
	allImagesLoad() {
		this.imagesLoadedCount++;
	}
}