• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

geonetwork / geonetwork-ui / 14169610927

31 Mar 2025 10:36AM UTC coverage: 84.156% (+2.4%) from 81.764%
14169610927

Pull #1193

github

web-flow
Merge 62f0868ad into e122921ef
Pull Request #1193: Section subsection rework

1573 of 2112 branches covered (74.48%)

Branch coverage included in aggregate %.

5045 of 5752 relevant lines covered (87.71%)

10.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.07
/libs/ui/elements/src/lib/image-input/image-input.component.ts
1
import { CommonModule } from '@angular/common'
1✔
2
import { HttpClient } from '@angular/common/http'
1✔
3
import {
1✔
4
  ChangeDetectionStrategy,
5
  ChangeDetectorRef,
6
  Component,
7
  EventEmitter,
8
  Input,
9
  Output,
10
} from '@angular/core'
11
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'
1✔
12
import { marker } from '@biesbjerg/ngx-translate-extract-marker'
1✔
13
import {
1✔
14
  ButtonComponent,
15
  FilesDropDirective,
16
  TextInputComponent,
17
  UrlInputComponent,
18
} from '@geonetwork-ui/ui/inputs'
19
import { downgradeImage, megabytesToBytes } from '@geonetwork-ui/util/shared'
1✔
20
import {
1✔
21
  NgIconComponent,
22
  provideIcons,
23
  provideNgIconsConfig,
24
} from '@ng-icons/core'
25
import {
1✔
26
  iconoirBin,
27
  iconoirFramePlusIn,
28
  iconoirLink,
29
  iconoirMediaImage,
30
  iconoirMediaImageXmark,
31
  iconoirPlus,
32
} from '@ng-icons/iconoir'
33
import { TranslateModule } from '@ngx-translate/core'
1✔
34
import { firstValueFrom } from 'rxjs'
1✔
35
import { ImageOverlayPreviewComponent } from '../image-overlay-preview/image-overlay-preview.component'
1✔
36

