Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

2.7 KB
Raw Blame
/*
 * Copyright 2026, OpenRemote Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
import type { DashboardWidget } from "@openremote/model";
import { widgetTypes } from "../index";
import type { WidgetConfig } from "../util/widget-config";
import { Util } from "@openremote/core";
import type { WidgetManifest } from "../util/or-widget";

export class WidgetService {
  public static getManifest(widgetTypeId: string) {
    const manifest = widgetTypes.get(widgetTypeId);
    if (!manifest) {
      throw new Error(`Widget manifest could not be found during widget creation. [ID=${widgetTypeId}]`);
    }
    return manifest;
  }

  public static async placeNew(widgetTypeId: string, x: number, y: number): Promise<DashboardWidget> {
    const randomId = Util.generateUniqueUUID();
    const manifest = this.getManifest(widgetTypeId);
    return {
      id: randomId,
      displayName: manifest.displayName,
      gridItem: {
        id: randomId,
        x,
        y,
        w: 2,
        h: 2,
        minW: manifest.minColumnWidth,
        minH: manifest.minColumnHeight,
        minPixelW: manifest.minPixelWidth,
        minPixelH: manifest.minPixelHeight,
        noResize: false,
        noMove: false,
        locked: false,
      }, // Left empty until it is generated by or-dashboard-preview
      widgetConfig: manifest.getDefaultConfig(),
      widgetTypeId,
    } as DashboardWidget;
  }

  // Method used to correct the OrWidgetConfig specification
  // So, if certain fields are removed or invalid, it will be corrected by merging the object with the default OrWidgetConfig.
  // Will only return a different object if it actually changes. This prevents child objects being returned in a different order.
  public static correctToConfigSpec(manifest: WidgetManifest, widgetConfig: WidgetConfig) {
    const newConfig = Util.mergeObjects(manifest.getDefaultConfig(), widgetConfig, false) as WidgetConfig;
    if (Util.objectsEqual(newConfig, widgetConfig)) {
      return widgetConfig;
    } else {
      return newConfig;
    }
  }
}