ajsfw
Version:
Ajs Framework
319 lines (318 loc) • 15.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var ajs = require("ajsfw");
var logger = require("ajsfw/dbg/logger");
var utils = require("ajsfw/utils");
var exceptions = require("./exceptions");
var events_1 = require("ajsfw/events");
var navigation_1 = require("ajsfw/navigation");
var routing_1 = require("ajsfw/routing");
var ViewComponent_1 = require("./ViewComponent");
var ViewComponentManager = (function () {
function ViewComponentManager(container, documentManager, templateManager, viewManager) {
this.__container = container;
this.__documentManager = documentManager;
this.__templateManager = templateManager;
this.__viewManager = viewManager;
this.__components = {};
this.__componentDependencies = [];
this.__componentInstances = {};
this.__rootViewComponent = null;
this.__navigationNotifier = new events_1.Notifier();
if (ajs.viewComponentsToRegister instanceof Array) {
this.registerComponents.apply(this, ajs.viewComponentsToRegister);
}
}
Object.defineProperty(ViewComponentManager.prototype, "components", {
get: function () { return this.__components; },
enumerable: true,
configurable: true
});
Object.defineProperty(ViewComponentManager.prototype, "navigationNotifier", {
get: function () { return this.__navigationNotifier; },
enumerable: true,
configurable: true
});
ViewComponentManager.prototype.registerComponents = function () {
var componentConstructor = [];
for (var _i = 0; _i < arguments.length; _i++) {
componentConstructor[_i] = arguments[_i];
}
for (var i = 0; i < componentConstructor.length; i++) {
if (!this.__isComponentConstructorRegistered(componentConstructor[i])) {
this.__registerComponent(componentConstructor[i]);
}
else {
throw new exceptions.SameViewComponentRegisteredAlreadyException();
}
}
};
ViewComponentManager.prototype.addComponentDependencies = function (component) {
var dependencies = [];
for (var _i = 1; _i < arguments.length; _i++) {
dependencies[_i - 1] = arguments[_i];
}
this.__addComponentDependencies(component, dependencies);
return this;
};
ViewComponentManager.prototype.createViewComponent = function (name, id, parentComponent, state, parentComponentInitStateNotify) {
return __awaiter(this, void 0, void 0, function () {
var visualComponent, viewComponentConstructor, navigator, router, componentViewId, viewComponent, dependencies;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
logger.log(logger.LogType.Enter, 0, "ajs.mvvm.viewmodel", this);
visualComponent = this.__templateManager.getVisualComponent(name);
if (visualComponent === null) {
logger.log(logger.LogType.Error, 0, "ajs.mvvm.view", this, "Visual component is not defined (probably the appropriate template is not loaded): " + name);
throw new exceptions.VisualComponentNotRegisteredException(name);
}
if (this.__components.hasOwnProperty(name.toUpperCase())) {
viewComponentConstructor = this.__components[name.toUpperCase()];
}
else {
viewComponentConstructor = ViewComponent_1.ViewComponent;
}
return [4, this.__container.resolve(navigation_1.IINavigator)];
case 1:
navigator = _a.sent();
return [4, this.__container.resolve(routing_1.IIRouter)];
case 2:
router = _a.sent();
componentViewId = this.__viewManager.getNewComponentId();
logger.log(logger.LogType.Info, 0, "ajs.mvvm.viewmodel", this, "Creating the view component instance: " +
utils.getClassName(viewComponentConstructor) + "[" + componentViewId + "]:" + id, this.__viewManager, parentComponent, state);
viewComponent = new viewComponentConstructor(navigator, router, this.__documentManager, this.__templateManager, this.__viewManager, this, id, componentViewId, parentComponent, visualComponent, state, parentComponentInitStateNotify);
this.__componentInstances[componentViewId] = viewComponent;
return [4, this.__resolveComponentDependencies(viewComponent)];
case 3:
dependencies = _a.sent();
return [4, viewComponent.configure.apply(viewComponent, dependencies)];
case 4:
_a.sent();
return [4, viewComponent.initialize()];
case 5:
_a.sent();
logger.log(logger.LogType.Exit, 0, "ajs.mvvm.viewmodel", this);
return [2, viewComponent];
}
});
});
};
ViewComponentManager.prototype.removeComponentInstance = function (component) {
delete (this.__componentInstances[component.componentViewId]);
};
ViewComponentManager.prototype.getComponentConstructorByName = function (name) {
if (this.__components.hasOwnProperty(name.toUpperCase())) {
return this.__components[name.toUpperCase()];
}
return null;
};
ViewComponentManager.prototype.getComponentInstanceByComponentId = function (componentId) {
if (this.__componentInstances.hasOwnProperty(componentId.toString())) {
return this.__componentInstances[componentId];
}
return null;
};
ViewComponentManager.prototype.getChildrenComponentInstances = function (component) {
var childrenInstances = [];
for (var key in this.__componentInstances) {
if (this.__componentInstances.hasOwnProperty(key)) {
if (this.__componentInstances[key].ajs.parentComponent === component) {
childrenInstances.push(component);
}
}
}
return childrenInstances;
};
ViewComponentManager.prototype.getComponentInstances = function (component, id, userKey) {
var viewComponentInstances = [];
var componentConstructorName = utils.getClassName(component);
for (var key in this.__componentInstances) {
if (this.__componentInstances.hasOwnProperty(key)) {
var constructorName = utils.getClassName(this.__componentInstances[key]);
if (constructorName === componentConstructorName) {
if (id) {
if (this.__componentInstances[key].ajs.id === id) {
if (userKey) {
if (this.__componentInstances[key].hasOwnProperty("key")) {
if (this.__componentInstances[key].ajs.key === userKey) {
viewComponentInstances.push(this.__componentInstances[key]);
}
}
}
else {
viewComponentInstances.push(this.__componentInstances[key]);
}
}
}
else {
viewComponentInstances.push(this.__componentInstances[key]);
}
}
}
}
return viewComponentInstances;
};
ViewComponentManager.prototype.getFirstComponentInstance = function (component, id, userKey) {
var componentConstructorName = utils.getClassName(component);
for (var key in this.__componentInstances) {
if (this.__componentInstances.hasOwnProperty(key)) {
var constructorName = utils.getClassName(this.__componentInstances[key]);
if (constructorName === componentConstructorName) {
if (id) {
if (this.__componentInstances[key].ajs.id === id) {
if (userKey) {
if (this.__componentInstances[key].hasOwnProperty("key")) {
if (this.__componentInstances[key].ajs.key === userKey) {
return this.__componentInstances[key];
}
}
}
else {
return this.__componentInstances[key];
}
}
}
else {
return this.__componentInstances[key];
}
}
}
}
return null;
};
ViewComponentManager.prototype.setRootViewComponent = function (name) {
return __awaiter(this, void 0, void 0, function () {
var visualComponent, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
visualComponent = this.__templateManager.getVisualComponent(name);
if (visualComponent === null) {
logger.log(logger.LogType.Error, 0, "ajs.mvvm.view", this, "Visual component is not defined (probably the appropriate template is not loaded): " + name);
throw new exceptions.VisualComponentNotRegisteredException(name);
}
if (this.__rootViewComponent !== null) {
this.__rootViewComponent.destroy();
}
this.__viewManager.cleanTargetDocument();
_a = this;
return [4, this.createViewComponent(name, "rootViewComponent", null)];
case 1:
_a.__rootViewComponent = _b.sent();
this.__viewManager.rootViewComponent = this.__rootViewComponent;
this.__navigationNotifier.notify(this);
this.__viewManager.render(this.__rootViewComponent);
return [2];
}
});
});
};
ViewComponentManager.prototype.onNavigate = function () {
this.__navigationNotifier.notify(this);
};
ViewComponentManager.prototype.__registerComponent = function (componentConstructor) {
if (componentConstructor instanceof Function) {
var componentName = utils.getClassName(componentConstructor).toUpperCase();
if (this.__components[componentName] === undefined) {
this.__components[componentName] = componentConstructor;
}
}
};
ViewComponentManager.prototype.__isComponentConstructorRegistered = function (componentConstructor) {
for (var key in this.__components) {
if (this.__components[key] === componentConstructor) {
return true;
}
}
return false;
};
ViewComponentManager.prototype.__addComponentDependencies = function (componentCtor) {
var dependencies = [];
for (var _i = 1; _i < arguments.length; _i++) {
dependencies[_i - 1] = arguments[_i];
}
var deps = this.__getComponentDependencies(componentCtor);
if (deps === null) {
this.__componentDependencies.push({
viewComponentCtor: componentCtor,
dependencies: dependencies[0]
});
}
else {
throw new exceptions.ComponentDependenciesConfiguredAlreadyException();
}
};
ViewComponentManager.prototype.__getComponentDependencies = function (componentCtor) {
for (var _i = 0, _a = this.__componentDependencies; _i < _a.length; _i++) {
var cd = _a[_i];
if (cd.viewComponentCtor === componentCtor) {
return cd;
}
}
return null;
};
ViewComponentManager.prototype.__resolveComponentDependencies = function (viewComponent) {
return __awaiter(this, void 0, void 0, function () {
var proto, ctor, deps, params, i, _i, _a, parameter, instance;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
proto = Object.getPrototypeOf(viewComponent);
if (proto === null) {
return [2, []];
}
if (proto instanceof Function) {
ctor = proto;
}
else {
if (proto.constructor) {
ctor = proto.constructor;
}
else {
return [2, []];
}
}
deps = this.__getComponentDependencies(ctor);
if (deps === null) {
return [2, []];
}
if (!("_onConfigure" in viewComponent)) {
return [2, []];
}
params = [];
if (deps.dependencies.length === 0) {
return [2, params];
}
i = 0;
_i = 0, _a = deps.dependencies;
_b.label = 1;
case 1:
if (!(_i < _a.length)) return [3, 4];
parameter = _a[_i];
return [4, this.__container.resolve(parameter, false)];
case 2:
instance = _b.sent();
if (instance !== null) {
params.push(instance);
}
else {
if (parameter && typeof parameter === "object" && parameter !== null && "__diService__" in parameter) {
throw new exceptions.InvalidViewComponentConfigurationArgumentException("Component: '" + utils.getClassName(viewComponent) + "', parameter index: '" + i + "'");
}
params.push(parameter);
}
i++;
_b.label = 3;
case 3:
_i++;
return [3, 1];
case 4: return [2, params];
}
});
});
};
return ViewComponentManager;
}());
exports.ViewComponentManager = ViewComponentManager;