dl
Version:
DreamLab Libs
112 lines (90 loc) • 3.56 kB
JavaScript
var Core = require("core"),
Class = Core.Class,
Event = Core.event.Event,
ErrorEvent = Core.event.ErrorEvent,
EventDispatcher = Core.event.EventDispatcher,
OpalLoader = require('../../opal/OpalLoader.js').OpalLoader,
Loader = Core.http.Loader,
OpalRequest = require('../../opal/OpalRequest.js').OpalRequest,
Types = Core.common.Types;
/**
* @class DriverAbstract
*/
var DriverAbstract = function () {
this.Extends = EventDispatcher;
/*
this._configuration = null;
this._host = null;
*/
this.initialize = function (application, configuration) {
this.parent();
this._application = application;
this._configuration = configuration;
this._host = null;
};
this.setServiceHost = function (host) {
this._host = host;
return this;
};
this.getServiceHost = function () {
//taki smieszny sposob bo nie da sie do this._host przypisac tej zmiennej bo jest robiona pozniej
if (Types.isNull(this._host)) {
return DriverAbstract.Host.IMAGES;
}
return this._host;
};
this.getShortAppId = function (appId) {
appId = appId.split('.');
if (appId[appId.length-3] == 'front' && appId.length > 5) {
if (appId[0]=='www') {
return appId[1]
} else{
return appId[0]
}
}
return appId[appId.length-3];
};
this.registerUrl = function (url, name) {
var tmpReq = {
'url': this.getServiceHost(),
'method': 'add_file',
'application': this.getShortAppId(this._application),
'params': {
'url': url,
'app_id': this.getShortAppId(this._application)
}
};
if (name) {
tmpReq.params.file_name = name;
}
var opalRequest = new OpalRequest(tmpReq);
// mozliwosc nadpisania gateway opala
//if(this.configuration.args.service.gateway){
// opalRequest.setHost(this.configuration.args.service.gateway.host);
// opalRequest.setPort(this.configuration.args.service.gateway.port);
//}
//opalRequest.setHost('rtx0.int.dev.onet');
var oLoader = new OpalLoader(opalRequest);
oLoader.setTimeout(5000);
oLoader.setFollowRedirects(false);
oLoader.addEventListener(OpalLoader.Event.JSON_RESPONSE, this._onComplete, this);
oLoader.addEventListener(OpalLoader.Event.HTTP_RESPONSE, this._onComplete, this);
oLoader.addEventListener(Loader.Event.ERROR, this._onError, this);
oLoader.load();
};
this._onComplete = function (e) {
this.dispatchEvent(new Event(DriverAbstract.Event.LOADED, e.data));
};
this._onError = function (e) {
this.dispatchEvent(new ErrorEvent(DriverAbstract.Event.ERROR, e, -4, 'DriverAbstract OpalLoader Error'));
};
};
DriverAbstract = new Class(new DriverAbstract());
DriverAbstract.Event = {};
DriverAbstract.Event.READY = 'DriverAbstract.Event.READY';
DriverAbstract.Event.LOADED = 'DriverAbstract.Event.LOADED';
DriverAbstract.Event.ERROR = 'DriverAbstract.Event.ERROR';
DriverAbstract.Host = {};
DriverAbstract.Host.IMAGES = 'images.ocdn.onetapi.pl';
DriverAbstract.Host.FILES = 'files.ocdn.onetapi.pl';
exports.DriverAbstract = DriverAbstract;