dl
Version:
DreamLab Libs
204 lines (173 loc) • 8 kB
JavaScript
var Core = require("core"),
Class = Core.Class,
Event = Core.event.Event,
ErrorEvent = Core.event.ErrorEvent,
EventDispatcher = Core.event.EventDispatcher;
var CdfBlock = require('./CdfBlock.js').CdfBlock,
CdfInsert = require('./CdfInsert.js').CdfInsert,
CdfConfig = require('./CdfConfig.js').CdfConfig,
CdfBlocks = require('./CdfBlocks.js').CdfBlocks,
CdfInserts = require('./CdfInserts.js').CdfInserts,
CdfApplication = require('./CdfApplication.js').CdfApplication,
CdfApplications = require('./CdfApplications.js').CdfApplications;
var CDF_TAG_PATTERN = /(<cdf:(.|\s)+?(<\/cdf:[^>]+>))/ig,
CDF_INSERT_PATTERN = /(<cdf:insert(.|\s)+?(<\/cdf:[^>]+>))/ig,
CDF_BLOCK_PATTERN = /(<(cdf:block)\s[^>]*>[\s\S]*?<\/\2>)/ig,
CDF_CONFIG_PATTERN = /(<cdf:config(.|\s)+?(<\/cdf:[^>]+>))/ig,
CDF_APPLICATION_PATTERN = /(<cdf:application(.|\s)+?(<\/cdf:[^>]+>))/ig;
var CdfPage = function () {
this.Extends = EventDispatcher;
/*
this._data = "";
this.inserts = null;
this.blocks = null;
this.config = null;
this.layout = null;
*/
/**
* Parsuje zadanego stringa w poszukiwaniu cdf:mode i cdf:target
* @ignore
*/
this.initialize = function (data) {
this.parent();
this._data = "";
this.inserts = new CdfInserts();
this.blocks = new CdfBlocks();
this.applications = new CdfApplications();
this.config = null;
this.layout = null;
this._data = data;
var i, max, chunks, chunk, model;
// parse cdf config
chunks = data.match(CDF_CONFIG_PATTERN);
if (chunks) {
this.config = new CdfConfig(chunks[0]);
if (this.config.main) {
this._data = "";
}
}
//parse data
chunks = data.match(CDF_BLOCK_PATTERN);
if (chunks) {
var model, chunk = null;
for (var i = 0, max = chunks.length; i < max; i++) {
chunk = chunks[i];
model = new CdfBlock(chunk);
this.blocks.add( model );
if (this.config && this.config.main) {
if (model.name == this.config.main) {
this.layout = model;
}
this._data += model.token;
}
}
}
//parse applications
chunks = data.match(CDF_APPLICATION_PATTERN);
if (chunks) {
var application;
for (i = 0, max = chunks.length; i < max; i++) {
chunk = chunks[i];
application = new CdfApplication(chunk);
this.applications.add(application);
if (this.config && this.config.main) {
this._data += application.token;
}
}
}
};
this.render = function () {
console.log(' -- CDF Page Render:');
var body = this._data;
if (this.layout) {
console.log(' -- I HAVE special layout model');
if (!this.layout.isCacheable()) {
// fixme czy czasem to ze dam tutaj content cdfowy to i tak nie jest zle?
var cachedBody = body + this.config.token;
this.dispatchEvent( new Event(CdfPage.Event.CACHE_READY, cachedBody));
body = body.replace(this.layout.token, this.layout.getRenderedData()+"\n\n");
} else {
body = body.replace(this.layout.token, this.layout.getRenderedData()+"\n\n");
}
} else {
console.log(' -- I DONT HAVE special layout model');
}
console.log(' -- Finding targets...');
var target = null, chunk = "", chunks = body.match( CDF_INSERT_PATTERN );
if (chunks) {
for (var i = 0, max = chunks.length; i < max; i++) {
chunk = chunks[i];
target = new CdfInsert(chunk);
if (this.blocks.hasTarget(target.src)) {
this.inserts.add(new CdfInsert(chunk));
} else {
body = body.replace(chunk, "");
}
}
}
console.log(' -- Targets found: ' + this.inserts.getLength());
if (this.blocks.hasCacheableModels()) {
console.log(' -- Replacing CACHEABLE models...');
body = this._replaceCdf(body, this.blocks.getCacheableModels() );
body = this._addCdfClientLib(body);
this.dispatchEvent(new Event(CdfPage.Event.CACHE_READY, body));
console.log(' -- Replacing NONCACHEABLE models...');
body = this._replaceCdf(body, this.blocks.getNonCacheableModels());
this.dispatchEvent(new Event(CdfPage.Event.RENDERED, body));
} else if (!this.blocks.hasNonCacheableModels()) {
console.log(' -- There is no CACHABLE and NONCACHEABLE models');
body = this._addCdfClientLib(body);
this.dispatchEvent(new Event(CdfPage.Event.CACHE_READY, body));
this.dispatchEvent(new Event(CdfPage.Event.RENDERED, body));
} else {
console.log(' -- Replacing NONCACHEABLE models...');
body = this._replaceCdf(body, this.blocks.getNonCacheableModels());
body = this._addCdfClientLib(body);
this.dispatchEvent(new Event(CdfPage.Event.RENDERED, body));
}
};
this._addCdfClientLib = function (body) {
if (this.blocks.hasAjaxModels() && body.indexOf('/_cdf/client.js') == -1){
body = body.replace(/<\/body>/i, '<script src="/_cdf/client.js"></script></body>');
}
return body;
}
this._replaceCdf = function (body, models) {
console.log(' -- replacing '+models.length+" blocks");
if (models) {
var model = null, insert = null;
for (var i = 0, max = models.length; i < max; i++) {
model = models[i];
if (model.name) {
insert = this.inserts.getById(model.name);
if (insert) {
console.log(' -- Replacing insert with block -> '+model.name);
body = body.replace(model.token, '');
body = body.replace(insert.token, model.getRenderedData());
} else {
console.log(' -- NOTICE: Cannot find \'insert\' for block: '+model.name);
console.log(' -- Replacing block "in place" -> '+model.name);
body = body.replace(model.token, model.getRenderedData());
}
} else {
console.log(' -- WARNING: BLOCK without name!');
body = body.replace(model.token, model.getRenderedData());
}
}
}
console.log(' -- replace CDF finished!');
return body;
};
};
CdfPage = new Class(new CdfPage());
CdfPage.Event = {};
CdfPage.Event.RENDERED = "PAGE_RENDERED";
CdfPage.Event.CACHE_READY = "PAGE_CACHE_READY";
CdfPage.Event.ERROR = "PAGE_ERROR";
CdfPage.Patterns = {};
CdfPage.Patterns.CDF_TAG_PATTERN = CDF_TAG_PATTERN;
CdfPage.Patterns.CDF_INSERT_PATTERN = CDF_INSERT_PATTERN;
CdfPage.Patterns.CDF_BLOCK_PATTERN = CDF_BLOCK_PATTERN;
CdfPage.Patterns.CDF_CONFIG_PATTERN = CDF_CONFIG_PATTERN;
CdfPage.Patterns.CDF_APPLICATION_PATTERN = CDF_APPLICATION_PATTERN;
exports.CdfPage = CdfPage;