99 changed files with 3099 additions and 6650 deletions
@ -0,0 +1,29 @@ |
|||||
|
/** |
||||
|
* 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 '../../../../../scss/constants'; |
||||
|
|
||||
|
.tb-add-widget-dialog { |
||||
|
.mat-mdc-dialog-content { |
||||
|
padding: 0; |
||||
|
position: relative; |
||||
|
} |
||||
|
@media #{$mat-gt-xs} { |
||||
|
width: 1200px; |
||||
|
.mat-mdc-dialog-content { |
||||
|
height: 600px; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,59 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
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. |
||||
|
|
||||
|
--> |
||||
|
<ng-container [formGroup]="valueCardWidgetConfigForm"> |
||||
|
<tb-timewindow-config-panel *ngIf="displayTimewindowConfig" |
||||
|
[onlyHistoryTimewindow]="onlyHistoryTimewindow()" |
||||
|
formControlName="timewindowConfig"> |
||||
|
</tb-timewindow-config-panel> |
||||
|
<tb-datasources |
||||
|
[configMode]="basicMode" |
||||
|
hideDataKeyLabel |
||||
|
hideDataKeyColor |
||||
|
hideDataKeyUnits |
||||
|
hideDataKeyDecimals |
||||
|
formControlName="datasources"> |
||||
|
</tb-datasources> |
||||
|
<div class="tb-form-panel"> |
||||
|
<div class="tb-form-panel-title" translate>widget-config.appearance</div> |
||||
|
<tb-image-cards-select rowHeight="{{ horizontal ? '3:1' : '7:5' }}" |
||||
|
[cols]="horizontal ? 2 : 4" |
||||
|
[colsLtMd]="horizontal ? 1 : 2" |
||||
|
label="{{ 'widgets.value-card.layout' | translate }}" formControlName="layout"> |
||||
|
<tb-image-cards-select-option *ngFor="let layout of valueCardLayouts" |
||||
|
[value]="layout" |
||||
|
[image]="valueCardLayoutImageMap.get(layout)"> |
||||
|
{{ valueCardLayoutTranslationMap.get(layout) | translate }} |
||||
|
</tb-image-cards-select-option> |
||||
|
</tb-image-cards-select> |
||||
|
<div class="tb-form-row"> |
||||
|
<mat-slide-toggle class="mat-slide fixed-title-width" formControlName="showLabel"> |
||||
|
{{ 'widgets.value-card.label' | translate }} |
||||
|
</mat-slide-toggle> |
||||
|
<div fxFlex fxLayout="row" fxLayoutAlign="start center" fxLayoutGap="8px"> |
||||
|
<mat-form-field fxFlex appearance="outline" subscriptSizing="dynamic"> |
||||
|
<input matInput formControlName="label" placeholder="{{ 'widget-config.set' | translate }}"> |
||||
|
</mat-form-field> |
||||
|
<tb-font-settings formControlName="labelFont" |
||||
|
[previewText]="valueCardWidgetConfigForm.get('label').value"> |
||||
|
</tb-font-settings> |
||||
|
<tb-color-settings formControlName="labelColor"> |
||||
|
</tb-color-settings> |
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
||||
|
</ng-container> |
||||
@ -0,0 +1,254 @@ |
|||||
|
///
|
||||
|
/// 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 } from '@angular/core'; |
||||
|
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; |
||||
|
import { Store } from '@ngrx/store'; |
||||
|
import { AppState } from '@core/core.state'; |
||||
|
import { BasicWidgetConfigComponent } from '@home/components/widget/config/widget-config.component.models'; |
||||
|
import { WidgetConfigComponentData } from '@home/models/widget-component.models'; |
||||
|
import { |
||||
|
datasourcesHasAggregation, |
||||
|
datasourcesHasOnlyComparisonAggregation, |
||||
|
WidgetConfig, |
||||
|
} from '@shared/models/widget.models'; |
||||
|
import { WidgetConfigComponent } from '@home/components/widget/widget-config.component'; |
||||
|
import { DataKeyType } from '@shared/models/telemetry/telemetry.models'; |
||||
|
import { getTimewindowConfig } from '@home/components/widget/config/timewindow-config-panel.component'; |
||||
|
import { isDefinedAndNotNull, isUndefined } from '@core/utils'; |
||||
|
import { getLabel, setLabel } from '@home/components/widget/config/widget-settings.models'; |
||||
|
import { |
||||
|
valueCardDefaultSettings, |
||||
|
ValueCardLayout, |
||||
|
valueCardLayoutImages, |
||||
|
valueCardLayouts, |
||||
|
valueCardLayoutTranslations, |
||||
|
ValueCardWidgetSettings |
||||
|
} from '@home/components/widget/lib/cards/value-card-widget.models'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-value-card-basic-config', |
||||
|
templateUrl: './value-card-basic-config.component.html', |
||||
|
styleUrls: ['../basic-config.scss'] |
||||
|
}) |
||||
|
export class ValueCardBasicConfigComponent extends BasicWidgetConfigComponent { |
||||
|
|
||||
|
public get displayTimewindowConfig(): boolean { |
||||
|
const datasources = this.valueCardWidgetConfigForm.get('datasources').value; |
||||
|
return datasourcesHasAggregation(datasources); |
||||
|
} |
||||
|
|
||||
|
public onlyHistoryTimewindow(): boolean { |
||||
|
const datasources = this.valueCardWidgetConfigForm.get('datasources').value; |
||||
|
return datasourcesHasOnlyComparisonAggregation(datasources); |
||||
|
} |
||||
|
|
||||
|
valueCardLayouts: ValueCardLayout[] = []; |
||||
|
|
||||
|
valueCardLayoutTranslationMap = valueCardLayoutTranslations; |
||||
|
valueCardLayoutImageMap = valueCardLayoutImages; |
||||
|
|
||||
|
horizontal = false; |
||||
|
|
||||
|
valueCardWidgetConfigForm: UntypedFormGroup; |
||||
|
|
||||
|
constructor(protected store: Store<AppState>, |
||||
|
protected widgetConfigComponent: WidgetConfigComponent, |
||||
|
private cd: ChangeDetectorRef, |
||||
|
private fb: UntypedFormBuilder) { |
||||
|
super(store, widgetConfigComponent); |
||||
|
} |
||||
|
|
||||
|
protected configForm(): UntypedFormGroup { |
||||
|
return this.valueCardWidgetConfigForm; |
||||
|
} |
||||
|
|
||||
|
protected setupConfig(widgetConfig: WidgetConfigComponentData) { |
||||
|
const params = widgetConfig.typeParameters as any; |
||||
|
this.horizontal = isDefinedAndNotNull(params.horizontal) ? params.horizontal : false; |
||||
|
this.valueCardLayouts = valueCardLayouts(this.horizontal); |
||||
|
super.setupConfig(widgetConfig); |
||||
|
} |
||||
|
|
||||
|
protected setupDefaults(configData: WidgetConfigComponentData) { |
||||
|
this.setupDefaultDatasource(configData, [{ name: 'temperature', label: 'Temperature', type: DataKeyType.timeseries }]); |
||||
|
} |
||||
|
|
||||
|
protected onConfigSet(configData: WidgetConfigComponentData) { |
||||
|
const settings: ValueCardWidgetSettings = {...valueCardDefaultSettings(this.horizontal), ...(configData.config.settings || {})}; |
||||
|
this.valueCardWidgetConfigForm = this.fb.group({ |
||||
|
timewindowConfig: [getTimewindowConfig(configData.config), []], |
||||
|
datasources: [configData.config.datasources, []], |
||||
|
layout: [settings.layout, []], |
||||
|
|
||||
|
showLabel: [settings.showLabel, []], |
||||
|
label: [getLabel(configData.config.datasources), []], |
||||
|
labelFont: [settings.labelFont, []], |
||||
|
labelColor: [settings.labelColor, []], |
||||
|
|
||||
|
showIcon: [settings.showIcon, []], |
||||
|
iconSize: [settings.iconSize, [Validators.min(0)]], |
||||
|
iconSizeUnit: [settings.iconSizeUnit, []], |
||||
|
icon: [settings.icon, []], |
||||
|
iconColor: [settings.iconColor, []], |
||||
|
|
||||
|
units: [configData.config.units, []], |
||||
|
decimals: [configData.config.decimals, []], |
||||
|
valueFont: [settings.valueFont, []], |
||||
|
valueColor: [settings.valueColor, []], |
||||
|
|
||||
|
showDate: [settings.showDate, []], |
||||
|
dateFormat: [settings.dateFormat, []], |
||||
|
dateFont: [settings.dateFont, []], |
||||
|
dateColor: [settings.dateColor, []], |
||||
|
|
||||
|
background: [settings.background, []], |
||||
|
|
||||
|
cardButtons: [this.getCardButtons(configData.config), []], |
||||
|
borderRadius: [configData.config.borderRadius, []], |
||||
|
|
||||
|
actions: [configData.config.actions || {}, []] |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
protected prepareOutputConfig(config: any): WidgetConfigComponentData { |
||||
|
this.widgetConfig.config.useDashboardTimewindow = config.timewindowConfig.useDashboardTimewindow; |
||||
|
this.widgetConfig.config.displayTimewindow = config.timewindowConfig.displayTimewindow; |
||||
|
this.widgetConfig.config.timewindow = config.timewindowConfig.timewindow; |
||||
|
this.widgetConfig.config.datasources = config.datasources; |
||||
|
|
||||
|
this.widgetConfig.config.settings = this.widgetConfig.config.settings || {}; |
||||
|
|
||||
|
this.widgetConfig.config.settings.layout = config.layout; |
||||
|
|
||||
|
this.widgetConfig.config.settings.showLabel = config.showLabel; |
||||
|
setLabel(config.label, this.widgetConfig.config.datasources); |
||||
|
this.widgetConfig.config.settings.labelFont = config.labelFont; |
||||
|
this.widgetConfig.config.settings.labelColor = config.labelColor; |
||||
|
|
||||
|
this.widgetConfig.config.settings.showIcon = config.showIcon; |
||||
|
this.widgetConfig.config.settings.iconSize = config.iconSize; |
||||
|
this.widgetConfig.config.settings.iconSizeUnit = config.iconSizeUnit; |
||||
|
this.widgetConfig.config.settings.icon = config.icon; |
||||
|
this.widgetConfig.config.settings.iconColor = config.iconColor; |
||||
|
|
||||
|
this.widgetConfig.config.units = config.units; |
||||
|
this.widgetConfig.config.decimals = config.decimals; |
||||
|
this.widgetConfig.config.settings.valueFont = config.valueFont; |
||||
|
this.widgetConfig.config.settings.valueColor = config.valueColor; |
||||
|
|
||||
|
this.widgetConfig.config.settings.showDate = config.showDate; |
||||
|
this.widgetConfig.config.settings.dateFormat = config.dateFormat; |
||||
|
this.widgetConfig.config.settings.dateFont = config.dateFont; |
||||
|
this.widgetConfig.config.settings.dateColor = config.dateColor; |
||||
|
|
||||
|
this.widgetConfig.config.settings.background = config.background; |
||||
|
|
||||
|
this.setCardButtons(config.cardButtons, this.widgetConfig.config); |
||||
|
this.widgetConfig.config.borderRadius = config.borderRadius; |
||||
|
|
||||
|
this.widgetConfig.config.actions = config.actions; |
||||
|
return this.widgetConfig; |
||||
|
} |
||||
|
|
||||
|
protected validatorTriggers(): string[] { |
||||
|
return ['layout', 'showLabel', 'showIcon', 'showDate']; |
||||
|
} |
||||
|
|
||||
|
protected updateValidators(emitEvent: boolean, trigger?: string) { |
||||
|
const layout: ValueCardLayout = this.valueCardWidgetConfigForm.get('layout').value; |
||||
|
const showLabel: boolean = this.valueCardWidgetConfigForm.get('showLabel').value; |
||||
|
const showIcon: boolean = this.valueCardWidgetConfigForm.get('showIcon').value; |
||||
|
const showDate: boolean = this.valueCardWidgetConfigForm.get('showDate').value; |
||||
|
|
||||
|
const dateEnabled = ![ValueCardLayout.vertical, ValueCardLayout.simplified].includes(layout); |
||||
|
const iconEnabled = layout !== ValueCardLayout.simplified; |
||||
|
|
||||
|
if (showLabel) { |
||||
|
this.valueCardWidgetConfigForm.get('label').enable(); |
||||
|
this.valueCardWidgetConfigForm.get('labelFont').enable(); |
||||
|
this.valueCardWidgetConfigForm.get('labelColor').enable(); |
||||
|
} else { |
||||
|
this.valueCardWidgetConfigForm.get('label').disable(); |
||||
|
this.valueCardWidgetConfigForm.get('labelFont').disable(); |
||||
|
this.valueCardWidgetConfigForm.get('labelColor').disable(); |
||||
|
} |
||||
|
|
||||
|
if (iconEnabled) { |
||||
|
this.valueCardWidgetConfigForm.get('showIcon').enable({emitEvent: false}); |
||||
|
if (showIcon) { |
||||
|
this.valueCardWidgetConfigForm.get('iconSize').enable(); |
||||
|
this.valueCardWidgetConfigForm.get('iconSizeUnit').enable(); |
||||
|
this.valueCardWidgetConfigForm.get('icon').enable(); |
||||
|
this.valueCardWidgetConfigForm.get('iconColor').enable(); |
||||
|
} else { |
||||
|
this.valueCardWidgetConfigForm.get('iconSize').disable(); |
||||
|
this.valueCardWidgetConfigForm.get('iconSizeUnit').disable(); |
||||
|
this.valueCardWidgetConfigForm.get('icon').disable(); |
||||
|
this.valueCardWidgetConfigForm.get('iconColor').disable(); |
||||
|
} |
||||
|
} else { |
||||
|
this.valueCardWidgetConfigForm.get('showIcon').disable({emitEvent: false}); |
||||
|
this.valueCardWidgetConfigForm.get('iconSize').disable(); |
||||
|
this.valueCardWidgetConfigForm.get('iconSizeUnit').disable(); |
||||
|
this.valueCardWidgetConfigForm.get('icon').disable(); |
||||
|
this.valueCardWidgetConfigForm.get('iconColor').disable(); |
||||
|
} |
||||
|
|
||||
|
if (dateEnabled) { |
||||
|
this.valueCardWidgetConfigForm.get('showDate').enable({emitEvent: false}); |
||||
|
if (showDate) { |
||||
|
this.valueCardWidgetConfigForm.get('dateFormat').enable(); |
||||
|
this.valueCardWidgetConfigForm.get('dateFont').enable(); |
||||
|
this.valueCardWidgetConfigForm.get('dateColor').enable(); |
||||
|
} else { |
||||
|
this.valueCardWidgetConfigForm.get('dateFormat').disable(); |
||||
|
this.valueCardWidgetConfigForm.get('dateFont').disable(); |
||||
|
this.valueCardWidgetConfigForm.get('dateColor').disable(); |
||||
|
} |
||||
|
} else { |
||||
|
this.valueCardWidgetConfigForm.get('showDate').disable({emitEvent: false}); |
||||
|
this.valueCardWidgetConfigForm.get('dateFormat').disable(); |
||||
|
this.valueCardWidgetConfigForm.get('dateFont').disable(); |
||||
|
this.valueCardWidgetConfigForm.get('dateColor').disable(); |
||||
|
} |
||||
|
this.valueCardWidgetConfigForm.get('showIcon').updateValueAndValidity({emitEvent: false}); |
||||
|
this.valueCardWidgetConfigForm.get('showDate').updateValueAndValidity({emitEvent: false}); |
||||
|
this.valueCardWidgetConfigForm.get('label').updateValueAndValidity({emitEvent}); |
||||
|
this.valueCardWidgetConfigForm.get('labelFont').updateValueAndValidity({emitEvent}); |
||||
|
this.valueCardWidgetConfigForm.get('labelColor').updateValueAndValidity({emitEvent}); |
||||
|
this.valueCardWidgetConfigForm.get('iconSize').updateValueAndValidity({emitEvent}); |
||||
|
this.valueCardWidgetConfigForm.get('iconSizeUnit').updateValueAndValidity({emitEvent}); |
||||
|
this.valueCardWidgetConfigForm.get('icon').updateValueAndValidity({emitEvent}); |
||||
|
this.valueCardWidgetConfigForm.get('iconColor').updateValueAndValidity({emitEvent}); |
||||
|
this.valueCardWidgetConfigForm.get('dateFormat').updateValueAndValidity({emitEvent}); |
||||
|
this.valueCardWidgetConfigForm.get('dateFont').updateValueAndValidity({emitEvent}); |
||||
|
this.valueCardWidgetConfigForm.get('dateColor').updateValueAndValidity({emitEvent}); |
||||
|
} |
||||
|
|
||||
|
private getCardButtons(config: WidgetConfig): string[] { |
||||
|
const buttons: string[] = []; |
||||
|
if (isUndefined(config.enableFullscreen) || config.enableFullscreen) { |
||||
|
buttons.push('fullscreen'); |
||||
|
} |
||||
|
return buttons; |
||||
|
} |
||||
|
|
||||
|
private setCardButtons(buttons: string[], config: WidgetConfig) { |
||||
|
config.enableFullscreen = buttons.includes('fullscreen'); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,263 @@ |
|||||
|
///
|
||||
|
/// 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 { isDefinedAndNotNull, isNumber, isNumeric, parseFunction } from '@core/utils'; |
||||
|
import { DataKey, Datasource, DatasourceData } from '@shared/models/widget.models'; |
||||
|
|
||||
|
export type ComponentStyle = {[klass: string]: any}; |
||||
|
|
||||
|
export const cssUnits = ['px', 'em', '%', 'rem', 'pt', 'pc', 'in', 'cm', 'mm', 'ex', 'ch', 'vw', 'vh', 'vmin', 'vmax'] as const; |
||||
|
type cssUnitTuple = typeof cssUnits; |
||||
|
export type cssUnit = cssUnitTuple[number]; |
||||
|
|
||||
|
export const fontWeights = ['normal', 'bold', 'bolder', 'lighter', '100', '200', '300', '400', '500', '600', '700', '800', '900'] as const; |
||||
|
type fontWeightTuple = typeof fontWeights; |
||||
|
export type fontWeight = fontWeightTuple[number]; |
||||
|
|
||||
|
export const fontWeightTranslations = new Map<fontWeight, string>( |
||||
|
[ |
||||
|
['normal', 'widgets.widget-font.font-weight-normal'], |
||||
|
['bold', 'widgets.widget-font.font-weight-bold'], |
||||
|
['bolder', 'widgets.widget-font.font-weight-bolder'], |
||||
|
['lighter', 'widgets.widget-font.font-weight-lighter'] |
||||
|
] |
||||
|
); |
||||
|
|
||||
|
export const fontStyles = ['normal', 'italic', 'oblique'] as const; |
||||
|
type fontStyleTuple = typeof fontStyles; |
||||
|
export type fontStyle = fontStyleTuple[number]; |
||||
|
|
||||
|
export const fontStyleTranslations = new Map<fontStyle, string>( |
||||
|
[ |
||||
|
['normal', 'widgets.widget-font.font-style-normal'], |
||||
|
['italic', 'widgets.widget-font.font-style-italic'], |
||||
|
['oblique', 'widgets.widget-font.font-style-oblique'] |
||||
|
] |
||||
|
); |
||||
|
|
||||
|
export const commonFonts = ['Roboto', 'monospace', 'sans-serif', 'serif']; |
||||
|
|
||||
|
export interface Font { |
||||
|
size: number; |
||||
|
sizeUnit: cssUnit; |
||||
|
family: string; |
||||
|
weight: fontWeight; |
||||
|
style: fontStyle; |
||||
|
} |
||||
|
|
||||
|
export enum ColorType { |
||||
|
constant = 'constant', |
||||
|
range = 'range', |
||||
|
function = 'function' |
||||
|
} |
||||
|
|
||||
|
export const colorTypeTranslations = new Map<ColorType, string>( |
||||
|
[ |
||||
|
[ColorType.constant, 'widgets.color.color-type-constant'], |
||||
|
[ColorType.range, 'widgets.color.color-type-range'], |
||||
|
[ColorType.function, 'widgets.color.color-type-function'] |
||||
|
] |
||||
|
); |
||||
|
|
||||
|
export interface ColorRange { |
||||
|
from?: number; |
||||
|
to?: number; |
||||
|
color: string; |
||||
|
} |
||||
|
|
||||
|
export interface ColorSettings { |
||||
|
type: ColorType; |
||||
|
color: string; |
||||
|
rangeList?: ColorRange[]; |
||||
|
colorFunction?: string; |
||||
|
} |
||||
|
|
||||
|
export const constantColor = (color: string): ColorSettings => ({ |
||||
|
type: ColorType.constant, |
||||
|
color, |
||||
|
colorFunction: 'var temperature = value;\n' + |
||||
|
'if (typeof temperature !== undefined) {\n' + |
||||
|
' var percent = (temperature + 60)/120 * 100;\n' + |
||||
|
' return tinycolor.mix(\'blue\', \'red\', percent).toHexString();\n' + |
||||
|
'}\n' + |
||||
|
'return \'blue\';' |
||||
|
}); |
||||
|
|
||||
|
type ValueColorFunction = (value: any) => string; |
||||
|
|
||||
|
export abstract class ColorProcessor { |
||||
|
|
||||
|
static fromSettings(color: ColorSettings): ColorProcessor { |
||||
|
switch (color.type) { |
||||
|
case ColorType.constant: |
||||
|
return new ConstantColorProcessor(color); |
||||
|
case ColorType.range: |
||||
|
return new RangeColorProcessor(color); |
||||
|
case ColorType.function: |
||||
|
return new FunctionColorProcessor(color); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
color: string; |
||||
|
|
||||
|
protected constructor(protected settings: ColorSettings) { |
||||
|
this.color = settings.color; |
||||
|
} |
||||
|
|
||||
|
abstract update(value: any): void; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
class ConstantColorProcessor extends ColorProcessor { |
||||
|
constructor(protected settings: ColorSettings) { |
||||
|
super(settings); |
||||
|
} |
||||
|
|
||||
|
update(value: any): void {} |
||||
|
} |
||||
|
|
||||
|
class RangeColorProcessor extends ColorProcessor { |
||||
|
|
||||
|
constructor(protected settings: ColorSettings) { |
||||
|
super(settings); |
||||
|
} |
||||
|
|
||||
|
update(value: any): void { |
||||
|
this.color = this.computeFromRange(value); |
||||
|
} |
||||
|
|
||||
|
private computeFromRange(value: any): string { |
||||
|
if (this.settings.rangeList?.length && isDefinedAndNotNull(value) && isNumeric(value)) { |
||||
|
const num = Number(value); |
||||
|
for (const range of this.settings.rangeList) { |
||||
|
if ((!isNumber(range.from) || num >= range.from) && (!isNumber(range.to) || num < range.to)) { |
||||
|
return range.color; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return this.settings.color; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class FunctionColorProcessor extends ColorProcessor { |
||||
|
|
||||
|
private readonly colorFunction: ValueColorFunction; |
||||
|
|
||||
|
constructor(protected settings: ColorSettings) { |
||||
|
super(settings); |
||||
|
this.colorFunction = parseFunction(settings.colorFunction, ['value']); |
||||
|
} |
||||
|
|
||||
|
update(value: any): void { |
||||
|
if (this.colorFunction) { |
||||
|
this.color = this.colorFunction(value) || this.settings.color; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export enum BackgroundType { |
||||
|
image = 'image', |
||||
|
imageUrl = 'imageUrl', |
||||
|
color = 'color' |
||||
|
} |
||||
|
|
||||
|
export interface OverlaySettings { |
||||
|
enabled: boolean; |
||||
|
color: string; |
||||
|
blur: number; |
||||
|
} |
||||
|
|
||||
|
export interface BackgroundSettings { |
||||
|
type: BackgroundType; |
||||
|
imageBase64?: string; |
||||
|
imageUrl?: string; |
||||
|
color?: string; |
||||
|
overlay: OverlaySettings; |
||||
|
} |
||||
|
|
||||
|
export const iconStyle = (size: number, sizeUnit: cssUnit): ComponentStyle => { |
||||
|
const iconSize = size + sizeUnit; |
||||
|
return { |
||||
|
width: iconSize, |
||||
|
height: iconSize, |
||||
|
fontSize: iconSize, |
||||
|
lineHeight: iconSize |
||||
|
}; |
||||
|
}; |
||||
|
|
||||
|
export const textStyle = (font: Font, lineHeight = '1.5', letterSpacing = '0.25px'): ComponentStyle => ({ |
||||
|
font: font.style + ' normal ' + font.weight + ' ' + (font.size+font.sizeUnit) + '/' + lineHeight + ' ' + font.family + |
||||
|
(font.family !== 'Roboto' ? ', Roboto' : ''), |
||||
|
letterSpacing |
||||
|
}); |
||||
|
|
||||
|
export const backgroundStyle = (background: BackgroundSettings): ComponentStyle => { |
||||
|
if (background.type === BackgroundType.color) { |
||||
|
return { |
||||
|
background: background.color |
||||
|
}; |
||||
|
} else { |
||||
|
const imageUrl = background.type === BackgroundType.image ? background.imageBase64 : background.imageUrl; |
||||
|
return { |
||||
|
background: `url(${imageUrl}) no-repeat`, |
||||
|
backgroundSize: 'cover', |
||||
|
backgroundPosition: '50% 50%' |
||||
|
}; |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
export const overlayStyle = (overlay: OverlaySettings): ComponentStyle => ( |
||||
|
{ |
||||
|
display: overlay.enabled ? 'block' : 'none', |
||||
|
background: overlay.color, |
||||
|
backdropFilter: `blur(${overlay.blur}px)` |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
export const getDataKey = (datasources?: Datasource[]): DataKey => { |
||||
|
if (datasources && datasources.length) { |
||||
|
const dataKeys = datasources[0].dataKeys; |
||||
|
if (dataKeys && dataKeys.length) { |
||||
|
return dataKeys[0]; |
||||
|
} |
||||
|
} |
||||
|
return null; |
||||
|
}; |
||||
|
|
||||
|
export const getLabel = (datasources?: Datasource[]): string => { |
||||
|
const dataKey = getDataKey(datasources); |
||||
|
if (dataKey) { |
||||
|
return dataKey.label; |
||||
|
} |
||||
|
return ''; |
||||
|
}; |
||||
|
|
||||
|
export const setLabel = (label: string, datasources?: Datasource[]): void => { |
||||
|
const dataKey = getDataKey(datasources); |
||||
|
if (dataKey) { |
||||
|
dataKey.label = label; |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
export const getSingleTsValue = (data: Array<DatasourceData>): [number, any] => { |
||||
|
if (data.length) { |
||||
|
const dsData = data[0]; |
||||
|
if (dsData.data.length) { |
||||
|
return dsData.data[0]; |
||||
|
} |
||||
|
} |
||||
|
return null; |
||||
|
}; |
||||
@ -0,0 +1,70 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
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-value-card-panel" [class]="this.layout" [style]="backgroundStyle"> |
||||
|
<div class="tb-value-card-overlay" [style]="overlayStyle"></div> |
||||
|
<ng-container [ngSwitch]="layout"> |
||||
|
<ng-template [ngSwitchCase]="valueCardLayout.square"> |
||||
|
<ng-container *ngTemplateOutlet="iconWithLabelTpl"></ng-container> |
||||
|
<ng-container *ngTemplateOutlet="valueTpl"></ng-container> |
||||
|
</ng-template> |
||||
|
<ng-template [ngSwitchCase]="valueCardLayout.vertical"> |
||||
|
<ng-container *ngTemplateOutlet="labelTpl"></ng-container> |
||||
|
<ng-container *ngTemplateOutlet="valueTpl"></ng-container> |
||||
|
<ng-container *ngTemplateOutlet="iconTpl"></ng-container> |
||||
|
</ng-template> |
||||
|
<ng-template [ngSwitchCase]="valueCardLayout.centered"> |
||||
|
<ng-container *ngTemplateOutlet="labelTpl"></ng-container> |
||||
|
<div class="tb-value-card-icon-row"> |
||||
|
<ng-container *ngTemplateOutlet="iconTpl"></ng-container> |
||||
|
<ng-container *ngTemplateOutlet="valueTpl"></ng-container> |
||||
|
</div> |
||||
|
<ng-container *ngTemplateOutlet="dateTpl"></ng-container> |
||||
|
</ng-template> |
||||
|
<ng-template [ngSwitchCase]="valueCardLayout.simplified"> |
||||
|
<ng-container *ngTemplateOutlet="valueTpl"></ng-container> |
||||
|
<ng-container *ngTemplateOutlet="labelTpl"></ng-container> |
||||
|
</ng-template> |
||||
|
<ng-template [ngSwitchCase]="layout === valueCardLayout.horizontal || |
||||
|
layout === valueCardLayout.horizontal_reversed ? layout : ''"> |
||||
|
<ng-container *ngTemplateOutlet="iconWithLabelTpl"></ng-container> |
||||
|
<div fxFlex></div> |
||||
|
<ng-container *ngTemplateOutlet="valueTpl"></ng-container> |
||||
|
</ng-template> |
||||
|
</ng-container> |
||||
|
</div> |
||||
|
<ng-template #iconWithLabelTpl> |
||||
|
<div class="tb-value-card-icon-row"> |
||||
|
<ng-container *ngTemplateOutlet="iconTpl"></ng-container> |
||||
|
<div class="tb-value-card-label-row"> |
||||
|
<ng-container *ngTemplateOutlet="labelTpl"></ng-container> |
||||
|
<ng-container *ngTemplateOutlet="dateTpl"></ng-container> |
||||
|
</div> |
||||
|
</div> |
||||
|
</ng-template> |
||||
|
<ng-template #iconTpl> |
||||
|
<tb-icon *ngIf="showIcon" [style]="iconStyle" [style.color]="iconColor.color">{{ icon }}</tb-icon> |
||||
|
</ng-template> |
||||
|
<ng-template #labelTpl> |
||||
|
<div *ngIf="showLabel" [style]="labelStyle" [style.color]="labelColor.color">{{ label }}</div> |
||||
|
</ng-template> |
||||
|
<ng-template #dateTpl> |
||||
|
<div *ngIf="showDate" [style]="dateStyle" [style.color]="dateColor.color">{{ dateText }}</div> |
||||
|
</ng-template> |
||||
|
<ng-template #valueTpl> |
||||
|
<div class="tb-value-card-value" [style]="valueStyle" [style.color]="valueColor.color">{{ valueText }}</div> |
||||
|
</ng-template> |
||||
@ -0,0 +1,70 @@ |
|||||
|
/** |
||||
|
* 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. |
||||
|
*/ |
||||
|
:host { |
||||
|
.tb-value-card-panel { |
||||
|
width: 100%; |
||||
|
height: 100%; |
||||
|
position: relative; |
||||
|
display: flex; |
||||
|
flex-direction: column; |
||||
|
align-items: center; |
||||
|
justify-content: center; |
||||
|
gap: 16px; |
||||
|
padding: 24px; |
||||
|
> div:not(.tb-value-card-overlay) { |
||||
|
z-index: 1; |
||||
|
} |
||||
|
&.square { |
||||
|
justify-content: space-evenly; |
||||
|
gap: 0; |
||||
|
} |
||||
|
&.horizontal { |
||||
|
flex-direction: row; |
||||
|
} |
||||
|
&.horizontal_reversed { |
||||
|
flex-direction: row-reverse; |
||||
|
} |
||||
|
.tb-value-card-overlay { |
||||
|
position: absolute; |
||||
|
top: 12px; |
||||
|
left: 12px; |
||||
|
bottom: 12px; |
||||
|
right: 12px; |
||||
|
} |
||||
|
.tb-value-card-icon-row { |
||||
|
display: flex; |
||||
|
flex-direction: row; |
||||
|
align-items: center; |
||||
|
gap: 8px; |
||||
|
} |
||||
|
&.horizontal_reversed { |
||||
|
.tb-value-card-icon-row { |
||||
|
flex-direction: row-reverse; |
||||
|
} |
||||
|
.tb-value-card-label-row { |
||||
|
align-items: flex-end; |
||||
|
} |
||||
|
} |
||||
|
.tb-value-card-label-row { |
||||
|
display: flex; |
||||
|
flex-direction: column; |
||||
|
justify-content: center; |
||||
|
} |
||||
|
.tb-value-card-value { |
||||
|
white-space: nowrap; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,144 @@ |
|||||
|
///
|
||||
|
/// 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, Input, OnInit } from '@angular/core'; |
||||
|
import { WidgetContext } from '@home/models/widget-component.models'; |
||||
|
import { formatValue, isDefinedAndNotNull } from '@core/utils'; |
||||
|
import { DatePipe } from '@angular/common'; |
||||
|
import { |
||||
|
backgroundStyle, |
||||
|
ColorProcessor, |
||||
|
ComponentStyle, |
||||
|
getDataKey, |
||||
|
getLabel, |
||||
|
getSingleTsValue, |
||||
|
iconStyle, |
||||
|
overlayStyle, |
||||
|
textStyle |
||||
|
} from '@home/components/widget/config/widget-settings.models'; |
||||
|
import { valueCardDefaultSettings, ValueCardLayout, ValueCardWidgetSettings } from './value-card-widget.models'; |
||||
|
import { WidgetComponent } from '@home/components/widget/widget.component'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-value-card-widget', |
||||
|
templateUrl: './value-card-widget.component.html', |
||||
|
styleUrls: ['./value-card-widget.component.scss'] |
||||
|
}) |
||||
|
export class ValueCardWidgetComponent implements OnInit { |
||||
|
|
||||
|
settings: ValueCardWidgetSettings; |
||||
|
|
||||
|
valueCardLayout = ValueCardLayout; |
||||
|
|
||||
|
@Input() |
||||
|
ctx: WidgetContext; |
||||
|
|
||||
|
layout: ValueCardLayout; |
||||
|
showIcon = true; |
||||
|
icon = ''; |
||||
|
iconStyle: ComponentStyle = {}; |
||||
|
iconColor: ColorProcessor; |
||||
|
|
||||
|
showLabel = true; |
||||
|
label = ''; |
||||
|
labelStyle: ComponentStyle = {}; |
||||
|
labelColor: ColorProcessor; |
||||
|
|
||||
|
valueText = 'N/A'; |
||||
|
valueStyle: ComponentStyle = {}; |
||||
|
valueColor: ColorProcessor; |
||||
|
|
||||
|
showDate = true; |
||||
|
dateText = ''; |
||||
|
dateStyle: ComponentStyle = {}; |
||||
|
dateColor: ColorProcessor; |
||||
|
|
||||
|
backgroundStyle: ComponentStyle = {}; |
||||
|
overlayStyle: ComponentStyle = {}; |
||||
|
|
||||
|
private horizontal = false; |
||||
|
private dateFormat: string; |
||||
|
private decimals = 0; |
||||
|
private units = ''; |
||||
|
|
||||
|
constructor(private date: DatePipe, |
||||
|
private widgetComponent: WidgetComponent, |
||||
|
private cd: ChangeDetectorRef) { |
||||
|
} |
||||
|
|
||||
|
ngOnInit(): void { |
||||
|
const params = this.widgetComponent.typeParameters as any; |
||||
|
this.horizontal = isDefinedAndNotNull(params.horizontal) ? params.horizontal : false; |
||||
|
this.ctx.$scope.valueCardWidget = this; |
||||
|
this.settings = {...valueCardDefaultSettings(this.horizontal), ...this.ctx.settings}; |
||||
|
|
||||
|
this.decimals = this.ctx.decimals; |
||||
|
this.units = this.ctx.units; |
||||
|
const dataKey = getDataKey(this.ctx.datasources); |
||||
|
if (isDefinedAndNotNull(dataKey?.decimals)) { |
||||
|
this.decimals = dataKey.decimals; |
||||
|
} |
||||
|
if (dataKey?.units) { |
||||
|
this.units = dataKey.units; |
||||
|
} |
||||
|
|
||||
|
this.layout = this.settings.layout; |
||||
|
|
||||
|
this.showIcon = this.settings.showIcon; |
||||
|
this.icon = this.settings.icon; |
||||
|
this.iconStyle = iconStyle(this.settings.iconSize, this.settings.iconSizeUnit ); |
||||
|
this.iconColor = ColorProcessor.fromSettings(this.settings.iconColor); |
||||
|
|
||||
|
this.showLabel = this.settings.showLabel; |
||||
|
this.label = getLabel(this.ctx.datasources); |
||||
|
this.labelStyle = textStyle(this.settings.labelFont, '1.5', '0.25px'); |
||||
|
this.labelColor = ColorProcessor.fromSettings(this.settings.labelColor); |
||||
|
this.valueStyle = textStyle(this.settings.valueFont, '100%', '0.13px'); |
||||
|
this.valueColor = ColorProcessor.fromSettings(this.settings.valueColor); |
||||
|
|
||||
|
this.showDate = this.settings.showDate; |
||||
|
this.dateFormat = this.settings.dateFormat; |
||||
|
this.dateStyle = textStyle(this.settings.dateFont, '1.33', '0.25px'); |
||||
|
this.dateColor = ColorProcessor.fromSettings(this.settings.dateColor); |
||||
|
|
||||
|
this.backgroundStyle = backgroundStyle(this.settings.background); |
||||
|
this.overlayStyle = overlayStyle(this.settings.background.overlay); |
||||
|
} |
||||
|
|
||||
|
public onInit() { |
||||
|
const borderRadius = this.ctx.$widgetElement.css('borderRadius'); |
||||
|
this.overlayStyle = {...this.overlayStyle, ...{borderRadius}}; |
||||
|
this.cd.detectChanges(); |
||||
|
} |
||||
|
|
||||
|
public onDataUpdated() { |
||||
|
const tsValue = getSingleTsValue(this.ctx.data); |
||||
|
let value; |
||||
|
if (tsValue) { |
||||
|
value = tsValue[1]; |
||||
|
this.valueText = formatValue(value, this.decimals, this.units, true); |
||||
|
this.dateText = this.date.transform(tsValue[0], this.dateFormat); |
||||
|
} else { |
||||
|
this.valueText = 'N/A'; |
||||
|
this.dateText = ''; |
||||
|
} |
||||
|
this.iconColor.update(value); |
||||
|
this.labelColor.update(value); |
||||
|
this.valueColor.update(value); |
||||
|
this.dateColor.update(value); |
||||
|
this.cd.detectChanges(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,127 @@ |
|||||
|
///
|
||||
|
/// 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 { |
||||
|
BackgroundSettings, |
||||
|
BackgroundType, |
||||
|
ColorSettings, |
||||
|
constantColor, |
||||
|
cssUnit, |
||||
|
Font |
||||
|
} from '@home/components/widget/config/widget-settings.models'; |
||||
|
|
||||
|
export enum ValueCardLayout { |
||||
|
square = 'square', |
||||
|
vertical = 'vertical', |
||||
|
centered = 'centered', |
||||
|
simplified = 'simplified', |
||||
|
horizontal = 'horizontal', |
||||
|
horizontal_reversed = 'horizontal_reversed' |
||||
|
} |
||||
|
|
||||
|
export const valueCardLayouts = (horizontal: boolean): ValueCardLayout[] => { |
||||
|
if (horizontal) { |
||||
|
return [ValueCardLayout.horizontal, ValueCardLayout.horizontal_reversed]; |
||||
|
} else { |
||||
|
return [ValueCardLayout.square, ValueCardLayout.vertical, ValueCardLayout.centered, ValueCardLayout.simplified]; |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
export const valueCardLayoutTranslations = new Map<ValueCardLayout, string>( |
||||
|
[ |
||||
|
[ValueCardLayout.square, 'widgets.value-card.layout-square'], |
||||
|
[ValueCardLayout.vertical, 'widgets.value-card.layout-vertical'], |
||||
|
[ValueCardLayout.centered, 'widgets.value-card.layout-centered'], |
||||
|
[ValueCardLayout.simplified, 'widgets.value-card.layout-simplified'], |
||||
|
[ValueCardLayout.horizontal, 'widgets.value-card.layout-horizontal'], |
||||
|
[ValueCardLayout.horizontal_reversed, 'widgets.value-card.layout-horizontal-reversed'] |
||||
|
] |
||||
|
); |
||||
|
|
||||
|
export const valueCardLayoutImages = new Map<ValueCardLayout, string>( |
||||
|
[ |
||||
|
[ValueCardLayout.square, 'assets/widget/value-card/square-layout.svg'], |
||||
|
[ValueCardLayout.vertical, 'assets/widget/value-card/vertical-layout.svg'], |
||||
|
[ValueCardLayout.centered, 'assets/widget/value-card/centered-layout.svg'], |
||||
|
[ValueCardLayout.simplified, 'assets/widget/value-card/simplified-layout.svg'], |
||||
|
[ValueCardLayout.horizontal, 'assets/widget/value-card/horizontal-layout.svg'], |
||||
|
[ValueCardLayout.horizontal_reversed, 'assets/widget/value-card/horizontal-reversed-layout.svg'] |
||||
|
] |
||||
|
); |
||||
|
|
||||
|
export interface ValueCardWidgetSettings { |
||||
|
layout: ValueCardLayout; |
||||
|
showLabel: boolean; |
||||
|
labelFont: Font; |
||||
|
labelColor: ColorSettings; |
||||
|
showIcon: boolean; |
||||
|
icon: string; |
||||
|
iconSize: number; |
||||
|
iconSizeUnit: cssUnit; |
||||
|
iconColor: ColorSettings; |
||||
|
valueFont: Font; |
||||
|
valueColor: ColorSettings; |
||||
|
showDate: boolean; |
||||
|
dateFormat: string; |
||||
|
dateFont: Font; |
||||
|
dateColor: ColorSettings; |
||||
|
background: BackgroundSettings; |
||||
|
} |
||||
|
|
||||
|
export const valueCardDefaultSettings = (horizontal: boolean): ValueCardWidgetSettings => ({ |
||||
|
layout: horizontal ? ValueCardLayout.horizontal : ValueCardLayout.square, |
||||
|
showLabel: true, |
||||
|
labelFont: { |
||||
|
family: 'Roboto', |
||||
|
size: 16, |
||||
|
sizeUnit: 'px', |
||||
|
style: 'normal', |
||||
|
weight: '500' |
||||
|
}, |
||||
|
labelColor: constantColor('rgba(0, 0, 0, 0.87)'), |
||||
|
showIcon: true, |
||||
|
icon: 'thermostat', |
||||
|
iconSize: 40, |
||||
|
iconSizeUnit: 'px', |
||||
|
iconColor: constantColor('#5469FF'), |
||||
|
valueFont: { |
||||
|
family: 'Roboto', |
||||
|
size: 52, |
||||
|
sizeUnit: 'px', |
||||
|
style: 'normal', |
||||
|
weight: '500' |
||||
|
}, |
||||
|
valueColor: constantColor('rgba(0, 0, 0, 0.87)'), |
||||
|
showDate: true, |
||||
|
dateFormat: 'yyyy-MM-dd HH:mm:ss', |
||||
|
dateFont: { |
||||
|
family: 'Roboto', |
||||
|
size: 12, |
||||
|
sizeUnit: 'px', |
||||
|
style: 'normal', |
||||
|
weight: '500' |
||||
|
}, |
||||
|
dateColor: constantColor('rgba(0, 0, 0, 0.38)'), |
||||
|
background: { |
||||
|
type: BackgroundType.color, |
||||
|
color: '#fff', |
||||
|
overlay: { |
||||
|
enabled: false, |
||||
|
color: 'rgba(255,255,255,0.72)', |
||||
|
blur: 3 |
||||
|
} |
||||
|
} |
||||
|
}); |
||||
@ -0,0 +1,105 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
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-color-settings-panel" [formGroup]="colorSettingsFormGroup"> |
||||
|
<div class="tb-color-settings-title" translate>widgets.color.color-settings</div> |
||||
|
<div fxLayout="row"> |
||||
|
<tb-toggle-select formControlName="type" fxFlex.xs fxFlex="70%"> |
||||
|
<tb-toggle-option *ngFor="let type of colorTypes" |
||||
|
[value]="type"> |
||||
|
{{ colorTypeTranslationsMap.get(type) | translate }} |
||||
|
</tb-toggle-option> |
||||
|
</tb-toggle-select> |
||||
|
</div> |
||||
|
<div class="tb-form-row space-between"> |
||||
|
<div translate>widgets.color.color</div> |
||||
|
<tb-color-input asBoxInput |
||||
|
formControlName="color"> |
||||
|
</tb-color-input> |
||||
|
</div> |
||||
|
<div class="tb-color-settings-panel-body" [fxShow]="colorSettingsFormGroup.get('type').value === colorType.constant"> |
||||
|
</div> |
||||
|
<div class="tb-color-settings-panel-body" [fxShow]="colorSettingsFormGroup.get('type').value === colorType.range"> |
||||
|
<ng-container *ngTemplateOutlet="range"></ng-container> |
||||
|
</div> |
||||
|
<div class="tb-color-settings-panel-body" [fxShow]="colorSettingsFormGroup.get('type').value === colorType.function"> |
||||
|
<ng-container *ngTemplateOutlet="function"></ng-container> |
||||
|
</div> |
||||
|
<div class="tb-color-settings-panel-buttons"> |
||||
|
<button mat-button |
||||
|
color="primary" |
||||
|
type="button" |
||||
|
(click)="cancel()"> |
||||
|
{{ 'action.cancel' | translate }} |
||||
|
</button> |
||||
|
<button mat-raised-button |
||||
|
color="primary" |
||||
|
type="button" |
||||
|
(click)="applyColorSettings()" |
||||
|
[disabled]="colorSettingsFormGroup.invalid || !colorSettingsFormGroup.dirty"> |
||||
|
{{ 'action.apply' | translate }} |
||||
|
</button> |
||||
|
</div> |
||||
|
</div> |
||||
|
|
||||
|
<ng-template #range> |
||||
|
<div fxFlex class="tb-color-ranges-panel"> |
||||
|
<div class="tb-form-panel-title" translate>widgets.color.value-range</div> |
||||
|
<div class="tb-color-ranges" [formGroup]="colorSettingsFormGroup"> |
||||
|
<div class="tb-form-row no-padding no-border" [formGroup]="rangeFormGroup" *ngFor="let rangeFormGroup of rangeListFormGroups; trackBy: trackByRange; let $index = index;"> |
||||
|
<div fxFlex fxLayout="row" fxLayoutGap="24px"> |
||||
|
<div fxFlex fxLayout="row" fxLayoutGap="12px" fxLayoutAlign="start center"> |
||||
|
<div class="tb-value-range-text" translate>widgets.color.from</div> |
||||
|
<mat-form-field fxFlex appearance="outline" class="center number" subscriptSizing="dynamic"> |
||||
|
<input matInput type="number" formControlName="from" placeholder="{{ 'widget-config.set' | translate }}"> |
||||
|
</mat-form-field> |
||||
|
<div class="tb-value-range-text tb-value-range-text-to" translate>widgets.color.to</div> |
||||
|
<mat-form-field fxFlex appearance="outline" class="center number" subscriptSizing="dynamic"> |
||||
|
<input matInput type="number" formControlName="to" placeholder="{{ 'widget-config.set' | translate }}"> |
||||
|
</mat-form-field> |
||||
|
<tb-color-input asBoxInput |
||||
|
formControlName="color"> |
||||
|
</tb-color-input> |
||||
|
</div> |
||||
|
<button type="button" |
||||
|
mat-icon-button |
||||
|
class="tb-box-button" |
||||
|
(click)="removeRange($index)" |
||||
|
matTooltip="{{ 'action.remove' | translate }}" |
||||
|
matTooltipPosition="above"> |
||||
|
<mat-icon>delete</mat-icon> |
||||
|
</button> |
||||
|
</div> |
||||
|
</div> |
||||
|
</div> |
||||
|
<button class="tb-add-color-range" |
||||
|
mat-stroked-button |
||||
|
(click)="addRange()"> |
||||
|
<mat-icon>add</mat-icon> |
||||
|
</button> |
||||
|
</div> |
||||
|
</ng-template> |
||||
|
<ng-template #function> |
||||
|
<div class="tb-form-panel no-padding no-border" [formGroup]="colorSettingsFormGroup"> |
||||
|
<tb-js-func formControlName="colorFunction" |
||||
|
[functionArgs]="['value']" |
||||
|
[globalVariables]="functionScopeVariables" |
||||
|
functionTitle="{{ 'widgets.color.color-function' | translate }}" |
||||
|
helpId="widget/lib/card/value_color_fn"> |
||||
|
</tb-js-func> |
||||
|
</div> |
||||
|
</ng-template> |
||||
@ -0,0 +1,85 @@ |
|||||
|
/** |
||||
|
* 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 '../../../../../../../../scss/constants'; |
||||
|
|
||||
|
.tb-color-settings-panel { |
||||
|
width: 500px; |
||||
|
height: 470px; |
||||
|
display: flex; |
||||
|
flex-direction: column; |
||||
|
gap: 16px; |
||||
|
@media #{$mat-xs} { |
||||
|
width: 90vw; |
||||
|
} |
||||
|
.tb-color-settings-title { |
||||
|
font-size: 16px; |
||||
|
font-weight: 500; |
||||
|
line-height: 24px; |
||||
|
letter-spacing: 0.25px; |
||||
|
color: rgba(0, 0, 0, 0.87); |
||||
|
} |
||||
|
.tb-color-ranges-panel { |
||||
|
flex: 1; |
||||
|
min-height: 0; |
||||
|
gap: 16px; |
||||
|
display: flex; |
||||
|
flex-direction: column; |
||||
|
} |
||||
|
.tb-color-ranges { |
||||
|
flex: 1; |
||||
|
gap: 12px; |
||||
|
display: flex; |
||||
|
flex-direction: column; |
||||
|
overflow: auto; |
||||
|
} |
||||
|
.tb-form-row { |
||||
|
height: auto; |
||||
|
.tb-value-range-text { |
||||
|
width: 64px; |
||||
|
font-size: 14px; |
||||
|
color: rgba(0, 0, 0, 0.38); |
||||
|
@media #{$mat-xs} { |
||||
|
width: auto; |
||||
|
} |
||||
|
&.tb-value-range-text-to { |
||||
|
text-align: center; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
button.mat-mdc-button-base.tb-add-color-range { |
||||
|
&:not(:disabled) { |
||||
|
color: rgba(0, 0, 0, 0.54); |
||||
|
} |
||||
|
&:disabled { |
||||
|
color: rgba(0, 0, 0, 0.12); |
||||
|
} |
||||
|
} |
||||
|
.tb-color-settings-panel-body { |
||||
|
flex: 1; |
||||
|
display: flex; |
||||
|
flex-direction: column; |
||||
|
min-height: 0; |
||||
|
} |
||||
|
.tb-color-settings-panel-buttons { |
||||
|
height: 40px; |
||||
|
display: flex; |
||||
|
flex-direction: row; |
||||
|
gap: 16px; |
||||
|
justify-content: flex-end; |
||||
|
align-items: flex-end; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,131 @@ |
|||||
|
///
|
||||
|
/// 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 { Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation } from '@angular/core'; |
||||
|
import { PageComponent } from '@shared/components/page.component'; |
||||
|
import { |
||||
|
ColorRange, |
||||
|
ColorSettings, |
||||
|
ColorType, |
||||
|
colorTypeTranslations |
||||
|
} from '@home/components/widget/config/widget-settings.models'; |
||||
|
import { TbPopoverComponent } from '@shared/components/popover.component'; |
||||
|
import { |
||||
|
AbstractControl, |
||||
|
FormControl, |
||||
|
FormGroup, |
||||
|
UntypedFormArray, |
||||
|
UntypedFormBuilder, |
||||
|
UntypedFormGroup |
||||
|
} from '@angular/forms'; |
||||
|
import { Store } from '@ngrx/store'; |
||||
|
import { AppState } from '@core/core.state'; |
||||
|
import { Datasource, DatasourceType } from '@shared/models/widget.models'; |
||||
|
import { deepClone } from '@core/utils'; |
||||
|
import { DataKeyType } from '@shared/models/telemetry/telemetry.models'; |
||||
|
import { WidgetService } from '@core/http/widget.service'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-color-settings-panel', |
||||
|
templateUrl: './color-settings-panel.component.html', |
||||
|
providers: [], |
||||
|
styleUrls: ['./color-settings-panel.component.scss'], |
||||
|
encapsulation: ViewEncapsulation.None |
||||
|
}) |
||||
|
export class ColorSettingsPanelComponent extends PageComponent implements OnInit { |
||||
|
|
||||
|
@Input() |
||||
|
colorSettings: ColorSettings; |
||||
|
|
||||
|
@Input() |
||||
|
popover: TbPopoverComponent<ColorSettingsPanelComponent>; |
||||
|
|
||||
|
@Output() |
||||
|
colorSettingsApplied = new EventEmitter<ColorSettings>(); |
||||
|
|
||||
|
colorType = ColorType; |
||||
|
|
||||
|
colorTypes = Object.keys(ColorType) as ColorType[]; |
||||
|
|
||||
|
colorTypeTranslationsMap = colorTypeTranslations; |
||||
|
|
||||
|
colorSettingsFormGroup: UntypedFormGroup; |
||||
|
|
||||
|
functionScopeVariables = this.widgetService.getWidgetScopeVariables(); |
||||
|
|
||||
|
constructor(private fb: UntypedFormBuilder, |
||||
|
private widgetService: WidgetService, |
||||
|
protected store: Store<AppState>) { |
||||
|
super(store); |
||||
|
} |
||||
|
|
||||
|
ngOnInit(): void { |
||||
|
this.colorSettingsFormGroup = this.fb.group( |
||||
|
{ |
||||
|
type: [this.colorSettings?.type, []], |
||||
|
color: [this.colorSettings?.color, []], |
||||
|
rangeList: this.fb.array((this.colorSettings?.rangeList || []).map(r => this.colorRangeControl(r))), |
||||
|
colorFunction: [this.colorSettings?.colorFunction, []] |
||||
|
} |
||||
|
); |
||||
|
this.colorSettingsFormGroup.get('type').valueChanges.subscribe(() => { |
||||
|
setTimeout(() => {this.popover?.updatePosition();}, 0); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
private colorRangeControl(range: ColorRange): AbstractControl { |
||||
|
return this.fb.group({ |
||||
|
from: [range?.from, []], |
||||
|
to: [range?.to, []], |
||||
|
color: [range?.color, []] |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
get rangeListFormArray(): UntypedFormArray { |
||||
|
return this.colorSettingsFormGroup.get('rangeList') as UntypedFormArray; |
||||
|
} |
||||
|
|
||||
|
get rangeListFormGroups(): FormGroup[] { |
||||
|
return this.rangeListFormArray.controls as FormGroup[]; |
||||
|
} |
||||
|
|
||||
|
trackByRange(index: number, rangeControl: AbstractControl): any { |
||||
|
return rangeControl; |
||||
|
} |
||||
|
|
||||
|
removeRange(index: number) { |
||||
|
this.rangeListFormArray.removeAt(index); |
||||
|
setTimeout(() => {this.popover?.updatePosition();}, 0); |
||||
|
} |
||||
|
|
||||
|
addRange() { |
||||
|
const newRange: ColorRange = { |
||||
|
color: 'rgba(0,0,0,0.87)' |
||||
|
}; |
||||
|
this.rangeListFormArray.push(this.colorRangeControl(newRange), {emitEvent: true}); |
||||
|
setTimeout(() => {this.popover?.updatePosition();}, 0); |
||||
|
} |
||||
|
|
||||
|
cancel() { |
||||
|
this.popover?.hide(); |
||||
|
} |
||||
|
|
||||
|
applyColorSettings() { |
||||
|
const colorSettings = this.colorSettingsFormGroup.value; |
||||
|
this.colorSettingsApplied.emit(colorSettings); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
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. |
||||
|
|
||||
|
--> |
||||
|
<button type="button" |
||||
|
mat-stroked-button |
||||
|
class="tb-box-button" |
||||
|
[disabled]="disabled" |
||||
|
#matButton |
||||
|
(click)="openColorSettingsPopup($event, matButton)"> |
||||
|
<tb-icon matButtonIcon *ngIf="modelValue.type === colorType.function; else colorPreview">mdi:function-variant</tb-icon> |
||||
|
</button> |
||||
|
<ng-template #colorPreview> |
||||
|
<div class="tb-color-preview box" [ngClass]="{'disabled': disabled}"> |
||||
|
<div class="tb-color-result" [style]="colorStyle"></div> |
||||
|
</div> |
||||
|
</ng-template> |
||||
@ -0,0 +1,124 @@ |
|||||
|
///
|
||||
|
/// 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 { Component, forwardRef, Input, OnInit, Renderer2, ViewContainerRef } from '@angular/core'; |
||||
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; |
||||
|
import { ColorSettings, ColorType, ComponentStyle } from '@home/components/widget/config/widget-settings.models'; |
||||
|
import { MatButton } from '@angular/material/button'; |
||||
|
import { TbPopoverService } from '@shared/components/popover.service'; |
||||
|
import { |
||||
|
ColorSettingsPanelComponent |
||||
|
} from '@home/components/widget/lib/settings/common/color-settings-panel.component'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-color-settings', |
||||
|
templateUrl: './color-settings.component.html', |
||||
|
styleUrls: [], |
||||
|
providers: [ |
||||
|
{ |
||||
|
provide: NG_VALUE_ACCESSOR, |
||||
|
useExisting: forwardRef(() => ColorSettingsComponent), |
||||
|
multi: true |
||||
|
} |
||||
|
] |
||||
|
}) |
||||
|
export class ColorSettingsComponent implements OnInit, ControlValueAccessor { |
||||
|
|
||||
|
@Input() |
||||
|
disabled: boolean; |
||||
|
|
||||
|
colorType = ColorType; |
||||
|
|
||||
|
modelValue: ColorSettings; |
||||
|
|
||||
|
colorStyle: ComponentStyle = {}; |
||||
|
|
||||
|
private propagateChange = null; |
||||
|
|
||||
|
constructor(private popoverService: TbPopoverService, |
||||
|
private renderer: Renderer2, |
||||
|
private viewContainerRef: ViewContainerRef) {} |
||||
|
|
||||
|
ngOnInit(): void { |
||||
|
} |
||||
|
|
||||
|
registerOnChange(fn: any): void { |
||||
|
this.propagateChange = fn; |
||||
|
} |
||||
|
|
||||
|
registerOnTouched(fn: any): void { |
||||
|
} |
||||
|
|
||||
|
setDisabledState(isDisabled: boolean): void { |
||||
|
this.disabled = isDisabled; |
||||
|
this.updateColorStyle(); |
||||
|
} |
||||
|
|
||||
|
writeValue(value: ColorSettings): void { |
||||
|
this.modelValue = value; |
||||
|
this.updateColorStyle(); |
||||
|
} |
||||
|
|
||||
|
openColorSettingsPopup($event: Event, matButton: MatButton) { |
||||
|
if ($event) { |
||||
|
$event.stopPropagation(); |
||||
|
} |
||||
|
const trigger = matButton._elementRef.nativeElement; |
||||
|
if (this.popoverService.hasPopover(trigger)) { |
||||
|
this.popoverService.hidePopover(trigger); |
||||
|
} else { |
||||
|
const ctx: any = { |
||||
|
colorSettings: this.modelValue |
||||
|
}; |
||||
|
const colorSettingsPanelPopover = this.popoverService.displayPopover(trigger, this.renderer, |
||||
|
this.viewContainerRef, ColorSettingsPanelComponent, 'left', true, null, |
||||
|
ctx, |
||||
|
{}, |
||||
|
{}, {}, true); |
||||
|
colorSettingsPanelPopover.tbComponentRef.instance.popover = colorSettingsPanelPopover; |
||||
|
colorSettingsPanelPopover.tbComponentRef.instance.colorSettingsApplied.subscribe((colorSettings) => { |
||||
|
colorSettingsPanelPopover.hide(); |
||||
|
this.modelValue = colorSettings; |
||||
|
this.updateColorStyle(); |
||||
|
this.propagateChange(this.modelValue); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private updateColorStyle() { |
||||
|
if (!this.disabled) { |
||||
|
let colors: string[] = [this.modelValue.color]; |
||||
|
if (this.modelValue.type === ColorType.range && this.modelValue.rangeList?.length) { |
||||
|
const rangeColors = this.modelValue.rangeList.slice(0, Math.min(2, this.modelValue.rangeList.length)).map(r => r.color); |
||||
|
colors = colors.concat(rangeColors); |
||||
|
} |
||||
|
if (colors.length === 1) { |
||||
|
this.colorStyle = {backgroundColor: colors[0]}; |
||||
|
} else { |
||||
|
const gradientValues: string[] = []; |
||||
|
const step = 100 / colors.length; |
||||
|
for (let i = 0; i < colors.length; i++) { |
||||
|
gradientValues.push(`${colors[i]} ${step*i}%`); |
||||
|
gradientValues.push(`${colors[i]} ${step*(i+1)}%`); |
||||
|
} |
||||
|
this.colorStyle = {background: `linear-gradient(90deg, ${gradientValues.join(', ')})`}; |
||||
|
} |
||||
|
} else { |
||||
|
this.colorStyle = {}; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,95 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
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-font-settings-panel" [formGroup]="fontFormGroup"> |
||||
|
<div class="tb-font-settings-title" translate>widgets.widget-font.font-settings</div> |
||||
|
<div class="tb-form-row no-border no-padding"> |
||||
|
<div class="fixed-title-width" translate>widgets.widget-font.size</div> |
||||
|
<div fxFlex fxLayout="row" fxLayoutAlign="start center" fxLayoutGap="12px"> |
||||
|
<mat-form-field fxFlex appearance="outline" class="number" subscriptSizing="dynamic"> |
||||
|
<input matInput type="number" min="0" formControlName="size" placeholder="{{ 'widget-config.set' | translate }}"> |
||||
|
</mat-form-field> |
||||
|
<mat-form-field fxFlex appearance="outline" subscriptSizing="dynamic"> |
||||
|
<mat-select formControlName="sizeUnit"> |
||||
|
<mat-option *ngFor="let cssUnit of cssUnitsList" [value]="cssUnit">{{ cssUnit }}</mat-option> |
||||
|
</mat-select> |
||||
|
</mat-form-field> |
||||
|
</div> |
||||
|
</div> |
||||
|
<div class="tb-form-row no-border no-padding"> |
||||
|
<div class="fixed-title-width" translate>widgets.widget-font.font-family</div> |
||||
|
<mat-form-field fxFlex appearance="outline" subscriptSizing="dynamic"> |
||||
|
<input matInput #familyInput |
||||
|
formControlName="family" placeholder="{{ 'widget-config.set' | translate }}" |
||||
|
[matAutocomplete]="familyAutocomplete"> |
||||
|
<button *ngIf="fontFormGroup.get('family').value" |
||||
|
type="button" |
||||
|
matSuffix mat-icon-button aria-label="Clear" |
||||
|
(click)="clearFamily()"> |
||||
|
<mat-icon class="material-icons">close</mat-icon> |
||||
|
</button> |
||||
|
<mat-autocomplete |
||||
|
#familyAutocomplete="matAutocomplete" |
||||
|
class="tb-autocomplete" |
||||
|
panelWidth="fit-content"> |
||||
|
<mat-option *ngFor="let family of filteredFontFamilies | async" [value]="family"> |
||||
|
<span [innerHTML]="family | highlight:familySearchText:true:'ig'"></span> |
||||
|
</mat-option> |
||||
|
</mat-autocomplete> |
||||
|
</mat-form-field> |
||||
|
</div> |
||||
|
<div class="tb-form-row no-border no-padding"> |
||||
|
<div class="fixed-title-width" translate>widgets.widget-font.font-weight</div> |
||||
|
<mat-form-field fxFlex appearance="outline" subscriptSizing="dynamic"> |
||||
|
<mat-select formControlName="weight"> |
||||
|
<mat-option *ngFor="let weight of fontWeightsList" [value]="weight"> |
||||
|
{{ fontWeightTranslationsMap.has(weight) ? (fontWeightTranslationsMap.get(weight) | translate) : weight }} |
||||
|
</mat-option> |
||||
|
</mat-select> |
||||
|
</mat-form-field> |
||||
|
</div> |
||||
|
<div class="tb-form-row no-border no-padding"> |
||||
|
<div class="fixed-title-width" translate>widgets.widget-font.font-style</div> |
||||
|
<mat-form-field fxFlex appearance="outline" subscriptSizing="dynamic"> |
||||
|
<mat-select formControlName="style"> |
||||
|
<mat-option *ngFor="let style of fontStylesList" [value]="style"> |
||||
|
{{ fontStyleTranslationsMap.get(style) | translate }} |
||||
|
</mat-option> |
||||
|
</mat-select> |
||||
|
</mat-form-field> |
||||
|
</div> |
||||
|
<mat-divider></mat-divider> |
||||
|
<div class="tb-form-row no-border no-padding font-preview"> |
||||
|
<div class="fixed-title-width" translate>widgets.widget-font.preview</div> |
||||
|
<div class="preview-text" fxFlex [style]="previewStyle">{{ previewText }}</div> |
||||
|
</div> |
||||
|
<div class="tb-font-settings-panel-buttons"> |
||||
|
<button mat-button |
||||
|
color="primary" |
||||
|
type="button" |
||||
|
(click)="cancel()"> |
||||
|
{{ 'action.cancel' | translate }} |
||||
|
</button> |
||||
|
<button mat-raised-button |
||||
|
color="primary" |
||||
|
type="button" |
||||
|
(click)="applyFont()" |
||||
|
[disabled]="fontFormGroup.invalid || !fontFormGroup.dirty"> |
||||
|
{{ 'action.apply' | translate }} |
||||
|
</button> |
||||
|
</div> |
||||
|
</div> |
||||
@ -0,0 +1,51 @@ |
|||||
|
/** |
||||
|
* 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. |
||||
|
*/ |
||||
|
.tb-font-settings-panel { |
||||
|
width: 100%; |
||||
|
display: flex; |
||||
|
flex-direction: column; |
||||
|
gap: 16px; |
||||
|
.tb-font-settings-title { |
||||
|
font-size: 16px; |
||||
|
font-weight: 500; |
||||
|
line-height: 24px; |
||||
|
letter-spacing: 0.25px; |
||||
|
color: rgba(0, 0, 0, 0.87); |
||||
|
} |
||||
|
.tb-form-row { |
||||
|
.fixed-title-width { |
||||
|
min-width: 120px; |
||||
|
} |
||||
|
&.font-preview { |
||||
|
align-items: flex-start; |
||||
|
.preview-text { |
||||
|
max-height: 300px; |
||||
|
max-width: 400px; |
||||
|
overflow: hidden; |
||||
|
white-space: nowrap; |
||||
|
text-overflow: ellipsis; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
.tb-font-settings-panel-buttons { |
||||
|
height: 60px; |
||||
|
display: flex; |
||||
|
flex-direction: row; |
||||
|
gap: 16px; |
||||
|
justify-content: flex-end; |
||||
|
align-items: flex-end; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,136 @@ |
|||||
|
///
|
||||
|
/// 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 { |
||||
|
Component, |
||||
|
ElementRef, |
||||
|
EventEmitter, |
||||
|
Input, |
||||
|
OnInit, |
||||
|
Output, |
||||
|
ViewChild, |
||||
|
ViewEncapsulation |
||||
|
} from '@angular/core'; |
||||
|
import { PageComponent } from '@shared/components/page.component'; |
||||
|
import { |
||||
|
commonFonts, |
||||
|
ComponentStyle, |
||||
|
cssUnits, |
||||
|
Font, |
||||
|
fontStyles, |
||||
|
fontStyleTranslations, |
||||
|
fontWeights, |
||||
|
fontWeightTranslations, |
||||
|
textStyle |
||||
|
} from '@home/components/widget/config/widget-settings.models'; |
||||
|
import { TbPopoverComponent } from '@shared/components/popover.component'; |
||||
|
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; |
||||
|
import { Store } from '@ngrx/store'; |
||||
|
import { AppState } from '@core/core.state'; |
||||
|
import { Observable } from 'rxjs'; |
||||
|
import { map, startWith, tap } from 'rxjs/operators'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-font-settings-panel', |
||||
|
templateUrl: './font-settings-panel.component.html', |
||||
|
providers: [], |
||||
|
styleUrls: ['./font-settings-panel.component.scss'], |
||||
|
encapsulation: ViewEncapsulation.None |
||||
|
}) |
||||
|
export class FontSettingsPanelComponent extends PageComponent implements OnInit { |
||||
|
|
||||
|
@Input() |
||||
|
font: Font; |
||||
|
|
||||
|
@Input() |
||||
|
previewText = 'AaBbCcDd'; |
||||
|
|
||||
|
@Input() |
||||
|
popover: TbPopoverComponent<FontSettingsPanelComponent>; |
||||
|
|
||||
|
@Output() |
||||
|
fontApplied = new EventEmitter<Font>(); |
||||
|
|
||||
|
@ViewChild('familyInput', {static: true}) familyInput: ElementRef; |
||||
|
|
||||
|
cssUnitsList = cssUnits; |
||||
|
|
||||
|
fontWeightsList = fontWeights; |
||||
|
|
||||
|
fontWeightTranslationsMap = fontWeightTranslations; |
||||
|
|
||||
|
fontStylesList = fontStyles; |
||||
|
|
||||
|
fontStyleTranslationsMap = fontStyleTranslations; |
||||
|
|
||||
|
fontFormGroup: UntypedFormGroup; |
||||
|
|
||||
|
filteredFontFamilies: Observable<Array<string>>; |
||||
|
|
||||
|
familySearchText = ''; |
||||
|
|
||||
|
previewStyle: ComponentStyle = {}; |
||||
|
|
||||
|
constructor(private fb: UntypedFormBuilder, |
||||
|
protected store: Store<AppState>) { |
||||
|
super(store); |
||||
|
} |
||||
|
|
||||
|
ngOnInit(): void { |
||||
|
this.fontFormGroup = this.fb.group( |
||||
|
{ |
||||
|
size: [this.font?.size, [Validators.required, Validators.min(0)]], |
||||
|
sizeUnit: [this.font?.sizeUnit, [Validators.required]], |
||||
|
family: [this.font?.family, [Validators.required]], |
||||
|
weight: [this.font?.weight, [Validators.required]], |
||||
|
style: [this.font?.style, [Validators.required]] |
||||
|
} |
||||
|
); |
||||
|
if (this.font) { |
||||
|
this.previewStyle = textStyle(this.font, '1'); |
||||
|
} |
||||
|
this.fontFormGroup.valueChanges.subscribe((value: Font) => { |
||||
|
if (this.fontFormGroup.valid) { |
||||
|
this.previewStyle = textStyle(value, '1'); |
||||
|
setTimeout(() => {this.popover?.updatePosition();}, 0); |
||||
|
} |
||||
|
}); |
||||
|
this.filteredFontFamilies = this.fontFormGroup.get('family').valueChanges |
||||
|
.pipe( |
||||
|
startWith<string>(''), |
||||
|
tap((searchText) => { this.familySearchText = searchText || ''; }), |
||||
|
map(() => commonFonts.filter(f => f.toUpperCase().includes(this.familySearchText.toUpperCase()))) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
clearFamily() { |
||||
|
this.fontFormGroup.get('family').patchValue(null, {emitEvent: true}); |
||||
|
setTimeout(() => { |
||||
|
this.familyInput.nativeElement.blur(); |
||||
|
this.familyInput.nativeElement.focus(); |
||||
|
}, 0); |
||||
|
} |
||||
|
|
||||
|
cancel() { |
||||
|
this.popover?.hide(); |
||||
|
} |
||||
|
|
||||
|
applyFont() { |
||||
|
const font = this.fontFormGroup.value; |
||||
|
this.fontApplied.emit(font); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
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. |
||||
|
|
||||
|
--> |
||||
|
<button type="button" |
||||
|
mat-stroked-button |
||||
|
class="tb-box-button" |
||||
|
[disabled]="disabled" |
||||
|
#matButton |
||||
|
(click)="openFontSettingsPopup($event, matButton)"> |
||||
|
<tb-icon matButtonIcon>text_format</tb-icon> |
||||
|
</button> |
||||
@ -0,0 +1,102 @@ |
|||||
|
///
|
||||
|
/// 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 { Component, forwardRef, Input, OnInit, Renderer2, ViewContainerRef } from '@angular/core'; |
||||
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; |
||||
|
import { Font } from '@home/components/widget/config/widget-settings.models'; |
||||
|
import { MatButton } from '@angular/material/button'; |
||||
|
import { TbPopoverService } from '@shared/components/popover.service'; |
||||
|
import { FontSettingsPanelComponent } from '@home/components/widget/lib/settings/common/font-settings-panel.component'; |
||||
|
import { isDefinedAndNotNull } from '@core/utils'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-font-settings', |
||||
|
templateUrl: './font-settings.component.html', |
||||
|
styleUrls: [], |
||||
|
providers: [ |
||||
|
{ |
||||
|
provide: NG_VALUE_ACCESSOR, |
||||
|
useExisting: forwardRef(() => FontSettingsComponent), |
||||
|
multi: true |
||||
|
} |
||||
|
] |
||||
|
}) |
||||
|
export class FontSettingsComponent implements OnInit, ControlValueAccessor { |
||||
|
|
||||
|
@Input() |
||||
|
disabled: boolean; |
||||
|
|
||||
|
@Input() |
||||
|
previewText: string | (() => string); |
||||
|
|
||||
|
private modelValue: Font; |
||||
|
|
||||
|
private propagateChange = null; |
||||
|
|
||||
|
constructor(private popoverService: TbPopoverService, |
||||
|
private renderer: Renderer2, |
||||
|
private viewContainerRef: ViewContainerRef) {} |
||||
|
|
||||
|
ngOnInit(): void { |
||||
|
} |
||||
|
|
||||
|
registerOnChange(fn: any): void { |
||||
|
this.propagateChange = fn; |
||||
|
} |
||||
|
|
||||
|
registerOnTouched(fn: any): void { |
||||
|
} |
||||
|
|
||||
|
setDisabledState(isDisabled: boolean): void { |
||||
|
this.disabled = isDisabled; |
||||
|
} |
||||
|
|
||||
|
writeValue(value: Font): void { |
||||
|
this.modelValue = value; |
||||
|
} |
||||
|
|
||||
|
openFontSettingsPopup($event: Event, matButton: MatButton) { |
||||
|
if ($event) { |
||||
|
$event.stopPropagation(); |
||||
|
} |
||||
|
const trigger = matButton._elementRef.nativeElement; |
||||
|
if (this.popoverService.hasPopover(trigger)) { |
||||
|
this.popoverService.hidePopover(trigger); |
||||
|
} else { |
||||
|
const ctx: any = { |
||||
|
font: this.modelValue |
||||
|
}; |
||||
|
if (isDefinedAndNotNull(this.previewText)) { |
||||
|
const previewText = typeof this.previewText === 'string' ? this.previewText : this.previewText(); |
||||
|
if (previewText) { |
||||
|
ctx.previewText = previewText; |
||||
|
} |
||||
|
} |
||||
|
const fontSettingsPanelPopover = this.popoverService.displayPopover(trigger, this.renderer, |
||||
|
this.viewContainerRef, FontSettingsPanelComponent, 'left', true, null, |
||||
|
ctx, |
||||
|
{}, |
||||
|
{}, {}, true); |
||||
|
fontSettingsPanelPopover.tbComponentRef.instance.popover = fontSettingsPanelPopover; |
||||
|
fontSettingsPanelPopover.tbComponentRef.instance.fontApplied.subscribe((font) => { |
||||
|
fontSettingsPanelPopover.hide(); |
||||
|
this.modelValue = font; |
||||
|
this.propagateChange(this.modelValue); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,43 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
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-image-cards-select tb-form-panel stroked no-padding"> |
||||
|
<mat-expansion-panel class="tb-settings" [expanded]="expanded" disabled> |
||||
|
<mat-expansion-panel-header class="fill-width" fxLayout="row wrap"> |
||||
|
<div fxFlex class="tb-form-row no-border" [class.expanded]="expanded"> |
||||
|
<div class="fixed-title-width">{{ label }}</div> |
||||
|
<mat-form-field class="tb-image-cards-value-field" fxFlex appearance="outline" subscriptSizing="dynamic" (click)="toggleSelectPanel($event)"> |
||||
|
<input readonly matInput [formControl]="valueFormControl" placeholder="{{ 'widget-config.set' | translate }}"> |
||||
|
<mat-icon matSuffix>{{ expanded ? 'expand_less' : 'expand_more' }}</mat-icon> |
||||
|
</mat-form-field> |
||||
|
</div> |
||||
|
</mat-expansion-panel-header> |
||||
|
<ng-template matExpansionPanelContent> |
||||
|
<mat-grid-list class="tb-image-cards-options" [cols]="cols$ | async" [rowHeight]="rowHeight" gutterSize="8"> |
||||
|
<mat-grid-tile *ngFor="let option of options" (click)="updateModel(option.value)"> |
||||
|
<div class="tb-image-cards-option" [class.selected]="modelValue === option.value"> |
||||
|
<div class="tb-image-cards-option-background"></div> |
||||
|
<div class="tb-image-cards-option-title">{{ option.name }}</div> |
||||
|
<div class="tb-image-cards-option-image-container"> |
||||
|
<img class="tb-image-cards-option-image" src="{{ option.image }}"/> |
||||
|
</div> |
||||
|
</div> |
||||
|
</mat-grid-tile> |
||||
|
</mat-grid-list> |
||||
|
</ng-template> |
||||
|
</mat-expansion-panel> |
||||
|
</div> |
||||
@ -0,0 +1,116 @@ |
|||||
|
/** |
||||
|
* 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. |
||||
|
*/ |
||||
|
.tb-image-cards-select.tb-form-panel { |
||||
|
.tb-form-row { |
||||
|
transition: all .3s; |
||||
|
&.expanded { |
||||
|
padding: 11px 7px 11px 16px; |
||||
|
} |
||||
|
} |
||||
|
.tb-image-cards-value-field { |
||||
|
cursor: pointer; |
||||
|
user-select: none; |
||||
|
input { |
||||
|
cursor: pointer; |
||||
|
pointer-events: none; |
||||
|
} |
||||
|
} |
||||
|
.mat-expansion-panel { |
||||
|
&.tb-settings { |
||||
|
> .mat-expansion-panel-header { |
||||
|
height: auto; |
||||
|
.mat-content { |
||||
|
margin: 0; |
||||
|
} |
||||
|
.tb-form-row { |
||||
|
font-weight: normal; |
||||
|
font-size: 16px; |
||||
|
color: rgba(0, 0, 0, 0.87); |
||||
|
} |
||||
|
.mat-expansion-indicator { |
||||
|
display: none; |
||||
|
} |
||||
|
} |
||||
|
> .mat-expansion-panel-content { |
||||
|
> .mat-expansion-panel-body { |
||||
|
padding: 0 16px 16px !important; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
.tb-image-cards-option { |
||||
|
width: 100%; |
||||
|
height: 100%; |
||||
|
cursor: pointer; |
||||
|
padding: 8px 12px 12px 12px; |
||||
|
display: flex; |
||||
|
flex-direction: column; |
||||
|
gap: 8px; |
||||
|
align-items: start; |
||||
|
position: relative; |
||||
|
.tb-image-cards-option-background { |
||||
|
border-radius: 4px; |
||||
|
position: absolute; |
||||
|
top: 1px; |
||||
|
left: 1px; |
||||
|
right: 1px; |
||||
|
bottom: 1px; |
||||
|
background: rgba(0, 0, 0, 0.04); |
||||
|
} |
||||
|
&:before { |
||||
|
content: unset; |
||||
|
border-radius: 4px; |
||||
|
position: absolute; |
||||
|
top: 0; |
||||
|
left: 0; |
||||
|
right: 0; |
||||
|
bottom: 0; |
||||
|
} |
||||
|
.tb-image-cards-option-title { |
||||
|
z-index: 1; |
||||
|
font-size: 12px; |
||||
|
font-style: normal; |
||||
|
font-weight: 400; |
||||
|
line-height: 16px; |
||||
|
letter-spacing: 0.25px; |
||||
|
color: rgba(0, 0, 0, 0.54); |
||||
|
} |
||||
|
.tb-image-cards-option-image-container { |
||||
|
z-index: 1; |
||||
|
flex: 1; |
||||
|
width: 100%; |
||||
|
min-height: 0; |
||||
|
display: flex; |
||||
|
justify-content: center; |
||||
|
} |
||||
|
&.selected { |
||||
|
.tb-image-cards-option-background { |
||||
|
background: #305680; |
||||
|
opacity: 0.04; |
||||
|
} |
||||
|
&:before { |
||||
|
content: ""; |
||||
|
border: 1px solid #305680; |
||||
|
opacity: 0.32; |
||||
|
} |
||||
|
.tb-image-cards-option-title { |
||||
|
font-size: 13px; |
||||
|
font-weight: 500; |
||||
|
color: #305680; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,190 @@ |
|||||
|
///
|
||||
|
/// 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 { |
||||
|
AfterContentInit, |
||||
|
Component, |
||||
|
ContentChildren, |
||||
|
Directive, |
||||
|
ElementRef, |
||||
|
forwardRef, |
||||
|
Input, |
||||
|
OnDestroy, OnInit, |
||||
|
QueryList, |
||||
|
ViewEncapsulation |
||||
|
} from '@angular/core'; |
||||
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR, UntypedFormControl } from '@angular/forms'; |
||||
|
import { coerceBoolean } from '@shared/decorators/coercion'; |
||||
|
import { Observable, Subject } from 'rxjs'; |
||||
|
import { map, share, startWith, takeUntil } from 'rxjs/operators'; |
||||
|
import { BreakpointObserver } from '@angular/cdk/layout'; |
||||
|
import { MediaBreakpoints } from '@shared/models/constants'; |
||||
|
|
||||
|
export interface ImageCardsSelectOption { |
||||
|
name: string; |
||||
|
value: any; |
||||
|
image: string; |
||||
|
} |
||||
|
|
||||
|
@Directive( |
||||
|
{ |
||||
|
// eslint-disable-next-line @angular-eslint/directive-selector
|
||||
|
selector: 'tb-image-cards-select-option', |
||||
|
} |
||||
|
) |
||||
|
export class ImageCardsSelectOptionDirective { |
||||
|
|
||||
|
@Input() value: any; |
||||
|
|
||||
|
@Input() image: string; |
||||
|
|
||||
|
get viewValue(): string { |
||||
|
return (this._element?.nativeElement.textContent || '').trim(); |
||||
|
} |
||||
|
|
||||
|
constructor( |
||||
|
private _element: ElementRef<HTMLElement> |
||||
|
) {} |
||||
|
} |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-image-cards-select', |
||||
|
templateUrl: './image-cards-select.component.html', |
||||
|
styleUrls: ['./image-cards-select.component.scss'], |
||||
|
providers: [ |
||||
|
{ |
||||
|
provide: NG_VALUE_ACCESSOR, |
||||
|
useExisting: forwardRef(() => ImageCardsSelectComponent), |
||||
|
multi: true |
||||
|
} |
||||
|
], |
||||
|
encapsulation: ViewEncapsulation.None |
||||
|
}) |
||||
|
export class ImageCardsSelectComponent implements ControlValueAccessor, OnInit, AfterContentInit, OnDestroy { |
||||
|
|
||||
|
@ContentChildren(ImageCardsSelectOptionDirective) imageCardsSelectOptions: QueryList<ImageCardsSelectOptionDirective>; |
||||
|
|
||||
|
@Input() |
||||
|
@coerceBoolean() |
||||
|
disabled: boolean; |
||||
|
|
||||
|
@Input() |
||||
|
cols = 4; |
||||
|
|
||||
|
@Input() |
||||
|
colsLtMd = 2; |
||||
|
|
||||
|
@Input() |
||||
|
rowHeight = '9:5'; |
||||
|
|
||||
|
@Input() |
||||
|
label: string; |
||||
|
|
||||
|
valueFormControl: UntypedFormControl; |
||||
|
|
||||
|
options: ImageCardsSelectOption[] = []; |
||||
|
|
||||
|
modelValue: any; |
||||
|
|
||||
|
expanded = false; |
||||
|
|
||||
|
cols$: Observable<number>; |
||||
|
|
||||
|
private propagateChange = null; |
||||
|
|
||||
|
private _destroyed = new Subject<void>(); |
||||
|
|
||||
|
constructor(private breakpointObserver: BreakpointObserver) { |
||||
|
this.valueFormControl = new UntypedFormControl(''); |
||||
|
} |
||||
|
|
||||
|
ngOnInit(): void { |
||||
|
const gridColumns = this.breakpointObserver.isMatched(MediaBreakpoints['lt-md']) ? this.colsLtMd : this.cols; |
||||
|
this.cols$ = this.breakpointObserver |
||||
|
.observe(MediaBreakpoints['lt-md']).pipe( |
||||
|
map((state) => state.matches ? this.colsLtMd : this.cols), |
||||
|
startWith(gridColumns), |
||||
|
share() |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
ngAfterContentInit(): void { |
||||
|
this.imageCardsSelectOptions.changes.pipe(startWith(null), takeUntil(this._destroyed)).subscribe(() => { |
||||
|
this.syncImageCardsSelectOptions(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
ngOnDestroy() { |
||||
|
this._destroyed.next(); |
||||
|
this._destroyed.complete(); |
||||
|
} |
||||
|
|
||||
|
private syncImageCardsSelectOptions() { |
||||
|
if (this.imageCardsSelectOptions?.length) { |
||||
|
this.options.length = 0; |
||||
|
this.imageCardsSelectOptions.forEach(option => { |
||||
|
this.options.push( |
||||
|
{ name: option.viewValue, |
||||
|
value: option.value, |
||||
|
image: option.image |
||||
|
} |
||||
|
); |
||||
|
}); |
||||
|
this.updateDisplayValue(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
registerOnChange(fn: any): void { |
||||
|
this.propagateChange = fn; |
||||
|
} |
||||
|
|
||||
|
registerOnTouched(fn: any): void { |
||||
|
} |
||||
|
|
||||
|
setDisabledState(isDisabled: boolean): void { |
||||
|
this.disabled = isDisabled; |
||||
|
if (this.disabled) { |
||||
|
this.valueFormControl.disable(); |
||||
|
} else { |
||||
|
this.valueFormControl.enable(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
writeValue(value: any): void { |
||||
|
this.modelValue = value; |
||||
|
this.updateDisplayValue(); |
||||
|
} |
||||
|
|
||||
|
updateModel(value: any) { |
||||
|
this.modelValue = value; |
||||
|
this.updateDisplayValue(); |
||||
|
this.propagateChange(this.modelValue); |
||||
|
this.expanded = false; |
||||
|
} |
||||
|
|
||||
|
toggleSelectPanel($event: Event) { |
||||
|
$event.stopPropagation(); |
||||
|
if (!this.disabled) { |
||||
|
this.expanded = !this.expanded; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private updateDisplayValue() { |
||||
|
const currentOption = this.options.find(o => o.value === this.modelValue); |
||||
|
const displayValue = currentOption ? currentOption.name : ''; |
||||
|
this.valueFormControl.patchValue(displayValue, {emitEvent: false}); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,281 @@ |
|||||
|
///
|
||||
|
/// 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 { CanColor, mixinColor } from '@angular/material/core'; |
||||
|
import { |
||||
|
AfterContentInit, |
||||
|
AfterViewChecked, |
||||
|
ChangeDetectionStrategy, |
||||
|
Component, |
||||
|
ElementRef, |
||||
|
ErrorHandler, |
||||
|
Inject, |
||||
|
OnDestroy, |
||||
|
Renderer2, |
||||
|
ViewChild, |
||||
|
ViewEncapsulation |
||||
|
} from '@angular/core'; |
||||
|
import { MAT_ICON_LOCATION, MatIconLocation, MatIconRegistry } from '@angular/material/icon'; |
||||
|
import { Subscription } from 'rxjs'; |
||||
|
import { take } from 'rxjs/operators'; |
||||
|
import { isSvgIcon, splitIconName } from '@shared/models/icon.models'; |
||||
|
import { ContentObserver } from '@angular/cdk/observers'; |
||||
|
|
||||
|
const _TbIconBase = mixinColor( |
||||
|
class { |
||||
|
constructor(public _elementRef: ElementRef) {} |
||||
|
}, |
||||
|
); |
||||
|
|
||||
|
const funcIriAttributes = [ |
||||
|
'clip-path', |
||||
|
'color-profile', |
||||
|
'src', |
||||
|
'cursor', |
||||
|
'fill', |
||||
|
'filter', |
||||
|
'marker', |
||||
|
'marker-start', |
||||
|
'marker-mid', |
||||
|
'marker-end', |
||||
|
'mask', |
||||
|
'stroke', |
||||
|
]; |
||||
|
|
||||
|
const funcIriAttributeSelector = funcIriAttributes.map(attr => `[${attr}]`).join(', '); |
||||
|
|
||||
|
const funcIriPattern = /^url\(['"]?#(.*?)['"]?\)$/; |
||||
|
|
||||
|
@Component({ |
||||
|
template: '<span style="display: none;" #iconNameContent><ng-content></ng-content></span>', |
||||
|
selector: 'tb-icon', |
||||
|
exportAs: 'tbIcon', |
||||
|
styleUrls: [], |
||||
|
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
|
||||
|
inputs: ['color'], |
||||
|
// eslint-disable-next-line @angular-eslint/no-host-metadata-property
|
||||
|
host: { |
||||
|
role: 'img', |
||||
|
class: 'mat-icon notranslate', |
||||
|
'[attr.data-mat-icon-type]': '!_useSvgIcon ? "font" : "svg"', |
||||
|
'[attr.data-mat-icon-name]': '_svgName', |
||||
|
'[attr.data-mat-icon-namespace]': '_svgNamespace', |
||||
|
'[class.mat-icon-no-color]': 'color !== "primary" && color !== "accent" && color !== "warn"', |
||||
|
}, |
||||
|
encapsulation: ViewEncapsulation.None, |
||||
|
changeDetection: ChangeDetectionStrategy.OnPush, |
||||
|
}) |
||||
|
export class TbIconComponent extends _TbIconBase |
||||
|
implements AfterContentInit, AfterViewChecked, CanColor, OnDestroy { |
||||
|
|
||||
|
@ViewChild('iconNameContent', {static: true}) |
||||
|
_iconNameContent: ElementRef; |
||||
|
|
||||
|
private icon: string; |
||||
|
|
||||
|
get viewValue(): string { |
||||
|
return (this._iconNameContent?.nativeElement.textContent || '').trim(); |
||||
|
} |
||||
|
|
||||
|
private _contentChanges: Subscription = null; |
||||
|
private _previousFontSetClass: string[] = []; |
||||
|
|
||||
|
_useSvgIcon = false; |
||||
|
_svgName: string | null; |
||||
|
_svgNamespace: string | null; |
||||
|
|
||||
|
private _textElement = null; |
||||
|
|
||||
|
private _previousPath?: string; |
||||
|
|
||||
|
private _elementsWithExternalReferences?: Map<Element, {name: string; value: string}[]>; |
||||
|
|
||||
|
private _currentIconFetch = Subscription.EMPTY; |
||||
|
|
||||
|
constructor(elementRef: ElementRef<HTMLElement>, |
||||
|
private contentObserver: ContentObserver, |
||||
|
private renderer: Renderer2, |
||||
|
private _iconRegistry: MatIconRegistry, |
||||
|
@Inject(MAT_ICON_LOCATION) private _location: MatIconLocation, |
||||
|
private readonly _errorHandler: ErrorHandler) { |
||||
|
super(elementRef); |
||||
|
} |
||||
|
|
||||
|
ngAfterContentInit(): void { |
||||
|
this.icon = this.viewValue; |
||||
|
this._updateIcon(); |
||||
|
this._contentChanges = this.contentObserver.observe(this._iconNameContent.nativeElement) |
||||
|
.subscribe(() => { |
||||
|
const content = this.viewValue; |
||||
|
if (content && this.icon !== content) { |
||||
|
this.icon = content; |
||||
|
this._updateIcon(); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
ngAfterViewChecked() { |
||||
|
const cachedElements = this._elementsWithExternalReferences; |
||||
|
if (cachedElements && cachedElements.size) { |
||||
|
const newPath = this._location.getPathname(); |
||||
|
if (newPath !== this._previousPath) { |
||||
|
this._previousPath = newPath; |
||||
|
this._prependPathToReferences(newPath); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
ngOnDestroy() { |
||||
|
this._contentChanges.unsubscribe(); |
||||
|
this._currentIconFetch.unsubscribe(); |
||||
|
if (this._elementsWithExternalReferences) { |
||||
|
this._elementsWithExternalReferences.clear(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private _updateIcon() { |
||||
|
const useSvgIcon = isSvgIcon(this.icon); |
||||
|
if (this._useSvgIcon !== useSvgIcon) { |
||||
|
this._useSvgIcon = useSvgIcon; |
||||
|
if (!this._useSvgIcon) { |
||||
|
this._updateSvgIcon(undefined); |
||||
|
} else { |
||||
|
this._updateFontIcon(undefined); |
||||
|
} |
||||
|
} |
||||
|
if (this._useSvgIcon) { |
||||
|
this._updateSvgIcon(this.icon); |
||||
|
} else { |
||||
|
this._updateFontIcon(this.icon); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private _updateFontIcon(rawName: string | undefined) { |
||||
|
if (rawName) { |
||||
|
this._clearFontIcon(); |
||||
|
const iconName = splitIconName(rawName)[1]; |
||||
|
this._textElement = this.renderer.createText(iconName); |
||||
|
const elem: HTMLElement = this._elementRef.nativeElement; |
||||
|
this.renderer.insertBefore(elem, this._textElement, this._iconNameContent.nativeElement); |
||||
|
const fontSetClasses = ( |
||||
|
this._iconRegistry.getDefaultFontSetClass() |
||||
|
).filter(className => className.length > 0); |
||||
|
fontSetClasses.forEach(className => elem.classList.add(className)); |
||||
|
this._previousFontSetClass = fontSetClasses; |
||||
|
} else { |
||||
|
this._clearFontIcon(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private _clearFontIcon() { |
||||
|
const elem: HTMLElement = this._elementRef.nativeElement; |
||||
|
if (this._textElement !== null) { |
||||
|
this.renderer.removeChild(elem, this._textElement); |
||||
|
this._textElement = null; |
||||
|
} |
||||
|
this._previousFontSetClass.forEach(className => elem.classList.remove(className)); |
||||
|
this._previousFontSetClass = []; |
||||
|
} |
||||
|
|
||||
|
private _updateSvgIcon(rawName: string | undefined) { |
||||
|
this._svgNamespace = null; |
||||
|
this._svgName = null; |
||||
|
this._currentIconFetch.unsubscribe(); |
||||
|
|
||||
|
if (rawName) { |
||||
|
const [namespace, iconName] = splitIconName(rawName); |
||||
|
if (namespace) { |
||||
|
this._svgNamespace = namespace; |
||||
|
} |
||||
|
if (iconName) { |
||||
|
this._svgName = iconName; |
||||
|
} |
||||
|
this._iconRegistry.getDefaultFontSetClass(); |
||||
|
this._currentIconFetch = this._iconRegistry |
||||
|
.getNamedSvgIcon(iconName, namespace) |
||||
|
.pipe(take(1)) |
||||
|
.subscribe({ |
||||
|
next: (svg) => this._setSvgElement(svg), |
||||
|
error: (err: Error) => { |
||||
|
const errorMessage = `Error retrieving icon ${namespace}:${iconName}! ${err.message}`; |
||||
|
this._errorHandler.handleError(new Error(errorMessage)); |
||||
|
} |
||||
|
}); |
||||
|
} else { |
||||
|
this._clearSvgElement(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private _setSvgElement(svg: SVGElement) { |
||||
|
this._clearSvgElement(); |
||||
|
const path = this._location.getPathname(); |
||||
|
this._previousPath = path; |
||||
|
this._cacheChildrenWithExternalReferences(svg); |
||||
|
this._prependPathToReferences(path); |
||||
|
this.renderer.insertBefore(this._elementRef.nativeElement, svg, this._iconNameContent.nativeElement); |
||||
|
} |
||||
|
|
||||
|
private _clearSvgElement() { |
||||
|
const layoutElement: HTMLElement = this._elementRef.nativeElement; |
||||
|
let childCount = layoutElement.childNodes.length; |
||||
|
if (this._elementsWithExternalReferences) { |
||||
|
this._elementsWithExternalReferences.clear(); |
||||
|
} |
||||
|
while (childCount--) { |
||||
|
const child = layoutElement.childNodes[childCount]; |
||||
|
if (child.nodeType !== 1 || child.nodeName.toLowerCase() === 'svg') { |
||||
|
child.remove(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private _cacheChildrenWithExternalReferences(element: SVGElement) { |
||||
|
const elementsWithFuncIri = element.querySelectorAll(funcIriAttributeSelector); |
||||
|
const elements = (this._elementsWithExternalReferences = this._elementsWithExternalReferences || new Map()); |
||||
|
elementsWithFuncIri.forEach( |
||||
|
(elementWithFuncIri) => { |
||||
|
funcIriAttributes.forEach(attr => { |
||||
|
const elementWithReference = elementWithFuncIri; |
||||
|
const value = elementWithReference.getAttribute(attr); |
||||
|
const match = value ? value.match(funcIriPattern) : null; |
||||
|
|
||||
|
if (match) { |
||||
|
let attributes = elements.get(elementWithReference); |
||||
|
|
||||
|
if (!attributes) { |
||||
|
attributes = []; |
||||
|
elements.set(elementWithReference, attributes); |
||||
|
} |
||||
|
|
||||
|
attributes.push({name: attr, value: match[1]}); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
private _prependPathToReferences(path: string) { |
||||
|
const elements = this._elementsWithExternalReferences; |
||||
|
if (elements) { |
||||
|
elements.forEach((attrs, element) => { |
||||
|
attrs.forEach(attr => { |
||||
|
element.setAttribute(attr.name, `url('${path}#${attr.value}')`); |
||||
|
}); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,40 @@ |
|||||
|
#### Color function |
||||
|
|
||||
|
<div class="divider"></div> |
||||
|
<br/> |
||||
|
|
||||
|
*function (value): string* |
||||
|
|
||||
|
A JavaScript function used to compute a color. |
||||
|
|
||||
|
**Parameters:** |
||||
|
|
||||
|
<ul> |
||||
|
<li><b>value:</b> <code>primitive (number/string/boolean)</code> - A value of the current datapoint. |
||||
|
</li> |
||||
|
</ul> |
||||
|
|
||||
|
**Returns:** |
||||
|
|
||||
|
Should return string value presenting color. |
||||
|
|
||||
|
In case no data is returned, color value from **Color** settings field will be used. |
||||
|
|
||||
|
<div class="divider"></div> |
||||
|
|
||||
|
##### Examples |
||||
|
|
||||
|
* Calculate color depending on `temperature` telemetry value: |
||||
|
|
||||
|
```javascript |
||||
|
var temperature = value; |
||||
|
if (typeof temperature !== undefined) { |
||||
|
var percent = (temperature + 60)/120 * 100; |
||||
|
return tinycolor.mix('blue', 'red', percent).toHexString(); |
||||
|
} |
||||
|
return 'blue'; |
||||
|
{:copy-code} |
||||
|
``` |
||||
|
|
||||
|
<br> |
||||
|
<br> |
||||
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 37 KiB |
|
After Width: | Height: | Size: 37 KiB |
|
After Width: | Height: | Size: 37 KiB |
|
After Width: | Height: | Size: 17 KiB |
|
After Width: | Height: | Size: 31 KiB |
|
After Width: | Height: | Size: 18 KiB |
Loading…
Reference in new issue