mirror of
https://github.com/nasa/openmct.git
synced 2025-06-17 06:38:17 +00:00
Merge branch 'open-master' into open1241
Merge latest from master branch into topic branch for WTD-1241.
This commit is contained in:
92
platform/core/src/capabilities/MetadataCapability.js
Normal file
92
platform/core/src/capabilities/MetadataCapability.js
Normal file
@ -0,0 +1,92 @@
|
||||
/*global define*/
|
||||
|
||||
define(
|
||||
['moment'],
|
||||
function (moment) {
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* A piece of information about a domain object.
|
||||
* @typedef {Object} MetadataProperty
|
||||
* @property {string} name the human-readable name of this property
|
||||
* @property {string} value the human-readable value of this property,
|
||||
* for this specific domain object
|
||||
*/
|
||||
|
||||
var TIME_FORMAT = "YYYY-MM-DD HH:mm:ss";
|
||||
|
||||
/**
|
||||
* Implements the `metadata` capability of a domain object, providing
|
||||
* properties of that object for display.
|
||||
*
|
||||
* Usage: `domainObject.useCapability("metadata")`
|
||||
*
|
||||
* ...which will return an array of objects containing `name` and
|
||||
* `value` properties describing that domain object (suitable for
|
||||
* display.)
|
||||
*
|
||||
* @constructor
|
||||
*/
|
||||
function MetadataCapability(domainObject) {
|
||||
var model = domainObject.getModel();
|
||||
|
||||
function hasDisplayableValue(metadataProperty) {
|
||||
var t = typeof metadataProperty.value;
|
||||
return (t === 'string' || t === 'number');
|
||||
}
|
||||
|
||||
function formatTimestamp(timestamp) {
|
||||
return typeof timestamp === 'number' ?
|
||||
(moment.utc(timestamp).format(TIME_FORMAT) + " UTC") :
|
||||
undefined;
|
||||
}
|
||||
|
||||
function getProperties() {
|
||||
var type = domainObject.getCapability('type');
|
||||
|
||||
function lookupProperty(typeProperty) {
|
||||
return {
|
||||
name: typeProperty.getDefinition().name,
|
||||
value: typeProperty.getValue(model)
|
||||
};
|
||||
}
|
||||
|
||||
return (type ? type.getProperties() : []).map(lookupProperty);
|
||||
}
|
||||
|
||||
function getCommonMetadata() {
|
||||
var type = domainObject.getCapability('type');
|
||||
// Note that invalid values will be filtered out later
|
||||
return [
|
||||
{
|
||||
name: "Updated",
|
||||
value: formatTimestamp(model.modified)
|
||||
},
|
||||
{
|
||||
name: "Type",
|
||||
value: type && type.getName()
|
||||
},
|
||||
{
|
||||
name: "ID",
|
||||
value: domainObject.getId()
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
function getMetadata() {
|
||||
return getProperties().concat(getCommonMetadata())
|
||||
.filter(hasDisplayableValue);
|
||||
}
|
||||
|
||||
return {
|
||||
/**
|
||||
* Get metadata about this object.
|
||||
* @returns {MetadataProperty[]} metadata about this object
|
||||
*/
|
||||
invoke: getMetadata
|
||||
};
|
||||
}
|
||||
|
||||
return MetadataCapability;
|
||||
}
|
||||
);
|
@ -77,7 +77,8 @@ define(
|
||||
// Get the object's model and clone it, so the
|
||||
// mutator function has a temporary copy to work with.
|
||||
var model = domainObject.getModel(),
|
||||
clone = JSON.parse(JSON.stringify(model));
|
||||
clone = JSON.parse(JSON.stringify(model)),
|
||||
useTimestamp = arguments.length > 1;
|
||||
|
||||
// Function to handle copying values to the actual
|
||||
function handleMutation(mutationResult) {
|
||||
@ -94,8 +95,7 @@ define(
|
||||
if (model !== result) {
|
||||
copyValues(model, result);
|
||||
}
|
||||
model.modified = (typeof timestamp === 'number') ?
|
||||
timestamp : now();
|
||||
model.modified = useTimestamp ? timestamp : now();
|
||||
}
|
||||
|
||||
// Report the result of the mutation
|
||||
|
63
platform/core/src/services/Throttle.js
Normal file
63
platform/core/src/services/Throttle.js
Normal file
@ -0,0 +1,63 @@
|
||||
/*global define*/
|
||||
|
||||
define(
|
||||
[],
|
||||
function () {
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Throttler for function executions, registered as the `throttle`
|
||||
* service.
|
||||
*
|
||||
* Usage:
|
||||
*
|
||||
* throttle(fn, delay, [apply])
|
||||
*
|
||||
* Returns a function that, when invoked, will invoke `fn` after
|
||||
* `delay` milliseconds, only if no other invocations are pending.
|
||||
* The optional argument `apply` determines whether.
|
||||
*
|
||||
* The returned function will itself return a `Promise` which will
|
||||
* resolve to the returned value of `fn` whenever that is invoked.
|
||||
*
|
||||
* @returns {Function}
|
||||
*/
|
||||
function Throttle($timeout) {
|
||||
/**
|
||||
* Throttle this function.
|
||||
* @param {Function} fn the function to throttle
|
||||
* @param {number} [delay] the delay, in milliseconds, before
|
||||
* executing this function; defaults to 0.
|
||||
* @param {boolean} apply true if a `$apply` call should be
|
||||
* invoked after this function executes; defaults to
|
||||
* `false`.
|
||||
*/
|
||||
return function (fn, delay, apply) {
|
||||
var activeTimeout;
|
||||
|
||||
// Clear active timeout, so that next invocation starts
|
||||
// a new one.
|
||||
function clearActiveTimeout() {
|
||||
activeTimeout = undefined;
|
||||
}
|
||||
|
||||
// Defaults
|
||||
delay = delay || 0;
|
||||
apply = apply || false;
|
||||
|
||||
return function () {
|
||||
// Start a timeout if needed
|
||||
if (!activeTimeout) {
|
||||
activeTimeout = $timeout(fn, delay, apply);
|
||||
activeTimeout.then(clearActiveTimeout);
|
||||
}
|
||||
// Return whichever timeout is active (to get
|
||||
// a promise for the results of fn)
|
||||
return activeTimeout;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
return Throttle;
|
||||
}
|
||||
);
|
Reference in New Issue
Block a user