[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) {
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",

View File

@ -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",

View File

@ -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('<!-- hidden mct element -->')(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;
};