From 67b9af54b3e4bb37d806f7494d61b3d2e1ec21e6 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Fri, 5 Dec 2014 09:43:22 -0800 Subject: [PATCH] [Layout] Add notion of representers Add representers as a category of extension; these are extra steps to perform when representing a domain object. This will be used to support automatic mutation/persistence of domain objects from a watch, while avoiding bloat within the mct-representation directive itself. This, in turn, simplifies the persistence strategy to be employed by Layout views when editing concludes. WTD-535. --- platform/representation/bundle.json | 8 +++- .../representation/src/MCTRepresentation.js | 31 +++++++++------- .../src/gestures/GestureRepresenter.js | 37 +++++++++++++++++++ 3 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 platform/representation/src/gestures/GestureRepresenter.js diff --git a/platform/representation/bundle.json b/platform/representation/bundle.json index 548f287656..96a9459cfc 100644 --- a/platform/representation/bundle.json +++ b/platform/representation/bundle.json @@ -9,7 +9,7 @@ { "key": "mctRepresentation", "implementation": "MCTRepresentation.js", - "depends": [ "representations[]", "views[]", "gestureService", "$q", "$log" ] + "depends": [ "representations[]", "views[]", "representers[]", "$q", "$log" ] } ], "gestures": [ @@ -36,6 +36,12 @@ "implementation": "gestures/GestureProvider.js", "depends": ["gestures[]"] } + ], + "representers": [ + { + "implementation": "gestures/GestureRepresenter.js", + "depends": [ "gestureService" ] + } ] } } \ No newline at end of file diff --git a/platform/representation/src/MCTRepresentation.js b/platform/representation/src/MCTRepresentation.js index db3ca467cf..aa6f6bd6ee 100644 --- a/platform/representation/src/MCTRepresentation.js +++ b/platform/representation/src/MCTRepresentation.js @@ -31,7 +31,7 @@ define( * representation extensions * @param {ViewDefinition[]} views an array of view extensions */ - function MCTRepresentation(representations, views, gestureService, $q, $log) { + function MCTRepresentation(representations, views, representers, $q, $log) { var pathMap = {}, representationMap = {}, gestureMap = {}; @@ -52,8 +52,10 @@ define( }); - function link($scope, element) { - var gestureHandle; + function link($scope, element, attrs) { + var activeRepresenters = representers.map(function (Representer) { + return new Representer($scope, element, attrs); + }); // General-purpose refresh mechanism; should set up the scope // as appropriate for current representation key and @@ -73,9 +75,9 @@ define( $scope.inclusion = pathMap[$scope.key]; // Any existing gestures are no longer valid; release them. - if (gestureHandle) { - gestureHandle.destroy(); - } + activeRepresenters.forEach(function (activeRepresenter) { + activeRepresenter.destroy(); + }); // Log if a key was given, but no matching representation // was found. @@ -89,6 +91,11 @@ define( // Always provide the model, as "model" $scope.model = domainObject.getModel(); + // Also provide the view configuration, + // for the specific view + $scope.configuration = + ($scope.model.configuration || {})[$scope.key] || {}; + // Also provide any of the capabilities requested uses.forEach(function (used) { $log.debug([ @@ -104,13 +111,11 @@ define( }); }); - // Finally, wire up any gestures that should be - // associated with this representation. - gestureHandle = gestureService.attachGestures( - element, - domainObject, - gestureKeys - ); + // Finally, wire up any additional behavior (such as + // gestures) associated with this representation. + activeRepresenters.forEach(function (representer) { + representer.represent(representation, domainObject); + }); } } diff --git a/platform/representation/src/gestures/GestureRepresenter.js b/platform/representation/src/gestures/GestureRepresenter.js new file mode 100644 index 0000000000..285e9084ff --- /dev/null +++ b/platform/representation/src/gestures/GestureRepresenter.js @@ -0,0 +1,37 @@ +/*global define*/ + +define( + [], + function () { + "use strict"; + + function GestureRepresenter(gestureService, scope, element) { + var gestureHandle; + + function destroy() { + if (gestureHandle) { + gestureHandle.destroy(); + } + } + + function represent(representation, domainObject) { + // Clear out any existing gestures + destroy(); + + // Attach gestures - by way of the service. + gestureHandle = gestureService.attachGestures( + element, + domainObject, + (representation || {}).gestures || [] + ); + } + + return { + represent: represent, + destroy: destroy + }; + } + + return GestureRepresenter; + } +); \ No newline at end of file