From ccc5a455b5bf1d7d1b858f2bbe5f8d0bb2ba9ef7 Mon Sep 17 00:00:00 2001 From: GermanBluefox Date: Fri, 12 Jul 2024 12:21:35 +0900 Subject: [PATCH] Added "adapters" setting for attributes --- .../src/src/Attributes/Widget/SCHEMA.md | 1 + .../src/src/Attributes/Widget/WidgetField.tsx | 35 +++++++++++++++---- .../src/Vis/Widgets/Tabs/TabsSliderTabs.tsx | 2 +- .../src/src/Vis/visRxWidget.tsx | 5 ++- .../src/src/Vis/visWidgetsCatalog.tsx | 2 ++ packages/types-vis-2/index.d.ts | 2 ++ 6 files changed, 36 insertions(+), 11 deletions(-) diff --git a/packages/iobroker.vis-2/src/src/Attributes/Widget/SCHEMA.md b/packages/iobroker.vis-2/src/src/Attributes/Widget/SCHEMA.md index 4d6e6c5a..1ed0dde4 100644 --- a/packages/iobroker.vis-2/src/src/Attributes/Widget/SCHEMA.md +++ b/packages/iobroker.vis-2/src/src/Attributes/Widget/SCHEMA.md @@ -140,6 +140,7 @@ onChange: async (field, data, changeData, socket) => { - `type`: default value is '' and just an input field. It can have the following values: - `instance` - Instance selector. It could have additional settings: - `adapter` - [optional] Additionally, you can provide `adapter` to filter the instances of specific adapter. With special adapter name `_dataSources` you can get all adapters with flag `common.getHistory`. + - `adapters` - [optional] Additionally, you can provide `adapters` to filter the instances of specific adapters. It is an array of adapter names. - `isShort` - [optional] In this case only instance number (like `0`) is shown and not `history.0`. It can be set to true only with non-empty `adapter` setting. - `number` - Number input. It could have additional settings: - `min` - [optional] minimum value diff --git a/packages/iobroker.vis-2/src/src/Attributes/Widget/WidgetField.tsx b/packages/iobroker.vis-2/src/src/Attributes/Widget/WidgetField.tsx index 08d87808..4bd96da2 100644 --- a/packages/iobroker.vis-2/src/src/Attributes/Widget/WidgetField.tsx +++ b/packages/iobroker.vis-2/src/src/Attributes/Widget/WidgetField.tsx @@ -443,18 +443,39 @@ const WidgetField = (props: WidgetFieldProps) => { idShort: obj._id.split('.').pop(), name: obj.common.name, icon: obj.common.icon, - })); + })) + .sort((a, b) => + (a.name > b.name ? 1 : (a.name < b.name ? -1 : 0))); + setInstances(inst); + }); + } else if (Array.isArray(field.adapters)) { + props.socket.getAdapterInstances('') + .then(_instances => { + const inst = _instances + .filter(obj => field.adapters.includes(obj.common.name)) + .map(obj => ({ + id: obj._id.replace('system.adapter.', ''), + idShort: obj._id.split('.').pop(), + name: obj.common.name, + icon: obj.common.icon, + })) + .sort((a, b) => + (a.name > b.name ? 1 : (a.name < b.name ? -1 : 0))); setInstances(inst); }); } else { props.socket.getAdapterInstances(field.adapter || '') .then(_instances => { - const inst = _instances.map(obj => ({ - id: obj._id.replace('system.adapter.', ''), - idShort: obj._id.split('.').pop(), - name: obj.common.name, - icon: obj.common.icon, - })); + const inst = _instances + .map(obj => ({ + id: obj._id.replace('system.adapter.', ''), + idShort: obj._id.split('.').pop(), + name: obj.common.name, + icon: obj.common.icon, + })) + .sort((a, b) => + (a.name > b.name ? 1 : (a.name < b.name ? -1 : 0))); + setInstances(inst); }); } diff --git a/packages/iobroker.vis-2/src/src/Vis/Widgets/Tabs/TabsSliderTabs.tsx b/packages/iobroker.vis-2/src/src/Vis/Widgets/Tabs/TabsSliderTabs.tsx index 5654ef84..e51ca621 100644 --- a/packages/iobroker.vis-2/src/src/Vis/Widgets/Tabs/TabsSliderTabs.tsx +++ b/packages/iobroker.vis-2/src/src/Vis/Widgets/Tabs/TabsSliderTabs.tsx @@ -18,9 +18,9 @@ import React from 'react'; import { Tab, Tabs } from '@mui/material'; import { Icon } from '@iobroker/adapter-react-v5'; +import type { RxRenderWidgetProps } from '@iobroker/types-vis-2'; // eslint-disable-next-line import/no-cycle import VisRxWidget, { type VisRxWidgetState } from '../../visRxWidget'; -import type {RxRenderWidgetProps} from "@iobroker/types-vis-2"; interface RxData { show_tabs: number; diff --git a/packages/iobroker.vis-2/src/src/Vis/visRxWidget.tsx b/packages/iobroker.vis-2/src/src/Vis/visRxWidget.tsx index 99fa62e2..9ed8fb0c 100644 --- a/packages/iobroker.vis-2/src/src/Vis/visRxWidget.tsx +++ b/packages/iobroker.vis-2/src/src/Vis/visRxWidget.tsx @@ -13,8 +13,7 @@ * (Free for non-commercial use). */ -import type { Component } from 'react'; -import React from 'react'; +import React, { type Component } from 'react'; import { Card, CardContent, @@ -22,7 +21,7 @@ import { import { type LegacyConnection, I18n, Icon } from '@iobroker/adapter-react-v5'; -import { +import type { Project, AnyWidgetId, RxWidgetInfo, diff --git a/packages/iobroker.vis-2/src/src/Vis/visWidgetsCatalog.tsx b/packages/iobroker.vis-2/src/src/Vis/visWidgetsCatalog.tsx index 3c7314a5..fe19fbbc 100644 --- a/packages/iobroker.vis-2/src/src/Vis/visWidgetsCatalog.tsx +++ b/packages/iobroker.vis-2/src/src/Vis/visWidgetsCatalog.tsx @@ -77,6 +77,8 @@ export type RxWidgetInfoAttributesFieldAll = { filter?: ObjectBrowserCustomFilter | ObjectBrowserType | ((data: WidgetData, index: number) => Record) | string; /** Additionally, you can provide `adapter` to filter the instances of specific adapter. With special adapter name `_dataSources` you can get all adapters with flag `common.getHistory`. */ readonly adapter?: string; + /** Additionally, you can provide `adapters` to filter the instances of specific adapters. */ + readonly adapters?: string[]; /** In this case, only instance number (like `0`) is shown and not `history.0`. It can be set to true only with non-empty `adapter` setting. */ readonly iShort?: boolean; /** Options for a select type */ diff --git a/packages/types-vis-2/index.d.ts b/packages/types-vis-2/index.d.ts index 2d9d1109..6f8f22c7 100644 --- a/packages/types-vis-2/index.d.ts +++ b/packages/types-vis-2/index.d.ts @@ -197,6 +197,8 @@ export type RxWidgetInfoAttributesFieldInstance = { readonly default?: string; /** Additionally, you can provide `adapter` to filter the instances of specific adapter. With special adapter name `_dataSources` you can get all adapters with flag `common.getHistory`. */ readonly adapter?: string; + /** Additionally, you can provide `adapters` to filter the instances of specific adapters. */ + readonly adapters?: string; /** In this case, only instance number (like `0`) is shown and not `history.0`. It can be set to true only with non-empty `adapter` setting. */ readonly iShort?: boolean;