dl
Version:
DreamLab Libs
146 lines (133 loc) • 5.02 kB
JavaScript
var Class = require('core').Class,
Types = require('core').common.Types;
var CdfBlock = require('./CdfBlock.js').CdfBlock;
/**
* @class CdfApplication
* @extends CdfBlock
*/
var CdfApplication = function () {
this.Extends = CdfBlock;
this.initialize = function (data) {
this.name = null;
this.controller = null;
this.controllerType = null;
this.src = null;
this.cache = null;
this.credential = null;
this.onError = CdfBlock.onError.ABORT;
this.overrideMode = CdfApplication.OVERRIDE_MODE.FULL;
this.method = null;
this.timeout = null;
this.cacheByHeader = null;
this.cacheByCookie = null;
this.trustedCookie = null;
this.postBody = null;
this.parent(data);
this._isRendered = true;
};
this._parse = function(){
var matches = this.token.match(/^<cdf:application ([^>]+)><\/cdf:application>$/i);
if (!matches) {
return;
}
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]);
}
if (this.src == null) {
this.src = this.name;
}
};
this._parseParam = function (name, value) {
switch (name) {
case "name":
this.name = value;
break;
case "controller-type":
this.controllerType = value;
break;
case "controller":
this.controller = value;
break;
case "src":
this.src = value;
break;
case "cache":
this.cache = value;
break;
case "credential":
this.credential = value;
break;
case "onError": //TODO: testy
this.onError = value;
break;
case CdfApplication.OVERRIDE_MODE_TAG:
if (CdfApplication.OVERRIDE_MODES.indexOf(value) > -1) {
this.overrideMode = value;
} else {
console.warn('Invalid value for override-mode in cdf:application:', value);
}
break;
case "method":
if ( CdfApplication.ALLOWED_REQUEST_METHOD.indexOf(value.toLowerCase()) > -1 ) {
this.method = value;
} else {
console.warn('Invalid value for method in cdf:application: (allowed: post, get)', value);
}
break;
case "timeout":
this.timeout = value;
break;
case "cache-by-header":
this.cacheByHeader = value;
break;
case "cache-by-cookie":
this.cacheByCookie = value;
break;
case "trusted-cookie":
this.trustedCookie = value;
break;
default:
console.log('ERROR: CdfApplication::_parseParam : unsupported param name: '+name);
break;
}
};
this._getPostBody = function () {
if (Types.isString(this.postBody)) {
return this.postBody;
} else {
return JSON.stringify(this.postBody);
}
}
this.toString = function () {
return '<cdf:application ' +
( this.method ? 'method="' + this.method + '" ' : '') +
( this.timeout ? 'timeout="' + this.timeout + '" ' : '') +
'src="' + this.src + '" ' +
'name="' + this.name + '" ' +
'controller="' + this.controller +'" ' +
'controller-type="' + this.controllerType + '" ' +
'onError="' + this.onError + '" ' +
'cache="' + this.cache + '" ' +
( this.cacheByHeader ? 'cache-by-header="' + this.cacheByHeader.join(' ') + '" ' : '') +
( this.cacheByCookie ? 'cache-by-cookie="' + this.cacheByCookie.join(' ') + '" ' : '') +
( this.trustedCookie ? 'trusted-cookie="' + this.trustedCookie.join(' ') + '" ' : '') +
( this.credential ? 'credential="' + this.credential + '" ' : '') +
CdfApplication.OVERRIDE_MODE_TAG +'="' + this.overrideMode + '">' +
( this.postBody ? '<![CDATA[\n' + this._getPostBody() + '\n]]>' : '' ) +
'</cdf:application>';
};
};
CdfApplication = new Class(new CdfApplication());
CdfApplication.OVERRIDE_MODE_TAG = "override-mode";
CdfApplication.OVERRIDE_MODE = {};
CdfApplication.OVERRIDE_MODE.FULL = "full";
CdfApplication.OVERRIDE_MODE.URI = "uri";
CdfApplication.OVERRIDE_MODE.NONE = "none";
CdfApplication.OVERRIDE_MODES = [CdfApplication.OVERRIDE_MODE.FULL, CdfApplication.OVERRIDE_MODE.URI, CdfApplication.OVERRIDE_MODE.NONE];
CdfApplication.ALLOWED_REQUEST_METHOD = ['post', 'get'];
exports.CdfApplication = CdfApplication;