100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Add sprintf to i18next and restirect write access
Robin Dekkers7c83a47e2a30
diff --git a/deployment/manager/shared/locales/de/or.json b/deployment/manager/shared/locales/de/or.jsonindex 472fc9ae5..03390c2f0 100644--- a/deployment/manager/shared/locales/de/or.json+++ b/deployment/manager/shared/locales/de/or.json@@ -108,5 +108,10 @@"attributes": "Attribute","info": "Info","logs":"Protokolle",- "choose_lang":"Sprache wählen"+ "choose_lang":"Sprache wählen",+ "attributeValueType": {+ "PERCENTAGE": "%.2f",+ "NUMBER": "%.0f",+ "TEMPERATURE": "%.1f"+ }}diff --git a/deployment/manager/shared/locales/en/or.json b/deployment/manager/shared/locales/en/or.jsonindex 4382f0f38..2be7ff56e 100644--- a/deployment/manager/shared/locales/en/or.json+++ b/deployment/manager/shared/locales/en/or.json@@ -169,5 +169,10 @@"reset": "Reset","noLongerMatches": "No longer matches","logs":"Logs",- "choose_lang":"Choose language"+ "choose_lang":"Choose language",+ "attributeValueType": {+ "PERCENTAGE": "%.2f",+ "NUMBER": "%.0f",+ "TEMPERATURE": "%.1f"+ }}diff --git a/deployment/manager/shared/locales/es/or.json b/deployment/manager/shared/locales/es/or.jsonindex f28043f4c..0afc02973 100644--- a/deployment/manager/shared/locales/es/or.json+++ b/deployment/manager/shared/locales/es/or.json@@ -108,5 +108,10 @@"attributes": "Atributos","info": "Informacion","logs":"Registros",- "choose_lang":"Elige lengua"+ "choose_lang":"Elige lengua",+ "attributeValueType": {+ "PERCENTAGE": "%.2f",+ "NUMBER": "%.0f",+ "TEMPERATURE": "%.1f"+ }}diff --git a/deployment/manager/shared/locales/fr/or.json b/deployment/manager/shared/locales/fr/or.jsonindex 01af70372..2f4e5783a 100644--- a/deployment/manager/shared/locales/fr/or.json+++ b/deployment/manager/shared/locales/fr/or.json@@ -108,5 +108,10 @@"attributes": "Attributs","info": "Info","logs":"Journaux",- "choose_lang":"Choisissez la langue"+ "choose_lang":"Choisissez la langue",+ "attributeValueType": {+ "PERCENTAGE": "%.2f",+ "NUMBER": "%.0f",+ "TEMPERATURE": "%.1f"+ }}diff --git a/deployment/manager/shared/locales/nl/or.json b/deployment/manager/shared/locales/nl/or.jsonindex 69bab9cfd..ee887b1ae 100644--- a/deployment/manager/shared/locales/nl/or.json+++ b/deployment/manager/shared/locales/nl/or.json@@ -169,5 +169,10 @@"reset": "Reset","noLongerMatches": "Niet meer geldig","logs":"Logs",- "choose_lang":"Kies een taal"+ "choose_lang":"Kies een taal",+ "attributeValueType": {+ "PERCENTAGE": "%.2f",+ "NUMBER": "%.0f",+ "TEMPERATURE": "%.1f"+ }}diff --git a/ui/component/core/@types/i18next-sprintf-postprocessor.d.ts b/ui/component/core/@types/i18next-sprintf-postprocessor.d.tsnew file mode 100644index 000000000..0a5c7a4d0--- /dev/null+++ b/ui/component/core/@types/i18next-sprintf-postprocessor.d.ts@@ -0,0 +1 @@+declare module "i18next-sprintf-postprocessor";\ No newline at end of filediff --git a/ui/component/core/package.json b/ui/component/core/package.jsonindex e9bc601da..940ba7fd1 100644--- a/ui/component/core/package.json+++ b/ui/component/core/package.json@@ -20,6 +20,7 @@"@openremote/rest": "^1.1.0","i18next": "^15.0.9","i18next-xhr-backend": "^2.0.1",+ "i18next-sprintf-postprocessor": "0.2.2","moment": "^2.24.0","platform": "^1.3.5","url-search-params-polyfill": "^5.0.0"diff --git a/ui/component/core/src/index.ts b/ui/component/core/src/index.tsindex f01f66e5e..5814d0aae 100644--- a/ui/component/core/src/index.ts+++ b/ui/component/core/src/index.ts@@ -5,6 +5,7 @@ import {AxiosRequestConfig} from "axios";import {EventProvider, EventProviderFactory, EventProviderStatus, WebSocketEventProvider} from "./event";import i18next from "i18next";import i18nextXhr from "i18next-xhr-backend";+import sprintf from 'i18next-sprintf-postprocessor';import moment from "moment";import {AssetDescriptor,@@ -664,7 +665,7 @@ export class Manager implements EventProviderFactory {}try {- await i18next.use(i18nextXhr).init(initOptions);+ await i18next.use(sprintf).use(i18nextXhr).init(initOptions);} catch (e) {console.error(e);return false;diff --git a/ui/component/core/src/util.ts b/ui/component/core/src/util.tsindex 42cf0ca06..b4969452a 100644--- a/ui/component/core/src/util.ts+++ b/ui/component/core/src/util.ts@@ -13,7 +13,8 @@ import {AssetAttribute,AttributeDescriptor,MetaItemType,- MetaItemDescriptor+ MetaItemDescriptor,+ AttributeValueType} from "@openremote/model";import {AssetModelUtil} from "./index";import i18next from "i18next";@@ -230,6 +231,7 @@ export function getAssetAttributes(asset: Asset, exclude?: string[]): AssetAttrireturn [];}+export function getFirstMetaItem(attribute: Attribute | undefined, name: string): MetaItem | undefined {if (!attribute || !attribute.meta) {return;@@ -246,7 +248,7 @@ export function getMetaValue(metaItemUrn: string | MetaItemDescriptor, attributeconst urn = typeof metaItemUrn === "string" ? metaItemUrn : (metaItemUrn as MetaItemDescriptor).urn;if (attribute && attribute.meta) {- const metaItem = attribute.meta.find((mi) => mi.name === urn);+ const metaItem = attribute.meta.find((mi) => mi.name === urn);return metaItem ? metaItem.value : undefined;}@@ -266,6 +268,19 @@ export function getAttributeLabel(attribute: Attribute | undefined, descriptor:return i18next.t([name, fallback || labelMetaValue || name]);}+export function getAttributeValue(attribute: Attribute, descriptor: AttributeDescriptor | undefined, fallbackFormat?: string): any {+ if (!attribute && !descriptor) {+ return fallbackFormat || "";+ }+ const format = getMetaValue(MetaItemType.FORMAT, attribute , undefined);+ const valueType = attribute.type;+ if(attribute.value) {+ return i18next.t([format || "attributeValueType."+valueType, fallbackFormat || "%s"], { postProcess: 'sprintf', sprintf: [attribute.value] });+ } else {+ return attribute.value;+ }+}+/*** Immutable update of an asset using the supplied attribute event*/diff --git a/ui/component/core/tsconfig.json b/ui/component/core/tsconfig.jsonindex 099d11fc7..34aa2fbd1 100644--- a/ui/component/core/tsconfig.json+++ b/ui/component/core/tsconfig.json@@ -6,7 +6,8 @@"rootDir": "src"},"include": [- "./src"+ "./src",+ "./@types"],"references": [{ "path": "../model" },diff --git a/ui/component/or-attribute-input/src/index.ts b/ui/component/or-attribute-input/src/index.tsindex c6e4e6f62..f25a8d0ac 100644--- a/ui/component/or-attribute-input/src/index.ts+++ b/ui/component/or-attribute-input/src/index.ts@@ -134,7 +134,7 @@ export function getAttributeValueTemplate(value = JSON.stringify(value, null, 2);}- return value;+ return Util.getAttributeValue(attribute, attributeDescriptor);};const setValue = (value: any) => {diff --git a/ui/component/or-input/src/index.ts b/ui/component/or-input/src/index.tsindex 31413df9f..9059733da 100644--- a/ui/component/or-input/src/index.ts+++ b/ui/component/or-input/src/index.ts@@ -12,9 +12,10 @@ import {MDCList, MDCListActionEvent} from '@material/list';import {MDCFormField, MDCFormFieldInput} from "@material/form-field";import {MDCIconButtonToggle, MDCIconButtonToggleEventDetail} from "@material/icon-button";import moment from "moment";-import {DefaultColor1, DefaultColor4, DefaultColor8} from "@openremote/core";+import manager, {DefaultColor1, DefaultColor4, DefaultColor8} from "@openremote/core";import i18next from "i18next";+import { ClientRole } from "@openremote/model/src";// TODO: Add webpack/rollup to build so consumers aren't forced to use the same toolingconst buttonStyle = require("!!raw-loader!@material/button/dist/mdc.button.css");@@ -350,7 +351,8 @@ export class OrInput extends LitElement {}protected render() {-+ const readonly = this.readonly || !manager.hasRole(ClientRole.WRITE_ASSETS)+if (this.type) {const showLabel = !this.fullWidth && this.label;@@ -366,14 +368,14 @@ export class OrInput extends LitElement {case InputType.SWITCH:return html`<span id="wrapper">- <div id="component" class="mdc-switch ${this.disabled || this.readonly ? "mdc-switch--disabled" : ""} ${this.value ? "mdc-switch--checked" : ""}">+ <div id="component" class="mdc-switch ${this.disabled || readonly ? "mdc-switch--disabled" : ""} ${this.value ? "mdc-switch--checked" : ""}"><div class="mdc-switch__track"></div><div class="mdc-switch__thumb-underlay"><div class="mdc-switch__thumb"><input type="checkbox" id="elem" class="mdc-switch__native-control"?checked="${this.value}"?required="${this.required}"- ?disabled="${this.disabled || this.readonly}"+ ?disabled="${this.disabled || readonly}"@change="${(e: Event) => this.onValueChange((e.target as HTMLInputElement), (e.target as HTMLInputElement).checked)}"role="switch"></div>@@ -421,7 +423,7 @@ export class OrInput extends LitElement {case InputType.SELECT:const classes = {"mdc-select--outlined": outlined,- "mdc-select--disabled": this.disabled,+ "mdc-select--disabled": this.disabled || readonly,"mdc-select--required": this.required,"mdc-select--dense": false, // this.dense,"mdc-select--no-label": !this.label,@@ -444,7 +446,9 @@ export class OrInput extends LitElement {this._selectedIndex = -1;return html`- <div id="component" class="mdc-select ${classMap(classes)}" @MDCSelect:change="${(e: MDCSelectEvent) => this.onValueChange(undefined, e.detail.index === -1 ? undefined : Array.isArray(this.options![e.detail.index]) ? this.options![e.detail.index][0] : this.options![e.detail.index])}">+ <div id="component"+ class="mdc-select ${classMap(classes)}"+ @MDCSelect:change="${(e: MDCSelectEvent) => this.onValueChange(undefined, e.detail.index === -1 ? undefined : Array.isArray(this.options![e.detail.index]) ? this.options![e.detail.index][0] : this.options![e.detail.index])}"><div id="menu-anchor" class="mdc-select__anchor select-class"><or-icon class="mdc-select__dropdown-icon" icon="menu-down"></or-icon><div id="elem" class="mdc-select__selected-text" role="button" aria-haspopup="listbox" aria-controls="component-helper-text" aria-describedby="component-helper-text" aria-labelledby="component-label component"></div>@@ -472,7 +476,7 @@ export class OrInput extends LitElement {case InputType.BUTTON_TOGGLE:return html`<button id="component" class="mdc-icon-button ${this.value ? "mdc-icon-button--on" : ""}"- ?readonly="${this.readonly}"+ ?readonly="${readonly}"?disabled="${this.disabled}"@MDCIconButtonToggle:change="${(evt: MDCIconButtonToggleEventDetail) => this.onValueChange(undefined, evt.isOn)}">${this.icon ? html`<or-icon class="mdc-icon-button__icon" aria-hidden="true" icon="${this.icon}"></or-icon>` : ``}@@ -497,7 +501,7 @@ export class OrInput extends LitElement {};return html`<button id="component" class="${classMap(classes)}"- ?readonly="${this.readonly}"+ ?readonly="${readonly}"?disabled="${this.disabled}"@onmousedown="${() => {if (isMomentary) this.onValueChange(undefined, true)}}" @onmouseup="${() => isMomentary ? this.onValueChange(undefined, false) : this.onValueChange(undefined, true)}">${!isIconButton ? html`<div class="mdc-button__ripple"></div>` : ``}@@ -514,7 +518,7 @@ export class OrInput extends LitElement {<input type="checkbox"?checked="${this.value}"?required="${this.required}"- ?disabled="${this.disabled || this.readonly}"+ ?disabled="${this.disabled || readonly}"@change="${(e: Event) => this.onValueChange((e.target as HTMLInputElement), (e.target as HTMLInputElement).checked)}"class="mdc-checkbox__native-control" id="elem"/><div class="mdc-checkbox__background">@@ -604,7 +608,7 @@ export class OrInput extends LitElement {${this.type === InputType.TEXTAREA || this.type === InputType.JSON ? html`<textarea id="elem" class="mdc-text-field__input"?required="${this.required}"- ?readonly="${this.readonly}"+ ?readonly="${readonly}"?disabled="${this.disabled}"@change="${(e: Event) => this.onValueChange((e.target as HTMLTextAreaElement), (e.target as HTMLTextAreaElement).value)}"minlength="${ifDefined(this.minLength)}"@@ -616,7 +620,7 @@ export class OrInput extends LitElement {` :html`<input type="${this.type}" id="elem" class="mdc-text-field__input"?required="${this.required}"- ?readonly="${this.readonly}"+ ?readonly="${readonly}"?disabled="${this.disabled}"@change="${(e: Event) => this.onValueChange((e.target as HTMLInputElement), (e.target as HTMLInputElement).value)}".value="${val !== null && val !== undefined ? val : ""}"@@ -635,7 +639,7 @@ export class OrInput extends LitElement {${hasHelper ? html`<div class="mdc-text-field-helper-line"><div class="mdc-text-field-helper-text ${classMap(helperClasses)}">${showValidationMessage ? this.validationMessage : this.helperText}</div>- ${this.charCounter && !this.readonly ? html`<div class="mdc-text-field-character-counter"></div>` : ``}+ ${this.charCounter && !readonly ? html`<div class="mdc-text-field-character-counter"></div>` : ``}</div>` : ``}`;diff --git a/ui/component/or-map/src/index.ts b/ui/component/or-map/src/index.tsindex ddf0046e7..442f77ff2 100644--- a/ui/component/or-map/src/index.ts+++ b/ui/component/or-map/src/index.ts@@ -126,31 +126,35 @@ export class OrMap extends LitElement {this.addEventListener(OrMapMarkerChangedEvent.NAME, this._onMarkerChangedEvent);}+ protected firstUpdated(_changedProperties: PropertyValues): void {+ super.firstUpdated(_changedProperties);+ manager.addListener(this.onManagerEvent);+ if (manager.ready) {+ this.loadMap();+ }+ }++ protected onManagerEvent = (event: OREvent) => {+ switch (event) {+ case OREvent.READY:+ if (!manager.ready) {+ this.loadMap();+ }+ break;+ case OREvent.DISPLAY_REALM_CHANGED:+ break;+ }+ }+public get markers(): OrMapMarker[] {return this._markers;}+public disconnectedCallback() {super.disconnectedCallback();this._observer!.disconnect();- }-- protected firstUpdated(_changedProperties: PropertyValues): void {- super.firstUpdated(_changedProperties);-- if (!manager.ready) {- // Defer until openremote is initialised- this._initCallback = (initEvent: OREvent) => {- if (initEvent === OREvent.READY) {- this.loadMap();-- manager.removeListener(this._initCallback!);- }- };- manager.addListener(this._initCallback);- } else {- this.loadMap();- }+ manager.removeListener(this.onManagerEvent);}public loadMap() {diff --git a/ui/component/or-map/src/or-map-asset-card.ts b/ui/component/or-map/src/or-map-asset-card.tsindex 9bfea4831..f3e557571 100644--- a/ui/component/or-map/src/or-map-asset-card.ts+++ b/ui/component/or-map/src/or-map-asset-card.ts@@ -29,22 +29,11 @@ import {mapAssetCardStyle} from "./style";orAttributeTemplateProvider.setTemplate((attribute:Attribute) => {let template;- const value = attribute.value ? attribute.value : "-";+ const value = Util.getAttributeValue(attribute, undefined)switch (attribute.type) {-- case AttributeValueType.BOOLEAN.name:- template = html`<or-translate value="${value}"></or-translate>`;- break;case AttributeValueType.SWITCH_TOGGLE.name:template = html`<or-translate value="${value ? "On" : "Off"}"></or-translate>`;break;- case AttributeValueType.NUMBER.name:- case AttributeValueType.SOUND.name:- template = attribute.value ? new Intl.NumberFormat().format(value) : "-";- break;- case AttributeValueType.STRING.name:- template = html`<or-translate value="${value}"></or-translate>`;- break;case AttributeValueType.OBJECT.name:case AttributeValueType.ARRAY.name:if(attribute.name === "status") {@@ -61,7 +50,7 @@ orAttributeTemplateProvider.setTemplate((attribute:Attribute) => {}break;default:- template = value;+ template = value ? value : "-";break;}return template;diff --git a/ui/component/or-rules/src/json-viewer/or-rule-then-otherwise.ts b/ui/component/or-rules/src/json-viewer/or-rule-then-otherwise.tsindex a3693e9bc..3b991efdb 100644--- a/ui/component/or-rules/src/json-viewer/or-rule-then-otherwise.ts+++ b/ui/component/or-rules/src/json-viewer/or-rule-then-otherwise.ts@@ -185,6 +185,7 @@ class OrRuleThenOtherwise extends translate(i18next)(LitElement) {@property({type: Object, attribute: false})public rule!: JsonRule;+ @property({type: Boolean})public readonly?: boolean;public config?: RulesConfig;@@ -307,13 +308,14 @@ class OrRuleThenOtherwise extends translate(i18next)(LitElement) {}protected render() {+ const thenAllowAdd = !this.readonly && (this.thenAllowAdd || this.rule.then!.length > 1);return html`<div><or-panel .heading="${i18next.t("then")}...">${this.ruleRecurrenceTemplate(this.rule.recurrence)}${!this.rule.then ? `` : this.rule.then.map((action: RuleActionUnion) => this.ruleActionTemplate(this.rule.then!, action))}- ${this.thenAllowAdd ? html`+ ${thenAllowAdd ? html`<span class="add-button-wrapper">${getContentWithMenuTemplate(html`<or-input class="plus-button" type="${InputType.BUTTON}" icon="plus"></or-input>`,diff --git a/ui/component/or-rules/src/json-viewer/or-rule-when.ts b/ui/component/or-rules/src/json-viewer/or-rule-when.tsindex 40db993cc..e611e64a1 100644--- a/ui/component/or-rules/src/json-viewer/or-rule-when.ts+++ b/ui/component/or-rules/src/json-viewer/or-rule-when.ts@@ -106,6 +106,7 @@ class OrRuleWhen extends translate(i18next)(LitElement) {@property({type: Object})public rule?: JsonRule;+ @property({type: Boolean})public readonly?: boolean;public config?: RulesConfig;@@ -176,7 +177,9 @@ class OrRuleWhen extends translate(i18next)(LitElement) {<div class="rule-group-item"><div class="rule-condition"><or-rule-condition .config="${this.config}" .assetDescriptors="${this.assetDescriptors}" .ruleCondition="${condition}" .readonly="${this.readonly}" ></or-rule-condition>- <button class="button-clear ${showRemoveCondition ? "" : "hidden"}" @click="${() => this.removeItem(condition, group, false)}"><or-icon icon="close-circle"></or-icon></input>+ ${showRemoveGroup ? html`+ <button class="button-clear ${showRemoveCondition ? "" : "hidden"}" @click="${() => this.removeItem(condition, group, false)}"><or-icon icon="close-circle"></or-icon></input>+ ` : ``}</div></div>`;