project.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { Component, inject } from '@angular/core';
  2. import { ActivatedRoute } from '@angular/router';
  3. import { Title } from '@angular/platform-browser';
  4. import { ProjectService } from '../service/project-service';
  5. import { TranslateService } from '@ngx-translate/core';
  6. import { UtilService } from '../service/util-service';
  7. import { ProjectModel } from '../model/project';
  8. import { environment } from '../../environments/environment';
  9. @Component({
  10. selector: 'app-project', templateUrl: './project.html', styleUrl: './project.scss'
  11. })
  12. export class Project {
  13. project: ProjectModel | undefined;
  14. apiURL: string = environment.apiUrl;
  15. utilService: UtilService;
  16. private translate = inject(TranslateService)
  17. /**
  18. * Index of the photo currently on the gallery.
  19. */
  20. private curIndex: number = -1;
  21. /**
  22. * Total number of images.
  23. */
  24. private maxIndex: number = -1;
  25. constructor(
  26. private route: ActivatedRoute, private projectService:
  27. ProjectService, private titleService: Title
  28. ){
  29. this.utilService = new UtilService();
  30. }
  31. ngOnInit(): void {
  32. const id = this.route.snapshot.paramMap.get('id');
  33. // Use the id to fetch data or perform actions
  34. this.projectService.getProject("" + id).subscribe(data => {
  35. this.project = data;
  36. this.maxIndex = this.project.images.length;
  37. this.titleService.setTitle(
  38. this.project.title + " - " + this.translate.instant('SITE.TITLE')
  39. );
  40. });
  41. }
  42. /**
  43. * Opens the gallery.
  44. *
  45. * @param index Index of the photo to display
  46. */
  47. galleryOpen(index: any){
  48. let cover: HTMLDivElement = <HTMLDivElement> document.getElementById('gallery-cover');
  49. let gallery: HTMLDivElement = <HTMLDivElement> document.getElementById('gallery');
  50. cover.style.display = 'block';
  51. cover.style.opacity = '0.6';
  52. gallery.style.display = 'block';
  53. gallery.style.opacity = '1';
  54. if (index >= 0 && index <= this.maxIndex){
  55. this.curIndex = index;
  56. this.gallerySet();
  57. }
  58. else console.error('Invalid image index: ' + index + '/' + this.maxIndex);
  59. }
  60. /**
  61. * Closes the gallery.
  62. */
  63. galleryClose(){
  64. let cover: HTMLDivElement = <HTMLDivElement> document.getElementById('gallery-cover');
  65. let gallery: HTMLDivElement = <HTMLDivElement> document.getElementById('gallery');
  66. let video: HTMLVideoElement = <HTMLVideoElement> document.getElementById('gallery-video');
  67. cover.style.display = 'none';
  68. cover.style.opacity = '0';
  69. gallery.style.display = 'none';
  70. gallery.style.opacity = '0';
  71. video.pause();
  72. video.currentTime = 0;
  73. }
  74. private delay(ms: number){ return new Promise(resolve => setTimeout(resolve, ms)); }
  75. /**
  76. * Pupulates the data in the gallery.
  77. */
  78. async gallerySet(){
  79. var fade = <HTMLDivElement> document.getElementById('gallery-fade');
  80. fade.style.display = 'block';
  81. fade.style.opacity = '1';
  82. await this.delay(200);
  83. let titleText = document.getElementById('img-' + this.curIndex)?.getAttribute("alt") || "";
  84. if (titleText != null && titleText.length > 0 && titleText != "" + this.project?.title)
  85. titleText = ': ' + titleText;
  86. else titleText = '';
  87. let title: HTMLSpanElement = <HTMLSpanElement> document.getElementById('gallery-title');
  88. let text: HTMLParagraphElement
  89. = <HTMLParagraphElement> document.getElementById('gallery-text');
  90. let image: HTMLImageElement = <HTMLImageElement> document.getElementById('gallery-img');
  91. let video: HTMLVideoElement = <HTMLVideoElement> document.getElementById('gallery-video');
  92. title.innerHTML = titleText;
  93. if (document.getElementById('img-' + this.curIndex)?.dataset["video"] == 'true'){
  94. image.style.display = 'none';
  95. video.style.display = 'block';
  96. video.setAttribute(
  97. "src", document.getElementById('img-' + this.curIndex)?.getAttribute("src") || ""
  98. );
  99. }
  100. else{
  101. video.pause();
  102. video.currentTime = 0;
  103. video.style.display = 'none';
  104. image.style.display = 'block';
  105. image.src = document.getElementById('img-' + this.curIndex)?.getAttribute("src") || "";
  106. image.srcset
  107. = document.getElementById('img-' + this.curIndex)?.getAttribute("srcset") || "";
  108. }
  109. let description: string
  110. = document.getElementById('img-' + this.curIndex)?.dataset["description"] || "";
  111. text.innerHTML = description;
  112. if (description.length > 0) text.style.display = 'block';
  113. else text.style.display = 'none';
  114. fade.style.opacity = '0';
  115. await this.delay(200);
  116. fade.style.display = 'none';
  117. }
  118. /**
  119. * Displays the next photo in the gallery.
  120. */
  121. galleryNext(){
  122. this.curIndex ++;
  123. if (this.curIndex >= this.maxIndex) this.curIndex = 1;
  124. this.gallerySet();
  125. }
  126. /**
  127. * Displays the previous photo in the gallery.
  128. */
  129. galleryPrev(){
  130. this.curIndex --;
  131. if (this.curIndex < 1) this.curIndex = this.maxIndex;
  132. this.gallerySet();
  133. }
  134. private swipeCoord?: [number, number];
  135. private swipeTime?: number;
  136. gallerySwipe(e: TouchEvent, when: string): void {
  137. console.log("SWIPE");
  138. const coord: [number, number] = [e.changedTouches[0].pageX, e.changedTouches[0].pageY];
  139. const time = new Date().getTime();
  140. if (when === 'start') {
  141. this.swipeCoord = coord;
  142. this.swipeTime = time;
  143. }
  144. else if (this.swipeCoord && this.swipeTime && when === 'end') {
  145. const direction = [coord[0] - this.swipeCoord[0], coord[1] - this.swipeCoord[1]];
  146. const duration = time - this.swipeTime;
  147. if (
  148. duration < 1000
  149. && Math.abs(direction[0]) > 30 && Math.abs(direction[0]) > Math.abs(direction[1] * 3)
  150. ){
  151. if (direction[0] < 0) this.galleryNext();
  152. else this.galleryPrev();
  153. }
  154. }
  155. }
  156. }