r3-legacy/todo/soc-query.js

93 lines
1.8 KiB
JavaScript

SOC.Query = function (options) {
if (options && options.path) {
this.path = options.path;
} else {
this.path = null;
}
if (options && options.queryStart) {
this.queryStart = options.queryStart;
} else {
this.queryStart = 'now-7d';
}
if (options && options.queryEnd) {
this.queryEnd = options.queryEnd;
} else {
this.queryEnd = 'now+4h';
}
if (options && options.query) {
this.query = options.query;
} else {
this.query = null;
}
if (options && options.onData) {
this.onData = options.onData;
} else {
this.onData = function() {
throw new Error(
"You should specify a onData function in the constructor to your query, ex. SOC.Query.SomeQuery({onData : myCallbackFunction})"
)
};
}
this.run();
};
SOC.Query.prototype = Object.create(SOC.prototype);
SOC.Query.prototype.constructor = SOC;
SOC.Query.prototype.run = function() {
if (this.path === null) {
throw new Error('query path must be set');
}
if (this.query === null) {
throw new Error('query must be set');
}
var xhr = new XMLHttpRequest();
xhr.open(
'POST',
this.apiUrl + this.path,
true
);
xhr.onload = function(event){
console.log('loaded');
this.onData(JSON.parse(event.target.responseText));
}.bind(this);
xhr.onerror = function(event) {
throw new Error('An error occured during query execution');
};
xhr.setRequestHeader(
'Content-Type',
'application/json'
);
var queryText = JSON.stringify(this.query);
queryText = queryText.replace('%QUERY_START', this.queryStart);
queryText = queryText.replace('%QUERY_END', this.queryEnd);
xhr.send(
queryText
);
};
SOC.Query.prototype.drawTable = function(divId, data) {
return function() {
}
};