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

geonetwork / geonetwork-ui / 14089329334

26 Mar 2025 05:09PM UTC coverage: 81.764% (-2.7%) from 84.416%
14089329334

Pull #1185

github

web-flow
Merge 67ed3c5f6 into 4de378079
Pull Request #1185: [Datahub] Update the UI for Metadata Quality Component

1891 of 2660 branches covered (71.09%)

Branch coverage included in aggregate %.

6 of 7 new or added lines in 3 files covered. (85.71%)

64 existing lines in 7 files now uncovered.

6256 of 7304 relevant lines covered (85.65%)

9.9 hits per line

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

51.4
/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
  downloadError = false
6✔
84
  showAltTextInput = false
6✔
85

86
  lastUploadType?: 'file' | 'url'
87
  lastUploadContent?: string | File
88

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

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

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

108
  getSecondaryText() {
109
    if (this.uploadError) {
6!
110
      return marker('input.image.uploadErrorRetry')
×
111
    }
112
    if (this.uploadProgress) {
6!
113
      return marker('input.image.uploadProgressCancel')
×
114
    }
115
    return marker('input.image.dropFileLabel')
6✔
116
  }
117

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

125
  handleDropFiles(files: File[]) {
126
    const validFiles = this.filterTypeImage(files)
×
127
    if (validFiles.length > 0) {
×
128
      this.showUrlInput = false
×
129
      this.resizeAndEmit(validFiles[0])
×
130
    }
131
  }
132

133
  handleFileInput(event: Event) {
UNCOV
134
    const inputFiles = Array.from((event.target as HTMLInputElement).files)
×
135
    const validFiles = this.filterTypeImage(inputFiles)
×
136
    if (validFiles.length > 0) {
×
137
      this.resizeAndEmit(validFiles[0])
×
138
    }
139
  }
140

141
  displayUrlInput() {
UNCOV
142
    this.uploadCancel.emit()
×
143
    this.showUrlInput = true
×
144
  }
145

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

178
  handleSecondaryTextClick(event: Event) {
UNCOV
179
    if (this.uploadError) {
×
UNCOV
180
      this.handleRetry()
×
181
    } else if (this.uploadProgress) {
×
182
      this.handleCancel()
×
183
      event.preventDefault()
×
184
    }
185
  }
186

187
  handleCancel() {
UNCOV
188
    this.uploadCancel.emit()
×
189
  }
190

191
  handleRetry() {
UNCOV
192
    switch (this.lastUploadType) {
×
193
      case 'file':
194
        this.fileChange.emit(this.lastUploadContent as File)
×
UNCOV
195
        break
×
196
      case 'url':
197
        this.urlChange.emit(this.lastUploadContent as string)
×
UNCOV
198
        break
×
199
    }
200
  }
201

202
  handleDelete() {
UNCOV
203
    this.delete.emit()
×
204
  }
205

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

210
  handleAltTextChange(altText: string) {
UNCOV
211
    this.altTextChange.emit(altText)
×
212
  }
213

214
  private filterTypeImage(files: File[]) {
215
    return files.filter((file) => {
1✔
216
      return file.type.startsWith('image/')
2✔
217
    })
218
  }
219

220
  private resizeAndEmit(imageToResize: File) {
UNCOV
221
    const maxSizeBytes = megabytesToBytes(this.maxSizeMB)
×
UNCOV
222
    downgradeImage(imageToResize, maxSizeBytes).then((resizedImage) => {
×
223
      const fileToEmit = new File([resizedImage], imageToResize.name)
×
224
      this.fileChange.emit(fileToEmit)
×
225
    })
226
  }
227
}
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