mframejs
Version:
simple framework
175 lines • 7.54 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var exported_1 = require("../interface/exported");
var view_1 = require("./view");
var viewController_1 = require("./viewController");
var exported_2 = require("../container/exported");
var exported_3 = require("../binding/exported");
var exported_4 = require("../utils/exported");
var exported_5 = require("../utils/exported");
var ElementController = (function () {
function ElementController(bindingContext, htmlNode, classInstance, elementName, templateString, parentViewController) {
this.bindingContext = bindingContext;
this.htmlNode = htmlNode;
this._process = true;
this.internalAttached = false;
this.logger = exported_4.Logger.getLogger(htmlNode.tagName, 'element');
this.templateString = templateString;
if (!classInstance) {
this.logger.log('constructor');
}
else {
this.logger.log('constructor - using supplied class');
}
this.classInstance = classInstance || exported_2.ContainerClasses.get(elementName);
this.viewController = new viewController_1.ViewController(htmlNode, parentViewController);
this.viewController.addElement(this);
if (this.classInstance.__proto__[exported_1.CONSTANTS.CONTAINER_FREE]) {
this.anchorNode = exported_5.DOM.document.createComment('containerless-anchor');
this.elements = [];
}
if (this.classInstance.__proto__[exported_1.CONSTANTS.SHADOW_DOM]) {
this.shadowDom = true;
}
}
ElementController.prototype.searchForInstance = function (_customElement) {
return this.viewController.searchForInstance(_customElement);
};
ElementController.prototype.getView = function () {
return this.viewController;
};
ElementController.prototype.init = function () {
var _this = this;
this.classInstance.$element = this.htmlNode;
this.classInstance.$bindingContext = this.bindingContext;
this.classInstance.$attributes = this.htmlNode.attributes;
this.classInstance.$controller = this;
if (this.shadowDom) {
if (this.classInstance.__proto__[exported_1.CONSTANTS.CONTAINER_FREE]) {
console.warn('containerfree can not be used with showdow dom');
}
else {
var mode = this.classInstance.__proto__[exported_1.CONSTANTS.SHADOW_DOM].mode;
try {
this.htmlNode = this.classInstance.$shadowdom;
this.classInstance.$shadowdom = this.htmlNode.attachShadow({ mode: mode });
}
catch (e) {
console.warn('error adding showdow dom:', e);
}
}
}
exported_3.BindingEngine.subscribeClassMetaBinding(this.classInstance);
var result = this.loadTemplate();
Promise.resolve(result).then(function (template) {
_this.templateString = _this.templateString || template || '<template></template>';
var arr = [];
_this.template = view_1.View.createTemplate(_this.templateString);
_this.create();
_this.processContent();
if (_this._process) {
arr = view_1.View.parseTemplate(_this.template, exported_3.createBindingContext(_this.classInstance), _this.viewController);
}
if (_this._process) {
while (_this.template.childNodes.length) {
if (_this.anchorNode) {
_this.htmlNode.parentNode.insertBefore(_this.anchorNode, _this.htmlNode.nextSibling);
_this.elements.push(_this.template.firstChild);
_this.anchorNode.parentNode.insertBefore(_this.template.firstChild, _this.anchorNode.nextSibling);
}
else {
_this.htmlNode.appendChild(_this.template.firstChild);
}
}
if (_this.anchorNode) {
_this.htmlNode.parentNode.removeChild(_this.htmlNode);
}
}
arr.reverse();
_this.contentProcessed(arr);
arr.forEach(function (a) {
if (a.attached) {
a.attached();
}
});
_this.internalAttached = true;
_this.attached();
});
return this;
};
ElementController.prototype.loadTemplate = function () {
this.logger.log('loadTemplate');
if (this.templateString) {
return this.templateString;
}
else {
return this.classInstance.loadTemplate && this.classInstance.loadTemplate();
}
};
ElementController.prototype.create = function () {
this.logger.log('create');
if (this.classInstance.created) {
this.classInstance.created();
}
};
ElementController.prototype.processContent = function () {
this.logger.log('processContent');
if (this.classInstance.processContent) {
this._process = this.classInstance.processContent(this.template);
}
else {
while (this.htmlNode.childNodes[0]) {
this.template.appendChild(this.htmlNode.childNodes[0]);
}
}
};
ElementController.prototype.contentProcessed = function (controllers) {
this.logger.log('contentProcessed');
if (this.classInstance.contentProcessed && this._process) {
this.classInstance.contentProcessed(controllers);
}
};
ElementController.prototype.attached = function () {
if (this.internalAttached) {
if (!this.classInstance.$element.parentNode) {
this.logger.log('attached', 'missing parent node');
}
else {
this.logger.log('attached');
}
}
if (this.classInstance.attached && this.internalAttached) {
this.internalAttached = false;
this.classInstance.attached();
}
};
ElementController.prototype.detached = function () {
this.logger.log('detached');
if (this.classInstance.detached) {
this.classInstance.detached();
}
exported_3.BindingEngine.unSubscribeClassMetaBinding(this.classInstance);
if (this.anchorNode) {
this.elements.forEach(function (element) {
if (element.parentNode) {
element.parentNode.removeChild(element);
}
});
if (this.anchorNode.parentNode) {
this.anchorNode.parentNode.removeChild(this.anchorNode);
}
}
this.anchorNode = null;
this.elements = null;
this.bindingContext = null;
this.htmlNode = null;
this.classInstance.$element = null;
this.classInstance.$bindingContext = null;
this.classInstance.$attributes = null;
this.classInstance.$controller = null;
this.classInstance = null;
this.viewController = null;
};
return ElementController;
}());
exports.ElementController = ElementController;
//# sourceMappingURL=elementController.js.map