Source: user_info.js

(
/**
* Contains the functionalities related to the user contact info page.
*
* @module user_info
* @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(){

/**
* Goes through the contact information and sets the primary
* email and phone number to the corresponding fields.
* @memberof module:user_info
* @param {object} user - The current user.
*/
function setUserContactInfo(user){
    for(var i = 0; i < user.info_list.length; i++){
        var info = user.info_list[i];
        if(info.type === "primary_email_id"){
            $("#userInfoEmail").text(info.value);
        }
        if(info.type === "primary_phone_id"){
            $("#userInfoPhone").text(info.value);
        }
    }
}

/**
* Goes through the user roles of the current user and displays the active roles.
* @memberof module:user_info
* @param {object} user - The current user.
*/
function setUserRoles(user){
    for(var i = 0; i < user.roles.length; i++){
        var role = user.roles[i];
        var roleName = role.name;
        if (role.unit_type_name)
            roleName += ", " + role.unit_type_name;
        var roleDiv = $("<div>").text(roleName);
        $("#userInfoRoles").append(roleDiv);
    }
}

/**
* Changes the language preference of the site when the save language button is
* clicked.
* @function
* @name saveLangBtn click
* @memberof module:user_info
*/
$('#saveLangBtn').click(function() {
    var locale = '';
    if($('#user_info_finnish').is(':checked')){
        locale = 'fi';
    } else {
        locale = 'en';
    }
    kepler.setUserLanguage(locale, function(data) {
        location.reload(true);
    });
});

/**
* Checks the radio button that corresponds to the default language of the user.
* @memberof module:user_info
* @param {object} user - The current user.
*/
function setCurrentLanguage(user){
    if (user.language_locale === 'fi') {
       $("#user_info_finnish").prop('checked', true);
    } else {
       $("#user_info_english").prop('checked', true);
    }
}

/**
* 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:user_info
*/
function doc_ready() {
    //Highlight the corresponding navigation areas.
    $("#toggleUserInfo").addClass("active");
    $("#navUserInfo").addClass("active");

    kepler.getUserInfo(function (data) {
        var user = data.user;

        //Set the user's information to the correct fields.
        $("#userInfoUsername").text(user.account_name);
        $("#userInfoName").text(user.first_names + " " + user.last_name);
        setUserContactInfo(user);
        setUserRoles(user);
        setCurrentLanguage(user);
    });
}

$(document).ready(doc_ready);
}());