-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To be used for image file upload in forms, with live preview
- Loading branch information
Showing
16 changed files
with
266 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<ajf-file-input *ngIf="control|async as ctrl" accept="image/*" [formControl]="ctrl!"> | ||
<div ajfFilePreview class="ajf-image-preview"> | ||
<img [src]="imageUrl|async"> | ||
</div> | ||
</ajf-file-input> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
ajf-image-field { | ||
img { | ||
min-width: 32px; | ||
min-height: 32px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* @license | ||
* Copyright (C) Gnucoop soc. coop. | ||
* | ||
* This file is part of the Advanced JSON forms (ajf). | ||
* | ||
* Advanced JSON forms (ajf) is free software: you can redistribute it and/or | ||
* modify it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the License, | ||
* or (at your option) any later version. | ||
* | ||
* Advanced JSON forms (ajf) is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero | ||
* General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Advanced JSON forms (ajf). | ||
* If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
import {AjfFile} from '@ajf/core/file-input'; | ||
import { | ||
ChangeDetectionStrategy, | ||
ChangeDetectorRef, | ||
Component, | ||
Inject, | ||
ViewEncapsulation | ||
} from '@angular/core'; | ||
import {FormControl} from '@angular/forms'; | ||
import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser'; | ||
import {Observable} from 'rxjs'; | ||
import {filter, map, shareReplay, startWith, switchMap} from 'rxjs/operators'; | ||
|
||
import {AjfBaseFieldComponent} from './base-field'; | ||
import {AjfFormRendererService} from './form-renderer'; | ||
import {AJF_WARNING_ALERT_SERVICE, AjfWarningAlertService} from './warning-alert-service'; | ||
|
||
@Component({ | ||
selector: 'ajf-image-field', | ||
templateUrl: 'image-field.html', | ||
styleUrls: ['image-field.css'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
encapsulation: ViewEncapsulation.None, | ||
}) | ||
export class AjfImageFieldComponent extends AjfBaseFieldComponent { | ||
readonly imageUrl: Observable<SafeResourceUrl>; | ||
|
||
constructor( | ||
cdr: ChangeDetectorRef, service: AjfFormRendererService, | ||
@Inject(AJF_WARNING_ALERT_SERVICE) was: AjfWarningAlertService, domSanitizer: DomSanitizer) { | ||
super(cdr, service, was); | ||
const fileStream = this.control.pipe( | ||
filter(control => control != null), | ||
switchMap(control => { | ||
control = control as FormControl; | ||
return control.valueChanges.pipe( | ||
startWith(control.value), | ||
); | ||
}), | ||
filter(value => value != null), | ||
shareReplay(1), | ||
) as Observable<AjfFile>; | ||
this.imageUrl = fileStream.pipe( | ||
map(file => domSanitizer.bypassSecurityTrustResourceUrl(file.content)), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,5 +37,6 @@ export enum AjfFieldType { | |
Geolocation, | ||
Barcode, | ||
File, | ||
Image, | ||
LENGTH | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* @license | ||
* Copyright (C) Gnucoop soc. coop. | ||
* | ||
* This file is part of the Advanced JSON forms (ajf). | ||
* | ||
* Advanced JSON forms (ajf) is free software: you can redistribute it and/or | ||
* modify it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the License, | ||
* or (at your option) any later version. | ||
* | ||
* Advanced JSON forms (ajf) is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero | ||
* General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Advanced JSON forms (ajf). | ||
* If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
import {AjfField} from './field'; | ||
import {AjfFieldType} from './field-type'; | ||
|
||
export interface AjfImageField extends AjfField { | ||
fieldType: AjfFieldType.Image; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<img [src]="imageUrl|async"> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* @license | ||
* Copyright (C) Gnucoop soc. coop. | ||
* | ||
* This file is part of the Advanced JSON forms (ajf). | ||
* | ||
* Advanced JSON forms (ajf) is free software: you can redistribute it and/or | ||
* modify it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the License, | ||
* or (at your option) any later version. | ||
* | ||
* Advanced JSON forms (ajf) is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero | ||
* General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Advanced JSON forms (ajf). | ||
* If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
import {AjfFile} from '@ajf/core/file-input'; | ||
import { | ||
ChangeDetectionStrategy, | ||
ChangeDetectorRef, | ||
Component, | ||
Inject, | ||
ViewEncapsulation | ||
} from '@angular/core'; | ||
import {FormControl} from '@angular/forms'; | ||
import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser'; | ||
import {Observable} from 'rxjs'; | ||
import {filter, map, shareReplay, startWith, switchMap} from 'rxjs/operators'; | ||
|
||
import {AjfBaseFieldComponent} from './base-field'; | ||
import {AjfFormRendererService} from './form-renderer'; | ||
import {AJF_WARNING_ALERT_SERVICE, AjfWarningAlertService} from './warning-alert-service'; | ||
|
||
@Component({ | ||
selector: 'ajf-read-only-image-field', | ||
templateUrl: 'read-only-image-field.html', | ||
styleUrls: ['read-only-image-field.css'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
encapsulation: ViewEncapsulation.None, | ||
}) | ||
export class AjfReadOnlyImageFieldComponent extends AjfBaseFieldComponent { | ||
readonly imageUrl: Observable<SafeResourceUrl>; | ||
|
||
constructor( | ||
cdr: ChangeDetectorRef, service: AjfFormRendererService, | ||
@Inject(AJF_WARNING_ALERT_SERVICE) was: AjfWarningAlertService, domSanitizer: DomSanitizer) { | ||
super(cdr, service, was); | ||
const fileStream = this.control.pipe( | ||
filter(control => control != null), | ||
switchMap(control => { | ||
control = control as FormControl; | ||
return control.valueChanges.pipe( | ||
startWith(control.value), | ||
); | ||
}), | ||
filter(value => value != null), | ||
shareReplay(1), | ||
) as Observable<AjfFile>; | ||
this.imageUrl = fileStream.pipe( | ||
map(file => domSanitizer.bypassSecurityTrustResourceUrl(file.content)), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.