dl
Version:
DreamLab Libs
230 lines (187 loc) • 7.07 kB
JavaScript
var Class = require("core").Class,
Types = require("core").common.Types;
var CdfTag = require("./CdfTag.js").CdfTag;
/**
* @class CdfBlock
* @extends CdfTag
*/
var CdfBlock = function(){
this.Extends = CdfTag;
this.initialize = function (data) {
this.parent(data);
this.name = null;
this.contentType = CdfBlock.ContentType.JSON;
this.cache = null; // czy tutaj nie powinna byc domyslna wartosc???
this.onError = null;
this.encode = 'none';
this.callback = null;
this._body = null;
this._renderedBody = null;
this._isRendered = false;
if (data) {
this._parse();
}
};
this._parse = function(){
var matches = this.token.match(/^<cdf:block([^>]+)>([\s\S]*)<\/cdf:block>/i);
var attrMatches = matches[1].match(/\S+="\S+?"/ig);
var match = null;
var valuePair = null;
for (var i = 0, max = attrMatches.length; i < max; i++) {
match = attrMatches[i];
valuePair = match.match(/(\S+)="(\S+?)"/i);
this._parseParam(valuePair[1], valuePair[2]);
}
var tmp = matches[2].match(/^<!\[CDATA\[([\s\S]*)\]\]>/i);
if (tmp) {
matches[2] = tmp[1];
}
this.setBody(matches[2]);
if(this.contentType == CdfBlock.ContentType.AJAX) {
// jezeli bloczek ma cache none i jest ajaxowy to cachuj na medium
if (this.cache == CdfBlock.Cache.Label.NONE) {
this.cache = CdfBlock.Cache.Label.MEDIUM;
}
var cdfBlock = this.token.replace('content-type="'+CdfBlock.ContentType.AJAX+'"', 'content-type="' + CdfBlock.ContentType.JSON + '"');
cdfBlock = new Buffer(cdfBlock).toString('base64');
var renderedBlock = '<div class="cdfblock" id="cdf-' + this.name + '" data-cdf-block="'+cdfBlock+'" data-cdf-block-name="' + this.name + '" data-cdf-block-callback="' + this.callback + '"></div>';
this.setRenderedData(renderedBlock);
} else if (!this.isRenderable()) {
this.setRenderedData(this.getBody());
}
};
this._parseParam = function (name, value) {
switch (name) {
case "name":
this.name = value;
break;
case "content-type":
this.contentType = value;
break;
case "cache":
this.cache = value;
break;
case "onError":
this.onError = value;
break;
case "encode":
this.encode = value;
break;
case "callback":
this.callback = value;
break;
default:
console.log('ERROR: CdfBlock::_parseParam : unsupported param name: '+name);
break;
}
};
this.setBody = function (data) {
if (Buffer.isBuffer(data)) {
this._body = data;
} else if (Types.isString(data)) {
this._body = data;
} else {
this._body = JSON.stringify(data);
}
return this;
};
/**
* Returns orginal body
*/
this.getBody = function () {
return this._body;
};
this._encode = function (data) {
if (this.encode=='none') {
return data;
} else if (this.encode == 'json') {
var tmp = JSON.stringify(data);
tmp = tmp.substring(1, tmp.length - 1);
return tmp;
} else if (this.encode == 'url') {
return escape(data);
}
throw('CdfBlock: Inproper "encode" value: ' + this.encode);
};
//Zastanawiam sie nad podejsciem setBody("dane", contentType)
this.setRenderedData = function (data) {
this._isRendered = true;
this._renderedBody = this._encode(data);
};
this.getRenderedData = function () {
return this._renderedBody;
};
this.isRendered = function () {
return this._isRendered;
};
this.isCacheable = function () {
// zakladam ze zawsze jest domyslna wartosc
return this.cache != CdfBlock.Cache.Label.NONE
};
this.getCacheTime = function () {
if (this.cache == CdfBlock.Cache.Label.NONE) {
return CdfBlock.Cache.Value.NONE;
} else if (this.cache == CdfBlock.Cache.Label.MIN) {
return CdfBlock.Cache.Value.MIN;
} else if (this.cache == CdfBlock.Cache.Label.MEDIUM) {
return CdfBlock.Cache.Value.MEDIUM;
} else if (this.cache == CdfBlock.Cache.Label.MAX) {
return CdfBlock.Cache.Value.MAX;
} else {
console.log(' -- WARNING: inproper CDF cache value: '+this.cache);
return CdfBlock.Cache.Value.NONE;
}
};
this.isRenderable = function () {
return this.contentType == CdfBlock.ContentType.JSON;
}
this.isAjaxRenderable = function () {
return this.contentType == CdfBlock.ContentType.AJAX;
}
/**
* Zwraca kompletnego stringa cdf <cdf:
*/
this.toString = function () {
var arrVal = [];
if (this.name) {
arrVal.push('name="' + this.name + '"');
}
if (this.contentType) {
arrVal.push('content-type="' + this.contentType + '"');
}
if (this.cache) {
arrVal.push('cache="' + this.cache + '"');
}
if (this.onError) {
arrVal.push('onError="' + this.onError + '"');
}
if (this.encode) {
arrVal.push('encode="' + this.encode + '"');
}
if (this.callback) {
arrVal.push('callback="' + this.callback + '"');
}
return "<cdf:block " + arrVal.join(" ") + ">"+ '<![CDATA[\n' + this._body+ '\n]]>' + "</cdf:block>\n";
};
};
CdfBlock = new Class(new CdfBlock());
CdfBlock.onError = {};
CdfBlock.onError.ABORT ="abort";
CdfBlock.onError.DISCARD ="discard";
CdfBlock.onError.PROPAGATE ="propagete";
CdfBlock.onError.FORWARD = "forward";
CdfBlock.Cache = {};
CdfBlock.Cache.Label = {};
CdfBlock.Cache.Label.NONE = 'none';
CdfBlock.Cache.Label.MIN = 'min';
CdfBlock.Cache.Label.MEDIUM = 'mdm';
CdfBlock.Cache.Label.MAX = 'max';
CdfBlock.Cache.Value = {};
CdfBlock.Cache.Value.NONE = 0;
CdfBlock.Cache.Value.MIN = 60;
CdfBlock.Cache.Value.MEDIUM = 300;
CdfBlock.Cache.Value.MAX = 18000;
CdfBlock.ContentType = {};
CdfBlock.ContentType.AJAX = 'application/cdf';
CdfBlock.ContentType.JSON = 'application/json';
exports.CdfBlock = CdfBlock;