Skip to content

armgit / core

Public Imported from GitHub
0

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

2.9 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
 */
// Plain JS on purpose: the component-test bundle compiles .ts files against ui/test's tsconfig
// (rootDir ui/test), so TS component sources outside that dir fail the build.
import { html, LitElement } from "lit";
import { AssetModelUtil } from "@openremote/model";
import { getValueHolderInputTemplateProvider } from "@openremote/or-vaadin-components/value-input-provider";

/**
 * Renders the input for a ThingAsset attribute of `valueType` the way or-attribute-input does: the descriptors are
 * resolved from the asset model, the provider is built once per attribute and its template is re-rendered with the
 * current value. Every value the input reports is re-dispatched as a `value-change` event carrying it as the detail.
 */
export class ValueInputProviderHarness extends LitElement {
  static properties = {
    valueType: { type: String },
    value: {},
    constraints: { type: Array },
    format: { type: Object },
    multiline: { type: Boolean },
    options: { type: Object },
  };

  willUpdate(changed) {
    if (
      changed.has("valueType") ||
      changed.has("constraints") ||
      changed.has("format") ||
      changed.has("multiline") ||
      changed.has("options")
    ) {
      const meta = {};
      if (this.constraints) meta.constraints = this.constraints;
      if (this.format) meta.format = this.format;
      if (this.multiline) meta.multiline = true;
      const attribute = { name: "attribute", type: this.valueType, meta };
      const [attributeDescriptor, valueDescriptor] = AssetModelUtil.getAttributeAndValueDescriptors(
        "ThingAsset",
        attribute.name,
        attribute
      );
      this._provider = getValueHolderInputTemplateProvider(
        "ThingAsset",
        attribute,
        attributeDescriptor,
        valueDescriptor,
        (value) => this.dispatchEvent(new CustomEvent("value-change", { detail: value })),
        { label: "Attribute", ...this.options }
      );
    }
  }

  render() {
    return this._provider?.templateFunction?.(this.value, false, false, false, false, undefined) ?? html``;
  }
}
customElements.define("test-value-input-provider-harness", ValueInputProviderHarness);