From bcc42d705ea5024f155617b5bceea20d6b6b4fd8 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Wed, 28 Oct 2015 15:29:57 -0700 Subject: [PATCH] [Representation] Hide elements without transclusion --- platform/representation/src/MCTInclude.js | 16 ++------- .../representation/src/MCTRepresentation.js | 34 +++++++------------ platform/representation/src/TemplateLinker.js | 26 +++++++------- 3 files changed, 29 insertions(+), 47 deletions(-) diff --git a/platform/representation/src/MCTInclude.js b/platform/representation/src/MCTInclude.js index 400a9d3ad8..e00ef8599d 100644 --- a/platform/representation/src/MCTInclude.js +++ b/platform/representation/src/MCTInclude.js @@ -57,15 +57,11 @@ define( function MCTInclude(templates, templateLinker) { var templateMap = {}; - function link(scope, element, attrs, ctrl, transclude) { - var changeTemplates = templateLinker.link( - scope, - element, - transclude - ); + function link(scope, element) { + var changeTemplate = templateLinker.link(scope, element); scope.$watch('key', function (key) { - changeTemplates(templateMap[key]); + changeTemplate(templateMap[key]); }); } @@ -82,12 +78,6 @@ define( }); return { - transclude: 'element', - - priority: 601, - - terminal: true, - // Only show at the element level restrict: "E", diff --git a/platform/representation/src/MCTRepresentation.js b/platform/representation/src/MCTRepresentation.js index cefda36b42..9d5f612960 100644 --- a/platform/representation/src/MCTRepresentation.js +++ b/platform/representation/src/MCTRepresentation.js @@ -100,11 +100,7 @@ define( }), toClear = [], // Properties to clear out of scope on change counter = 0, - changeTemplate = templateLinker.link( - $scope, - element, - transclude - ); + changeTemplate = templateLinker.link($scope, element); // Populate scope with any capabilities indicated by the // representation's extension definition @@ -155,15 +151,9 @@ define( function refresh() { var domainObject = $scope.domainObject, representation = lookup($scope.key, domainObject), - uses = ((representation || {}).uses || []); - - // Create an empty object named "representation", for this - // representation to store local variables into. - $scope.representation = {}; - - // Look up the actual template path, pass it to ng-include - // via the "inclusion" field - changeTemplate(representation && getPath(representation)); + path = representation && getPath(representation), + uses = ((representation || {}).uses || []), + canRepresent = !!(path && domainObject); // Any existing representers are no longer valid; release them. destroyRepresenters(); @@ -179,9 +169,17 @@ define( delete $scope[property]; }); + // Create an empty object named "representation", for this + // representation to store local variables into. + $scope.representation = {}; + + // Change templates (passing in undefined to clear + // if we don't have enough info to show a template.) + changeTemplate(canRepresent ? path : undefined); + // Populate scope with fields associated with the current // domain object (if one has been passed in) - if (domainObject) { + if (canRepresent) { // Track how many representations we've made in this scope, // to ensure that the correct representations are matched to // the correct object/key pairs. @@ -232,12 +230,6 @@ define( } return { - transclude: 'element', - - priority: 601, - - terminal: true, - // Only applicable at the element level restrict: "E", diff --git a/platform/representation/src/TemplateLinker.js b/platform/representation/src/TemplateLinker.js index c3f8eb0208..0c5e76cca7 100644 --- a/platform/representation/src/TemplateLinker.js +++ b/platform/representation/src/TemplateLinker.js @@ -60,33 +60,31 @@ define( * @returns {Function} a function which can be called with a template * URL to switch templates, or `undefined` to remove. */ - TemplateLinker.prototype.link = function (scope, element, transclude) { - var originalElement = element, - activeElement = element, + TemplateLinker.prototype.link = function (scope, element) { + var activeElement = element, activeTemplateUrl, + comment = this.$compile('')(scope), self = this; function removeElement() { - if (activeElement !== originalElement) { - activeElement.replaceWith(originalElement); - activeElement = originalElement; + if (activeElement !== comment) { + activeElement.replaceWith(comment); + activeElement = comment; } } - function replaceElement(clone, template) { - activeElement.replaceWith(clone); - activeElement = clone; + function replaceElement(template) { + activeElement.replaceWith(element); + activeElement = element; activeElement.empty(); template(scope, function (innerClone) { - clone.append(innerClone); + element.append(innerClone); }); } function applyTemplate(template) { if (template) { - transclude(function (clone) { - replaceElement(clone, template); - }); + replaceElement(template); } else { removeElement(); } @@ -103,6 +101,8 @@ define( } } + removeElement(); + return changeTemplate; };