37
@Component({
38
  selector: 'gn-ui-image-input',
39
  templateUrl: './image-input.component.html',
40
  styleUrls: ['./image-input.component.css'],
41
  changeDetection: ChangeDetectionStrategy.OnPush,
42
  standalone: true,
43
  imports: [
44
    CommonModule,
45
    ButtonComponent,
46
    FilesDropDirective,
47
    MatProgressSpinnerModule,
48
    TranslateModule,
49
    UrlInputComponent,
50
    TextInputComponent,
51
    NgIconComponent,
52
    ImageOverlayPreviewComponent,
53
  ],
54
  providers: [
55
    provideIcons({
56
      iconoirMediaImage,
57
      iconoirFramePlusIn,
58
      iconoirMediaImageXmark,
59
      iconoirBin,
60
      iconoirPlus,
61
      iconoirLink,
62
    }),
63
    provideNgIconsConfig({
64
      size: '1.5rem',
65
    }),
66
  ],
67
})
68
export class ImageInputComponent {
1✔
69
  @Input() maxSizeMB: number
70
  @Input() previewUrl?: string
71
  @Input() altText?: string
72
  @Input() uploadProgress?: number
73
  @Input() uploadError?: boolean
74
  @Input() disabled?: boolean = false
6✔
75
  @Output() fileChange: EventEmitter<File> = new EventEmitter()
6✔
76
  @Output() urlChange: EventEmitter<string> = new EventEmitter()
6✔
77
  @Output() uploadCancel: EventEmitter<void> = new EventEmitter()
6✔
78
  @Output() delete: EventEmitter<void> = new EventEmitter()
6✔
79
  @Output() altTextChange: EventEmitter<string> = new EventEmitter()
6✔
80

81
  dragFilesOver = false
6✔
82
  showUrlInput = false
6✔
83
  imageFileError = this.uploadError
6✔
84
  showAltTextInput = false
6✔
85
  pendingAltText: string
86

87
  get isUploadInProgress() {
88
    return this.uploadProgress !== undefined
36✔
89
  }
90

91
  constructor(
92
    private http: HttpClient,
6!
93
    private cd: ChangeDetectorRef
6✔
94
  ) {}
95

96
  getIsActionBlocked() {
97
    return this.isUploadInProgress || this.disabled
12✔
98
  }
99

100
  getPrimaryText() {
101
    if (this.imageFileError) {
6!
102
      return marker('input.image.uploadErrorLabel')
×
103
    }
104
    if (this.uploadProgress) {
6!
105
      return marker('input.image.uploadProgressLabel')
×
106
    }
107
    return marker('input.image.selectFileLabel')
6✔
108
  }
109

110
  getSecondaryText() {
111
    if (this.imageFileError) {
6!
112
      return '\u00A0' // (only to keep same spacing, next step is to handle "Retry")
×
113
    }
114
    if (this.uploadProgress) {
6!
115
      return marker('input.image.uploadProgressCancel')
×
116
    }
117
    return marker('input.image.dropFileLabel')
6✔
118
  }
119

120
  handleDragFilesOver(dragFilesOver: boolean) {
121
    if (!this.showUrlInput) {
×
122
      this.dragFilesOver = dragFilesOver
×
123
      this.cd.markForCheck()
×
124
    }
125
  }
126

127
  handleDropFiles(files: File[]) {
128
    this.resetErrors()
×
129
    const validFiles = this.filterTypeImage(files)
×
130
    if (validFiles.length > 0) {
×
131
      this.showUrlInput = false
×
132
      this.resizeAndEmit(validFiles[0])
×
133
    } else {
134
      this.imageFileError = true
×
135
    }
136
  }
137

138
  handleFileInput(event: Event) {
139
    this.resetErrors()
×
140
    const inputFiles = Array.from((event.target as HTMLInputElement).files)
×
141
    const validFiles = this.filterTypeImage(inputFiles)
×
142
    if (validFiles.length > 0) {
×
143
      this.resizeAndEmit(validFiles[0])
×
144
    } else {
145
      this.imageFileError = true
×
146
    }
147
  }
148

149
  displayUrlInput() {
150
    this.uploadCancel.emit()
×
151
    this.showUrlInput = true
×
152
  }
153

154
  async downloadUrl(url: string) {
155
    this.resetErrors()
4✔
156
    const name = url.split('/').pop()
4✔
157
    try {
4✔
158
      const response = await firstValueFrom(
4✔
159
        this.http.head(url, { observe: 'response' })
160
      )
161
      if (
2✔
162
        response.headers.get('content-type')?.startsWith('image/') &&
4✔
163
        parseInt(response.headers.get('content-length')) <
164
          megabytesToBytes(this.maxSizeMB)
165
      ) {
166
        this.http.get(url, { responseType: 'blob' }).subscribe({
2✔
167
          next: (blob) => {
168
            this.cd.markForCheck()
1✔
169
            const file = new File([blob], name)
1✔
170
            this.fileChange.emit(file)
1✔
171
          },
172
          error: () => {
173
            this.imageFileError = true
1✔
174
            this.cd.markForCheck()
1✔
175
            this.urlChange.emit(url)
1✔
176
          },
177
        })
178
      }
179
    } catch {
180
      this.imageFileError = true
×
181
      this.cd.markForCheck()
×
182
      return
×
183
    }
184
  }
185

186
  handleSecondaryTextClick(event: Event) {
187
    if (this.uploadProgress) {
×
188
      this.handleCancelUpload()
×
189
      event.preventDefault()
×
190
    }
191
  }
192

193
  handleCancelUpload() {
194
    this.uploadCancel.emit()
×
195
  }
196

197
  handleDelete() {
198
    this.delete.emit()
×
199
  }
200

201
  resetErrors() {
202
    this.imageFileError = false
4✔
203
    this.uploadError = false
4✔
204
  }
205

206
  toggleAltTextInput() {
207
    this.showAltTextInput = !this.showAltTextInput
×
208
  }
209

210
  handleAltTextChange(altText: string) {
211
    this.altTextChange.emit(altText)
×
212
  }
213
  private filterTypeImage(files: File[]) {
214
    return files.filter((file) => {
1✔
215
      return file.type.startsWith('image/')
2✔
216
    })
217
  }
218

219
  private resizeAndEmit(imageToResize: File) {
220
    const maxSizeBytes = megabytesToBytes(this.maxSizeMB)
×
221
    downgradeImage(imageToResize, maxSizeBytes).then((resizedImage) => {
×
222
      const fileToEmit = new File([resizedImage], imageToResize.name)
×
223
      this.fileChange.emit(fileToEmit)
×
224
    })
225
  }
226
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc