class Runner
{
constructor(obj)
{
if (Runner.validators === undefined)
{
Runner.validators =
new NoValidation()
;
}
if (Runner.crossValidators === undefined)
{
Runner.crossValidators =
new NoValidation()
;
}
if (obj != null)
{
//console.log("Assigning " + JSON.stringify(obj) + " to a Runner");
Object.assign(this, obj);
}
}
isParentOf(anObject)
{
return anObject.runnerId == this.id;
}
validate(result)
{
console.log("Validating " + JSON.stringify(this));
try
{
var ok = Runner.validators.validate(this, result);
console.log(ok + JSON.stringify(result));
return ok;
}
catch(err)
{
console.log(err);
return true;
}
}
crossValidate(other, result)
{
console.log("Cross-validating " + JSON.stringify(this) + " with " + JSON.stringify(other));
try
{
var ok = Runner.crossValidators.validate(this, other, result);
console.log(ok + JSON.stringify(result));
return ok;
}
catch(err)
{
console.log(err);
return true;
}
}
iconTD(mini, info) {
var caption = this.name + (info !== null && info !== '' ? ' ' + info : '');
var html = '';
if (mini) {
html += '
';
html += this.iconIMG(true, caption);
} else {
html += ' | ';
html += this.iconIMG(false, caption);
html += ' ' + caption;
}
html += ' | ';
return html;
}
iconIMG(mini, caption) {
if (mini) {
return '
';
} else {
return '
';
}
}
thumb() {
if (this.photoId === null) {
return this.iconFileName;
} else {
return '/photo/thumb/' + this.photoId +'.png';
}
}
icon() {
if (this.photoId === null) {
return this.iconFileName;
} else {
return '/photo/icon/' + this.photoId +'.png';
}
}
}
function loadRunnerList(url, addCallback, doneCallback, data)
{
console.log((data ? 'POST' : 'GET') + ' ' + url + (data ? data : ''));
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200)
{
var theRunnerList = [];
var rawRunnerList = JSON.parse(this.responseText);
rawRunnerList.forEach( function (rawRunner)
{
var aRunner = new Runner(rawRunner);
theRunnerList.push(aRunner);
addCallback(aRunner);
});
doneCallback(theRunnerList);
}
};
req.open(data ? 'POST' : 'GET', url, true);
req.setRequestHeader("Content-Type", "application/json");
req.send(data);
}
function loadRunner(url, doneCallback)
{
var request = new XMLHttpRequest();
request.open('GET', url, false); // `false` makes the request synchronous
request.send(null);
if (request.status === 200)
{
doneCallback(new Runner(JSON.parse(request.responseText)));
}
else
{
console.log("Got error: " + request);
}
}
function loadAndReturnRunner(url)
{
var request = new XMLHttpRequest();
request.open('GET', url, false); // `false` makes the request synchronous
request.send(null);
if (request.status === 200)
{
return new Runner(JSON.parse(request.responseText));
}
else
{
console.log("Got error: " + request);
return null;
}
}
function loadRunnerListInto(url, selectElementName, doneCallback)
{
console.log('url = ' + url);
var RunnerSelector = document.getElementById(selectElementName);
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200)
{
var rawRunnerList = JSON.parse(this.responseText);
rawRunnerList.forEach( function (rawRunner)
{
var aRunner = new Runner(rawRunner);
var option = document.createElement("option");
option.value = aRunner.id;
option.text = aRunner.name;
RunnerSelector.add(option);
if (doneCallback) doneCallback(RunnerSelector);
});
}
};
req.open('GET', url, true);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.send();
}
function loadRunnerPage(url, doneCallback)
{
console.log('url = ' + url);
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200)
{
var pageResult = JSON.parse(this.responseText);
pageResult.contents = [];
pageResult.pageContents.forEach( function (rawRunner)
{
var aRunner = new Runner(rawRunner);
pageResult.contents.push(aRunner);
});
doneCallback(pageResult);
}
};
req.open('GET', url, true);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.send();
}
function saveRunner(aRunner, url, okCallback, failCallback)
{
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4)
{
console.log(url + ": " + xhr.status + ": " + xhr.responseText);
var result = JSON.parse(this.responseText);
if (this.status == 200 && okCallback)
{
okCallback(result);
}
if (this.status != 200 && failCallback)
{
failCallback(result);
}
}
};
var data = JSON.stringify(aRunner);
console.log("Sending:\n" + data);
xhr.send(data);
}