@gpa-gemstone/common-pages
Version:
Common UI pages for GPA products
229 lines (228 loc) • 16.5 kB
JavaScript
"use strict";
//******************************************************************************************************
// TimeFilter.tsx - Gbtc
//
// Copyright © 2020, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 09/16/2021 - Christoph Lackner
// Generated original version of source code.
// 06/20/2024 - Ali Karrar
// Moved TimeFilter from SEBrowser to gemstone
//******************************************************************************************************
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTimeWindowFromFilter = getTimeWindowFromFilter;
var React = __importStar(require("react"));
var react_forms_1 = require("@gpa-gemstone/react-forms");
var TimeWindowUtils_1 = require("./TimeWindowUtils");
var moment_1 = __importDefault(require("moment"));
var QuickSelects_1 = require("./QuickSelects");
var lodash_1 = __importDefault(require("lodash"));
var TimeFilter = function (props) {
var _a, _b, _c, _d;
var format = (0, QuickSelects_1.getFormat)(props.format);
var QuickSelects = React.useMemo(function () { return QuickSelects_1.AvailableQuickSelects.filter(function (qs) { return !qs.hideQuickPick(props.format); }); }, [props.format]);
var _e = React.useState(-1), activeQP = _e[0], setActiveQP = _e[1];
var _f = React.useState(getTimeWindowFromFilter(props.filter, format)), filter = _f[0], setFilter = _f[1];
// Checks typing of ITimeFilter and then compares to ITimeWindow
function isEqual(timeWindow, timeFilter) {
var flt = getTimeWindowFromFilter(timeFilter, format);
return lodash_1.default.isEqual(timeWindow, flt);
}
React.useEffect(function () {
if (!isEqual(filter, props.filter)) {
props.setFilter(filter.start, filter.end, filter.unit, filter.duration);
}
}, [filter]);
React.useEffect(function () {
if (!isEqual(filter, props.filter)) {
var flt = getTimeWindowFromFilter(props.filter, format);
setFilter(flt);
}
}, [props.filter]);
return (React.createElement("fieldset", { className: "border", style: { padding: '10px', height: '100%', overflow: 'hidden' } },
React.createElement("legend", { className: "w-auto", style: { fontSize: 'large' } }, "Date/Time Filter:"),
React.createElement(Row, { addRow: props.isHorizontal, class: 'm-0' },
props.dateTimeSetting === 'startWindow' || props.dateTimeSetting === 'startEnd' ?
React.createElement(Row, { addRow: !props.isHorizontal, class: 'm-0' },
React.createElement("div", { className: props.isHorizontal ? (props.showQuickSelect ? props.dateTimeSetting === 'startEnd' ? 'col-2' : 'col-4' : 'col-6') : 'col-12 p-0' },
React.createElement(react_forms_1.DatePicker, { Record: filter, Field: "start", Help: ((_a = props.showHelpMessage) !== null && _a !== void 0 ? _a : true) ? "All times shown are in system time (".concat(props.timeZone, ").") : undefined, Setter: function (r) {
var flt = props.dateTimeSetting === 'startWindow' ?
getTimeWindowFromFilter({ start: r.start, duration: r.duration, unit: r.unit }, format) :
getTimeWindowFromFilter({ start: r.start, end: r.end }, format);
setFilter(flt);
setActiveQP(-1);
}, Label: 'Start', Type: (_b = props.format) !== null && _b !== void 0 ? _b : 'datetime-local', Valid: function () { return true; }, Format: format, Accuracy: props.accuracy }),
props.showQuickSelect && props.dateTimeSetting === 'startWindow' ?
React.createElement(StartWindowForm, { IsHorizontal: props.isHorizontal, Filter: filter, SetFilter: setFilter, SetActiveQP: setActiveQP, Format: format, ShowQuickSelect: props.showQuickSelect })
: null))
: null,
props.dateTimeSetting === 'endWindow' || props.dateTimeSetting === 'startEnd' ?
React.createElement(Row, { addRow: !props.isHorizontal, class: 'm-0' },
React.createElement("div", { className: props.isHorizontal ? (props.showQuickSelect ? props.dateTimeSetting === 'startEnd' ? 'col-2' : 'col-4' : 'col-6') : 'col-12 p-0' },
React.createElement(react_forms_1.DatePicker, { Record: filter, Field: "end", Help: ((_c = props.showHelpMessage) !== null && _c !== void 0 ? _c : true) ? "All times shown are in system time (".concat(props.timeZone, ").") : undefined, Setter: function (r) {
var flt = props.dateTimeSetting === 'endWindow' ?
getTimeWindowFromFilter({ end: r.end, duration: r.duration, unit: r.unit }, format) :
getTimeWindowFromFilter({ start: r.start, end: r.end }, format);
setFilter(flt);
setActiveQP(-1);
}, Label: 'End', Type: (_d = props.format) !== null && _d !== void 0 ? _d : 'datetime-local', Valid: function () { return true; }, Format: format, Accuracy: props.accuracy }),
props.showQuickSelect && props.dateTimeSetting === 'endWindow' ?
React.createElement(EndWindowForm, { IsHorizontal: props.isHorizontal, Filter: filter, SetFilter: setFilter, SetActiveQP: setActiveQP, Format: format, ShowQuickSelect: props.showQuickSelect })
: null))
: null,
props.dateTimeSetting === 'startWindow' && !props.showQuickSelect ?
React.createElement(StartWindowForm, { IsHorizontal: props.isHorizontal, Filter: filter, SetFilter: setFilter, SetActiveQP: setActiveQP, Format: format, ShowQuickSelect: props.showQuickSelect })
: null,
props.dateTimeSetting === 'endWindow' && !props.showQuickSelect ?
React.createElement(EndWindowForm, { IsHorizontal: props.isHorizontal, Filter: filter, SetFilter: setFilter, SetActiveQP: setActiveQP, Format: format, ShowQuickSelect: props.showQuickSelect })
: null,
props.showQuickSelect ?
React.createElement("div", { className: props.isHorizontal ? "col-8 ".concat(props.dateTimeSetting !== 'startEnd' ? 'pt-3' : '') : 'row m-0 flex-grow-1' },
React.createElement(Row, { addRow: props.isHorizontal, class: "m-0 justify-content-center align-items-center" }, QuickSelects.map(function (qs, i) {
if (i % 3 !== 0)
return null;
return (React.createElement("div", { key: i, className: props.isHorizontal && props.dateTimeSetting === 'startEnd' ? 'col-2' : "col-4", style: {
paddingLeft: (props.isHorizontal && props.dateTimeSetting === 'startEnd' ? 0 : (i % 9 == 0 ? 15 : 0)),
paddingRight: (props.isHorizontal && props.dateTimeSetting === 'startEnd' ? 2 : ((i % 18 == 6 || i % 18 == 15) ? 15 : 2)),
marginTop: 10
} },
React.createElement("ul", { className: "list-group", key: i },
React.createElement("li", { key: i, style: { cursor: 'pointer' }, onClick: function () {
var flt = getTimeWindowFromFilter(QuickSelects[i].createFilter(props.timeZone, props.format), format);
props.setFilter(flt.start, flt.end, flt.unit, flt.duration);
setActiveQP(i);
}, className: "item badge badge-" + (i == activeQP ? "primary" : "secondary") }, QuickSelects[i].label),
i + 1 < QuickSelects.length ?
React.createElement("li", { key: i + 1, style: { marginTop: 3, cursor: 'pointer' }, className: "item badge badge-" + (i + 1 == activeQP ? "primary" : "secondary"), onClick: function () {
var flt = getTimeWindowFromFilter(QuickSelects[i + 1].createFilter(props.timeZone, props.format), format);
props.setFilter(flt.start, flt.end, flt.unit, flt.duration);
setActiveQP(i + 1);
} }, QuickSelects[i + 1].label)
: null,
i + 2 < QuickSelects.length ?
React.createElement("li", { key: i + 2, style: { marginTop: 3, cursor: 'pointer' }, className: "item badge badge-" + (i + 2 == activeQP ? "primary" : "secondary"), onClick: function () {
var flt = getTimeWindowFromFilter(QuickSelects[i + 2].createFilter(props.timeZone, props.format), format);
props.setFilter(flt.start, flt.end, flt.unit, flt.duration);
setActiveQP(i + 2);
} }, QuickSelects[i + 2].label)
: null)));
})))
: null)));
};
var StartWindowForm = function (props) {
return (React.createElement(Row, { addRow: !props.IsHorizontal, class: 'm-0' },
React.createElement("div", { className: props.IsHorizontal ? props.ShowQuickSelect ? 'col-12 p-0' : 'col-6' : 'col-12 p-0' },
React.createElement("div", { className: 'form-group' },
React.createElement("label", { style: { width: '100%', position: 'relative', float: "left" } }, "Span(+)"),
React.createElement("div", { className: 'row' },
React.createElement("div", { className: 'col-6' },
React.createElement(react_forms_1.Input, { Record: props.Filter, Field: 'duration', Label: '', Valid: function () { return true; }, Type: 'number', Setter: function (r) {
props.SetFilter(getTimeWindowFromFilter({ start: r.start, duration: r.duration, unit: r.unit }, props.Format));
props.SetActiveQP(-1);
} })),
React.createElement("div", { className: 'col-6' },
React.createElement(react_forms_1.Select, { Record: props.Filter, Label: '', Field: 'unit', Options: TimeWindowUtils_1.units.map(function (unit) { return ({ Value: unit, Label: (0, TimeWindowUtils_1.readableUnit)(unit) }); }), Setter: function (r) {
props.SetFilter(getTimeWindowFromFilter({ start: r.start, duration: r.duration, unit: r.unit }, props.Format));
props.SetActiveQP(-1);
} })))))));
};
var EndWindowForm = function (props) {
return (React.createElement(Row, { addRow: !props.IsHorizontal, class: 'm-0' },
React.createElement("div", { className: props.IsHorizontal ? props.ShowQuickSelect ? 'col-12 p-0' : 'col-6' : 'col-12 p-0' },
React.createElement("div", { className: 'form-group' },
React.createElement("label", { style: { width: '100%', position: 'relative', float: "left" } }, "Span(-)"),
React.createElement("div", { className: 'row' },
React.createElement("div", { className: 'col-6' },
React.createElement(react_forms_1.Input, { Record: props.Filter, Field: 'duration', Label: '', Valid: function () { return true; }, Type: 'number', Setter: function (r) {
props.SetFilter(getTimeWindowFromFilter({ end: r.end, duration: r.duration, unit: r.unit }, props.Format));
props.SetActiveQP(-1);
} })),
React.createElement("div", { className: 'col-6' },
React.createElement(react_forms_1.Select, { Record: props.Filter, Label: '', Field: 'unit', Options: TimeWindowUtils_1.units.map(function (unit) { return ({ Value: unit, Label: (0, TimeWindowUtils_1.readableUnit)(unit) }); }), Setter: function (r) {
props.SetFilter(getTimeWindowFromFilter({ end: r.end, duration: r.duration, unit: r.unit }, props.Format));
props.SetActiveQP(-1);
} })))))));
};
// Returns a row div element with props as children of row
function Row(props) {
var _a;
if (props.addRow) {
return React.createElement("div", { className: "row ".concat((_a = props.class) !== null && _a !== void 0 ? _a : '') }, props.children);
}
return React.createElement(React.Fragment, null, props.children);
}
// Converts ITimeFilter to an ITimeWindow filter
function getTimeWindowFromFilter(flt, format) {
var start;
var end;
var unit;
var duration;
var formatFunction = function (start, end, unit, duration) { return ({
start: start.format(format),
end: end.format(format),
unit: unit,
duration: duration,
}); };
if ('start' in flt && 'duration' in flt) { // type is IStartDuration
start = (0, moment_1.default)(flt.start, format);
duration = flt.duration;
unit = flt.unit;
end = (0, TimeWindowUtils_1.addDuration)(start, duration, unit);
return formatFunction(start, end, unit, duration);
}
if ('end' in flt && 'duration' in flt) { // type is IEndDuration
end = (0, moment_1.default)(flt.end, format);
duration = flt.duration;
unit = flt.unit;
start = (0, TimeWindowUtils_1.addDuration)(end, -duration, unit);
return formatFunction(start, end, unit, duration);
}
if ('start' in flt && 'end' in flt) { // type is IStartEnd
start = (0, moment_1.default)(flt.start, format);
end = (0, moment_1.default)(flt.end, format);
unit = (0, TimeWindowUtils_1.findAppropriateUnit)(start, end);
duration = end.diff(start, unit);
return formatFunction(start, end, unit, duration);
}
throw TypeError("Unexpected type in getTimeWindowFromFilter, filter is: ".concat(flt.toString()));
}
exports.default = TimeFilter;