UI: add pattern support for labels to button widgets.

This commit is contained in:
Igor Kulikov 2024-02-05 19:54:04 +02:00
parent 147f817445
commit fd8f6c2763
6 changed files with 49 additions and 5 deletions

View File

@ -24,6 +24,7 @@
[borderRadius]="borderRadius" [borderRadius]="borderRadius"
[disabled]="disabled" [disabled]="disabled"
[activated]="activated" [activated]="activated"
[ctx]="ctx"
(clicked)="onClick($event)"> (clicked)="onClick($event)">
</tb-widget-button> </tb-widget-button>
<div *ngIf="error" class="tb-action-widget-error-container"> <div *ngIf="error" class="tb-action-widget-error-container">

View File

@ -23,6 +23,7 @@
[appearance]="appearance" [appearance]="appearance"
[borderRadius]="borderRadius" [borderRadius]="borderRadius"
[disabled]="disabled" [disabled]="disabled"
[ctx]="ctx"
(clicked)="onClick($event)"> (clicked)="onClick($event)">
</tb-widget-button> </tb-widget-button>
<div *ngIf="error" class="tb-action-widget-error-container"> <div *ngIf="error" class="tb-action-widget-error-container">

View File

@ -48,6 +48,7 @@
widgets.button.preview widgets.button.preview
</div> </div>
<tb-widget-button <tb-widget-button
#widgetButtonPreview
[appearance]="previewAppearance" [appearance]="previewAppearance"
[borderRadius]="borderRadius" [borderRadius]="borderRadius"
disableEvents disableEvents

View File

