[Representation] Hide elements without transclusion

This commit is contained in:
Victor Woeltjen
2015-10-28 15:29:57 -07:00
parent ab008ae497
commit bcc42d705e
3 changed files with 29 additions and 47 deletions

View File

@ -57,15 +57,11 @@ define(
function MCTInclude(templates, templateLinker) { function MCTInclude(templates, templateLinker) {
var templateMap = {}; var templateMap = {};
function link(scope, element, attrs, ctrl, transclude) { function link(scope, element) {
var changeTemplates = templateLinker.link( var changeTemplate = templateLinker.link(scope, element);
scope,
element,
transclude
);
scope.$watch('key', function (key) { scope.$watch('key', function (key) {
changeTemplates(templateMap[key]); changeTemplate(templateMap[key]);
}); });
} }
@ -82,12 +78,6 @@ define(
}); });
return { return {
transclude: 'element',
priority: 601,
terminal: true,
// Only show at the element level // Only show at the element level
restrict: "E", restrict: "E",

View File

@ -100,11 +100,7 @@ define(
}), }),
toClear = [], // Properties to clear out of scope on change toClear = [], // Properties to clear out of scope on change
counter = 0, counter = 0,
changeTemplate = templateLinker.link( changeTemplate = templateLinker.link($scope, element);
$scope,
element,
transclude
);
// Populate scope with any capabilities indicated by the // Populate scope with any capabilities indicated by the
// representation's extension definition // representation's extension definition
@ -155,15 +151,9 @@ define(
function refresh() { function refresh() {
var domainObject = $scope.domainObject, var domainObject = $scope.domainObject,
representation = lookup($scope.key, domainObject), representation = lookup($scope.key, domainObject),
uses = ((representation || {}).uses || []); path = representation && getPath(representation),
uses = ((representation || {}).uses || []),
// Create an empty object named "representation", for this canRepresent = !!(path && domainObject);
// 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));
// Any existing representers are no longer valid; release them. // Any existing representers are no longer valid; release them.
destroyRepresenters(); destroyRepresenters();
@ -179,9 +169,17 @@ define(
delete $scope[property]; 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 // Populate scope with fields associated with the current
// domain object (if one has been passed in) // domain object (if one has been passed in)
if (domainObject) { if (canRepresent) {
// Track how many representations we've made in this scope, // Track how many representations we've made in this scope,
// to ensure that the correct representations are matched to // to ensure that the correct representations are matched to
// the correct object/key pairs. // the correct object/key pairs.
@ -232,12 +230,6 @@ define(
} }
return { return {
transclude: 'element',
priority: 601,
terminal: true,
// Only applicable at the element level // Only applicable at the element level
restrict: "E", restrict: "E",

View File

@ -60,33 +60,31 @@ define(
* @returns {Function} a function which can be called with a template * @returns {Function} a function which can be called with a template
* URL to switch templates, or `undefined` to remove. * URL to switch templates, or `undefined` to remove.
*/ */
TemplateLinker.prototype.link = function (scope, element, transclude) { TemplateLinker.prototype.link = function (scope, element) {
var originalElement = element, var activeElement = element,
activeElement = element,
activeTemplateUrl, activeTemplateUrl,
comment = this.$compile('<!-- hidden mct element -->')(scope),
self = this; self = this;
function removeElement() { function removeElement() {
if (activeElement !== originalElement) { if (activeElement !== comment) {
activeElement.replaceWith(originalElement); activeElement.replaceWith(comment);
activeElement = originalElement; activeElement = comment;
} }
} }
function replaceElement(clone, template) { function replaceElement(template) {
activeElement.replaceWith(clone); activeElement.replaceWith(element);
activeElement = clone; activeElement = element;
activeElement.empty(); activeElement.empty();
template(scope, function (innerClone) { template(scope, function (innerClone) {
clone.append(innerClone); element.append(innerClone);
}); });
} }
function applyTemplate(template) { function applyTemplate(template) {
if (template) { if (template) {
transclude(function (clone) { replaceElement(template);
replaceElement(clone, template);
});
} else { } else {
removeElement(); removeElement();
} }
@ -103,6 +101,8 @@ define(
} }
} }
removeElement();
return changeTemplate; return changeTemplate;
}; };