// The host URL for the RESTful services
//var hostURL = window.location.protocol + '//' + window.location.hostname;
//if (window.location.port != 80) {
// hostURL += ':' + window.location.port;
//}
var hostURL = '';
// The root URL for the RESTful services
var rootURL = "/cloud/fujitsu/";
// Data type of response: use "json" for same server URL or "jsonp" for different server URL
var dataType = "json";
// List of top resources available via the REST API
var topResources = new Array();
var cachedList = new Array();
var currentType;
var currentId;
var currentWine;
// Initialize console if available
try {
console.log("Loading rest.js")
}
catch(e) {
console={},
console.log=function(a){}
}
// http://stackoverflow.com/questions/159914/css-javascript-use-div-to-grey-out-section-of-page
//function to grey out the screen
/*
$(function() {
// Create overlay and append to body:
$('
').css({
opacity: 0.5,
position: 'fixed',
top: 0,
left: 0,
width: '100%',
height: $(window).height() + 'px',
background: 'white no-repeat center'
}).hide().appendTo('body');
});
// background: 'white url(../images/loading.gif) no-repeat center'
*/
// see also http://api.jquery.com/ajaxError/
$('#json_log').hide()
$('#json_log').ajaxError(function(xhr, ajaxOptions, thrownError) {
// CHECKME: this also fires when doing a redirect using jsonp !!!
//alert('status: ' + ajaxOptions.status + '-' + ajaxOptions.statusText + ' \n' + 'error:\n' + ajaxOptions.responseText);
$('#json_error').val('status: ' + ajaxOptions.status + '-' + ajaxOptions.statusText + ' \n' + 'error:\n' + ajaxOptions.responseText);
$(this).show();
$("#tabs").tabs("select", 3);
});
// Retrieve resource types when application starts
getResourceTypes();
// Nothing to delete in initial application state
//$('#btnDelete').hide();
// Register listeners
$('#btnSearch').click(function() {
search($('#searchKey').val());
return false;
});
// Trigger search when pressing 'Return' on search key input field
$('#searchKey').keypress(function(e){
if(e.which == 13) {
search($('#searchKey').val());
e.preventDefault();
return false;
}
});
$('#btnAdd').click(function() {
newResourceItem(currentType);
return false;
});
$('#btnSave').click(function() {
if ($('#wineId').val() == '')
addWine();
else
updateWine();
return false;
});
$('#btnDelete').click(function() {
deleteWine();
return false;
});
$('#wineList a').live('click', function() {
findById($(this).data('identity'));
});
// Replace broken images with generic wine bottle
$("img").error(function(){
$(this).attr("src", "pics/generic.jpg");
});
$('#resourcetypes a').live('click', function() {
getResourceList($(this).data('resource'));
});
$('#resourcemenu a').live('click', function() {
if ($(this).data('resource') == currentType) {
/*
$('#resource_item').hide();
// show all rows again
$('#resourcelist tr').each(function(index, row) {
$(row).show();
});
*/
currentId = '';
}
else {
getResourceList($(this).data('resource'));
}
});
$('#resourceprop a').live('click', function() {
// TODO: add link to get child resource list
if (currentId) {
getResourceList($(this).data('resource'), currentId);
}
});
$('#resourcelist a').live('click', function() {
// hide all rows except the current one
/*
var rowidx = $(this).parents('tr').index();
var rows = $(this).parents('tbody').children('tr');
$.each(rows, function(index, row) {
if (rowidx != index) {
$(row).hide();
}
});
*/
// TODO: add link to get child resource list ?
//alert(rows.length);
getResourceItem($(this).data('identity'), $(this).data('resource'));
});
$('#itemproperties a').live('click', function() {
getResourceItem($(this).data('identity'), $(this).data('resource'));
});
function search(searchKey) {
if (searchKey == '')
getResourceTypes();
else
findByName(searchKey);
}
function newWine() {
$('#btnDelete').hide();
currentWine = {};
renderDetails(currentWine); // Display empty form
}
function findByName(searchKey) {
console.log('findByName: ' + searchKey);
$.ajax({
type: 'GET',
url: rootURL + '/search/' + searchKey,
dataType: dataType,
success: showResourceList
});
}
function findById(id) {
console.log('findById: ' + id);
$.ajax({
type: 'GET',
url: rootURL + '/' + id,
dataType: dataType,
success: function(data){
$('#btnDelete').show();
console.log('findById success: ' + data.name);
currentWine = data;
showResourceItem(currentWine);
}
});
}
function addWine() {
console.log('addWine');
$('#json_query').val('$.ajax({\n type: "POST",\n url: "' + rootURL + '",\n dataType: "' + dataType + '",\n success: ' + 'TODO' + '\n});');
$('#json_result').val('');
$('#json_log').hide()
$('#json_error').val('');
$.ajax({
type: 'POST',
contentType: 'application/json',
url: rootURL,
dataType: dataType,
data: formToJSON(),
success: function(data, textStatus, jqXHR){
alert('Wine created successfully');
$('#btnDelete').show();
$('#wineId').val(data.id);
},
error: function(jqXHR, textStatus, errorThrown){
alert('addWine error: ' + textStatus);
}
});
}
function updateWine() {
console.log('updateWine');
$('#json_query').val('$.ajax({\n type: "PUT",\n url: "' + rootURL + '",\n dataType: "' + dataType + '",\n success: ' + 'TODO' + '\n});');
$('#json_result').val('');
$('#json_log').hide()
$('#json_error').val('');
$.ajax({
type: 'PUT',
contentType: 'application/json',
url: rootURL + '/' + $('#wineId').val(),
dataType: dataType,
data: formToJSON(),
success: function(data, textStatus, jqXHR){
alert('Wine updated successfully');
},
error: function(jqXHR, textStatus, errorThrown){
alert('updateWine error: ' + textStatus);
}
});
}
function deleteWine() {
console.log('deleteWine');
$('#json_query').val('$.ajax({\n type: "DELETE",\n url: "' + rootURL + '",\n dataType: "' + dataType + '",\n success: ' + 'TODO' + '\n});');
$('#json_result').val('');
$('#json_log').hide()
$('#json_error').val('');
$.ajax({
type: 'DELETE',
url: rootURL + '/' + $('#wineId').val(),
success: function(data, textStatus, jqXHR){
alert('Wine deleted successfully');
},
//error: function(jqXHR, textStatus, errorThrown){
//alert('deleteWine error');
//}
});
}
function getResourceTypes() {
console.log('getResourceTypes');
var todo = "showResourceTypes";
$('#resource_list').hide();
$('#child_list').hide();
//$('#resource_item').hide();
$('#json_query').val('$.ajax({\n type: "GET",\n url: "' + hostURL + rootURL + '",\n dataType: "' + dataType + '",\n success: ' + todo + '\n});');
$('#json_result').val('');
//$('#json_log').hide()
$('#json_error').val('');
$.ajax({
type: 'GET',
url: hostURL + rootURL,
dataType: dataType,
success: showResourceTypes
});
}
function showResourceTypes(data) {
console.log('showResourceTypes');
$('#json_result').val(JSON.stringify(data));
// JAX-RS serializes an empty list as null, and a 'collection of one' as an object (not an 'array of one')
var list = data == null ? [] : (data instanceof Array ? data : [data]);
$('#resourcetypes tr').remove();
$('#api_name').val(data.api.name);
$('#api_version').val(data.api.version);
$('#api_link').val(data.api.href);
// $('#api_description').val(data.api.description);
$('#api_description').html('
' + data.api.description.split('\n').join('
') + '
');
// alert(object_to_var(data));
var str = '';
var menu_str = '';
$.each(data.resources, function(index, toplevel) {
if (index % 2 == 0)
str += '
';
$('#resourceprop tr').remove();
$('#resourceprop').append(str);
// Add resource list
str = '';
for (var i in data) {
// TODO: add link to resourceList for Array properties !?
if (i % 2 == 0)
str += '
';
else
str += '
';
for (var a in attr) {
var p = attr[a];
var obj = data[i][p];
if (p == 'href') {
str += '