@ -14,7 +14,16 @@
/// limitations under the License. /// limitations under the License.
/// ///
import { ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation } from '@angular/core'; import {
ChangeDetectorRef,
Component,
EventEmitter,
Input,
OnInit,
Output,
ViewChild,
ViewEncapsulation
} from '@angular/core';
import { PageComponent } from '@shared/components/page.component'; import { PageComponent } from '@shared/components/page.component';
import { TbPopoverComponent } from '@shared/components/popover.component'; import { TbPopoverComponent } from '@shared/components/popover.component';
import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms'; import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
@ -32,6 +41,7 @@ import {
} from '@shared/components/button/widget-button.models'; } from '@shared/components/button/widget-button.models';
import { merge } from 'rxjs'; import { merge } from 'rxjs';
import { deepClone } from '@core/utils'; import { deepClone } from '@core/utils';
import { WidgetButtonComponent } from '@shared/components/button/widget-button.component';
@Component({ @Component({
selector: 'tb-widget-button-custom-style-panel', selector: 'tb-widget-button-custom-style-panel',
@ -42,6 +52,9 @@ import { deepClone } from '@core/utils';
}) })
export class WidgetButtonCustomStylePanelComponent extends PageComponent implements OnInit { export class WidgetButtonCustomStylePanelComponent extends PageComponent implements OnInit {
@ViewChild('widgetButtonPreview')
widgetButtonPreview: WidgetButtonComponent;
@Input() @Input()
appearance: WidgetButtonAppearance; appearance: WidgetButtonAppearance;
@ -54,8 +67,19 @@ export class WidgetButtonCustomStylePanelComponent extends PageComponent impleme
@Input() @Input()
customStyle: WidgetButtonCustomStyle; customStyle: WidgetButtonCustomStyle;
private popoverValue: TbPopoverComponent<WidgetButtonCustomStylePanelComponent>;
@Input() @Input()
popover: TbPopoverComponent<WidgetButtonCustomStylePanelComponent>; set popover(popover: TbPopoverComponent<WidgetButtonCustomStylePanelComponent>) {
this.popoverValue = popover;
popover.tbAnimationDone.subscribe(() => {
this.widgetButtonPreview?.validateSize();
});
}
get popover(): TbPopoverComponent<WidgetButtonCustomStylePanelComponent> {
return this.popoverValue;
}
@Output() @Output()
customStyleApplied = new EventEmitter<WidgetButtonCustomStyle>(); customStyleApplied = new EventEmitter<WidgetButtonCustomStyle>();
@ -125,7 +149,7 @@ export class WidgetButtonCustomStylePanelComponent extends PageComponent impleme
if (customStyle?.overrideBackgroundColor) { if (customStyle?.overrideBackgroundColor) {
backgroundColor = customStyle?.backgroundColor; backgroundColor = customStyle?.backgroundColor;
} }
let dropShadow = this.appearance.type === WidgetButtonType.basic ? false : true; let dropShadow = this.appearance.type !== WidgetButtonType.basic;
if (customStyle?.overrideDropShadow) { if (customStyle?.overrideDropShadow) {
dropShadow = customStyle?.dropShadow; dropShadow = customStyle?.dropShadow;
} }

View File

@ -34,6 +34,6 @@
[style.pointer-events]="disableEvents ? 'none' : ''"> [style.pointer-events]="disableEvents ? 'none' : ''">
<div #widgetButtonContent class="tb-widget-button-content" *ngIf="appearance.showIcon || appearance.showLabel"> <div #widgetButtonContent class="tb-widget-button-content" *ngIf="appearance.showIcon || appearance.showLabel">
<tb-icon matButtonIcon *ngIf="appearance.showIcon" [style]="iconStyle">{{ appearance.icon }}</tb-icon> <tb-icon matButtonIcon *ngIf="appearance.showIcon" [style]="iconStyle">{{ appearance.icon }}</tb-icon>
<span *ngIf="appearance.showLabel" class="tb-widget-button-label">{{ appearance.label }}</span> <span *ngIf="appearance.showLabel" class="tb-widget-button-label">{{ label$ | async }}</span>
</div> </div>
</button> </button>

View File

@ -25,7 +25,8 @@ import {
OnInit, OnInit,
Output, Output,
Renderer2, Renderer2,
SimpleChanges, ViewChild, SimpleChanges,
ViewChild,
ViewEncapsulation ViewEncapsulation
} from '@angular/core'; } from '@angular/core';
import { import {
@ -36,6 +37,8 @@ import { coerceBoolean } from '@shared/decorators/coercion';
import { ComponentStyle, iconStyle } from '@shared/models/widget-settings.models'; import { ComponentStyle, iconStyle } from '@shared/models/widget-settings.models';
import { UtilsService } from '@core/services/utils.service'; import { UtilsService } from '@core/services/utils.service';
import { ResizeObserver } from '@juggle/resize-observer'; import { ResizeObserver } from '@juggle/resize-observer';
import { Observable, of } from 'rxjs';
import { WidgetContext } from '@home/models/widget-component.models';
const initialButtonHeight = 60; const initialButtonHeight = 60;
const horizontalLayoutPadding = 24; const horizontalLayoutPadding = 24;
@ -81,9 +84,14 @@ export class WidgetButtonComponent implements OnInit, AfterViewInit, OnDestroy,
@coerceBoolean() @coerceBoolean()
disableEvents = false; disableEvents = false;
@Input()
ctx: WidgetContext;
@Output() @Output()
clicked = new EventEmitter<MouseEvent>(); clicked = new EventEmitter<MouseEvent>();
label$: Observable<string>;
iconStyle: ComponentStyle = {}; iconStyle: ComponentStyle = {};
mousePressed = false; mousePressed = false;
@ -122,11 +130,20 @@ export class WidgetButtonComponent implements OnInit, AfterViewInit, OnDestroy,
this.clearAppearanceCss(); this.clearAppearanceCss();
} }
public validateSize() {
if (this.appearance.autoScale && this.widgetButton.nativeElement) {
this.onResize();
}
}
private updateAppearance(): void { private updateAppearance(): void {
this.clearAppearanceCss(); this.clearAppearanceCss();
if (this.appearance.showIcon) { if (this.appearance.showIcon) {
this.iconStyle = iconStyle(this.appearance.iconSize, this.appearance.iconSizeUnit); this.iconStyle = iconStyle(this.appearance.iconSize, this.appearance.iconSizeUnit);
} }
if (this.appearance.showLabel) {
this.label$ = this.ctx ? this.ctx.registerLabelPattern(this.appearance.label, this.label$) : of(this.appearance.label);
}
const appearanceCss = generateWidgetButtonAppearanceCss(this.appearance); const appearanceCss = generateWidgetButtonAppearanceCss(this.appearance);
this.appearanceCssClass = this.utils.applyCssToElement(this.renderer, this.elementRef.nativeElement, this.appearanceCssClass = this.utils.applyCssToElement(this.renderer, this.elementRef.nativeElement,
'tb-widget-button', appearanceCss); 'tb-widget-button', appearanceCss);