aurelia-templating
Version:
An extensible HTML templating engine supporting databinding, custom elements, attached behaviors and more.
1,253 lines (1,238 loc) • 183 kB
JavaScript
import { DOM, FEATURE, PLATFORM } from 'aurelia-pal';
import { TemplateRegistryEntry, Loader } from 'aurelia-loader';
import { metadata, Origin, protocol } from 'aurelia-metadata';
import { relativeToFile } from 'aurelia-path';
import * as LogManager from 'aurelia-logging';
import { subscriberCollection, bindingMode, createOverrideContext, ValueConverterResource, BindingBehaviorResource, ObserverLocator, camelCase, EventManager } from 'aurelia-binding';
import { Container, resolver, inject } from 'aurelia-dependency-injection';
import { TaskQueue } from 'aurelia-task-queue';
var ElementEvents = (function () {
function ElementEvents(element) {
this.element = element;
this.subscriptions = {};
}
ElementEvents.prototype._enqueueHandler = function (handler) {
this.subscriptions[handler.eventName] = this.subscriptions[handler.eventName] || [];
this.subscriptions[handler.eventName].push(handler);
};
ElementEvents.prototype._dequeueHandler = function (handler) {
var index;
var subscriptions = this.subscriptions[handler.eventName];
if (subscriptions) {
index = subscriptions.indexOf(handler);
if (index > -1) {
subscriptions.splice(index, 1);
}
}
return handler;
};
ElementEvents.prototype.publish = function (eventName, detail, bubbles, cancelable) {
if (detail === void 0) { detail = {}; }
if (bubbles === void 0) { bubbles = true; }
if (cancelable === void 0) { cancelable = true; }
var event = DOM.createCustomEvent(eventName, { cancelable: cancelable, bubbles: bubbles, detail: detail });
this.element.dispatchEvent(event);
};
ElementEvents.prototype.subscribe = function (eventName, handler, captureOrOptions) {
if (typeof handler === 'function') {
if (captureOrOptions === undefined) {
captureOrOptions = ElementEvents.defaultListenerOptions;
}
var eventHandler = new EventHandlerImpl(this, eventName, handler, captureOrOptions, false);
return eventHandler;
}
return undefined;
};
ElementEvents.prototype.subscribeOnce = function (eventName, handler, captureOrOptions) {
if (typeof handler === 'function') {
if (captureOrOptions === undefined) {
captureOrOptions = ElementEvents.defaultListenerOptions;
}
var eventHandler = new EventHandlerImpl(this, eventName, handler, captureOrOptions, true);
return eventHandler;
}
return undefined;
};
ElementEvents.prototype.dispose = function (eventName) {
if (eventName && typeof eventName === 'string') {
var subscriptions = this.subscriptions[eventName];
if (subscriptions) {
while (subscriptions.length) {
var subscription = subscriptions.pop();
if (subscription) {
subscription.dispose();
}
}
}
}
else {
this.disposeAll();
}
};
ElementEvents.prototype.disposeAll = function () {
for (var key in this.subscriptions) {
this.dispose(key);
}
};
ElementEvents.defaultListenerOptions = true;
return ElementEvents;
}());
var EventHandlerImpl = (function () {
function EventHandlerImpl(owner, eventName, handler, captureOrOptions, once) {
this.owner = owner;
this.eventName = eventName;
this.handler = handler;
this.capture = typeof captureOrOptions === 'boolean' ? captureOrOptions : captureOrOptions.capture;
this.bubbles = !this.capture;
this.captureOrOptions = captureOrOptions;
this.once = once;
owner.element.addEventListener(eventName, this, captureOrOptions);
owner._enqueueHandler(this);
}
EventHandlerImpl.prototype.handleEvent = function (e) {
var fn = this.handler;
fn(e);
if (this.once) {
this.dispose();
}
};
EventHandlerImpl.prototype.dispose = function () {
this.owner.element.removeEventListener(this.eventName, this, this.captureOrOptions);
this.owner._dequeueHandler(this);
this.owner = this.handler = null;
};
return EventHandlerImpl;
}());
var ResourceLoadContext = (function () {
function ResourceLoadContext() {
this.dependencies = {};
}
ResourceLoadContext.prototype.addDependency = function (url) {
this.dependencies[url] = true;
};
ResourceLoadContext.prototype.hasDependency = function (url) {
return url in this.dependencies;
};
return ResourceLoadContext;
}());
var ViewCompileInstruction = (function () {
function ViewCompileInstruction(targetShadowDOM, compileSurrogate) {
if (targetShadowDOM === void 0) { targetShadowDOM = false; }
if (compileSurrogate === void 0) { compileSurrogate = false; }
this.targetShadowDOM = targetShadowDOM;
this.compileSurrogate = compileSurrogate;
this.associatedModuleId = null;
}
ViewCompileInstruction.normal = new ViewCompileInstruction();
return ViewCompileInstruction;
}());
var BehaviorInstruction = (function () {
function BehaviorInstruction() {
}
BehaviorInstruction.enhance = function () {
var instruction = new BehaviorInstruction();
instruction.enhance = true;
return instruction;
};
BehaviorInstruction.unitTest = function (type, attributes) {
var instruction = new BehaviorInstruction();
instruction.type = type;
instruction.attributes = attributes || {};
return instruction;
};
BehaviorInstruction.element = function (node, type) {
var instruction = new BehaviorInstruction();
instruction.type = type;
instruction.attributes = {};
instruction.anchorIsContainer = !(node.hasAttribute('containerless') || type.containerless);
instruction.initiatedByBehavior = true;
return instruction;
};
BehaviorInstruction.attribute = function (attrName, type) {
var instruction = new BehaviorInstruction();
instruction.attrName = attrName;
instruction.type = type || null;
instruction.attributes = {};
return instruction;
};
BehaviorInstruction.dynamic = function (host, viewModel, viewFactory) {
var instruction = new BehaviorInstruction();
instruction.host = host;
instruction.viewModel = viewModel;
instruction.viewFactory = viewFactory;
instruction.inheritBindingContext = true;
return instruction;
};
BehaviorInstruction.normal = new BehaviorInstruction();
return BehaviorInstruction;
}());
var biProto = BehaviorInstruction.prototype;
biProto.initiatedByBehavior = false;
biProto.enhance = false;
biProto.partReplacements = null;
biProto.viewFactory = null;
biProto.originalAttrName = null;
biProto.skipContentProcessing = false;
biProto.contentFactory = null;
biProto.viewModel = null;
biProto.anchorIsContainer = false;
biProto.host = null;
biProto.attributes = null;
biProto.type = null;
biProto.attrName = null;
biProto.inheritBindingContext = false;
var TargetInstruction = (function () {
function TargetInstruction() {
}
TargetInstruction.shadowSlot = function (parentInjectorId) {
var instruction = new TargetInstruction();
instruction.parentInjectorId = parentInjectorId;
instruction.shadowSlot = true;
return instruction;
};
TargetInstruction.contentExpression = function (expression) {
var instruction = new TargetInstruction();
instruction.contentExpression = expression;
return instruction;
};
TargetInstruction.letElement = function (expressions) {
var instruction = new TargetInstruction();
instruction.expressions = expressions;
instruction.letElement = true;
return instruction;
};
TargetInstruction.lifting = function (parentInjectorId, liftingInstruction) {
var instruction = new TargetInstruction();
instruction.parentInjectorId = parentInjectorId;
instruction.expressions = TargetInstruction.noExpressions;
instruction.behaviorInstructions = [liftingInstruction];
instruction.viewFactory = liftingInstruction.viewFactory;
instruction.providers = [liftingInstruction.type.target];
instruction.lifting = true;
return instruction;
};
TargetInstruction.normal = function (injectorId, parentInjectorId, providers, behaviorInstructions, expressions, elementInstruction) {
var instruction = new TargetInstruction();
instruction.injectorId = injectorId;
instruction.parentInjectorId = parentInjectorId;
instruction.providers = providers;
instruction.behaviorInstructions = behaviorInstructions;
instruction.expressions = expressions;
instruction.anchorIsContainer = elementInstruction ? elementInstruction.anchorIsContainer : true;
instruction.elementInstruction = elementInstruction;
return instruction;
};
TargetInstruction.surrogate = function (providers, behaviorInstructions, expressions, values) {
var instruction = new TargetInstruction();
instruction.expressions = expressions;
instruction.behaviorInstructions = behaviorInstructions;
instruction.providers = providers;
instruction.values = values;
return instruction;
};
TargetInstruction.noExpressions = Object.freeze([]);
return TargetInstruction;
}());
var tiProto = TargetInstruction.prototype;
tiProto.injectorId = null;
tiProto.parentInjectorId = null;
tiProto.shadowSlot = false;
tiProto.slotName = null;
tiProto.slotFallbackFactory = null;
tiProto.contentExpression = null;
tiProto.letElement = false;
tiProto.expressions = null;
tiProto.expressions = null;
tiProto.providers = null;
tiProto.viewFactory = null;
tiProto.anchorIsContainer = false;
tiProto.elementInstruction = null;
tiProto.lifting = false;
tiProto.values = null;
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
var capitalMatcher = /([A-Z])/g;
function addHyphenAndLower(char) {
return '-' + char.toLowerCase();
}
function _hyphenate(name) {
return (name.charAt(0).toLowerCase() + name.slice(1)).replace(capitalMatcher, addHyphenAndLower);
}
function _isAllWhitespace(node) {
return !(node.auInterpolationTarget || (/[^\t\n\r ]/.test(node.textContent)));
}
var BehaviorPropertyObserver = (function () {
function BehaviorPropertyObserver(taskQueue, obj, propertyName, selfSubscriber, initialValue) {
this.taskQueue = taskQueue;
this.obj = obj;
this.propertyName = propertyName;
this.notqueued = true;
this.publishing = false;
this.selfSubscriber = selfSubscriber;
this.currentValue = this.oldValue = initialValue;
}
BehaviorPropertyObserver.prototype.getValue = function () {
return this.currentValue;
};
BehaviorPropertyObserver.prototype.setValue = function (newValue) {
var oldValue = this.currentValue;
if (!Object.is(newValue, oldValue)) {
this.oldValue = oldValue;
this.currentValue = newValue;
if (this.publishing && this.notqueued) {
if (this.taskQueue.flushing) {
this.call();
}
else {
this.notqueued = false;
this.taskQueue.queueMicroTask(this);
}
}
}
};
BehaviorPropertyObserver.prototype.call = function () {
var oldValue = this.oldValue;
var newValue = this.currentValue;
this.notqueued = true;
if (Object.is(newValue, oldValue)) {
return;
}
if (this.selfSubscriber) {
this.selfSubscriber(newValue, oldValue);
}
this.callSubscribers(newValue, oldValue);
this.oldValue = newValue;
};
BehaviorPropertyObserver.prototype.callSubscribers = function (newValue, oldValue) {
throw new Error('Method not implemented.');
};
BehaviorPropertyObserver.prototype.subscribe = function (context, callable) {
this.addSubscriber(context, callable);
};
BehaviorPropertyObserver.prototype.addSubscriber = function (context, callable) {
throw new Error('Method not implemented.');
};
BehaviorPropertyObserver.prototype.unsubscribe = function (context, callable) {
this.removeSubscriber(context, callable);
};
BehaviorPropertyObserver.prototype.removeSubscriber = function (context, callable) {
throw new Error('Method not implemented.');
};
BehaviorPropertyObserver = __decorate([
subscriberCollection()
], BehaviorPropertyObserver);
return BehaviorPropertyObserver;
}());
function getObserver(instance, name) {
var lookup = instance.__observers__;
if (lookup === undefined) {
var ctor = Object.getPrototypeOf(instance).constructor;
var behavior = metadata.get(metadata.resource, ctor);
if (!behavior.isInitialized) {
behavior.initialize(Container.instance || new Container(), instance.constructor);
}
lookup = behavior.observerLocator.getOrCreateObserversLookup(instance);
behavior._ensurePropertiesDefined(instance, lookup);
}
return lookup[name];
}
var BindableProperty = (function () {
function BindableProperty(nameOrConfig) {
if (typeof nameOrConfig === 'string') {
this.name = nameOrConfig;
}
else {
Object.assign(this, nameOrConfig);
}
this.attribute = this.attribute || _hyphenate(this.name);
var defaultBindingMode = this.defaultBindingMode;
if (defaultBindingMode === null || defaultBindingMode === undefined) {
this.defaultBindingMode = bindingMode.oneWay;
}
else if (typeof defaultBindingMode === 'string') {
this.defaultBindingMode = bindingMode[defaultBindingMode] || bindingMode.oneWay;
}
this.changeHandler = this.changeHandler || null;
this.owner = null;
this.descriptor = null;
}
BindableProperty.prototype.registerWith = function (target, behavior, descriptor) {
behavior.properties.push(this);
behavior.attributes[this.attribute] = this;
this.owner = behavior;
if (descriptor) {
this.descriptor = descriptor;
return this._configureDescriptor(descriptor);
}
return undefined;
};
BindableProperty.prototype._configureDescriptor = function (descriptor) {
var name = this.name;
descriptor.configurable = true;
descriptor.enumerable = true;
if ('initializer' in descriptor) {
this.defaultValue = descriptor.initializer;
delete descriptor.initializer;
delete descriptor.writable;
}
if ('value' in descriptor) {
this.defaultValue = descriptor.value;
delete descriptor.value;
delete descriptor.writable;
}
descriptor.get = function () {
return getObserver(this, name).getValue();
};
descriptor.set = function (value) {
getObserver(this, name).setValue(value);
};
descriptor.get.getObserver = function (obj) {
return getObserver(obj, name);
};
return descriptor;
};
BindableProperty.prototype.defineOn = function (target, behavior) {
var name = this.name;
var handlerName;
if (this.changeHandler === null) {
handlerName = name + 'Changed';
if (handlerName in target.prototype) {
this.changeHandler = handlerName;
}
}
if (this.descriptor === null) {
Object.defineProperty(target.prototype, name, this._configureDescriptor({}));
}
};
BindableProperty.prototype.createObserver = function (viewModel) {
var selfSubscriber = null;
var defaultValue = this.defaultValue;
var changeHandlerName = this.changeHandler;
var name = this.name;
var initialValue;
if (this.hasOptions) {
return undefined;
}
if (changeHandlerName in viewModel) {
if ('propertyChanged' in viewModel) {
selfSubscriber = function (newValue, oldValue) {
viewModel[changeHandlerName](newValue, oldValue);
viewModel.propertyChanged(name, newValue, oldValue);
};
}
else {
selfSubscriber = function (newValue, oldValue) { return viewModel[changeHandlerName](newValue, oldValue); };
}
}
else if ('propertyChanged' in viewModel) {
selfSubscriber = function (newValue, oldValue) { return viewModel.propertyChanged(name, newValue, oldValue); };
}
else if (changeHandlerName !== null) {
throw new Error("Change handler ".concat(changeHandlerName, " was specified but not declared on the class."));
}
if (defaultValue !== undefined) {
initialValue = typeof defaultValue === 'function' ? defaultValue.call(viewModel) : defaultValue;
}
return new BehaviorPropertyObserver(this.owner.taskQueue, viewModel, this.name, selfSubscriber, initialValue);
};
BindableProperty.prototype._initialize = function (viewModel, observerLookup, attributes, behaviorHandlesBind, boundProperties) {
var selfSubscriber;
var observer;
var attribute;
var defaultValue = this.defaultValue;
if (this.isDynamic) {
for (var key in attributes) {
this._createDynamicProperty(viewModel, observerLookup, behaviorHandlesBind, key, attributes[key], boundProperties);
}
}
else if (!this.hasOptions) {
observer = observerLookup[this.name];
if (attributes !== null) {
selfSubscriber = observer.selfSubscriber;
attribute = attributes[this.attribute];
if (behaviorHandlesBind) {
observer.selfSubscriber = null;
}
if (typeof attribute === 'string') {
viewModel[this.name] = attribute;
observer.call();
}
else if (attribute) {
boundProperties.push({ observer: observer, binding: attribute.createBinding(viewModel) });
}
else if (defaultValue !== undefined) {
observer.call();
}
observer.selfSubscriber = selfSubscriber;
}
observer.publishing = true;
}
};
BindableProperty.prototype._createDynamicProperty = function (viewModel, observerLookup, behaviorHandlesBind, name, attribute, boundProperties) {
var changeHandlerName = name + 'Changed';
var selfSubscriber = null;
var observer;
var info;
if (changeHandlerName in viewModel) {
if ('propertyChanged' in viewModel) {
selfSubscriber = function (newValue, oldValue) {
viewModel[changeHandlerName](newValue, oldValue);
viewModel.propertyChanged(name, newValue, oldValue);
};
}
else {
selfSubscriber = function (newValue, oldValue) { return viewModel[changeHandlerName](newValue, oldValue); };
}
}
else if ('propertyChanged' in viewModel) {
selfSubscriber = function (newValue, oldValue) { return viewModel.propertyChanged(name, newValue, oldValue); };
}
observer = observerLookup[name] = new BehaviorPropertyObserver(this.owner.taskQueue, viewModel, name, selfSubscriber);
Object.defineProperty(viewModel, name, {
configurable: true,
enumerable: true,
get: observer.getValue.bind(observer),
set: observer.setValue.bind(observer)
});
if (behaviorHandlesBind) {
observer.selfSubscriber = null;
}
if (typeof attribute === 'string') {
viewModel[name] = attribute;
observer.call();
}
else if (attribute) {
info = { observer: observer, binding: attribute.createBinding(viewModel) };
boundProperties.push(info);
}
observer.publishing = true;
observer.selfSubscriber = selfSubscriber;
};
return BindableProperty;
}());
var ViewLocator = (function () {
function ViewLocator() {
}
ViewLocator.prototype.getViewStrategy = function (value) {
if (!value) {
return null;
}
if (typeof value === 'object' && 'getViewStrategy' in value) {
var origin_1 = Origin.get(value.constructor);
value = value.getViewStrategy();
if (typeof value === 'string') {
value = new RelativeViewStrategy(value);
}
viewStrategy.assert(value);
if (origin_1.moduleId) {
value.makeRelativeTo(origin_1.moduleId);
}
return value;
}
if (typeof value === 'string') {
value = new RelativeViewStrategy(value);
}
if (viewStrategy.validate(value)) {
return value;
}
if (typeof value !== 'function') {
value = value.constructor;
}
if ('$view' in value) {
var c = value.$view;
var view = void 0;
c = typeof c === 'function' ? c.call(value) : c;
if (c === null) {
view = new NoViewStrategy();
}
else {
view = c instanceof StaticViewStrategy ? c : new StaticViewStrategy(c);
}
metadata.define(ViewLocator.viewStrategyMetadataKey, view, value);
return view;
}
var origin = Origin.get(value);
var strategy = metadata.get(ViewLocator.viewStrategyMetadataKey, value);
if (!strategy) {
if (!origin.moduleId) {
throw new Error('Cannot determine default view strategy for object.\n' + value);
}
strategy = this.createFallbackViewStrategy(origin);
}
else if (origin.moduleId) {
strategy.moduleId = origin.moduleId;
}
return strategy;
};
ViewLocator.prototype.createFallbackViewStrategy = function (origin) {
return new ConventionalViewStrategy(this, origin);
};
ViewLocator.prototype.convertOriginToViewUrl = function (origin) {
var moduleId = origin.moduleId;
var id = (moduleId.endsWith('.js') || moduleId.endsWith('.ts')) ? moduleId.substring(0, moduleId.length - 3) : moduleId;
return id + '.html';
};
ViewLocator.viewStrategyMetadataKey = 'aurelia:view-strategy';
return ViewLocator;
}());
function mi(name) {
throw new Error("BindingLanguage must implement ".concat(name, "()."));
}
var BindingLanguage = (function () {
function BindingLanguage() {
}
BindingLanguage.prototype.inspectAttribute = function (resources, elementName, attrName, attrValue) {
mi('inspectAttribute');
};
BindingLanguage.prototype.createAttributeInstruction = function (resources, element, info, existingInstruction, context) {
mi('createAttributeInstruction');
};
BindingLanguage.prototype.createLetExpressions = function (resources, element) {
mi('createLetExpressions');
};
BindingLanguage.prototype.inspectTextContent = function (resources, value) {
mi('inspectTextContent');
};
return BindingLanguage;
}());
var noNodes = Object.freeze([]);
var SlotCustomAttribute = (function () {
function SlotCustomAttribute(element) {
this.element = element;
this.element.auSlotAttribute = this;
}
SlotCustomAttribute.inject = function () {
return [DOM.Element];
};
SlotCustomAttribute.prototype.valueChanged = function (newValue, oldValue) { };
return SlotCustomAttribute;
}());
var PassThroughSlot = (function () {
function PassThroughSlot(anchor, name, destinationName, fallbackFactory) {
this.anchor = anchor;
this.anchor.viewSlot = this;
this.name = name;
this.destinationName = destinationName;
this.fallbackFactory = fallbackFactory;
this.destinationSlot = null;
this.projections = 0;
this.contentView = null;
var attr = new SlotCustomAttribute(this.anchor);
attr.value = this.destinationName;
}
Object.defineProperty(PassThroughSlot.prototype, "needsFallbackRendering", {
get: function () {
return this.fallbackFactory && this.projections === 0;
},
enumerable: false,
configurable: true
});
PassThroughSlot.prototype.renderFallbackContent = function (view, nodes, projectionSource, index) {
if (this.contentView === null) {
this.contentView = this.fallbackFactory.create(this.ownerView.container);
this.contentView.bind(this.ownerView.bindingContext, this.ownerView.overrideContext);
var slots = Object.create(null);
slots[this.destinationSlot.name] = this.destinationSlot;
ShadowDOM.distributeView(this.contentView, slots, projectionSource, index, this.destinationSlot.name);
}
};
PassThroughSlot.prototype.passThroughTo = function (destinationSlot) {
this.destinationSlot = destinationSlot;
};
PassThroughSlot.prototype.addNode = function (view, node, projectionSource, index) {
if (this.contentView !== null) {
this.contentView.removeNodes();
this.contentView.detached();
this.contentView.unbind();
this.contentView = null;
}
if (node.viewSlot instanceof PassThroughSlot) {
node.viewSlot.passThroughTo(this);
return;
}
this.projections++;
this.destinationSlot.addNode(view, node, projectionSource, index);
};
PassThroughSlot.prototype.removeView = function (view, projectionSource) {
this.projections--;
this.destinationSlot.removeView(view, projectionSource);
if (this.needsFallbackRendering) {
this.renderFallbackContent(null, noNodes, projectionSource);
}
};
PassThroughSlot.prototype.removeAll = function (projectionSource) {
this.projections = 0;
this.destinationSlot.removeAll(projectionSource);
if (this.needsFallbackRendering) {
this.renderFallbackContent(null, noNodes, projectionSource);
}
};
PassThroughSlot.prototype.projectFrom = function (view, projectionSource) {
this.destinationSlot.projectFrom(view, projectionSource);
};
PassThroughSlot.prototype.created = function (ownerView) {
this.ownerView = ownerView;
};
PassThroughSlot.prototype.bind = function (view) {
if (this.contentView) {
this.contentView.bind(view.bindingContext, view.overrideContext);
}
};
PassThroughSlot.prototype.attached = function () {
if (this.contentView) {
this.contentView.attached();
}
};
PassThroughSlot.prototype.detached = function () {
if (this.contentView) {
this.contentView.detached();
}
};
PassThroughSlot.prototype.unbind = function () {
if (this.contentView) {
this.contentView.unbind();
}
};
return PassThroughSlot;
}());
var ShadowSlot = (function () {
function ShadowSlot(anchor, name, fallbackFactory) {
this.anchor = anchor;
this.anchor.isContentProjectionSource = true;
this.anchor.viewSlot = this;
this.name = name;
this.fallbackFactory = fallbackFactory;
this.contentView = null;
this.projections = 0;
this.children = [];
this.projectFromAnchors = null;
this.destinationSlots = null;
}
Object.defineProperty(ShadowSlot.prototype, "needsFallbackRendering", {
get: function () {
return this.fallbackFactory && this.projections === 0;
},
enumerable: false,
configurable: true
});
ShadowSlot.prototype.addNode = function (view, node, projectionSource, index, destination) {
var $node = node;
if (this.contentView !== null) {
this.contentView.removeNodes();
this.contentView.detached();
this.contentView.unbind();
this.contentView = null;
}
if ($node.viewSlot instanceof PassThroughSlot) {
$node.viewSlot.passThroughTo(this);
return;
}
if (this.destinationSlots !== null) {
ShadowDOM.distributeNodes(view, [$node], this.destinationSlots, this, index);
}
else {
$node.auOwnerView = view;
$node.auProjectionSource = projectionSource;
$node.auAssignedSlot = this;
var anchor = this._findAnchor(view, $node, projectionSource, index);
var parent_1 = anchor.parentNode;
parent_1.insertBefore($node, anchor);
this.children.push($node);
this.projections++;
}
};
ShadowSlot.prototype.removeView = function (view, projectionSource) {
if (this.destinationSlots !== null) {
ShadowDOM.undistributeView(view, this.destinationSlots, this);
}
else if (this.contentView && this.contentView.hasSlots) {
ShadowDOM.undistributeView(view, this.contentView.slots, projectionSource);
}
else {
var found = this.children.find(function (x) { return x.auSlotProjectFrom === projectionSource; });
if (found) {
var children = found.auProjectionChildren;
var ownChildren = this.children;
for (var i = 0, ii = children.length; i < ii; ++i) {
var child = children[i];
if (child.auOwnerView === view) {
children.splice(i, 1);
view.fragment.appendChild(child);
i--;
ii--;
this.projections--;
var idx = ownChildren.indexOf(child);
if (idx > -1) {
ownChildren.splice(idx, 1);
}
}
}
if (this.needsFallbackRendering) {
this.renderFallbackContent(view, noNodes, projectionSource);
}
}
}
};
ShadowSlot.prototype.removeAll = function (projectionSource) {
if (this.destinationSlots !== null) {
ShadowDOM.undistributeAll(this.destinationSlots, this);
}
else if (this.contentView && this.contentView.hasSlots) {
ShadowDOM.undistributeAll(this.contentView.slots, projectionSource);
}
else {
var found = this.children.find(function (x) { return x.auSlotProjectFrom === projectionSource; });
if (found) {
var children = found.auProjectionChildren;
var ownChildren = this.children;
for (var i = 0, ii = children.length; i < ii; ++i) {
var child = children[i];
child.auOwnerView.fragment.appendChild(child);
this.projections--;
var idx = ownChildren.indexOf(child);
if (idx > -1) {
ownChildren.splice(idx, 1);
}
}
found.auProjectionChildren = [];
if (this.needsFallbackRendering) {
this.renderFallbackContent(null, noNodes, projectionSource);
}
}
}
};
ShadowSlot.prototype._findAnchor = function (view, node, projectionSource, index) {
if (projectionSource) {
var found = this.children.find(function (x) { return x.auSlotProjectFrom === projectionSource; });
if (found) {
if (index !== undefined) {
var children = found.auProjectionChildren;
var viewIndex = -1;
var lastView = void 0;
for (var i = 0, ii = children.length; i < ii; ++i) {
var current = children[i];
if (current.auOwnerView !== lastView) {
viewIndex++;
lastView = current.auOwnerView;
if (viewIndex >= index && lastView !== view) {
children.splice(i, 0, node);
return current;
}
}
}
}
found.auProjectionChildren.push(node);
return found;
}
}
return this.anchor;
};
ShadowSlot.prototype.projectTo = function (slots) {
this.destinationSlots = slots;
};
ShadowSlot.prototype.projectFrom = function (view, projectionSource) {
var anchor = DOM.createComment('anchor');
var parent = this.anchor.parentNode;
anchor.auSlotProjectFrom = projectionSource;
anchor.auOwnerView = view;
anchor.auProjectionChildren = [];
parent.insertBefore(anchor, this.anchor);
this.children.push(anchor);
if (this.projectFromAnchors === null) {
this.projectFromAnchors = [];
}
this.projectFromAnchors.push(anchor);
};
ShadowSlot.prototype.renderFallbackContent = function (view, nodes, projectionSource, index) {
if (this.contentView === null) {
this.contentView = this.fallbackFactory.create(this.ownerView.container);
this.contentView.bind(this.ownerView.bindingContext, this.ownerView.overrideContext);
this.contentView.insertNodesBefore(this.anchor);
}
if (this.contentView.hasSlots) {
var slots = this.contentView.slots;
var projectFromAnchors = this.projectFromAnchors;
if (projectFromAnchors !== null) {
for (var slotName in slots) {
var slot = slots[slotName];
for (var i = 0, ii = projectFromAnchors.length; i < ii; ++i) {
var anchor = projectFromAnchors[i];
slot.projectFrom(anchor.auOwnerView, anchor.auSlotProjectFrom);
}
}
}
this.fallbackSlots = slots;
ShadowDOM.distributeNodes(view, nodes, slots, projectionSource, index);
}
};
ShadowSlot.prototype.created = function (ownerView) {
this.ownerView = ownerView;
};
ShadowSlot.prototype.bind = function (view) {
if (this.contentView) {
this.contentView.bind(view.bindingContext, view.overrideContext);
}
};
ShadowSlot.prototype.attached = function () {
if (this.contentView) {
this.contentView.attached();
}
};
ShadowSlot.prototype.detached = function () {
if (this.contentView) {
this.contentView.detached();
}
};
ShadowSlot.prototype.unbind = function () {
if (this.contentView) {
this.contentView.unbind();
}
};
return ShadowSlot;
}());
var ShadowDOM = (function () {
function ShadowDOM() {
}
ShadowDOM.getSlotName = function (node) {
if (node.auSlotAttribute === undefined) {
return ShadowDOM.defaultSlotKey;
}
return node.auSlotAttribute.value;
};
ShadowDOM.distributeView = function (view, slots, projectionSource, index, destinationOverride) {
var nodes;
if (view === null) {
nodes = noNodes;
}
else {
var childNodes = view.fragment.childNodes;
var ii = childNodes.length;
nodes = new Array(ii);
for (var i = 0; i < ii; ++i) {
nodes[i] = childNodes[i];
}
}
ShadowDOM.distributeNodes(view, nodes, slots, projectionSource, index, destinationOverride);
};
ShadowDOM.undistributeView = function (view, slots, projectionSource) {
for (var slotName in slots) {
slots[slotName].removeView(view, projectionSource);
}
};
ShadowDOM.undistributeAll = function (slots, projectionSource) {
for (var slotName in slots) {
slots[slotName].removeAll(projectionSource);
}
};
ShadowDOM.distributeNodes = function (view, nodes, slots, projectionSource, index, destinationOverride) {
for (var i = 0, ii = nodes.length; i < ii; ++i) {
var currentNode = nodes[i];
var nodeType = currentNode.nodeType;
if (currentNode.isContentProjectionSource) {
currentNode.viewSlot.projectTo(slots);
for (var slotName in slots) {
slots[slotName].projectFrom(view, currentNode.viewSlot);
}
nodes.splice(i, 1);
ii--;
i--;
}
else if (nodeType === 1 || nodeType === 3 || currentNode.viewSlot instanceof PassThroughSlot) {
if (nodeType === 3 && _isAllWhitespace(currentNode)) {
nodes.splice(i, 1);
ii--;
i--;
}
else {
var found = slots[destinationOverride || ShadowDOM.getSlotName(currentNode)];
if (found) {
found.addNode(view, currentNode, projectionSource, index);
nodes.splice(i, 1);
ii--;
i--;
}
}
}
else {
nodes.splice(i, 1);
ii--;
i--;
}
}
for (var slotName in slots) {
var slot = slots[slotName];
if (slot.needsFallbackRendering) {
slot.renderFallbackContent(view, nodes, projectionSource, index);
}
}
};
ShadowDOM.defaultSlotKey = '__au-default-slot-key__';
return ShadowDOM;
}());
var CompositionTransactionNotifier = (function () {
function CompositionTransactionNotifier(owner) {
this.owner = owner;
this.owner._compositionCount++;
}
CompositionTransactionNotifier.prototype.done = function () {
this.owner._compositionCount--;
this.owner._tryCompleteTransaction();
};
return CompositionTransactionNotifier;
}());
var CompositionTransactionOwnershipToken = (function () {
function CompositionTransactionOwnershipToken(owner) {
this.owner = owner;
this.owner._ownershipToken = this;
this.thenable = this._createThenable();
}
CompositionTransactionOwnershipToken.prototype.waitForCompositionComplete = function () {
this.owner._tryCompleteTransaction();
return this.thenable;
};
CompositionTransactionOwnershipToken.prototype.resolve = function () {
this._resolveCallback();
};
CompositionTransactionOwnershipToken.prototype._resolveCallback = function () {
throw new Error("Method not implemented.");
};
CompositionTransactionOwnershipToken.prototype._createThenable = function () {
var _this = this;
return new Promise(function (resolve) {
_this._resolveCallback = resolve;
});
};
return CompositionTransactionOwnershipToken;
}());
var CompositionTransaction = (function () {
function CompositionTransaction() {
this._ownershipToken = null;
this._compositionCount = 0;
}
CompositionTransaction.prototype.tryCapture = function () {
return this._ownershipToken === null
? new CompositionTransactionOwnershipToken(this)
: null;
};
CompositionTransaction.prototype.enlist = function () {
return new CompositionTransactionNotifier(this);
};
CompositionTransaction.prototype._tryCompleteTransaction = function () {
if (this._compositionCount <= 0) {
this._compositionCount = 0;
if (this._ownershipToken !== null) {
var token = this._ownershipToken;
this._ownershipToken = null;
token.resolve();
}
}
};
return CompositionTransaction;
}());
var View = (function () {
function View(container, viewFactory, fragment, controllers, bindings, children, slots) {
this.container = container;
this.viewFactory = viewFactory;
this.resources = viewFactory.resources;
this.fragment = fragment;
this.firstChild = fragment.firstChild;
this.lastChild = fragment.lastChild;
this.controllers = controllers;
this.bindings = bindings;
this.children = children;
this.slots = slots;
this.hasSlots = false;
this.fromCache = false;
this.isBound = false;
this.isAttached = false;
this.bindingContext = null;
this.overrideContext = null;
this.controller = null;
this.viewModelScope = null;
this.animatableElement = undefined;
this._isUserControlled = false;
this.contentView = null;
for (var _ in slots) {
this.hasSlots = true;
break;
}
}
View.prototype.returnToCache = function () {
this.viewFactory.returnViewToCache(this);
};
View.prototype.created = function () {
var i;
var ii;
var controllers = this.controllers;
for (i = 0, ii = controllers.length; i < ii; ++i) {
controllers[i].created(this);
}
};
View.prototype.bind = function (bindingContext, overrideContext, _systemUpdate) {
var controllers;
var bindings;
var children;
var i;
var ii;
if (_systemUpdate && this._isUserControlled) {
return;
}
if (this.isBound) {
if (this.bindingContext === bindingContext) {
return;
}
this.unbind();
}
this.isBound = true;
this.bindingContext = bindingContext;
this.overrideContext = overrideContext || createOverrideContext(bindingContext);
this.resources._invokeHook('beforeBind', this);
bindings = this.bindings;
for (i = 0, ii = bindings.length; i < ii; ++i) {
bindings[i].bind(this);
}
if (this.viewModelScope !== null) {
bindingContext.bind(this.viewModelScope.bindingContext, this.viewModelScope.overrideContext);
this.viewModelScope = null;
}
controllers = this.controllers;
for (i = 0, ii = controllers.length; i < ii; ++i) {
controllers[i].bind(this);
}
children = this.children;
for (i = 0, ii = children.length; i < ii; ++i) {
children[i].bind(bindingContext, overrideContext, true);
}
if (this.hasSlots) {
ShadowDOM.distributeView(this.contentView, this.slots);
}
};
View.prototype.addBinding = function (binding) {
this.bindings.push(binding);
if (this.isBound) {
binding.bind(this);
}
};
View.prototype.unbind = function () {
var controllers;
var bindings;
var children;
var i;
var ii;
if (this.isBound) {
this.isBound = false;
this.resources._invokeHook('beforeUnbind', this);
if (this.controller !== null) {
this.controller.unbind();
}
bindings = this.bindings;
for (i = 0, ii = bindings.length; i < ii; ++i) {
bindings[i].unbind();
}
controllers = this.controllers;
for (i = 0, ii = controllers.length; i < ii; ++i) {
controllers[i].unbind();
}
children = this.children;
for (i = 0, ii = children.length; i < ii; ++i) {
children[i].unbind();
}
this.bindingContext = null;
this.overrideContext = null;
}
};
View.prototype.insertNodesBefore = function (refNode) {
refNode.parentNode.insertBefore(this.fragment, refNode);
};
View.prototype.appendNodesTo = function (parent) {
parent.appendChild(this.fragment);
};
View.prototype.removeNodes = function () {
var fragment = this.fragment;
var current = this.firstChild;
var end = this.lastChild;
var next;
while (current) {
next = current.nextSibling;
fragment.appendChild(current);
if (current === end) {
break;
}
current = next;
}
};
View.prototype.attached = function () {
var controllers;
var children;
var i;
var ii;
if (this.isAttached) {
return;
}
this.isAttached = true;
if (this.controller !== null) {
this.controller.attached();
}
controllers = this.controllers;
for (i = 0, ii = controllers.length; i < ii; ++i) {
controllers[i].attached();
}
children = this.children;
for (i = 0, ii = children.length; i < ii; ++i) {
children[i].attached();
}
};
View.prototype.detached = function () {
var controllers;
var children;
var i;
var ii;
if (this.isAttached) {
this.isAttached = false;
if (this.controller !== null) {
this.controller.detached();
}
controllers = this.controllers;
for (i = 0, ii = controllers.length; i < ii; ++i) {
controllers[i].detached();
}
children = this.children;
for (i = 0, ii = children.length; i < ii; ++i) {
children[i].detached();
}
}
};
return View;
}());
var Animator = (function () {
function Animator() {
}
Animator.prototype.enter = function (element) {
return Promise.resolve(false);
};
Animator.prototype.leave = function (element) {
return Promise.resolve(false);
};
Animator.prototype.removeClass = function (element, className) {
element.classList.remove(className);
return Promise.resolve(false);
};
Animator.prototype.addClass = function (element, className) {
element.classList.add(className);
return Promise.re