From 6622ca70316dd7af0ad5d4a8deaf1ff561723734 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Thu, 4 Jun 2015 12:26:57 -0700 Subject: [PATCH] [Info Bubble] Add common properties to metadata Expose common properties (like time updated) from the metadata capability, to appear in the Info bubble. WTD-884. --- .../src/capabilities/MetadataCapability.js | 74 ++++++++++++------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/platform/core/src/capabilities/MetadataCapability.js b/platform/core/src/capabilities/MetadataCapability.js index 680e8b9fb1..f4b9061b8c 100644 --- a/platform/core/src/capabilities/MetadataCapability.js +++ b/platform/core/src/capabilities/MetadataCapability.js @@ -1,7 +1,8 @@ /*global define*/ define( - function () { + ['moment'], + function (moment) { "use strict"; /** @@ -12,6 +13,8 @@ define( * 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. @@ -25,35 +28,56 @@ define( * @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() { + return [ + { + name: "Updated", + value: formatTimestamp(model.modified) + }, + { + 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: function () { - var type = domainObject.getCapability('type'), - model = domainObject.getModel(), - metadata = [{ - name: "ID", - value: domainObject.getId() - }]; - - function addProperty(typeProperty) { - var name = typeProperty.getDefinition().name, - value = typeProperty.getValue(model); - if (typeof value === 'string' || - typeof value === 'number') { - metadata.push({ - name: name, - value: value - }); - } - } - - (type ? type.getProperties() : []).forEach(addProperty); - - return metadata; - } + invoke: getMetadata }; }