diff --git a/.gitignore b/.gitignore index aaad9b3..aca5b1a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ node_modules dist coverage +.tmp *.log *.ini -*.log.* \ No newline at end of file +*.log.* +.tmp \ No newline at end of file diff --git a/grafana/common/constants.ts b/grafana/common/constants.ts deleted file mode 100644 index fbdd799..0000000 --- a/grafana/common/constants.ts +++ /dev/null @@ -1,2 +0,0 @@ -export const floorId = 'ab0b04b7-9811-42be-aa53-9fe693ff8e75' -export const token = '73852421-4688-4373-b39c-74bf1652faa7' diff --git a/grafana/common/index.ts b/grafana/common/index.ts index b3ee1cf..191f001 100644 --- a/grafana/common/index.ts +++ b/grafana/common/index.ts @@ -1,3 +1,2 @@ -export * from './constants'; export * from './colors'; export * from './get-series'; diff --git a/grafana/data-source/src/types.ts b/grafana/data-source/src/types.ts index a7f28b9..d7ed627 100644 --- a/grafana/data-source/src/types.ts +++ b/grafana/data-source/src/types.ts @@ -1,5 +1,5 @@ import { DataQuery, DataSourceJsonData } from '@grafana/data'; -import { floorId, token } from '@grafana-common'; +import { floorId, token } from '@bi-plugin-utils'; export interface MyQuery extends DataQuery { token: string; diff --git a/grafana/data-source/tsconfig.json b/grafana/data-source/tsconfig.json index 2d96d3a..6a29dff 100644 --- a/grafana/data-source/tsconfig.json +++ b/grafana/data-source/tsconfig.json @@ -5,6 +5,7 @@ "rootDir": "../..", "baseUrl": "../..", "paths": { + "@bi-plugin-utils": ["./utils"], "@grafana-common": ["./grafana/common/index.ts"], }, } diff --git a/grafana/floor-panel/src/components/FloorPanel.tsx b/grafana/floor-panel/src/components/FloorPanel.tsx index 3e18447..bcc02e8 100644 --- a/grafana/floor-panel/src/components/FloorPanel.tsx +++ b/grafana/floor-panel/src/components/FloorPanel.tsx @@ -2,7 +2,7 @@ import React from 'react' import { FloorPlanEngine } from '@archilogic/floor-plan-sdk' import { PanelProps } from '@grafana/data' -import { FloorPlan, getAssetsAndSpaces, getNodeByid, hexToRgb } from '@bi-plugin-utils' +import { FloorPlan, getAssetsAndSpaces, getNodeById, hexToRgb } from '@bi-plugin-utils' import { getGradients, getSeries } from '@grafana-common' import { FloorOptions } from '../types' @@ -33,7 +33,7 @@ export const FloorPanel: React.FC = props => { }) } function handleSpaceId() { - const node = getNodeByid(floorPlan, nodeId) + const node = getNodeById(floorPlan, nodeId) if (node) { node?.setHighlight({ fill: HIGHLIGHT_COLOR }) } diff --git a/grafana/floor-panel/src/module.ts b/grafana/floor-panel/src/module.ts index 3c4f7e2..b2433f5 100644 --- a/grafana/floor-panel/src/module.ts +++ b/grafana/floor-panel/src/module.ts @@ -1,39 +1,39 @@ -import { floorId, token } from '@grafana-common'; -import { PanelPlugin } from '@grafana/data'; -import { FloorOptions } from './types'; -import { FloorPanel } from './components/FloorPanel'; +import { floorId, token } from '@bi-plugin-utils' +import { PanelPlugin } from '@grafana/data' +import { FloorOptions } from './types' +import { FloorPanel } from './components/FloorPanel' -export const plugin = new PanelPlugin(FloorPanel).setPanelOptions((builder) => { +export const plugin = new PanelPlugin(FloorPanel).setPanelOptions(builder => { return builder .addTextInput({ path: 'id', name: 'Floor plan ID', description: 'Description of panel option', - defaultValue: floorId, + defaultValue: floorId }) .addTextInput({ path: 'token', name: 'Publishable Token', description: 'Description of panel option', - defaultValue: token, + defaultValue: token }) .addTextInput({ path: 'nodeId', name: 'Node id', description: 'Space or Asset id', - defaultValue: '', + defaultValue: '' }) .addColorPicker({ path: 'colorFrom', name: 'Color From', defaultValue: '#fff000', settings: { - mode: 'custom', - }, + mode: 'custom' + } }) .addColorPicker({ path: 'colorTo', name: 'Color To', - defaultValue: '#000fff', - }); -}); + defaultValue: '#000fff' + }) +}) diff --git a/package-lock.json b/package-lock.json index 75bdac9..e624ba0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "devDependencies": { "@types/react": "^18.0.27", "prettier": "2.5.1", + "process": "^0.11.10", "react": "^18.2.0", "react-dom": "^18.2.0", "typescript": "4.5.4" @@ -88,6 +89,15 @@ "node": ">=10.13.0" } }, + "node_modules/process": { + "version": "0.11.10", + "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", + "integrity": "sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==", + "dev": true, + "engines": { + "node": ">= 0.6.0" + } + }, "node_modules/react": { "version": "18.2.0", "resolved": "https://registry.npmjs.org/react/-/react-18.2.0.tgz", @@ -197,6 +207,12 @@ "integrity": "sha512-vBZcPRUR5MZJwoyi3ZoyQlc1rXeEck8KgeC9AwwOn+exuxLxq5toTRDTSaVrXHxelDMHy9zlicw8u66yxoSUFg==", "dev": true }, + "process": { + "version": "0.11.10", + "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", + "integrity": "sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==", + "dev": true + }, "react": { "version": "18.2.0", "resolved": "https://registry.npmjs.org/react/-/react-18.2.0.tgz", diff --git a/package.json b/package.json index 30deffb..418cc80 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ }, "devDependencies": { "@types/react": "^18.0.27", + "process": "^0.11.10", "prettier": "2.5.1", "react": "^18.2.0", "react-dom": "^18.2.0", diff --git a/utils/components/FloorPlan/FloorPlan.tsx b/utils/components/FloorPlan/FloorPlan.tsx index eb7bf88..198003d 100644 --- a/utils/components/FloorPlan/FloorPlan.tsx +++ b/utils/components/FloorPlan/FloorPlan.tsx @@ -1,4 +1,4 @@ -import React, { useEffect } from 'react' +import React from 'react' import { FloorPlanEngine } from '@archilogic/floor-plan-sdk' import './FloorPlan.css' @@ -31,7 +31,7 @@ const FloorPanel: React.FC = props => { } } } - useEffect(initFloorPlan) + setTimeout(initFloorPlan, 500) return (
diff --git a/utils/index.ts b/utils/index.ts index 1399887..1af2df9 100644 --- a/utils/index.ts +++ b/utils/index.ts @@ -1,3 +1,4 @@ export * from './components'; export * from './utils/colors'; -export * from './utils/nodes'; \ No newline at end of file +export * from './utils/nodes'; +export * from './utils/constants'; \ No newline at end of file diff --git a/utils/utils/colors.ts b/utils/utils/colors.ts index 296381f..76b2910 100644 --- a/utils/utils/colors.ts +++ b/utils/utils/colors.ts @@ -2,7 +2,7 @@ import colorGradient from 'javascript-color-gradient' export function valueToHex(color: number) { - var hex = color.toString(16) + const hex = color.toString(16) return hex } diff --git a/utils/utils/constants.ts b/utils/utils/constants.ts new file mode 100644 index 0000000..f00f50f --- /dev/null +++ b/utils/utils/constants.ts @@ -0,0 +1,2 @@ +export const floorId = 'e9c82007-ae74-4b95-9174-8df619a08bd8' +export const token = 'a983595e-94ee-4ecf-8cd0-1f5f1612fc96' diff --git a/utils/utils/nodes.ts b/utils/utils/nodes.ts index 561cb8b..94f05aa 100644 --- a/utils/utils/nodes.ts +++ b/utils/utils/nodes.ts @@ -1,11 +1,12 @@ import { FloorPlanEngine } from '@archilogic/floor-plan-sdk' export const getAssetsAndSpaces = (floorPlan: FloorPlanEngine) => { + if (!floorPlan) return [] const { spaces, assets } = floorPlan.resources return [...spaces, ...assets] } -export const getNodeByid = (floorPlan: FloorPlanEngine, id: string) => { +export const getNodeById = (floorPlan: FloorPlanEngine, id: string) => { const nodes = getAssetsAndSpaces(floorPlan) return nodes.find(node => node.id === id) } @@ -13,6 +14,12 @@ export const getNodeByid = (floorPlan: FloorPlanEngine, id: string) => { export const getNodeByClick = (floorPlan: FloorPlanEngine, evt: any) => { const { nodeId } = evt if (nodeId) { - return getNodeByid(floorPlan, nodeId) + return getNodeById(floorPlan, nodeId) } } + +export const getSpaceByPosition = (floorPlan: FloorPlanEngine, position: number[]) => { + const point2d = position + const positionResources = floorPlan.getResourcesFromPosition(point2d as [number, number]) + return positionResources.spaces ? positionResources.spaces[0] : false +}