30 changed files with 1399 additions and 23 deletions
@ -0,0 +1,23 @@ |
|||
{ |
|||
"fqn": "battery_level", |
|||
"name": "Battery level", |
|||
"deprecated": false, |
|||
"image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAACgCAMAAAB+IdObAAAAilBMVEXg4ODf39/g4OAAAADg4ODf39////9c35Dg4ODL9dz5/vzX9+QhISHl+u3f+enB89XC89XHx8c9PT2QkJCu8MiF56x0dHRw453j4+Px8fFYWFisrKz0/fcvLy/S9uDq+/GCgoK6uro8PDzV1dVKSkpw457O9t6enp7i+etmZmarq6tm4ZeZ67p65aQon2vGAAAABnRSTlPvIL8Ar7DvmsykAAAEcklEQVR42u3dbVebMBjG8Tq3Sym3zARlSSA8ax+2ff+vtyCds666oOsSuvxf5CCnL/o7gUIRThfnZx8Wl3Pv49n54mwhMfukYXw8AYeRmM0KJ9HiVCCXAeJZAeJbAeJbAeJbAeJbAeJb/wOkzky5wDx6BcJJKU1cPqHxDmh4Aw97DcLNkFEO06jJiAH5uAaPQD++Yf4JsqKVGTRRJqA06bo2gwJYb1ZJMF5z6ngNs5TCQXYQIRjXAoz6JqUNGkUpYxllDQS1eU01GJHKoLVETQwOsoLQUAp0uRhYTzetjMwqpQ0kA5Ca9byFg+xnZEMNwLKe0x6kJ6WUJsEGqOHUwogcZL+PCNogp75h+zPCKRuSIwRKr9xsWfYQZiAZdc83rc2wChI7SEq6hYMsITpNa05bpFQ3NRlITTVDQ4oZAG8apX5CJDnasux3drUF5IZI9SQhWsog28G0bYn6boSYFHVwl/25lhQYE3I3mFHgV6rFm/LspLGraYuJeQlRpJyfp/wViOjgvP/h+8i8ChDfChDfChDfChDfChDfChDfChDfejekiKOnxUvY5RtkGT3r3lLiGySJnreGVZ5BzIR4MiXvgxRl9HtlAYs8gBTruBy7jw53X47Fycsm55BlYt5obJeBJhYbmhPIsoziwn7ykqi0kLiAlNEVplRZSFxArgZH/O3WsmsjsZA7gJQlcHNh341hH/48dgspogrVxZTM66MKB3ILqaIC8STINZaHj/VuIVdvgiQ4kGvIMkCeFSABEiABEiABEiAm95BqEiT2F4JvExy38BiCm+9fLLspvIZMKUD+BSS5sa3yGnI95eKDx5Di7mJChb+QkzkgBsh+ARIgARIgM4KINMdQrnifwiRTzlU+M4hcKaL0wUE6aykzSxnpTUv5vCAp6X6EcBKQLTEwamEGjbEmFXOAMIb0AdKRMmNNq+HvnWuo7jMuwbyHmMY3zh4gOdW7508VMQz1DJyJVhwZkkyCxBaQ7BlEpVJ3fIsjQ3A7wXGHN0C6Vqd5hqNDim/WjttqKmSX6MUsvrM/7uy75ZpW+5AsTTXfzgUiSZtxQ83DrACaJMbyVpJo+LEhxWfLoupVCBSlXUOthNSUi9podnEhNNixIcnXC9u+Xr8KES0R6cYsbbVZevzAlQxos74+MuTO2mEkxUGIYAIPNatcjmuaFdt/SdPM4IA4dBJnv6YACZAACZAACZAAMQVIgLzv4kPkL2TaxQeP/xmK6vuEiw8+Qwwltqvy/IaBk7nzAUlsmd+Q6s5+X098hky6+HAydz54fBw5mSN7gOwXIAESIAESIAEyZ8iJPBlaRdXkiw+FxXPgDp6eXk87jb8rkJhZPJBTyPhw+tpe8nWNZVniUI4hRRQvDeezXcNcJBaPszuA4GqQ2LeMLZ5mdwIxkigpiqVV1fr+xY8s5xBUZWRfWeGF3EOAorqyqyrwYj5AvCxAfCtAfCtAfCtAfCtAfOtyscBJdLn44PyHKv5G8tPp/Ij2+dnHy7m3+HB2/gO3i0/vBj05fgAAAABJRU5ErkJggg==", |
|||
"description": "Displays current battery level of device.", |
|||
"descriptor": { |
|||
"type": "latest", |
|||
"sizeX": 2.5, |
|||
"sizeY": 2.5, |
|||
"resources": [], |
|||
"templateHtml": "<tb-battery-level-widget \n [ctx]=\"ctx\"\n [widgetTitlePanel]=\"widgetTitlePanel\">\n</tb-battery-level-widget>", |
|||
"templateCss": "", |
|||
"controllerScript": "self.onInit = function() {\n self.ctx.$scope.batteryLevelWidget.onInit();\n};\n\nself.onDataUpdated = function() {\n self.ctx.$scope.batteryLevelWidget.onDataUpdated();\n};\n\nself.typeParameters = function() {\n return {\n maxDatasources: 1,\n maxDataKeys: 1,\n singleEntity: true,\n previewWidth: '200px',\n previewHeight: '200px',\n embedTitlePanel: true\n };\n};\n\nself.actionSources = function() {\n return {\n 'cardClick': {\n name: 'widget-action.card-click',\n multiple: false\n }\n };\n}\n\nself.onDestroy = function() {\n};\n", |
|||
"settingsSchema": "", |
|||
"dataKeySettingsSchema": "", |
|||
"settingsDirective": "tb-battery-level-widget-settings", |
|||
"hasBasicMode": true, |
|||
"basicModeDirective": "tb-battery-level-basic-config", |
|||
"defaultConfig": "{\"datasources\":[{\"type\":\"function\",\"name\":\"function\",\"dataKeys\":[{\"name\":\"f(x)\",\"type\":\"function\",\"label\":\"batteryLevel\",\"color\":\"#2196f3\",\"settings\":{},\"_hash\":0.2392660816082064,\"funcBody\":\"var value = prevValue + Math.random() * 7;\\nif (value < 0) {\\n\\tvalue = 0;\\n} else if (value > 100) {\\n\\tvalue = 0;\\n}\\nreturn value;\",\"aggregationType\":null,\"units\":null,\"decimals\":null,\"usePostProcessing\":null,\"postFuncBody\":null}],\"alarmFilterConfig\":{\"statusList\":[\"ACTIVE\"]}}],\"timewindow\":{\"realtime\":{\"timewindowMs\":60000}},\"showTitle\":true,\"backgroundColor\":\"rgba(0, 0, 0, 0)\",\"color\":\"rgba(0, 0, 0, 0.87)\",\"padding\":\"0px\",\"settings\":{\"background\":{\"type\":\"color\",\"color\":\"#fff\",\"overlay\":{\"enabled\":false,\"color\":\"rgba(255,255,255,0.72)\",\"blur\":3}},\"layout\":\"vertical_solid\",\"showValue\":true,\"autoScaleValueSize\":true,\"valueFont\":{\"family\":\"Roboto\",\"size\":20,\"sizeUnit\":\"px\",\"style\":\"normal\",\"weight\":\"500\",\"lineHeight\":\"24px\"},\"valueColor\":{\"type\":\"constant\",\"color\":\"rgba(0, 0, 0, 0.87)\",\"colorFunction\":\"var temperature = value;\\nif (typeof temperature !== undefined) {\\n var percent = (temperature + 60)/120 * 100;\\n return tinycolor.mix('blue', 'red', percent).toHexString();\\n}\\nreturn 'blue';\"},\"batteryLevelColor\":{\"color\":\"rgba(92, 223, 144, 1)\",\"type\":\"range\",\"rangeList\":[{\"from\":0,\"to\":25,\"color\":\"rgba(227, 71, 71, 1)\"},{\"from\":25,\"to\":50,\"color\":\"rgba(246, 206, 67, 1)\"},{\"from\":50,\"to\":100,\"color\":\"rgba(92, 223, 144, 1)\"}],\"colorFunction\":\"var temperature = value;\\nif (typeof temperature !== undefined) {\\n var percent = (temperature + 60)/120 * 100;\\n return tinycolor.mix('blue', 'red', percent).toHexString();\\n}\\nreturn 'blue';\"},\"batteryShapeColor\":{\"color\":\"rgba(92, 223, 144, 0.32)\",\"type\":\"range\",\"rangeList\":[{\"from\":0,\"to\":25,\"color\":\"rgba(227, 71, 71, 0.32)\"},{\"from\":25,\"to\":50,\"color\":\"rgba(246, 206, 67, 0.32)\"},{\"from\":50,\"to\":100,\"color\":\"rgba(92, 223, 144, 0.32)\"}],\"colorFunction\":\"var temperature = value;\\nif (typeof temperature !== undefined) {\\n var percent = (temperature + 60)/120 * 100;\\n return tinycolor.mix('blue', 'red', percent).toHexString();\\n}\\nreturn 'blue';\"}},\"title\":\"Battery level\",\"dropShadow\":true,\"enableFullscreen\":false,\"titleStyle\":{\"fontSize\":\"16px\",\"fontWeight\":400},\"units\":\"%\",\"decimals\":0,\"useDashboardTimewindow\":true,\"showLegend\":false,\"widgetStyle\":{},\"actions\":{},\"configMode\":\"basic\",\"displayTimewindow\":true,\"margin\":\"0px\",\"borderRadius\":\"0px\",\"widgetCss\":\"\",\"pageSize\":1024,\"noDataDisplayMessage\":\"\",\"showTitleIcon\":false,\"titleTooltip\":\"\",\"titleFont\":{\"size\":16,\"sizeUnit\":\"px\",\"family\":\"Roboto\",\"weight\":\"500\",\"style\":\"normal\",\"lineHeight\":\"24px\"},\"titleIcon\":\"\",\"iconColor\":\"rgba(0, 0, 0, 0.87)\",\"iconSize\":\"14px\",\"timewindowStyle\":{\"showIcon\":true,\"iconSize\":\"14px\",\"icon\":\"query_builder\",\"iconPosition\":\"left\",\"font\":{\"size\":12,\"sizeUnit\":\"px\",\"family\":null,\"weight\":null,\"style\":null,\"lineHeight\":\"1\"},\"color\":null},\"titleColor\":\"rgba(0, 0, 0, 0.87)\"}" |
|||
}, |
|||
"externalId": null |
|||
} |
|||
@ -0,0 +1,129 @@ |
|||
<!-- |
|||
|
|||
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]="batteryLevelWidgetConfigForm"> |
|||
<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="1:1" |
|||
cols="4" |
|||
colsLtMd="2" |
|||
label="{{ 'widgets.battery-level.layout' | translate }}" formControlName="layout"> |
|||
<tb-image-cards-select-option *ngFor="let layout of batteryLevelLayouts" |
|||
[value]="layout" |
|||
[image]="batteryLevelLayoutImageMap.get(layout)"> |
|||
{{ batteryLevelLayoutTranslationMap.get(layout) | translate }} |
|||
</tb-image-cards-select-option> |
|||
</tb-image-cards-select> |
|||
<div class="tb-form-row column-xs"> |
|||
<mat-slide-toggle class="mat-slide fixed-title-width" formControlName="showTitle"> |
|||
{{ 'widget-config.title' | translate }} |
|||
</mat-slide-toggle> |
|||
<div fxFlex fxLayout="row" fxLayoutAlign="start center" fxLayoutGap="8px"> |
|||
<mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic"> |
|||
<input matInput formControlName="title" placeholder="{{ 'widget-config.set' | translate }}"> |
|||
</mat-form-field> |
|||
<tb-font-settings formControlName="titleFont" |
|||
clearButton |
|||
[previewText]="batteryLevelWidgetConfigForm.get('title').value" |
|||
[initialPreviewStyle]="widgetConfig.config.titleStyle"> |
|||
</tb-font-settings> |
|||
<tb-color-input asBoxInput |
|||
colorClearButton |
|||
formControlName="titleColor"> |
|||
</tb-color-input> |
|||
</div> |
|||
</div> |
|||
<div class="tb-form-row column-xs"> |
|||
<mat-slide-toggle class="mat-slide fixed-title-width" formControlName="showIcon"> |
|||
{{ 'widgets.battery-level.icon' | translate }} |
|||
</mat-slide-toggle> |
|||
<div fxFlex fxLayout="row" fxLayoutAlign="start center" fxLayoutGap="8px"> |
|||
<mat-form-field appearance="outline" class="flex number" subscriptSizing="dynamic"> |
|||
<input matInput type="number" min="0" formControlName="iconSize" placeholder="{{ 'widget-config.set' | translate }}"> |
|||
</mat-form-field> |
|||
<tb-css-unit-select fxFlex formControlName="iconSizeUnit"></tb-css-unit-select> |
|||
<tb-material-icon-select asBoxInput |
|||
iconClearButton |
|||
[color]="batteryLevelWidgetConfigForm.get('iconColor').value" |
|||
formControlName="icon"> |
|||
</tb-material-icon-select> |
|||
<tb-color-input asBoxInput |
|||
colorClearButton |
|||
formControlName="iconColor"> |
|||
</tb-color-input> |
|||
</div> |
|||
</div> |
|||
<div class="tb-form-row space-between column-xs"> |
|||
<mat-slide-toggle class="mat-slide fixed-title-width" formControlName="showValue"> |
|||
{{ 'widgets.battery-level.value' | translate }} |
|||
</mat-slide-toggle> |
|||
<div fxLayout="row" fxLayoutAlign="start center" fxLayoutGap="8px"> |
|||
<mat-chip-listbox formControlName="autoScaleValueSize"> |
|||
<mat-chip-option [value]="true">{{ 'widgets.battery-level.auto-scale' | translate }}</mat-chip-option> |
|||
</mat-chip-listbox> |
|||
<tb-font-settings formControlName="valueFont" |
|||
[autoScale]="batteryLevelWidgetConfigForm.get('autoScaleValueSize').value" |
|||
[previewText]="valuePreviewFn"> |
|||
</tb-font-settings> |
|||
<tb-color-settings formControlName="valueColor"> |
|||
</tb-color-settings> |
|||
</div> |
|||
</div> |
|||
<div class="tb-form-row space-between"> |
|||
<div>{{ 'widgets.battery-level.battery-level-color' | translate }}</div> |
|||
<tb-color-settings formControlName="batteryLevelColor"> |
|||
</tb-color-settings> |
|||
</div> |
|||
<div class="tb-form-row space-between"> |
|||
<div>{{ 'widgets.battery-level.battery-shape-color' | translate }}</div> |
|||
<tb-color-settings formControlName="batteryShapeColor"> |
|||
</tb-color-settings> |
|||
</div> |
|||
<div class="tb-form-row space-between"> |
|||
<div>{{ 'widgets.background.background' | translate }}</div> |
|||
<tb-background-settings formControlName="background"> |
|||
</tb-background-settings> |
|||
</div> |
|||
<div class="tb-form-row space-between column-lt-md"> |
|||
<div translate>widget-config.show-card-buttons</div> |
|||
<mat-chip-listbox multiple formControlName="cardButtons"> |
|||
<mat-chip-option value="fullscreen">{{ 'fullscreen.fullscreen' | translate }}</mat-chip-option> |
|||
</mat-chip-listbox> |
|||
</div> |
|||
<div class="tb-form-row space-between"> |
|||
<div>{{ 'widget-config.card-border-radius' | translate }}</div> |
|||
<mat-form-field appearance="outline" subscriptSizing="dynamic"> |
|||
<input matInput formControlName="borderRadius" placeholder="{{ 'widget-config.set' | translate }}"> |
|||
</mat-form-field> |
|||
</div> |
|||
</div> |
|||
<tb-widget-actions-panel |
|||
formControlName="actions"> |
|||
</tb-widget-actions-panel> |
|||
</ng-container> |
|||
@ -0,0 +1,221 @@ |
|||
///
|
|||
/// 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, Injector } 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, |
|||
setTimewindowConfig |
|||
} from '@home/components/widget/config/timewindow-config-panel.component'; |
|||
import { formatValue, isUndefined } from '@core/utils'; |
|||
import { cssSizeToStrSize, resolveCssSize } from '@shared/models/widget-settings.models'; |
|||
import { |
|||
batteryLevelDefaultSettings, |
|||
batteryLevelLayoutImages, |
|||
batteryLevelLayouts, |
|||
batteryLevelLayoutTranslations, |
|||
BatteryLevelWidgetSettings |
|||
} from '@home/components/widget/lib/indicator/battery-level-widget.models'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-battery-level-basic-config', |
|||
templateUrl: './battery-level-basic-config.component.html', |
|||
styleUrls: ['../basic-config.scss'] |
|||
}) |
|||
export class BatteryLevelBasicConfigComponent extends BasicWidgetConfigComponent { |
|||
|
|||
public get displayTimewindowConfig(): boolean { |
|||
const datasources = this.batteryLevelWidgetConfigForm.get('datasources').value; |
|||
return datasourcesHasAggregation(datasources); |
|||
} |
|||
|
|||
public onlyHistoryTimewindow(): boolean { |
|||
const datasources = this.batteryLevelWidgetConfigForm.get('datasources').value; |
|||
return datasourcesHasOnlyComparisonAggregation(datasources); |
|||
} |
|||
|
|||
batteryLevelLayouts = batteryLevelLayouts; |
|||
|
|||
batteryLevelLayoutTranslationMap = batteryLevelLayoutTranslations; |
|||
batteryLevelLayoutImageMap = batteryLevelLayoutImages; |
|||
|
|||
batteryLevelWidgetConfigForm: UntypedFormGroup; |
|||
|
|||
valuePreviewFn = this._valuePreviewFn.bind(this); |
|||
|
|||
constructor(protected store: Store<AppState>, |
|||
protected widgetConfigComponent: WidgetConfigComponent, |
|||
private $injector: Injector, |
|||
private fb: UntypedFormBuilder) { |
|||
super(store, widgetConfigComponent); |
|||
} |
|||
|
|||
protected configForm(): UntypedFormGroup { |
|||
return this.batteryLevelWidgetConfigForm; |
|||
} |
|||
|
|||
protected setupDefaults(configData: WidgetConfigComponentData) { |
|||
this.setupDefaultDatasource(configData, [{ name: 'batteryLevel', label: 'batteryLevel', type: DataKeyType.timeseries }]); |
|||
} |
|||
|
|||
protected onConfigSet(configData: WidgetConfigComponentData) { |
|||
const settings: BatteryLevelWidgetSettings = {...batteryLevelDefaultSettings, ...(configData.config.settings || {})}; |
|||
const iconSize = resolveCssSize(configData.config.iconSize); |
|||
this.batteryLevelWidgetConfigForm = this.fb.group({ |
|||
timewindowConfig: [getTimewindowConfig(configData.config), []], |
|||
datasources: [configData.config.datasources, []], |
|||
|
|||
layout: [settings.layout, []], |
|||
|
|||
showTitle: [configData.config.showTitle, []], |
|||
title: [configData.config.title, []], |
|||
titleFont: [configData.config.titleFont, []], |
|||
titleColor: [configData.config.titleColor, []], |
|||
|
|||
showIcon: [configData.config.showTitleIcon, []], |
|||
iconSize: [iconSize[0], [Validators.min(0)]], |
|||
iconSizeUnit: [iconSize[1], []], |
|||
icon: [configData.config.titleIcon, []], |
|||
iconColor: [configData.config.iconColor, []], |
|||
|
|||
showValue: [settings.showValue, []], |
|||
autoScaleValueSize: [settings.autoScaleValueSize, []], |
|||
valueFont: [settings.valueFont, []], |
|||
valueColor: [settings.valueColor, []], |
|||
|
|||
batteryLevelColor: [settings.batteryLevelColor, []], |
|||
batteryShapeColor: [settings.batteryShapeColor, []], |
|||
|
|||
background: [settings.background, []], |
|||
|
|||
cardButtons: [this.getCardButtons(configData.config), []], |
|||
borderRadius: [configData.config.borderRadius, []], |
|||
|
|||
actions: [configData.config.actions || {}, []] |
|||
}); |
|||
} |
|||
|
|||
protected prepareOutputConfig(config: any): WidgetConfigComponentData { |
|||
setTimewindowConfig(this.widgetConfig.config, config.timewindowConfig); |
|||
this.widgetConfig.config.datasources = config.datasources; |
|||
|
|||
this.widgetConfig.config.showTitle = config.showTitle; |
|||
this.widgetConfig.config.title = config.title; |
|||
this.widgetConfig.config.titleFont = config.titleFont; |
|||
this.widgetConfig.config.titleColor = config.titleColor; |
|||
|
|||
this.widgetConfig.config.showTitleIcon = config.showIcon; |
|||
this.widgetConfig.config.iconSize = cssSizeToStrSize(config.iconSize, config.iconSizeUnit); |
|||
this.widgetConfig.config.titleIcon = config.icon; |
|||
this.widgetConfig.config.iconColor = config.iconColor; |
|||
|
|||
this.widgetConfig.config.settings = this.widgetConfig.config.settings || {}; |
|||
|
|||
this.widgetConfig.config.settings.layout = config.layout; |
|||
|
|||
this.widgetConfig.config.settings.showValue = config.showValue; |
|||
this.widgetConfig.config.settings.autoScaleValueSize = config.autoScaleValueSize === true; |
|||
this.widgetConfig.config.settings.valueFont = config.valueFont; |
|||
this.widgetConfig.config.settings.valueColor = config.valueColor; |
|||
|
|||
this.widgetConfig.config.settings.batteryLevelColor = config.batteryLevelColor; |
|||
this.widgetConfig.config.settings.batteryShapeColor = config.batteryShapeColor; |
|||
|
|||
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 ['showTitle', 'showIcon', 'showValue']; |
|||
} |
|||
|
|||
protected updateValidators(emitEvent: boolean, trigger?: string) { |
|||
const showTitle: boolean = this.batteryLevelWidgetConfigForm.get('showTitle').value; |
|||
const showIcon: boolean = this.batteryLevelWidgetConfigForm.get('showIcon').value; |
|||
const showValue: boolean = this.batteryLevelWidgetConfigForm.get('showValue').value; |
|||
|
|||
if (showTitle) { |
|||
this.batteryLevelWidgetConfigForm.get('title').enable(); |
|||
this.batteryLevelWidgetConfigForm.get('titleFont').enable(); |
|||
this.batteryLevelWidgetConfigForm.get('titleColor').enable(); |
|||
this.batteryLevelWidgetConfigForm.get('showIcon').enable({emitEvent: false}); |
|||
if (showIcon) { |
|||
this.batteryLevelWidgetConfigForm.get('iconSize').enable(); |
|||
this.batteryLevelWidgetConfigForm.get('iconSizeUnit').enable(); |
|||
this.batteryLevelWidgetConfigForm.get('icon').enable(); |
|||
this.batteryLevelWidgetConfigForm.get('iconColor').enable(); |
|||
} else { |
|||
this.batteryLevelWidgetConfigForm.get('iconSize').disable(); |
|||
this.batteryLevelWidgetConfigForm.get('iconSizeUnit').disable(); |
|||
this.batteryLevelWidgetConfigForm.get('icon').disable(); |
|||
this.batteryLevelWidgetConfigForm.get('iconColor').disable(); |
|||
} |
|||
} else { |
|||
this.batteryLevelWidgetConfigForm.get('title').disable(); |
|||
this.batteryLevelWidgetConfigForm.get('titleFont').disable(); |
|||
this.batteryLevelWidgetConfigForm.get('titleColor').disable(); |
|||
this.batteryLevelWidgetConfigForm.get('showIcon').disable({emitEvent: false}); |
|||
this.batteryLevelWidgetConfigForm.get('iconSize').disable(); |
|||
this.batteryLevelWidgetConfigForm.get('iconSizeUnit').disable(); |
|||
this.batteryLevelWidgetConfigForm.get('icon').disable(); |
|||
this.batteryLevelWidgetConfigForm.get('iconColor').disable(); |
|||
} |
|||
|
|||
if (showValue) { |
|||
this.batteryLevelWidgetConfigForm.get('autoScaleValueSize').enable(); |
|||
this.batteryLevelWidgetConfigForm.get('valueFont').enable(); |
|||
this.batteryLevelWidgetConfigForm.get('valueColor').enable(); |
|||
} else { |
|||
this.batteryLevelWidgetConfigForm.get('autoScaleValueSize').disable(); |
|||
this.batteryLevelWidgetConfigForm.get('valueFont').disable(); |
|||
this.batteryLevelWidgetConfigForm.get('valueColor').disable(); |
|||
} |
|||
} |
|||
|
|||
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'); |
|||
} |
|||
|
|||
private _valuePreviewFn(): string { |
|||
const units: string = this.widgetConfig.config.units; |
|||
const decimals: number = this.widgetConfig.config.decimals; |
|||
return formatValue(55, decimals, units, true); |
|||
} |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
<!-- |
|||
|
|||
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-battery-level-panel" [style]="backgroundStyle" [class.tb-battery-level-pointer]="hasCardClickAction" (click)="cardClick($event)"> |
|||
<div class="tb-battery-level-overlay" [style]="overlayStyle"></div> |
|||
<div class="tb-battery-level-title-panel"> |
|||
<ng-container *ngTemplateOutlet="widgetTitlePanel"></ng-container> |
|||
</div> |
|||
<div #batteryLevelContent class="tb-battery-level-content" [class]="layoutClass"> |
|||
<div #batteryLevelBox class="tb-battery-level-box"> |
|||
<div #batteryLevelRectangle class="tb-battery-level-rectangle" [class]="layoutClass" [class.solid]="solid" [class.divided]="!solid"> |
|||
<div class="tb-battery-level-shape" [style.background]="batteryShapeColor.color"></div> |
|||
<div class="tb-battery-level-container"> |
|||
<div *ngIf="solid; else dividedIndicator" class="tb-battery-level-indicator-box solid" |
|||
[style.background-image]="'linear-gradient(0deg, ' + batteryLevelColor.color + ' 0% 100%)'" |
|||
[style.background-size]="vertical ? '100% ' + (value + 1) + '%' : (value + 1) + '% 100%'"> |
|||
</div> |
|||
</div> |
|||
</div> |
|||
</div> |
|||
<div *ngIf="showValue" #batteryLevelValueBox class="tb-battery-level-value-box"> |
|||
<div #batteryLevelValue class="tb-battery-level-value" [style]="valueStyle" [style.color]="valueColor.color">{{ valueText }}</div> |
|||
</div> |
|||
</div> |
|||
</div> |
|||
<ng-template #dividedIndicator> |
|||
<div *ngFor="let section of batterySections; trackBy: trackBySection" class="tb-battery-level-indicator-box divided" |
|||
[style.background]="batteryLevelColor.color" |
|||
[style.opacity]="section ? '1': '0'"></div> |
|||
</ng-template> |
|||
@ -0,0 +1,158 @@ |
|||
/** |
|||
* 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-battery-level-panel { |
|||
width: 100%; |
|||
height: 100%; |
|||
position: relative; |
|||
display: flex; |
|||
flex-direction: column; |
|||
gap: 16px; |
|||
padding: 20px 24px 24px 24px; |
|||
&.tb-battery-level-pointer { |
|||
cursor: pointer; |
|||
} |
|||
> div:not(.tb-battery-level-overlay) { |
|||
z-index: 1; |
|||
} |
|||
.tb-battery-level-overlay { |
|||
position: absolute; |
|||
top: 12px; |
|||
left: 12px; |
|||
bottom: 12px; |
|||
right: 12px; |
|||
} |
|||
.tb-battery-level-content { |
|||
min-height: 0; |
|||
flex: 1; |
|||
display: flex; |
|||
justify-content: center; |
|||
&.vertical { |
|||
flex-direction: row; |
|||
gap: 16px; |
|||
.tb-battery-level-value-box { |
|||
align-items: center; |
|||
.tb-battery-level-value { |
|||
padding: 8px 12px; |
|||
} |
|||
} |
|||
} |
|||
&.horizontal { |
|||
flex-direction: column-reverse; |
|||
gap: 8px; |
|||
align-items: center; |
|||
.tb-battery-level-value-box { |
|||
.tb-battery-level-value { |
|||
padding: 4px 6px; |
|||
} |
|||
} |
|||
} |
|||
.tb-battery-level-box { |
|||
display: flex; |
|||
align-items: center; |
|||
.tb-battery-level-rectangle { |
|||
width: 100%; |
|||
height: 100%; |
|||
position: relative; |
|||
.tb-battery-level-shape { |
|||
position: absolute; |
|||
inset: 0; |
|||
mask-repeat: no-repeat; |
|||
mask-size: cover; |
|||
mask-position: center; |
|||
} |
|||
.tb-battery-level-container { |
|||
position: absolute; |
|||
display: flex; |
|||
gap: 3%; |
|||
} |
|||
.tb-battery-level-indicator-box { |
|||
width: 100%; |
|||
height: 100%; |
|||
&.solid { |
|||
background-repeat: no-repeat; |
|||
transition: background 0.2s ease-out; |
|||
} |
|||
&.divided { |
|||
transition: opacity 0.2s ease-out; |
|||
} |
|||
} |
|||
&.vertical { |
|||
.tb-battery-level-shape { |
|||
mask-image: url(/assets/widget/battery-level/battery-shape-vertical.svg); |
|||
} |
|||
.tb-battery-level-container { |
|||
flex-direction: column-reverse; |
|||
} |
|||
&.solid { |
|||
.tb-battery-level-container { |
|||
inset: 8.85% 6.25% 3.54% 6.25%; |
|||
} |
|||
} |
|||
&.divided { |
|||
.tb-battery-level-container { |
|||
inset: 9.73% 7.81% 4.42% 7.81%; |
|||
} |
|||
} |
|||
.tb-battery-level-indicator-box { |
|||
&.solid { |
|||
border-radius: 10.7% / 6%; |
|||
background-position: 0 101%; |
|||
} |
|||
&.divided { |
|||
border-radius: 7.14% / 17.8%; |
|||
} |
|||
} |
|||
} |
|||
&.horizontal { |
|||
.tb-battery-level-shape { |
|||
mask-image: url(/assets/widget/battery-level/battery-shape-horizontal.svg); |
|||
} |
|||
.tb-battery-level-container { |
|||
inset: 6.25% 8.85% 6.25% 3.54%; |
|||
flex-direction: row; |
|||
} |
|||
&.solid { |
|||
.tb-battery-level-container { |
|||
inset: 6.25% 8.85% 6.25% 3.54%; |
|||
} |
|||
} |
|||
&.divided { |
|||
.tb-battery-level-container { |
|||
inset: 7.81% 9.73% 7.81% 4.42%; |
|||
} |
|||
} |
|||
.tb-battery-level-indicator-box { |
|||
&.solid { |
|||
border-radius: 6% / 10.7%; |
|||
background-position: -1% 0%; |
|||
} |
|||
&.divided { |
|||
border-radius: 17.8% / 7.14%; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
.tb-battery-level-value-box { |
|||
display: flex; |
|||
.tb-battery-level-value { |
|||
white-space: nowrap; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,293 @@ |
|||
///
|
|||
/// 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 { |
|||
AfterViewInit, |
|||
ChangeDetectorRef, |
|||
Component, |
|||
ElementRef, |
|||
Input, |
|||
OnDestroy, |
|||
OnInit, |
|||
Renderer2, |
|||
TemplateRef, |
|||
ViewChild |
|||
} from '@angular/core'; |
|||
import { WidgetContext } from '@home/models/widget-component.models'; |
|||
import { formatValue, isDefinedAndNotNull, isNumeric } from '@core/utils'; |
|||
import { DatePipe } from '@angular/common'; |
|||
import { |
|||
backgroundStyle, |
|||
ColorProcessor, |
|||
ComponentStyle, |
|||
getDataKey, |
|||
getSingleTsValue, |
|||
overlayStyle, |
|||
textStyle |
|||
} from '@shared/models/widget-settings.models'; |
|||
import { WidgetComponent } from '@home/components/widget/widget.component'; |
|||
import { |
|||
batteryLevelDefaultSettings, |
|||
BatteryLevelLayout, |
|||
BatteryLevelWidgetSettings |
|||
} from '@home/components/widget/lib/indicator/battery-level-widget.models'; |
|||
import { ResizeObserver } from '@juggle/resize-observer'; |
|||
|
|||
const verticalBatteryDimensions = { |
|||
shapeAspectRatio: 64 / 113, |
|||
widthRatio: { |
|||
valueTopBottomPaddingRatio: 8 / 64, |
|||
valueLeftRightPaddingRatio: 12 / 64, |
|||
valueFontSizeRatio: 20 / 64, |
|||
valueLineHeightRaio: 24 / 64 |
|||
}, |
|||
heightRatio: { |
|||
valueTopBottomPaddingRatio: 8 / 113, |
|||
valueLeftRightPaddingRatio: 12 / 113, |
|||
valueFontSizeRatio: 20 / 113, |
|||
valueLineHeightRaio: 24 / 113 |
|||
} |
|||
}; |
|||
|
|||
const horizontalBatteryDimensions = { |
|||
shapeAspectRatio: 113 / 64, |
|||
heightRatio: { |
|||
valueTopBottomPaddingRatio: 4 / 64, |
|||
valueFontSizeRatio: 20 / 64, |
|||
valueLineHeightRatio: 24 / 64 |
|||
} |
|||
}; |
|||
|
|||
@Component({ |
|||
selector: 'tb-battery-level-widget', |
|||
templateUrl: './battery-level-widget.component.html', |
|||
styleUrls: ['./battery-level-widget.component.scss'] |
|||
}) |
|||
export class BatteryLevelWidgetComponent implements OnInit, OnDestroy, AfterViewInit { |
|||
|
|||
@ViewChild('batteryLevelContent', {static: true}) |
|||
batteryLevelContent: ElementRef<HTMLElement>; |
|||
|
|||
@ViewChild('batteryLevelBox', {static: true}) |
|||
batteryLevelBox: ElementRef<HTMLElement>; |
|||
|
|||
@ViewChild('batteryLevelRectangle', {static: true}) |
|||
batteryLevelRectangle: ElementRef<HTMLElement>; |
|||
|
|||
@ViewChild('batteryLevelValueBox', {static: false}) |
|||
batteryLevelValueBox: ElementRef<HTMLElement>; |
|||
|
|||
@ViewChild('batteryLevelValue', {static: false}) |
|||
batteryLevelValue: ElementRef<HTMLElement>; |
|||
|
|||
settings: BatteryLevelWidgetSettings; |
|||
|
|||
@Input() |
|||
ctx: WidgetContext; |
|||
|
|||
@Input() |
|||
widgetTitlePanel: TemplateRef<any>; |
|||
|
|||
layout: BatteryLevelLayout; |
|||
layoutClass = 'vertical'; |
|||
|
|||
vertical = true; |
|||
solid = true; |
|||
|
|||
showValue = true; |
|||
autoScaleValueSize = true; |
|||
valueText = 'N/A'; |
|||
valueStyle: ComponentStyle = {}; |
|||
valueColor: ColorProcessor; |
|||
|
|||
value: number; |
|||
|
|||
batterySections: boolean[] = [false, false, false, false]; |
|||
|
|||
batteryLevelColor: ColorProcessor; |
|||
|
|||
batteryShapeColor: ColorProcessor; |
|||
|
|||
backgroundStyle: ComponentStyle = {}; |
|||
overlayStyle: ComponentStyle = {}; |
|||
|
|||
batteryBoxResize$: ResizeObserver; |
|||
|
|||
hasCardClickAction = false; |
|||
|
|||
private decimals = 0; |
|||
private units = ''; |
|||
|
|||
constructor(private date: DatePipe, |
|||
private widgetComponent: WidgetComponent, |
|||
private renderer: Renderer2, |
|||
private cd: ChangeDetectorRef) { |
|||
} |
|||
|
|||
ngOnInit(): void { |
|||
this.ctx.$scope.batteryLevelWidget = this; |
|||
this.settings = {...batteryLevelDefaultSettings, ...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.vertical = [BatteryLevelLayout.vertical_solid, BatteryLevelLayout.vertical_divided].includes(this.layout); |
|||
this.layoutClass = this.vertical ? 'vertical' : 'horizontal'; |
|||
this.solid = [BatteryLevelLayout.vertical_solid, BatteryLevelLayout.horizontal_solid].includes(this.layout); |
|||
|
|||
this.showValue = this.settings.showValue; |
|||
this.autoScaleValueSize = this.showValue && this.settings.autoScaleValueSize; |
|||
this.valueStyle = textStyle(this.settings.valueFont, '0.1px'); |
|||
this.valueColor = ColorProcessor.fromSettings(this.settings.valueColor); |
|||
|
|||
this.batteryLevelColor = ColorProcessor.fromSettings(this.settings.batteryLevelColor); |
|||
|
|||
this.batteryShapeColor = ColorProcessor.fromSettings(this.settings.batteryShapeColor); |
|||
|
|||
this.backgroundStyle = backgroundStyle(this.settings.background); |
|||
this.overlayStyle = overlayStyle(this.settings.background.overlay); |
|||
|
|||
this.hasCardClickAction = this.ctx.actionsApi.getActionDescriptors('cardClick').length > 0; |
|||
} |
|||
|
|||
ngAfterViewInit() { |
|||
this.batteryBoxResize$ = new ResizeObserver(() => { |
|||
this.onResize(); |
|||
}); |
|||
this.batteryBoxResize$.observe(this.batteryLevelContent.nativeElement); |
|||
if (this.showValue) { |
|||
this.batteryBoxResize$.observe(this.batteryLevelValueBox.nativeElement); |
|||
} |
|||
this.onResize(); |
|||
} |
|||
|
|||
ngOnDestroy() { |
|||
if (this.batteryBoxResize$) { |
|||
this.batteryBoxResize$.disconnect(); |
|||
} |
|||
} |
|||
|
|||
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); |
|||
this.value = 0; |
|||
if (tsValue && isDefinedAndNotNull(tsValue[1]) && isNumeric(tsValue[1])) { |
|||
this.value = tsValue[1]; |
|||
this.valueText = formatValue(this.value, this.decimals, this.units, true); |
|||
} else { |
|||
this.valueText = 'N/A'; |
|||
} |
|||
if (!this.solid) { |
|||
const sectionSize = 100 / this.batterySections.length; |
|||
for (let i=0; i<this.batterySections.length; i++) { |
|||
this.batterySections[i] = this.value > sectionSize * i; |
|||
} |
|||
} |
|||
this.valueColor.update(this.value); |
|||
this.batteryLevelColor.update(this.value); |
|||
this.batteryShapeColor.update(this.value); |
|||
this.cd.detectChanges(); |
|||
} |
|||
|
|||
public trackBySection(index: number): number { |
|||
return index; |
|||
} |
|||
|
|||
public cardClick($event: Event) { |
|||
this.ctx.actionsApi.cardClick($event); |
|||
} |
|||
|
|||
private onResize() { |
|||
if (this.vertical) { |
|||
if (this.batteryLevelValue) { |
|||
const contentWidth = this.batteryLevelContent.nativeElement.getBoundingClientRect().width; |
|||
const boxWidth = (contentWidth - 16) / 2; |
|||
const boxHeight = this.batteryLevelContent.nativeElement.getBoundingClientRect().height; |
|||
const ratios = contentWidth > boxHeight ? verticalBatteryDimensions.heightRatio : verticalBatteryDimensions.widthRatio; |
|||
const boxSize = contentWidth > boxHeight ? boxHeight : boxWidth; |
|||
const topBottomValuePadding = ratios.valueTopBottomPaddingRatio * boxSize; |
|||
const leftRightValuePadding = ratios.valueLeftRightPaddingRatio * boxSize; |
|||
const valuePadding = `${topBottomValuePadding}px ${leftRightValuePadding}px`; |
|||
this.renderer.setStyle(this.batteryLevelValue.nativeElement, 'padding', valuePadding); |
|||
if (this.autoScaleValueSize) { |
|||
const valueFontSize = ratios.valueFontSizeRatio * boxSize; |
|||
const valueLineHeight = ratios.valueLineHeightRaio * boxSize; |
|||
this.setValueFontSize(valueFontSize, valueLineHeight, boxWidth); |
|||
} |
|||
} |
|||
let height = this.batteryLevelContent.nativeElement.getBoundingClientRect().height; |
|||
const width = height * verticalBatteryDimensions.shapeAspectRatio; |
|||
this.renderer.setStyle(this.batteryLevelBox.nativeElement, 'width', width + 'px'); |
|||
const realWidth = this.batteryLevelBox.nativeElement.getBoundingClientRect().width; |
|||
if (realWidth < width) { |
|||
height = realWidth / verticalBatteryDimensions.shapeAspectRatio; |
|||
this.renderer.setStyle(this.batteryLevelRectangle.nativeElement, 'height', height + 'px'); |
|||
} else { |
|||
this.renderer.setStyle(this.batteryLevelRectangle.nativeElement, 'height', null); |
|||
} |
|||
} else { |
|||
const width = this.batteryLevelContent.nativeElement.getBoundingClientRect().width; |
|||
let height = width / horizontalBatteryDimensions.shapeAspectRatio; |
|||
this.renderer.setStyle(this.batteryLevelBox.nativeElement, 'height', height + 'px'); |
|||
const realHeight = this.batteryLevelBox.nativeElement.getBoundingClientRect().height; |
|||
if (realHeight < height) { |
|||
height = realHeight; |
|||
const newWidth = height * horizontalBatteryDimensions.shapeAspectRatio; |
|||
this.renderer.setStyle(this.batteryLevelRectangle.nativeElement, 'width', newWidth + 'px'); |
|||
} else { |
|||
this.renderer.setStyle(this.batteryLevelRectangle.nativeElement, 'width', null); |
|||
} |
|||
if (this.batteryLevelValue) { |
|||
const ratios = horizontalBatteryDimensions.heightRatio; |
|||
const valuePadding = `${(ratios.valueTopBottomPaddingRatio * height)}px 6px`; |
|||
this.renderer.setStyle(this.batteryLevelValue.nativeElement, 'padding', valuePadding); |
|||
if (this.autoScaleValueSize) { |
|||
const valueFontSize = ratios.valueFontSizeRatio * height; |
|||
const valueLineHeight = ratios.valueLineHeightRatio * height; |
|||
const boxWidth = this.batteryLevelContent.nativeElement.getBoundingClientRect().width; |
|||
this.setValueFontSize(valueFontSize, valueLineHeight, boxWidth); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
private setValueFontSize(valueFontSize: number, valueLineHeight: number, maxWidth: number) { |
|||
this.renderer.setStyle(this.batteryLevelValue.nativeElement, 'fontSize', valueFontSize + 'px'); |
|||
this.renderer.setStyle(this.batteryLevelValue.nativeElement, 'lineHeight', valueLineHeight + 'px'); |
|||
let valueWidth = this.batteryLevelValue.nativeElement.getBoundingClientRect().width; |
|||
while (valueWidth > maxWidth && valueFontSize > 6) { |
|||
valueFontSize--; |
|||
valueLineHeight--; |
|||
this.renderer.setStyle(this.batteryLevelValue.nativeElement, 'fontSize', valueFontSize + 'px'); |
|||
this.renderer.setStyle(this.batteryLevelValue.nativeElement, 'lineHeight', valueLineHeight + 'px'); |
|||
valueWidth = this.batteryLevelValue.nativeElement.getBoundingClientRect().width; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,108 @@ |
|||
///
|
|||
/// 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, |
|||
ColorType, |
|||
constantColor, |
|||
defaultColorFunction, |
|||
Font |
|||
} from '@shared/models/widget-settings.models'; |
|||
|
|||
export enum BatteryLevelLayout { |
|||
vertical_solid = 'vertical_solid', |
|||
horizontal_solid = 'horizontal_solid', |
|||
vertical_divided = 'vertical_divided', |
|||
horizontal_divided = 'horizontal_divided' |
|||
} |
|||
|
|||
export const batteryLevelLayouts = Object.keys(BatteryLevelLayout) as BatteryLevelLayout[]; |
|||
|
|||
export const batteryLevelLayoutTranslations = new Map<BatteryLevelLayout, string>( |
|||
[ |
|||
[BatteryLevelLayout.vertical_solid, 'widgets.battery-level.layout-vertical-solid'], |
|||
[BatteryLevelLayout.horizontal_solid, 'widgets.battery-level.layout-horizontal-solid'], |
|||
[BatteryLevelLayout.vertical_divided, 'widgets.battery-level.layout-vertical-divided'], |
|||
[BatteryLevelLayout.horizontal_divided, 'widgets.battery-level.layout-horizontal-divided'] |
|||
] |
|||
); |
|||
|
|||
export const batteryLevelLayoutImages = new Map<BatteryLevelLayout, string>( |
|||
[ |
|||
[BatteryLevelLayout.vertical_solid, 'assets/widget/battery-level/vertical-solid-layout.svg'], |
|||
[BatteryLevelLayout.horizontal_solid, 'assets/widget/battery-level/horizontal-solid-layout.svg'], |
|||
[BatteryLevelLayout.vertical_divided, 'assets/widget/battery-level/vertical-divided-layout.svg'], |
|||
[BatteryLevelLayout.horizontal_divided, 'assets/widget/battery-level/horizontal-divided-layout.svg'] |
|||
] |
|||
); |
|||
|
|||
export interface BatteryLevelWidgetSettings { |
|||
layout: BatteryLevelLayout; |
|||
showValue: boolean; |
|||
autoScaleValueSize: boolean; |
|||
valueFont: Font; |
|||
valueColor: ColorSettings; |
|||
batteryLevelColor: ColorSettings; |
|||
batteryShapeColor: ColorSettings; |
|||
background: BackgroundSettings; |
|||
} |
|||
|
|||
export const batteryLevelDefaultSettings: BatteryLevelWidgetSettings = { |
|||
layout: BatteryLevelLayout.vertical_solid, |
|||
showValue: true, |
|||
autoScaleValueSize: true, |
|||
valueFont: { |
|||
family: 'Roboto', |
|||
size: 20, |
|||
sizeUnit: 'px', |
|||
style: 'normal', |
|||
weight: '500', |
|||
lineHeight: '24px' |
|||
}, |
|||
valueColor: constantColor('rgba(0, 0, 0, 0.87)'), |
|||
batteryLevelColor: { |
|||
color: 'rgba(92, 223, 144, 1)', |
|||
type: ColorType.range, |
|||
rangeList: [ |
|||
{from: 0, to: 25, color: 'rgba(227, 71, 71, 1)'}, |
|||
{from: 25, to: 50, color: 'rgba(246, 206, 67, 1)'}, |
|||
{from: 50, to: 100, color: 'rgba(92, 223, 144, 1)'} |
|||
], |
|||
colorFunction: defaultColorFunction |
|||
}, |
|||
batteryShapeColor: { |
|||
color: 'rgba(92, 223, 144, 0.32)', |
|||
type: ColorType.range, |
|||
rangeList: [ |
|||
{from: 0, to: 25, color: 'rgba(227, 71, 71, 0.32)'}, |
|||
{from: 25, to: 50, color: 'rgba(246, 206, 67, 0.32)'}, |
|||
{from: 50, to: 100, color: 'rgba(92, 223, 144, 0.32)'} |
|||
], |
|||
colorFunction: defaultColorFunction |
|||
}, |
|||
background: { |
|||
type: BackgroundType.color, |
|||
color: '#fff', |
|||
overlay: { |
|||
enabled: false, |
|||
color: 'rgba(255,255,255,0.72)', |
|||
blur: 3 |
|||
} |
|||
} |
|||
}; |
|||
|
|||
@ -0,0 +1,63 @@ |
|||
<!-- |
|||
|
|||
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]="batteryLevelWidgetSettingsForm"> |
|||
<div class="tb-form-panel"> |
|||
<div class="tb-form-panel-title" translate>widgets.battery-level.battery-level-card-style</div> |
|||
<tb-image-cards-select rowHeight="1:1" |
|||
cols="4" |
|||
colsLtMd="2" |
|||
label="{{ 'widgets.battery-level.layout' | translate }}" formControlName="layout"> |
|||
<tb-image-cards-select-option *ngFor="let layout of batteryLevelLayouts" |
|||
[value]="layout" |
|||
[image]="batteryLevelLayoutImageMap.get(layout)"> |
|||
{{ batteryLevelLayoutTranslationMap.get(layout) | translate }} |
|||
</tb-image-cards-select-option> |
|||
</tb-image-cards-select> |
|||
<div class="tb-form-row space-between column-xs"> |
|||
<mat-slide-toggle class="mat-slide fixed-title-width" formControlName="showValue"> |
|||
{{ 'widgets.battery-level.value' | translate }} |
|||
</mat-slide-toggle> |
|||
<div fxLayout="row" fxLayoutAlign="start center" fxLayoutGap="8px"> |
|||
<mat-chip-listbox formControlName="autoScaleValueSize"> |
|||
<mat-chip-option [value]="true">{{ 'widgets.battery-level.auto-scale' | translate }}</mat-chip-option> |
|||
</mat-chip-listbox> |
|||
<tb-font-settings formControlName="valueFont" |
|||
[autoScale]="batteryLevelWidgetSettingsForm.get('autoScaleValueSize').value" |
|||
[previewText]="valuePreviewFn"> |
|||
</tb-font-settings> |
|||
<tb-color-settings formControlName="valueColor"> |
|||
</tb-color-settings> |
|||
</div> |
|||
</div> |
|||
<div class="tb-form-row space-between"> |
|||
<div>{{ 'widgets.battery-level.battery-level-color' | translate }}</div> |
|||
<tb-color-settings formControlName="batteryLevelColor"> |
|||
</tb-color-settings> |
|||
</div> |
|||
<div class="tb-form-row space-between"> |
|||
<div>{{ 'widgets.battery-level.battery-shape-color' | translate }}</div> |
|||
<tb-color-settings formControlName="batteryShapeColor"> |
|||
</tb-color-settings> |
|||
</div> |
|||
<div class="tb-form-row space-between"> |
|||
<div>{{ 'widgets.background.background' | translate }}</div> |
|||
<tb-background-settings formControlName="background"> |
|||
</tb-background-settings> |
|||
</div> |
|||
</div> |
|||
</ng-container> |
|||
@ -0,0 +1,104 @@ |
|||
///
|
|||
/// 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, Injector } from '@angular/core'; |
|||
import { WidgetSettings, WidgetSettingsComponent } from '@shared/models/widget.models'; |
|||
import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { formatValue } from '@core/utils'; |
|||
import { |
|||
batteryLevelDefaultSettings, |
|||
batteryLevelLayoutImages, |
|||
batteryLevelLayouts, |
|||
batteryLevelLayoutTranslations |
|||
} from '@home/components/widget/lib/indicator/battery-level-widget.models'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-battery-level-widget-settings', |
|||
templateUrl: './battery-level-widget-settings.component.html', |
|||
styleUrls: [] |
|||
}) |
|||
export class BatteryLevelWidgetSettingsComponent extends WidgetSettingsComponent { |
|||
|
|||
batteryLevelLayouts = batteryLevelLayouts; |
|||
|
|||
batteryLevelLayoutTranslationMap = batteryLevelLayoutTranslations; |
|||
batteryLevelLayoutImageMap = batteryLevelLayoutImages; |
|||
|
|||
batteryLevelWidgetSettingsForm: UntypedFormGroup; |
|||
|
|||
valuePreviewFn = this._valuePreviewFn.bind(this); |
|||
|
|||
constructor(protected store: Store<AppState>, |
|||
private $injector: Injector, |
|||
private fb: UntypedFormBuilder) { |
|||
super(store); |
|||
} |
|||
|
|||
protected settingsForm(): UntypedFormGroup { |
|||
return this.batteryLevelWidgetSettingsForm; |
|||
} |
|||
|
|||
protected defaultSettings(): WidgetSettings { |
|||
return {...batteryLevelDefaultSettings}; |
|||
} |
|||
|
|||
protected onSettingsSet(settings: WidgetSettings) { |
|||
this.batteryLevelWidgetSettingsForm = this.fb.group({ |
|||
layout: [settings.layout, []], |
|||
|
|||
showValue: [settings.showValue, []], |
|||
autoScaleValueSize: [settings.autoScaleValueSize, []], |
|||
valueFont: [settings.valueFont, []], |
|||
valueColor: [settings.valueColor, []], |
|||
|
|||
batteryLevelColor: [settings.batteryLevelColor, []], |
|||
batteryShapeColor: [settings.batteryShapeColor, []], |
|||
|
|||
background: [settings.background, []] |
|||
}); |
|||
} |
|||
|
|||
protected validatorTriggers(): string[] { |
|||
return ['showValue']; |
|||
} |
|||
|
|||
protected updateValidators(emitEvent: boolean) { |
|||
const showValue: boolean = this.batteryLevelWidgetSettingsForm.get('showValue').value; |
|||
|
|||
if (showValue) { |
|||
this.batteryLevelWidgetSettingsForm.get('autoScaleValueSize').enable(); |
|||
this.batteryLevelWidgetSettingsForm.get('valueFont').enable(); |
|||
this.batteryLevelWidgetSettingsForm.get('valueColor').enable(); |
|||
} else { |
|||
this.batteryLevelWidgetSettingsForm.get('autoScaleValueSize').disable(); |
|||
this.batteryLevelWidgetSettingsForm.get('valueFont').disable(); |
|||
this.batteryLevelWidgetSettingsForm.get('valueColor').disable(); |
|||
} |
|||
|
|||
this.batteryLevelWidgetSettingsForm.get('autoScaleValueSize').updateValueAndValidity({emitEvent}); |
|||
this.batteryLevelWidgetSettingsForm.get('valueFont').updateValueAndValidity({emitEvent}); |
|||
this.batteryLevelWidgetSettingsForm.get('valueColor').updateValueAndValidity({emitEvent}); |
|||
} |
|||
|
|||
private _valuePreviewFn(): string { |
|||
const units: string = this.widgetConfig.config.units; |
|||
const decimals: number = this.widgetConfig.config.decimals; |
|||
return formatValue(22, decimals, units, true); |
|||
} |
|||
|
|||
} |
|||
|
After Width: | Height: | Size: 659 B |
|
After Width: | Height: | Size: 661 B |
|
After Width: | Height: | Size: 17 KiB |
|
After Width: | Height: | Size: 17 KiB |
|
After Width: | Height: | Size: 16 KiB |
|
After Width: | Height: | Size: 17 KiB |
Loading…
Reference in new issue