/**
* Copyright (c) 2013/2014, Intel Performance Learning Solutions Ltd, Intel Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
**/
/**
* Contains functions to to support API Tester webpage functions
*
* Created by : Sridhar Voorakkara (vsridhar420@gmail.com)
*/
var waitWin;
var authenticate = function() {
clearApiMessage();
var form = document.forms["auth"];
var url = "api/user/"+ form["userid"].value +"/authorize";
//validation
if (form["userid"].value.length ==0) {
alert("'userid' is a required field");
return;
}
if (form["password"].value.length ==0) {
alert("'password' is a required field");
return;
}
var qs = "password="+ form.password.value;
//display waiting image
displayApiWait();
//Do ajax post
xmlhttpPost(url, qs, handleApiResponse);
};
var createproject = function() {
clearApiMessage();
var form = document.forms["proj_create"];
var url = "api/project/create";
//validation
if (form["projectname"].value.length ==0) {
alert("'projectname' is a required field");
return;
}
var qs = "projectname="+ form.projectname.value;
if (form.projectdescr.value.length > 0)
qs = qs + "&projectdescr="+ form.projectdescr.value;
if (form.riskdocument.value.length > 0)
qs = qs + "&riskdocument="+ form.riskdocument.value;
if (form.ontologyUri.value.length > 0)
qs = qs + "&ontologyUri="+ form.ontologyUri.value;
//display waiting image
displayApiWait();
//Do ajax post
xmlhttpPost(url, qs, handleApiResponse, form.authenticationKey.value);
};
var updateproject = function() {
clearApiMessage();
var form = document.forms["proj_update"];
//validation
if (form["projectid"].value.length ==0) {
alert("'projectid' is a required field");
return;
}
if (form.riskdocument.value.length == 0 && form.ontologyUri.value.length == 0) {
alert("'riskdocument' or 'ontologyUri' values should be provided");
return
}
var url = "api/project/"+ form.projectid.value +"/update";
var qs = "dummy=0";
if (form.riskdocument.value.length > 0)
qs = qs + "&riskdocument="+ form.riskdocument.value;
if (form.ontologyUri.value.length > 0)
qs = qs + "&ontologyUri="+ form.ontologyUri.value;
//display waiting image
displayApiWait();
//Do ajax post
xmlhttpPost(url, qs, handleApiResponse, form.authenticationKey.value);
};
var getriskalternatives = function() {
clearApiMessage();
var form = document.forms["risk_alternatives_get"];
var projectId = form.projectid.value;
var url = "api/project/"+projectId+"/riskalternatives";
var qs = "";
displayApiWait();
xmlhttpPost(url, qs, handleApiResponse, form.authenticationKey.value);
};
var getstatus = function() {
clearApiMessage();
var form = document.forms["status_get"];
var projectId = form.projectid.value;
var url = "api/project/"+projectId+"/status";
var qs = "";
displayApiWait();
xmlhttpPost(url, qs, handleApiResponse, form.authenticationKey.value);
};
var sendrecommendation = function() {
clearApiMessage();
var form = document.forms["recommendation_set"];
var projectId = form.projectid.value;
var url = "api/project/"+projectId+"/recommendation";
var qs = "preserve="+form.preserve.value+"&ontologyuri="+form.ontologyuri.value;
displayApiWait();
xmlhttpPost(url, qs, handleApiResponse, form.authenticationKey.value);
};
var handleApiResponse = function (err, result) {
clearApiMessage();
displayApiMessage(err ? JSON.stringify(err) : result);
};
//remove a user feedback message
var clearApiMessage = function () {
document.getElementById("feedback").innerHTML = "";
};
//show a user feedback message
var displayApiMessage = function (msg) {
//document.getElementById("feedback").innerHTML = "<textarea>" + msg + "</textarea>";
var height = screen.height/2;
var width = screen.width/2;
var top = height/2;
var left = width/2;
var settings = "width="+width+",height="+height+"scrollbars=yes,top="+top+",left="+left;
if (waitWin)
waitWin.close();
msg = "API Response:<br><textarea style='width:100%;height:90%'>" + msg + "</textarea>";
var respWin = window.open("", "", settings, "center");
respWin.document.write(msg);
};
//show a wait graphic to the user
var displayApiWait = function () {
var height = screen.height/2;
var width = screen.width/2;
var top = height/2;
var left = width/2;
var settings = "width="+width+",height="+height+"scrollbars=yes,top="+top+",left="+left;
waitWin = window.open("", "respWin", settings, "center");
waitWin.document.write("<img src='resources/images/ajaxloader.gif'>");
//document.getElementById("feedback").innerHTML = "<img src='resources/images/ajaxloader.gif'>";
};