ajsfw
Version:
Ajs Framework
185 lines (184 loc) • 8.28 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var resources = require("ajsfw/resources");
var exceptions = require("./exceptions");
var Template = (function () {
function Template(resourceManager, templateManager, templateResource, storageType, cachePolicy, loadingPreference) {
this._resourceManager = resourceManager;
this._templateManager = templateManager;
this._name = "";
this._storageType = storageType;
this._cachePolicy = cachePolicy;
this._loadingPreference = loadingPreference;
this._template = document.implementation.createHTMLDocument("ajstemplate");
var data = templateResource.data;
data = data.replace(/touchstart/g, "touchstart_ajs");
data = data.replace(/touchmove/g, "touchmove_ajs");
data = data.replace(/touchend/g, "touchend_ajs");
this._template.body.innerHTML = data;
this._styleSheetsLoaded = false;
this._styleSheetsUrls = [];
this._styleSheets = [];
this._visualComponents = {};
this._getTemplateData();
}
Object.defineProperty(Template.prototype, "name", {
get: function () { return this._name; },
enumerable: true,
configurable: true
});
Object.defineProperty(Template.prototype, "storageType", {
get: function () { return this._storageType; },
enumerable: true,
configurable: true
});
Object.defineProperty(Template.prototype, "cachePolicy", {
get: function () { return this._cachePolicy; },
enumerable: true,
configurable: true
});
Object.defineProperty(Template.prototype, "loadingPreference", {
get: function () { return this._loadingPreference; },
enumerable: true,
configurable: true
});
Object.defineProperty(Template.prototype, "styleSheetsUrls", {
get: function () { return this._styleSheetsUrls; },
enumerable: true,
configurable: true
});
Object.defineProperty(Template.prototype, "styleSheets", {
get: function () { return this._styleSheets; },
enumerable: true,
configurable: true
});
Object.defineProperty(Template.prototype, "template", {
get: function () { return this._template; },
enumerable: true,
configurable: true
});
Object.defineProperty(Template.prototype, "visualComponents", {
get: function () { return this._visualComponents; },
enumerable: true,
configurable: true
});
Template.prototype.loadStyleSheets = function () {
var _this = this;
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var resourcePromises, i, styleSheets, i, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this._styleSheetsLoaded || this._styleSheetsUrls.length === 0) {
resolve();
}
resourcePromises = [];
for (i = 0; i < this._styleSheetsUrls.length; i++) {
resourcePromises.push(this._resourceManager.getResource(this._styleSheetsUrls[i], this._storageType, this._cachePolicy, resources.LoadingPreference.Cache));
}
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4, Promise.all(resourcePromises)];
case 2:
styleSheets = _a.sent();
for (i = 0; i < styleSheets.length; i++) {
this._styleSheets.push(styleSheets[i].data);
}
this._styleSheetsLoaded = true;
return [3, 4];
case 3:
e_1 = _a.sent();
reject(new exceptions.FailedToLoadTemplateStylesheetsException(e_1));
return [3, 4];
case 4:
resolve();
return [2];
}
});
}); });
};
Template.prototype._walkHTMLTree = function (element, parentComponent, elementProcessor) {
if (element instanceof HTMLElement) {
for (var i = 0; i < element.children.length; i++) {
if (element.children.item(i).nodeType === Node.ELEMENT_NODE) {
var pc = elementProcessor(element.children.item(i), parentComponent);
this._walkHTMLTree(element.children.item(i), pc, elementProcessor);
}
}
}
};
Template.prototype._getTemplateData = function () {
var _this = this;
this._walkHTMLTree(this._template.body, null, function (element, parentComponent) {
if (element.nodeName === "AJSTEMPLATE") {
if (element.hasAttribute("name")) {
_this._name = element.getAttribute("name");
}
else {
throw new exceptions.MissingTemplateNameException();
}
if (element.hasAttribute("stylesheets")) {
var styleSheetsToLoad = element.getAttribute("stylesheets").split(";");
for (var i = 0; i < styleSheetsToLoad.length; i++) {
styleSheetsToLoad[i] = styleSheetsToLoad[i].trim();
}
_this._styleSheetsUrls = _this._styleSheetsUrls.concat(styleSheetsToLoad);
}
}
if (parentComponent !== null && element.hasAttribute("placeholder")) {
var id = element.getAttribute("placeholder");
parentComponent.placeholders[id] = {
placeholder: element
};
}
if (parentComponent !== null && element.nodeName === "STYLE") {
_this._styleSheets.push(element.textContent);
}
if (parentComponent !== null && element.hasAttribute("id")) {
var id = element.getAttribute("id");
var name_1 = element.getAttribute("name");
var cname = element.getAttribute("component");
if (cname !== null) {
parentComponent.children[id] = {
tagName: cname,
nameAttribute: null
};
}
else {
parentComponent.children[id] = {
tagName: element.nodeName.toUpperCase(),
nameAttribute: name_1
};
}
}
if (element.nodeName === "COMPONENT" || element.hasAttribute("component")) {
var name_2;
if (element.nodeName === "COMPONENT" && element.hasAttribute("name")) {
name_2 = element.getAttribute("name").toUpperCase();
}
else {
if (element.hasAttribute("component")) {
name_2 = element.getAttribute("component").toUpperCase();
}
else {
throw new exceptions.MissingVisualComponentNameException();
}
}
var visualComponent = {
component: element,
template: _this,
templateName: _this._name,
children: {},
placeholders: {}
};
_this._visualComponents[name_2] = visualComponent;
_this._templateManager.registerVisualComponent(name_2, visualComponent);
return _this._visualComponents[name_2];
}
return parentComponent;
});
};
return Template;
}());
exports.Template = Template;