dl
Version:
DreamLab Libs
127 lines (104 loc) • 3.83 kB
JavaScript
var Core = require("core"),
Class = Core.Class,
Types = Core.common.Types;
var CdfDatasource = require("./CdfDatasource.js").CdfDatasource;
/**
* @class CdfTemplate
*/
var CdfTemplate = function () {
/*
this.name = null;
this.role = null;
this.kind = null;
this.data = null;
this.children = [];
*/
this.initialize = function (name) {
this.name = name;
this.role = null;
this.kind = null;
this.data = null;
this.onerror = null;
// UCS
this.theme = null;
this.version = null;
this.configuration = null;
this.partials = null;
this.engine = null;
this.children = [];
};
this.addChild = function (cdfTemplate) {
this.children.push(cdfTemplate);
};
this.getChild = function(name){
for (var i = 0, max = this.children.length; i < max; i++) {
if (this.children[i].name == name) {
return this.children[i];
}
}
return null;
};
this.toJSON = function () {
var retVal = {}, i, max;
retVal[this.name] = {
"role": this.role,
"kind": this.kind
};
if (this.theme) {
retVal[this.name].theme = this.theme;
}
if (this.version) {
retVal[this.name].version = this.version;
}
if (this.configuration) {
retVal[this.name].configuration = this.configuration;
}
if (this.partials) {
retVal[this.name].partials = this.partials;
}
if (this.engine) {
retVal[this.name].engine = this.engine;
}
// jezeli jest ustawione onerror spawdzam czy jest jedna z 3 dozwolonych wartosci
if (this.onerror) {
// jezeli flaga ma akceptowalna wartosc to ja ustawiam
// jezeli nie: puszczam console error
if (this.onerror=='discard' || this.onerror=='abort' || this.onerror=='propagate') {
retVal[this.name].onerror = this.onerror;
} else {
console.error('CdfTemplate/toJSON: name:', this.name, 'Wrong onerror flag value:', this.onerror);
}
}
if (this.children.length > 0) {
if (Types.isInstanceOf(this.data, CdfDatasource)) {
throw "Template: '"+this.name+"' can't have CHILDREN and REMOTE_DATA together!";
} else if (this.data) {
retVal[this.name].data = this.data;
} else {
retVal[this.name].data = {};
}
retVal[this.name].require = [];
for (i = 0, max = this.children.length; i < max; i++) {
retVal[this.name].require.push(this.children[i].name);
}
} else if (Types.isInstanceOf(this.data, CdfDatasource)) {
retVal[this.name]['#data'] = this.data.toJSON();
} else if (this.data) {
retVal[this.name]['data'] = this.data;
}
for (i = 0, max = this.children.length; i < max; i++) {
var renderedChild = this.children[i].toJSON();
for (var elem in renderedChild) {
if (renderedChild.hasOwnProperty(elem)) {
retVal[elem] = renderedChild[elem];
}
}
}
return retVal;
};
this.toString = function (indent) {
return JSON.stringify(this.toJSON(), null, (indent || 4));
}
};
CdfTemplate = new Class(new CdfTemplate());
exports.CdfTemplate = CdfTemplate;