[Time API] Provided 'AddFormat' method for registering new formats

This commit is contained in:
Henry
2017-04-30 23:21:13 -07:00
parent 31897ec520
commit 0793442518
8 changed files with 63 additions and 106 deletions

View File

@ -22,13 +22,9 @@
define([ define([
"./src/FormatProvider", "./src/FormatProvider",
"./src/UTCTimeFormat",
"./src/DurationFormat",
'legacyRegistry' 'legacyRegistry'
], function ( ], function (
FormatProvider, FormatProvider,
UTCTimeFormat,
DurationFormat,
legacyRegistry legacyRegistry
) { ) {
@ -46,22 +42,6 @@ define([
] ]
} }
], ],
"formats": [
{
"key": "utc",
"implementation": UTCTimeFormat
},
{
"key": "duration",
"implementation": DurationFormat
}
],
"constants": [
{
"key": "DEFAULT_TIME_FORMAT",
"value": "utc"
}
],
"licenses": [ "licenses": [
{ {
"name": "d3", "name": "d3",

View File

@ -30,19 +30,23 @@ define([
* An object used to convert between numeric values and text values, * An object used to convert between numeric values and text values,
* typically used to display these values to the user and to convert * typically used to display these values to the user and to convert
* user input to a numeric format, particularly for time formats. * user input to a numeric format, particularly for time formats.
* @interface {Format} * @interface Format
*/ */
/** /**
* Parse text (typically user input) to a numeric value. * Parse text (typically user input) to a numeric value.
* Behavior is undefined when the text cannot be parsed; * Behavior is undefined when the text cannot be parsed;
* `validate` should be called first if the text may be invalid. * `validate` should be called first if the text may be invalid.
* @method parse * @method Format#parse
* @memberof Format# * @memberof Format#
* @param {string} text the text to parse * @param {string} text the text to parse
* @returns {number} the parsed numeric value * @returns {number} the parsed numeric value
*/ */
/**
* @property {string} key A unique identifier for this formatter.
* @memberof Format#
*/
/** /**
* Determine whether or not some text (typically user input) can * Determine whether or not some text (typically user input) can
* be parsed to a numeric value by this format. * be parsed to a numeric value by this format.
@ -58,10 +62,12 @@ define([
* @method format * @method format
* @memberof Format# * @memberof Format#
* @param {number} value the numeric value to format * @param {number} value the numeric value to format
* @param {number} [threshold] Optionally provides context to the * @param {number} [minValue] Contextual information for scaled formatting used in linear scales such as conductor
* format request, allowing for scale-appropriate formatting. This value * and plot axes. Specifies the smallest number on the scale.
* should be the minimum unit to be represented by this format, in ms. For * @param {number} [maxValue] Contextual information for scaled formatting used in linear scales such as conductor
* example, to display seconds, a threshold of 1 * 1000 should be provided. * and plot axes. Specifies the largest number on the scale
* @param {number} [count] Contextual information for scaled formatting used in linear scales such as conductor
* and plot axes. The number of labels on the scale.
* @returns {string} the text representation of the value * @returns {string} the text representation of the value
*/ */

View File

@ -1,60 +0,0 @@
/*****************************************************************************
* Open MCT, Copyright (c) 2014-2017, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
define(
['../src/UTCTimeFormat', 'moment'],
function (UTCTimeFormat, moment) {
describe("The UTCTimeFormat", function () {
var format;
beforeEach(function () {
format = new UTCTimeFormat();
});
it("formats UTC timestamps", function () {
var timestamp = 12345670000,
formatted = format.format(timestamp);
expect(formatted).toEqual(jasmine.any(String));
expect(moment.utc(formatted).valueOf()).toEqual(timestamp);
});
it("displays with millisecond precision", function () {
var timestamp = 12345670789,
formatted = format.format(timestamp);
expect(moment.utc(formatted).valueOf()).toEqual(timestamp);
});
it("validates time inputs", function () {
expect(format.validate("1977-05-25 11:21:22")).toBe(true);
expect(format.validate("garbage text")).toBe(false);
});
it("parses valid input", function () {
var text = "1977-05-25 11:21:22",
parsed = format.parse(text);
expect(parsed).toEqual(jasmine.any(Number));
expect(parsed).toEqual(moment.utc(text).valueOf());
});
});
}
);

View File

@ -316,6 +316,17 @@ define([
return this.formatMapCache.get(metadata); return this.formatMapCache.get(metadata);
}; };
/**
* Register a new telemetry data formatter.
* @param {Format} format the
*/
TelemetryAPI.prototype.addFormat = function (format) {
this.MCT.legacyExtension('formats', {
key: format.key,
implementation: function () { return format }
});
};
/** /**
* Get a limit evaluator for this domain object. * Get a limit evaluator for this domain object.
* Limit Evaluators help you evaluate limit and alarm status of individual telemetry datums for display purposes without having to interact directly with the Limit API. * Limit Evaluators help you evaluate limit and alarm status of individual telemetry datums for display purposes without having to interact directly with the Limit API.

View File

@ -44,6 +44,7 @@ define([
* @memberof platform/commonUI/formats * @memberof platform/commonUI/formats
*/ */
function DurationFormat() { function DurationFormat() {
this.key = "duration"
} }
DurationFormat.prototype.format = function (value) { DurationFormat.prototype.format = function (value) {

View File

@ -1,5 +1,5 @@
/***************************************************************************** /*****************************************************************************
* Open MCT, Copyright (c) 2014-2017, United States Government * Open MCT, Copyright (c) 2014-2016, United States Government
* as represented by the Administrator of the National Aeronautics and Space * as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved. * Administration. All rights reserved.
* *
@ -49,6 +49,7 @@ define([
* @memberof platform/commonUI/formats * @memberof platform/commonUI/formats
*/ */
function UTCTimeFormat() { function UTCTimeFormat() {
this.key = "utc";
} }
/** /**
@ -64,7 +65,7 @@ define([
* *
* Licensed * Licensed
*/ */
return [ var format = [
[".SSS", function (m) { [".SSS", function (m) {
return m.milliseconds(); return m.milliseconds();
}], }],
@ -93,23 +94,30 @@ define([
].filter(function (row) { ].filter(function (row) {
return row[1](momentified); return row[1](momentified);
})[0][0]; })[0][0];
if (format !== undefined) {
return moment.utc(d).format(format);
}
} }
/** /**
* * @param {number} value The value to format.
* @param value * @param {number} [minValue] Contextual information for scaled formatting used in linear scales such as conductor
* @param {Scale} [scale] Optionally provides context to the * and plot axes. Specifies the smallest number on the scale.
* format request, allowing for scale-appropriate formatting. * @param {number} [maxValue] Contextual information for scaled formatting used in linear scales such as conductor
* @returns {string} the formatted date * and plot axes. Specifies the largest number on the scale
* @param {number} [count] Contextual information for scaled formatting used in linear scales such as conductor
* and plot axes. The number of labels on the scale.
* @returns {string} the formatted date(s). If multiple values were requested, then an array of
* formatted values will be returned. Where a value could not be formatted, `undefined` will be returned at its position
* in the array.
*/ */
UTCTimeFormat.prototype.format = function (value, scale) { UTCTimeFormat.prototype.format = function (value, minValue, maxValue, count) {
if (scale !== undefined) { if (arguments.length > 1) {
var scaledFormat = getScaledFormat(value, scale); return getScaledFormat(value);
if (scaledFormat) { } else {
return moment.utc(value).format(scaledFormat);
}
}
return moment.utc(value).format(DATE_FORMAT) + "Z"; return moment.utc(value).format(DATE_FORMAT) + "Z";
}
}; };
UTCTimeFormat.prototype.parse = function (text) { UTCTimeFormat.prototype.parse = function (text) {

View File

@ -22,10 +22,14 @@
define([ define([
"./UTCTimeSystem", "./UTCTimeSystem",
"./LocalClock" "./LocalClock",
"./UTCTimeFormat",
"./DurationFormat",
], function ( ], function (
UTCTimeSystem, UTCTimeSystem,
LocalClock LocalClock,
UTCTimeFormat,
DurationFormat
) { ) {
/** /**
* Install a time system that supports UTC times. It also installs a local * Install a time system that supports UTC times. It also installs a local
@ -36,6 +40,13 @@ define([
var timeSystem = new UTCTimeSystem(); var timeSystem = new UTCTimeSystem();
openmct.time.addTimeSystem(timeSystem); openmct.time.addTimeSystem(timeSystem);
openmct.time.addClock(new LocalClock(100)); openmct.time.addClock(new LocalClock(100));
openmct.telemetry.addFormat(new UTCTimeFormat());
openmct.telemetry.addFormat(new DurationFormat());
openmct.legacyExtension("constants", {
"key": "DEFAULT_TIME_FORMAT",
"value": "utc"
});
} }
}; };
}); });