ajsfw
Version:
Ajs Framework
105 lines (104 loc) • 5.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var logger = require("ajsfw/dbg/logger");
var utils = require("ajsfw/utils");
var events_1 = require("ajsfw/events");
var ViewManager = (function () {
function ViewManager(documentManager) {
logger.log(logger.LogType.Constructor, 0, "ajs.mvvm.view", this, "", documentManager);
this.__renderDoneNotifier = new events_1.Notifier();
this.__documentManager = documentManager;
this.__rootViewComponent = null;
this.__shadowDom = document.implementation.createHTMLDocument("shadowDom");
this.__shadowDom.body.innerHTML = "";
this.__lastComponentId = 0;
logger.log(logger.LogType.Exit, 0, "ajs.mvvm.view", this);
}
Object.defineProperty(ViewManager.prototype, "rootViewComponent", {
set: function (value) { this.__rootViewComponent = value; },
enumerable: true,
configurable: true
});
Object.defineProperty(ViewManager.prototype, "renderDoneNotifier", {
get: function () { return this.__renderDoneNotifier; },
enumerable: true,
configurable: true
});
ViewManager.prototype.getNewComponentId = function () { this.__lastComponentId++; return this.__lastComponentId; };
ViewManager.prototype.cleanTargetDocument = function () {
this.__documentManager.clean(this.__documentManager.renderTarget);
};
ViewManager.prototype.stateChangeBegin = function (stateChangeInfo) {
logger.log(logger.LogType.Enter, 0, "ajs.mvvm.view", this);
logger.log(logger.LogType.Info, 0, "ajs.mvvm.view", this, "State change begun (" + utils.getClassName(stateChangeInfo.component) + "), " +
"id: " + stateChangeInfo.component.ajs.id + ", viewId: " + stateChangeInfo.component.componentViewId, stateChangeInfo.component);
logger.log(logger.LogType.Exit, 0, "ajs.mvvm.view", this);
};
ViewManager.prototype.stateChangeEnd = function (stateChangeInfo) {
logger.log(logger.LogType.Enter, 0, "ajs.mvvm.view", this);
logger.log(logger.LogType.Info, 0, "ajs.mvvm.view", this, "State change end (" + utils.getClassName(stateChangeInfo.component) + "), " +
"id: " + stateChangeInfo.component.ajs.id + ", viewId: " + stateChangeInfo.component.componentViewId +
", state changed: " + stateChangeInfo.component.ajs.stateChanged, stateChangeInfo.component);
if (stateChangeInfo.stateChangeRoot === null) {
if (this.__rootViewComponent !== null) {
var targetNewNode = this.render(stateChangeInfo.component);
this.__renderDoneNotifier.notify(this);
if (stateChangeInfo.component.ajs.hasVisualStateTransition) {
stateChangeInfo.component.ajsVisualStateTransitionBegin(this.__documentManager.getTargetNodeByUniqueId(stateChangeInfo.component.componentViewId));
}
}
}
logger.log(logger.LogType.Exit, 0, "ajs.mvvm.view", this);
};
ViewManager.prototype.render = function (viewComponent) {
logger.log(logger.LogType.Enter, 0, "ajs.mvvm.view", this);
logger.log(logger.LogType.Info, 0, "ajs.mvvm.view", this, "Rendering component, id: " + viewComponent.ajs.id + ", viewId: " + viewComponent.componentViewId, viewComponent);
var targetUpdateRoot = this.__documentManager.getTargetNodeByUniqueId(viewComponent.componentViewId);
if (targetUpdateRoot === null) {
if (viewComponent.ajs.parentComponent === null) {
targetUpdateRoot = this.__documentManager.renderTarget;
}
else {
this.render(viewComponent.ajs.parentComponent);
return;
}
}
var componentElement = viewComponent.render(this.__shadowDom.body, false);
if (componentElement !== null) {
try {
this.__documentManager.updateDom(componentElement, targetUpdateRoot);
}
catch (e) {
this.__shadowDom.body.innerHTML = "";
logger.log(logger.LogType.Error, 0, "ajs.mvvm.view", this, "Error while updating the DOM!", e);
throw new Error(e);
}
finally {
this.__shadowDom.body.innerHTML = "";
}
if (targetUpdateRoot instanceof Element) {
if (targetUpdateRoot === this.__documentManager.renderTarget) {
targetUpdateRoot = this.__documentManager.getTargetNodeByUniqueId(viewComponent.componentViewId);
}
if (targetUpdateRoot !== null) {
logger.log(logger.LogType.Exit, 0, "ajs.mvvm.view", this);
return targetUpdateRoot;
}
else {
logger.log(logger.LogType.Error, 0, "ajs.mvvm.view", this, "Something went wrong during the DOM update as the root element of the view component can't be located!");
throw new Error("Unrecoverable internal error. \
Something went wrong during the DOM update as the root element of the view component can't be located!");
}
}
else {
logger.log(logger.LogType.Error, 0, "ajs.mvvm.view", this, "Root of the component must be always element!");
throw new Error("Unrecoverable internal error. Root of the component must be always element!");
}
}
else {
}
logger.log(logger.LogType.Exit, 0, "ajs.mvvm.view", this);
};
return ViewManager;
}());
exports.ViewManager = ViewManager;