Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ui/app/shared/locales/cs/or.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
"filterInvite": "Filtrujte alespoň 3 znaky",
"location": "Umístění",
"longPressSetLoc": "Dlouhým stiskem nastavíte umístění",
"this": "Aktuální",
"current": "Aktuální",
"last": "Poslední",
"next": "Další",
"60minutes": "60 minut",
Expand Down
2 changes: 1 addition & 1 deletion ui/app/shared/locales/en/or.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
"filterInvite": "Filter with at least 3 characters",
"location": "Location",
"longPressSetLoc": "Long press to set location",
"this": "Current",
"current": "Current",
"last": "Last",
"next": "Next",
"60minutes": "60 minutes",
Expand Down
2 changes: 1 addition & 1 deletion ui/app/shared/locales/nl/or.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
"filterInvite": "Filter met minsters 3 karakters",
"location": "Locatie",
"longPressSetLoc": "Druk lang op de kaart om locatie in te stellen",
"this": "Huidige",
"current": "Huidige",
"last": "Afgelopen",
"next": "Aankomende",
"60minutes": "60 minuten",
Expand Down
2 changes: 1 addition & 1 deletion ui/app/shared/locales/pl/or.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
"filterInvite": "Filtruj używając co najmniej 3 znaków",
"location": "Lokalizacja",
"longPressSetLoc": "Przytrzymaj, aby ustawić lokalizację",
"this": "Bieżący",
"current": "Bieżący",
"last": "Ostatni",
"next": "Następny",
"60minutes": "60 minut",
Expand Down
2 changes: 1 addition & 1 deletion ui/app/shared/locales/uk/or.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
"filterInvite": "Фільтр за 3 і більше символами",
"location": "Місце розташування",
"longPressSetLoc": "Довге натискання, щоб встановити місце",
"this": "Поточний",
"current": "Поточний",
"last": "Останній",
"next": "Наступний",
"60minutes": "60 хвилин",
Expand Down
5 changes: 5 additions & 0 deletions ui/component/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ tasks.register('prepareUi') {
tasks.register('publishUi') {
dependsOn clean, npmPublish
}

tasks.register('npmTest', Exec) {
dependsOn getYarnInstallTask()
commandLine npmCommand('yarn'), 'run', 'test'
}
2 changes: 1 addition & 1 deletion ui/component/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"scripts": {
"build": "npx tsc -b",
"test": "echo \"No tests\" && exit 0",
"test": "npx tsc -b && npx playwright test",
"prepack": "npx rspack"
},
"author": "OpenRemote",
Expand Down
21 changes: 21 additions & 0 deletions ui/component/core/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 { defineCtConfig } from "@openremote/test/component.config";

export default defineCtConfig(__dirname);
1 change: 1 addition & 0 deletions ui/component/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export * from "./asset-mixin";
export * from "./console";
export * from "./event";
export * from "./defaults";
export * from "./time-window";

export const DEFAULT_ICONSET = "mdi";
export const OPENREMOTE_CLIENT_ID = "openremote";
Expand Down
131 changes: 131 additions & 0 deletions ui/component/core/src/time-window.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* 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 moment from "moment";

/** The unit and the number of units of a time window, such as `["hours", 24]`. */
export type TimeWindow = [moment.unitOfTime.DurationConstructor, number];

/** Where a time window is relative to now, such as the `last` in `last 6 hours`. */
export const TimePrefix = {
CURRENT: "current",
LAST: "last",
NEXT: "next",
} as const;

export type TimePrefix = (typeof TimePrefix)[keyof typeof TimePrefix];

/** All time prefixes, for a chart to offer. */
export const TIME_PREFIXES: readonly TimePrefix[] = Object.values(TimePrefix);

/** The way a time window moves, towards the `last` or the `next` window. */
export type TimeDirection = Exclude<TimePrefix, typeof TimePrefix.CURRENT>;

let warnedAboutFormerPrefix = false;

/**
* Returns a time prefix under its current name, as stored widgets and projects can still use `this` for `current`.
*/
export function normalizeTimePrefix(prefix: string): string {
if (prefix !== "this") {
return prefix;
}
if (!warnedAboutFormerPrefix) {
warnedAboutFormerPrefix = true;
console.warn('The time prefix "this" is deprecated, use "current" instead.');
}
return TimePrefix.CURRENT;
}

/**
* Returns a key that combines a time prefix and a time window, like `thisDay`, with the prefix under its current name.
*/
export function normalizeTimePresetKey(key: string): string {
return key.replace(/^this(?=[A-Z0-9])/u, "current");
}

/**
* Returns the start and end of a time window at the current time, in the time zone of the browser.
*
* A window of one unit, like `Day`, is a calendar unit: `current` is the one that holds now, `last` the one before it
* and `next` the one after it.
*
* A window of several units, like `24Hours`, is put around now: `current` has now in the middle, `last` ends at now and
* `next` starts at now.
*
* @param wholeUnits Puts a window of several units on whole units instead of on now, or on whole 5 minute bars for
* minutes, for a chart that has to fit whole bars in it. A `last` window then ends with the current unit.
*/
export function getTimeWindowDates(prefix: string, [unit, value]: TimeWindow, wholeUnits = false): [Date, Date] {
const startOfUnit = moment().startOf(unit);
const timePrefix = normalizeTimePrefix(prefix);

if (value === 1) {
switch (timePrefix) {
case TimePrefix.CURRENT:
return [startOfUnit.toDate(), moment().endOf(unit).toDate()];
case TimePrefix.LAST:
return [startOfUnit.clone().subtract(1, unit).toDate(), startOfUnit.toDate()];
case TimePrefix.NEXT:
return [startOfUnit.clone().add(1, unit).toDate(), startOfUnit.clone().add(1, unit).endOf(unit).toDate()];
}
} else {
const step = unit === "minutes" ? 5 : 1;
const anchor = wholeUnits ? startOfUnit.clone().subtract(startOfUnit.get(unit) % step, unit) : moment();

switch (timePrefix) {
case TimePrefix.CURRENT: {
const start = anchor.clone().subtract(Math.floor(value / 2), unit);
return [start.toDate(), start.clone().add(value, unit).toDate()];
}
case TimePrefix.LAST: {
if (wholeUnits) {
const end = anchor.clone().add(step, unit);
return [end.clone().subtract(value, unit).toDate(), end.toDate()];
}
return [moment().subtract(value, unit).startOf(unit).toDate(), anchor.toDate()];
}
case TimePrefix.NEXT:
return [anchor.toDate(), anchor.clone().add(value, unit).toDate()];
}
}

console.error(`Could not get the time window dates, as the time prefix "${prefix}" is not supported.`);
return [moment().toDate(), moment().toDate()];
}

/**
* Returns the start and end of a time window moved back or forward by its own length. Pass no time window for a custom
* timeframe, which moves by the time between its start and end.
*/
export function shiftTimeWindowDates(
start: Date,
end: Date,
[unit, value]: TimeWindow | undefined = ["milliseconds", end.getTime() - start.getTime()],
direction: TimeDirection
): [Date, Date] {
const amount = direction === TimePrefix.LAST ? -value : value;
const newEnd = moment(end);
const endsWithUnit = newEnd.isSame(newEnd.clone().endOf(unit));
newEnd.add(amount, unit);
// Adding months clamps the day, so a window that ends with its unit has to be extended again.
if (endsWithUnit) {
newEnd.endOf(unit);
}
return [moment(start).add(amount, unit).toDate(), newEnd.toDate()];
}
Loading
Loading