(
/**
* Contains the functionalities related to the resources page.
*
* @module resources
* @author Joonas Konki
* @author Anu Koskela
* @author Mikko Kuhno
* @author Henrik Paananen
* @author Atte Räty
* @license BSD 3-clause, see LICENSE for more details.
* @copyright 2015 Kepler project authors
*/
function(){
var resources = null;
var emptyResource = null;
var currentRes = null;
var currentResId = null;
/**
* Calculates the total count for the current resource.
* @memberof module:resources
*/
function updateTotalCount() {
var count = 0;
$("#statuses").find("input.quantity-input").each(function(index, elem){
count += parseInt($(elem).val());
});
$("#totalCount").val(count);
}
/**
* Determines an event for an element. Counts the sum of all status fields.
* @memberof module:resources
*/
function makeUpdateStatuses(i){
return function () {
updateTotalCount();
//TODO (Anu) Check that the input value has an integer!
currentRes.statuses[i].count = parseInt($(this).val());
};
}
/**
* Sets the information of the chosen resource to the corresponding fields.
* Adds a new label and text input fields for each status that the resource has.
* @memberof module:resources
*/
function setCurrentSelection(){
var res = currentRes;
for (var j = 0; j < res.name_desc.length; j++){
if (res.name_desc[j].locale === "fi") {
$("#resource_name_fi").val(res.name_desc[j].name);
$("#resource_descr_fi").val(res.name_desc[j].description);
}
if (res.name_desc[j].locale === "en") {
$("#resource_name_en").val(res.name_desc[j].name);
$("#resource_descr_en").val(res.name_desc[j].description);
}
}
$("#statuses").empty();
$("#statuses").show();
for(var i = 0; i < res.statuses.length; i++){
var helpDiv =$("<div>").css("clear", "both");
var statusLabel = $("<label>").addClass("control-label col-sm-3").text(
res.statuses[i].name + ":");
var statusQuantity = $("<input>").addClass(
"form-control quantity-input").attr(
"type", "number").attr(
"min", "0").val(res.statuses[i].count);
statusQuantity.change(makeUpdateStatuses(i));
var statusQuantityDiv = $("<div>").addClass("col-sm-3").css(
"clear", "right");
statusQuantityDiv.append(statusQuantity);
helpDiv.append(statusLabel);
helpDiv.append(statusQuantityDiv);
$("#statuses").append(helpDiv);
}
var quantityLabel = $("<label>").addClass("control-label col-sm-2").text(
kepler.translate('label.inTotal') + ":");
var count = 0;
$("#statuses").find("input.quantity-input").each(function(index, elem){
count += parseInt($(elem).val());
});
var quantityTotal = $("<input>").addClass("form-control").attr(
"id","totalCount").attr("type","text").attr("disabled",true).val(count);
var totalQuantityDiv = $("<div>").addClass("col-sm-2");
totalQuantityDiv.append(quantityTotal);
$("#statuses").append(quantityLabel);
$("#statuses").append(totalQuantityDiv);
}
/**
* Makes a new object by cloning the empty resource received from the database.
* @memberof module:resources
*/
function setNewResource() {
currentRes = kepler.clone(emptyResource);
currentResId = currentRes.id;
setCurrentSelection();
}
/**
* Displays the information of the current resource in the corresponding fields.
* @memberof module:resources
*/
function fillEditFields(){
$("#noSelectedResource").hide();
$("#editFields").show();
$("#resource_name_fi").val("").attr("placeholder",
"Anna resurssille nimi.");
$("#resource_name_en").val("").attr("placeholder",
"Give a name to the resource.");
$("#resource_descr_fi").val("").attr("placeholder",
"Anna tarkempia tietoja resurssista.");
$("#resource_descr_en").val("").attr("placeholder",
"Give more detailed information about the resource.");
setCurrentSelection();
}
/**
* Fills the current list of resources with the resources
* that match the given string.
* @memberof module:resources
* @param {string} inputText - The string that has to be found in the
* name of every displayed resource.
*/
function updateResList(inputText){
$("#resourceList").empty();
var filter = inputText.toLowerCase();
for (var i = 0; i < resources.length; i++) {
var res = resources[i];
var displayName = res.name;
var displayLower = displayName.toLowerCase();
if (displayLower.indexOf(filter) != -1) {
var option = $("<option>");
option.val(res.id);
option.text(displayName);
$("#resourceList").append(option);
}
}
}
/**
* Empties the edit fields and reloads the current list of resources.
* @memberof module:resources
*/
function resetView() {
$("#noSelectedResource").show();
$("#editFields").hide();
$("#resource_name_fi").val("").removeAttr("placeholder");
$("#resource_name_en").val("").removeAttr("placeholder");
$("#resource_descr_fi").val("").removeAttr("placeholder");
$("#resource_descr_en").val("").removeAttr("placeholder");
$("#resourceList").empty();
$("#statuses").empty();
$("#statuses").hide();
resourceList.load();
}
/**
* This is executed after the HTML page has been loaded.
* This is a common procedure of all of the client-side modules to
* initialise the page content.
* @memberof module:resources
*/
function doc_ready() {
$("#navResources").addClass("active");
$("#statuses").hide();
resourceList = kepler.createLoadElement("#resourceList",function(element) {
call = kepler.getResources(function (data) {
resources = data.resources;
emptyResource = data.empty_resource;
for (var i = 0; i < resources.length; i++) {
var res = resources[i];
var option = $("<option>");
option.attr("value", res.id);
option.text(res.name);
element.append(option);
}
if(currentRes === null && currentResId !== null) {
for (var k = 0; k < resources.length; k++){
if (resources[k].id === currentResId) {
currentRes = resources[k];
break;
}
}
}
if(currentRes !== null) {
$('#resourceList option[value="' + currentResId +
'"]').prop("selected", true);
fillEditFields();
}
});
return call;
});
resourceList.load();
$("#searchField").on("input", function(v){
var inputText = v.target.value;
updateResList(inputText);
});
/**
* Empties the edit fields and sets placeholders. Initialises the resource
* status fields.
*
* @function
* @name btnAddNewResource click
* @inner
* @memberof module:resources
*/
$("#btnAddNewResource").click(function(){
$("#noSelectedResource").hide();
$("#editFields").show();
$("#resource_name_fi").val("").attr("placeholder",
"Anna resurssille nimi.");
$("#resource_name_en").val("").attr("placeholder",
"Give a name to the resource.");
$("#resource_descr_fi").val("").attr("placeholder",
"Anna tarkempia tietoja resurssista.");
$("#resource_descr_en").val("").attr("placeholder",
"Give more detailed information about the resource.");
currentRes = null;
currentResId = null;
setNewResource();
});
/**
* Makes a clone of the selected resource and fill the edit fields with its
* information.
*
* @function
* @name resourceList change
* @inner
* @memberof module:resources
*/
$("#resourceList").change(function (v) {
if (currentResId !== v.target.value && v.target.value !== "") {
for (var i = 0; i < resources.length; i++) {
if (resources[i].id === parseInt(v.target.value)){
currentRes = kepler.clone(resources[i]);
currentResId = resources[i].id;
}
}
fillEditFields();
}
});
/**
* If the id of the current resource is -1, makes a call to create
* a new resource with the given parameters.
* Otherwise makes a call to save changes to the current resource.
* If any important fields are empty, shows a notification without
* making a call. Updates the view after the call has been completed.
*
* @function
* @name btnSaveChanges click
* @inner
* @memberof module:resources
*/
$("#btnSaveChanges").click(function() {
if(currentResId === null) {
alert(kepler.translate('content.resources_select_or_new') + "!");
//TODO (Anu) Don't use an alert!
} else if (currentRes.id !== -1) {
var params = {
"name_fi": $("#resource_name_fi").val(),
"name_en": $("#resource_name_en").val(),
"description_fi": $("#resource_descr_fi").val(),
"description_en": $("#resource_descr_en").val(),
"resource_id": currentResId,
"statuses": currentRes.statuses,
};
console.log(params);
kepler.editResource(
params,
function (data) {
kepler.showResult(data.result, "#notification");
currentRes = null;
resetView();
}
);
} else {
if($("#resource_name_fi").val() === "" &&
$("#resource_name_en").val() === ""){
alert(kepler.translate('label.give_name') + "!");
//TODO (Anu) Don't use alerts!
return;
} else {
var params = {
"name_fi": $("#resource_name_fi").val(),
"name_en": $("#resource_name_en").val(),
"description_fi": $("#resource_descr_fi").val(),
"description_en": $("#resource_descr_en").val(),
"statuses": currentRes.statuses,
};
//console.log("Tehdään uutta resurssia. Parametrit ovat:");
//console.log(params);
kepler.addResource(
params,
function (data) {
kepler.showResult(data.result, "#notification");
currentResId = data.result.resource_id;
currentRes = null;
resetView();
}
);
}
}
});
/**
* If a current resource is selected, displays a confirmation dialog and
* removes the resource from the database if approved by the user.
*
* @function
* @name btnDeleteResource click
* @inner
* @memberof module:resources
*/
$("#btnDeleteResource").click(function() {
if(currentResId === null) {
alert(kepler.translate('content.resource.not_selected') + ".");
// TODO Don't use an alert
} else {
$("#modalButtonYes").off();
/**
* Makes a call to remove the current resource from the database.
*
* @function
* @name modalButtonYes click
* @inner
* @memberof module:resources
*/
$("#modalButtonYes").click(function () {
if (currentRes.id === -1) {
currentRes = null;
currentResId = null;
resetView();
} else {
kepler.deleteResource(
{ "resource_id": currentResId },
function (data) {
kepler.showResult(data.result, "#notification");
currentRes = null;
currentResId = null;
resetView();
}
);
}
});
$("#modalLabel").text(currentRes.name);
$("#modalCancel").modal('show');
}
});
}
$(document).ready(doc_ready);
}());