15 changed files with 654 additions and 46 deletions
@ -0,0 +1,93 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
Copyright © 2016-2023 The Thingsboard Authors |
||||
|
|
||||
|
Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
you may not use this file except in compliance with the License. |
||||
|
You may obtain a copy of the License at |
||||
|
|
||||
|
http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
|
||||
|
Unless required by applicable law or agreed to in writing, software |
||||
|
distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
See the License for the specific language governing permissions and |
||||
|
limitations under the License. |
||||
|
|
||||
|
--> |
||||
|
<div class="tb-container"> |
||||
|
<label class="tb-title" *ngIf="label" [class]="{'tb-error': !disabled && (required && linkType === ImageLinkType.none), 'tb-required': !disabled && required}">{{label}}</label> |
||||
|
<div class="tb-image-select-container" [class]="{disabled: disabled && (linkType === ImageLinkType.none || linkType === ImageLinkType.base64)}"> |
||||
|
<div class="tb-image-container"> |
||||
|
<img *ngIf="linkType !== ImageLinkType.none && imageUrl; else noImage" class="tb-image-preview" [src]="imageUrl | image: {preview: true} | async"> |
||||
|
</div> |
||||
|
<div *ngIf="linkType !== ImageLinkType.none && (linkType !== ImageLinkType.base64 || !disabled)" class="tb-image-info-container"> |
||||
|
<div *ngIf="linkType === ImageLinkType.base64" class="tb-base64-image-container"> |
||||
|
</div> |
||||
|
<div *ngIf="linkType === ImageLinkType.resource" class="tb-resource-image-container" [class]="{loading: loadingImageResource}"> |
||||
|
<ng-container *ngIf="!loadingImageResource; else loading"> |
||||
|
<div class="tb-resource-image-name"> |
||||
|
{{ imageResource.title }} |
||||
|
</div> |
||||
|
<div class="tb-resource-image-details"> |
||||
|
<div>{{ imageResource.descriptor.width }}x{{ imageResource.descriptor.height }}</div> |
||||
|
<mat-divider vertical></mat-divider> |
||||
|
<div>{{ imageResource.descriptor.size | fileSize }}</div> |
||||
|
</div> |
||||
|
</ng-container> |
||||
|
</div> |
||||
|
<div *ngIf="linkType === ImageLinkType.external" class="tb-external-image-container"> |
||||
|
<div class="tb-external-link-label"> |
||||
|
{{ 'image.image-link' | translate }} |
||||
|
</div> |
||||
|
<div class="tb-external-link-input-container"> |
||||
|
<mat-form-field class="tb-inline-field" appearance="outline" subscriptSizing="dynamic"> |
||||
|
<input matInput [formControl]="externalLinkControl" placeholder="{{ 'widget-config.set' | translate }}"> |
||||
|
</mat-form-field> |
||||
|
<button *ngIf="!disabled" |
||||
|
class="tb-image-clear-btn" |
||||
|
type="button" |
||||
|
mat-icon-button |
||||
|
matTooltip="{{ 'image.clear-image' | translate }}" |
||||
|
matTooltipPosition="above" |
||||
|
(click)="clearImage()"> |
||||
|
<mat-icon>close</mat-icon> |
||||
|
</button> |
||||
|
</div> |
||||
|
</div> |
||||
|
<button *ngIf="!disabled && linkType !== ImageLinkType.external" |
||||
|
class="tb-image-clear-btn" |
||||
|
type="button" |
||||
|
mat-icon-button |
||||
|
matTooltip="{{ 'image.clear-image' | translate }}" |
||||
|
matTooltipPosition="above" |
||||
|
(click)="clearImage()"> |
||||
|
<mat-icon>close</mat-icon> |
||||
|
</button> |
||||
|
</div> |
||||
|
<div *ngIf="linkType === ImageLinkType.none && !disabled" class="tb-image-select-buttons-container"> |
||||
|
<button #browseGalleryButton |
||||
|
mat-stroked-button |
||||
|
color="primary" |
||||
|
class="tb-image-select-button" |
||||
|
(click)="toggleGallery($event, browseGalleryButton)"> |
||||
|
<tb-icon matButtonIcon>filter</tb-icon> |
||||
|
<span translate>image.browse-from-gallery</span> |
||||
|
</button> |
||||
|
<button mat-stroked-button |
||||
|
color="primary" |
||||
|
class="tb-image-select-button" |
||||
|
(click)="setLink($event)"> |
||||
|
<tb-icon matButtonIcon>link</tb-icon> |
||||
|
<span translate>image.set-link</span> |
||||
|
</button> |
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
||||
|
|
||||
|
<ng-template #noImage> |
||||
|
<div class="tb-no-image">{{ (disabled ? 'image.no-image' : 'image.no-image-selected') | translate }}</div> |
||||
|
</ng-template> |
||||
|
<ng-template #loading> |
||||
|
<mat-spinner [diameter]="80" strokeWidth="4"></mat-spinner> |
||||
|
</ng-template> |
||||
@ -0,0 +1,180 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
$containerHeight: 96px !default; |
||||
|
|
||||
|
:host { |
||||
|
.tb-container { |
||||
|
margin-top: 0; |
||||
|
padding: 0 0 16px; |
||||
|
display: flex; |
||||
|
flex-direction: column; |
||||
|
gap: 8px; |
||||
|
label.tb-title { |
||||
|
display: block; |
||||
|
padding-bottom: 0; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
.tb-image-select-container { |
||||
|
width: 100%; |
||||
|
height: $containerHeight; |
||||
|
border-radius: 4px; |
||||
|
border: 1px solid rgba(0, 0, 0, 0.12); |
||||
|
display: flex; |
||||
|
align-items: center; |
||||
|
&.disabled { |
||||
|
width: $containerHeight; |
||||
|
.tb-image-container { |
||||
|
width: $containerHeight - 2px; |
||||
|
border-right: none; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
.tb-image-container { |
||||
|
width: $containerHeight - 1px; |
||||
|
height: $containerHeight - 2px; |
||||
|
display: flex; |
||||
|
justify-content: center; |
||||
|
align-items: center; |
||||
|
border-radius: 4px; |
||||
|
border-right: 1px solid rgba(0, 0, 0, 0.12); |
||||
|
background: #fff; |
||||
|
overflow: hidden; |
||||
|
} |
||||
|
|
||||
|
.tb-image-preview { |
||||
|
width: auto; |
||||
|
max-width: $containerHeight - 2px; |
||||
|
height: auto; |
||||
|
max-height: $containerHeight - 2px; |
||||
|
} |
||||
|
|
||||
|
.tb-no-image { |
||||
|
text-align: center; |
||||
|
color: rgba(0, 0, 0, 0.38); |
||||
|
font-size: 12px; |
||||
|
font-style: normal; |
||||
|
font-weight: 400; |
||||
|
line-height: 16px; |
||||
|
letter-spacing: 0.4px; |
||||
|
} |
||||
|
|
||||
|
.tb-image-info-container { |
||||
|
display: flex; |
||||
|
flex: 1; |
||||
|
align-self: stretch; |
||||
|
padding: 0 8px; |
||||
|
justify-content: flex-end; |
||||
|
align-items: center; |
||||
|
gap: 4px; |
||||
|
.tb-base64-image-container, .tb-resource-image-container, .tb-external-image-container { |
||||
|
display: flex; |
||||
|
flex: 1; |
||||
|
align-self: stretch; |
||||
|
} |
||||
|
.tb-resource-image-container { |
||||
|
padding: 8px; |
||||
|
justify-content: center; |
||||
|
align-items: flex-start; |
||||
|
flex-direction: column; |
||||
|
gap: 4px; |
||||
|
.tb-resource-image-name { |
||||
|
color: rgba(0, 0, 0, 0.54); |
||||
|
font-size: 12px; |
||||
|
font-style: normal; |
||||
|
font-weight: 500; |
||||
|
line-height: 16px; |
||||
|
letter-spacing: 0.25px; |
||||
|
} |
||||
|
.tb-resource-image-details { |
||||
|
display: flex; |
||||
|
align-items: center; |
||||
|
gap: 8px; |
||||
|
color: rgba(0, 0, 0, 0.38); |
||||
|
font-size: 11px; |
||||
|
font-style: normal; |
||||
|
font-weight: 400; |
||||
|
line-height: 16px; |
||||
|
letter-spacing: 0.017px; |
||||
|
.mat-divider.mat-divider-vertical { |
||||
|
height: 16px; |
||||
|
} |
||||
|
} |
||||
|
&.loading { |
||||
|
align-items: center; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
.tb-external-image-container { |
||||
|
padding: 16px 8px 0 16px; |
||||
|
flex-direction: column; |
||||
|
align-items: flex-start; |
||||
|
gap: 4px; |
||||
|
.tb-external-link-label { |
||||
|
color: rgba(0, 0, 0, 0.54); |
||||
|
font-size: 12px; |
||||
|
font-style: normal; |
||||
|
font-weight: 400; |
||||
|
line-height: 16px; |
||||
|
letter-spacing: 0.4px; |
||||
|
} |
||||
|
.tb-external-link-input-container { |
||||
|
display: flex; |
||||
|
justify-content: flex-end; |
||||
|
align-items: center; |
||||
|
gap: 4px; |
||||
|
align-self: stretch; |
||||
|
.tb-inline-field { |
||||
|
width: 100%; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
.tb-image-clear-btn { |
||||
|
color: rgba(0,0,0,0.38); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
.tb-image-select-buttons-container { |
||||
|
display: flex; |
||||
|
flex: 1; |
||||
|
align-self: stretch; |
||||
|
padding: 8px; |
||||
|
gap: 8px; |
||||
|
justify-content: center; |
||||
|
align-items: flex-start; |
||||
|
.tb-image-select-button { |
||||
|
width: 100%; |
||||
|
height: 100%; |
||||
|
align-self: stretch; |
||||
|
display: flex; |
||||
|
flex-direction: column; |
||||
|
justify-content: center; |
||||
|
align-items: center; |
||||
|
gap: 4px; |
||||
|
padding: 16px; |
||||
|
line-height: normal; |
||||
|
font-size: 12px; |
||||
|
.mat-icon { |
||||
|
width: 24px; |
||||
|
height: 24px; |
||||
|
font-size: 24px; |
||||
|
margin: 0; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,224 @@ |
|||||
|
///
|
||||
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
||||
|
///
|
||||
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
/// you may not use this file except in compliance with the License.
|
||||
|
/// You may obtain a copy of the License at
|
||||
|
///
|
||||
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
///
|
||||
|
/// Unless required by applicable law or agreed to in writing, software
|
||||
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
/// See the License for the specific language governing permissions and
|
||||
|
/// limitations under the License.
|
||||
|
///
|
||||
|
|
||||
|
import { |
||||
|
ChangeDetectorRef, |
||||
|
Component, |
||||
|
forwardRef, |
||||
|
Input, |
||||
|
OnDestroy, |
||||
|
OnInit, |
||||
|
Renderer2, |
||||
|
ViewContainerRef |
||||
|
} from '@angular/core'; |
||||
|
import { PageComponent } from '@shared/components/page.component'; |
||||
|
import { Store } from '@ngrx/store'; |
||||
|
import { AppState } from '@core/core.state'; |
||||
|
import { ControlValueAccessor, FormControl, NG_VALUE_ACCESSOR, } from '@angular/forms'; |
||||
|
import { coerceBoolean } from '@shared/decorators/coercion'; |
||||
|
import { |
||||
|
extractParamsFromImageResourceUrl, |
||||
|
ImageResourceInfo, |
||||
|
isBase64DataImageUrl, |
||||
|
isImageResourceUrl |
||||
|
} from '@shared/models/resource.models'; |
||||
|
import { ImageService } from '@core/http/image.service'; |
||||
|
import { MatButton } from '@angular/material/button'; |
||||
|
import { TbPopoverService } from '@shared/components/popover.service'; |
||||
|
import { ImageGalleryComponent } from '@home/components/image/image-gallery.component'; |
||||
|
|
||||
|
enum ImageLinkType { |
||||
|
none = 'none', |
||||
|
base64 = 'base64', |
||||
|
external = 'external', |
||||
|
resource = 'resource' |
||||
|
} |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-gallery-image-input', |
||||
|
templateUrl: './gallery-image-input.component.html', |
||||
|
styleUrls: ['./gallery-image-input.component.scss'], |
||||
|
providers: [ |
||||
|
{ |
||||
|
provide: NG_VALUE_ACCESSOR, |
||||
|
useExisting: forwardRef(() => GalleryImageInputComponent), |
||||
|
multi: true |
||||
|
} |
||||
|
] |
||||
|
}) |
||||
|
export class GalleryImageInputComponent extends PageComponent implements OnInit, OnDestroy, ControlValueAccessor { |
||||
|
|
||||
|
@Input() |
||||
|
label: string; |
||||
|
|
||||
|
@Input() |
||||
|
@coerceBoolean() |
||||
|
required = false; |
||||
|
|
||||
|
@Input() |
||||
|
disabled: boolean; |
||||
|
|
||||
|
imageUrl: string; |
||||
|
|
||||
|
imageResource: ImageResourceInfo; |
||||
|
|
||||
|
loadingImageResource = false; |
||||
|
|
||||
|
ImageLinkType = ImageLinkType; |
||||
|
|
||||
|
linkType: ImageLinkType = ImageLinkType.none; |
||||
|
|
||||
|
externalLinkControl = new FormControl(null); |
||||
|
|
||||
|
private propagateChange = null; |
||||
|
|
||||
|
constructor(protected store: Store<AppState>, |
||||
|
private imageService: ImageService, |
||||
|
private cd: ChangeDetectorRef, |
||||
|
private renderer: Renderer2, |
||||
|
private viewContainerRef: ViewContainerRef, |
||||
|
private popoverService: TbPopoverService) { |
||||
|
super(store); |
||||
|
} |
||||
|
|
||||
|
ngOnInit() { |
||||
|
this.externalLinkControl.valueChanges.subscribe((value) => { |
||||
|
if (this.linkType === ImageLinkType.external) { |
||||
|
this.imageUrl = value; |
||||
|
this.updateModel(); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
ngOnDestroy() { |
||||
|
} |
||||
|
|
||||
|
registerOnChange(fn: any): void { |
||||
|
this.propagateChange = fn; |
||||
|
} |
||||
|
|
||||
|
registerOnTouched(fn: any): void { |
||||
|
} |
||||
|
|
||||
|
setDisabledState(isDisabled: boolean): void { |
||||
|
this.disabled = isDisabled; |
||||
|
if (this.disabled) { |
||||
|
this.detectLinkType(); |
||||
|
this.externalLinkControl.disable({emitEvent: false}); |
||||
|
} else { |
||||
|
this.externalLinkControl.enable({emitEvent: false}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
writeValue(value: string): void { |
||||
|
if (this.imageUrl !== value) { |
||||
|
this.reset(); |
||||
|
this.imageUrl = value; |
||||
|
this.detectLinkType(); |
||||
|
if (this.linkType === ImageLinkType.resource) { |
||||
|
const params = extractParamsFromImageResourceUrl(value); |
||||
|
this.loadingImageResource = true; |
||||
|
this.imageService.getImageInfo(params.type, params.key, {ignoreLoading: true, ignoreErrors: true}).subscribe( |
||||
|
{ |
||||
|
next: (res) => { |
||||
|
this.imageResource = res; |
||||
|
this.loadingImageResource = false; |
||||
|
this.cd.markForCheck(); |
||||
|
}, |
||||
|
error: () => { |
||||
|
this.reset(); |
||||
|
this.loadingImageResource = false; |
||||
|
this.cd.markForCheck(); |
||||
|
} |
||||
|
} |
||||
|
); |
||||
|
} else if (this.linkType === ImageLinkType.base64) { |
||||
|
this.cd.markForCheck(); |
||||
|
} else if (this.linkType === ImageLinkType.external) { |
||||
|
this.externalLinkControl.setValue(this.imageUrl, {emitEvent: false}); |
||||
|
this.cd.markForCheck(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private detectLinkType() { |
||||
|
if (this.imageUrl) { |
||||
|
if (isImageResourceUrl(this.imageUrl)) { |
||||
|
this.linkType = ImageLinkType.resource; |
||||
|
} else if (isBase64DataImageUrl(this.imageUrl)) { |
||||
|
this.linkType = ImageLinkType.base64; |
||||
|
} else { |
||||
|
this.linkType = ImageLinkType.external; |
||||
|
} |
||||
|
} else { |
||||
|
this.linkType = ImageLinkType.none; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private updateModel() { |
||||
|
this.cd.markForCheck(); |
||||
|
this.propagateChange(this.imageUrl); |
||||
|
} |
||||
|
|
||||
|
private reset() { |
||||
|
this.linkType = ImageLinkType.none; |
||||
|
this.imageUrl = null; |
||||
|
this.imageResource = null; |
||||
|
this.externalLinkControl.setValue(null, {emitEvent: false}); |
||||
|
} |
||||
|
|
||||
|
clearImage() { |
||||
|
this.reset(); |
||||
|
this.updateModel(); |
||||
|
} |
||||
|
|
||||
|
setLink($event: Event) { |
||||
|
if ($event) { |
||||
|
$event.stopPropagation(); |
||||
|
} |
||||
|
this.linkType = ImageLinkType.external; |
||||
|
} |
||||
|
|
||||
|
toggleGallery($event: Event, browseGalleryButton: MatButton) { |
||||
|
if ($event) { |
||||
|
$event.stopPropagation(); |
||||
|
} |
||||
|
const trigger = browseGalleryButton._elementRef.nativeElement; |
||||
|
if (this.popoverService.hasPopover(trigger)) { |
||||
|
this.popoverService.hidePopover(trigger); |
||||
|
} else { |
||||
|
const ctx: any = { |
||||
|
pageMode: false, |
||||
|
popoverMode: true, |
||||
|
mode: 'grid', |
||||
|
selectionMode: true |
||||
|
}; |
||||
|
const imageGalleryPopover = this.popoverService.displayPopover(trigger, this.renderer, |
||||
|
this.viewContainerRef, ImageGalleryComponent, 'top', true, null, |
||||
|
ctx, |
||||
|
{}, |
||||
|
{}, {}, true); |
||||
|
imageGalleryPopover.tbComponentRef.instance.imageSelected.subscribe((image) => { |
||||
|
imageGalleryPopover.hide(); |
||||
|
this.linkType = ImageLinkType.resource; |
||||
|
this.imageUrl = image.link; |
||||
|
this.imageResource = image; |
||||
|
this.updateModel(); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue