Source: edit_notice_board.js

(
/**
* Contains the functionalities related to the edit notice board page.
*
* @module edit_notice_board
* @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 editNoticeBoardDiv = null;
var notes = null;

/**
* Makes the given note hidden in the database and updates the view of
* the notice board.
* @memberof module:edit_notice_board
* @param note - Contains the information of the note to be hidden.
*/
function make_hideNote(note) {
    return function () {
        var params = {note_id: note.id};
        kepler.hideNoticeBoardNote(params, function(data) {
            kepler.showResult(data.result, "#notification");
            if (data.result.success) {
                reloadEditNoticeBoard();
            }
        });
    };
}

/**
* Makes the given hidden note visible again in the database and updates the
* view of the notice board.
* @memberof module:edit_notice_board
* @param note - Contains the information of the note to be made visible.
*/
function make_showNote(note) {
    return function () {
        var params = {note_id: note.id};
        kepler.showNoticeBoardNote(params, function(data) {
            kepler.showResult(data.result, "#notification");
            if (data.result.success) {
                reloadEditNoticeBoard();
            }
        });
    };
}

/**
* Shows editable information related to a note and saves it to
* the database.
* @memberof module:edit_notice_board
*/
function addNote() {
    $("#modalButtonYes").off();
    $("#modalButtonYes").click(function () {
        params = {
            title: $("#modalLabel").val(),
            message: $("#modalText").val()
        };
        kepler.addNoticeBoardNote(params, function(data) {
            kepler.showResult(data.result, "#notification");
            if (data.result.success) {
                reloadEditNoticeBoard();
            }
        });
    });
    $("#modalLabel").val("");
    $("#modalText").text("");
    $("#modalEdit").modal('show');
}

/**
* Shows information of the editable note, saves the changes to the database and
* updates the view of the notice board.
* @memberof module:edit_notice_board
* @param note - Contains the information of the note to be edited.
*/
function make_editNote(note) {
    return function() {
        $("#modalButtonYes").off();
        $("#modalButtonYes").click(function () {
            params = {
                note_id: note.id,
                title: $("#modalLabel").val(),
                message: $("#modalText").val()
            };
            kepler.editNoticeBoardNote(params, function(data) {
                kepler.showResult(data.result, "#notification");
                if (data.result.success) {
                    reloadEditNoticeBoard();
                }
            });
        });
        $("#modalLabel").val(note.title);
        $("#modalText").text(note.message);
        $("#modalEdit").modal('show');
    };
}

/**
* Initialises the notice board with given notes from the database.
* @memberof module:edit_notice_board
* @param note - Contains the information of all of the notes.
*/
function initNoticeBoard(notes) {
    editNoticeBoardDiv.empty();

    var showHidden = $("#showHidden").is(":checked");

    for (var i = 0; i < notes.length; i++) {
        var note = notes[i];
        if (note.hidden && !showHidden) {
            continue;
        }

        var createTime = kepler.formatDatetime(note.created_at);

        var noteDiv = $('<div>').addClass('list-group-item');
        noteDiv.addClass('note');

        if (note.hidden) {
            noteDiv.addClass('hidden-note');
        }

        var noteHeaderDiv = $('<h4>').addClass("list-group-item-heading");
        noteHeaderDiv.addClass("note-header");

        var titleSpan = $("<span>").text(note.title);
        noteHeaderDiv.append(titleSpan);

        if (note.hidden) {
            var hiddenSpan = $("<span>").addClass('specifier');
            hiddenSpan.text('(' + kepler.translate('msg.hidden') + ')');
            noteHeaderDiv.append(hiddenSpan);
        }

        var noteCreatedSpan = $('<span>').addClass('created-time');
        noteCreatedSpan.text(createTime);
        noteHeaderDiv.append(noteCreatedSpan);

        var creatorSpan = $("<span>").addClass('creator-name');
        creatorSpan.text(note.creator);
        noteHeaderDiv.append(creatorSpan);

        noteDiv.append(noteHeaderDiv);

        var noteMessageDiv = $('<div>').addClass("list-group-item-text");
        noteMessageDiv.addClass('note-message');
        noteMessageDiv.append($('<p>').text(note.message));
        noteDiv.append(noteMessageDiv);

        var editBtnDiv = $("<div>").attr("align", "right");

        var editBtn = $("<button>").addClass("btn btn-default");
        editBtn.text(kepler.translate('button.edit'));
        editBtn.click(make_editNote(note));
        editBtnDiv.append(editBtn);

        var showHideBtn = $("<button>").addClass("btn btn-default");
        if (note.hidden) {
            showHideBtn.text(kepler.translate('button.show'));
            showHideBtn.click(make_showNote(note));
        } else {
            showHideBtn.text(kepler.translate('button.hide'));
            showHideBtn.click(make_hideNote(note));
        }
        editBtnDiv.append(showHideBtn);

        noteDiv.append(editBtnDiv);

        if (note.modified_at) {
            var modifyTime = kepler.formatDatetime(note.modified_at);

            var modifiedDiv = $('<div>').addClass('modify-info');

            var modifiedTimeDiv = $('<span>').addClass('modified-time');
            modText = kepler.translate('note.last_modified');
            modifiedTimeDiv.text(modText + ": " + modifyTime);
            modifiedDiv.append(modifiedTimeDiv);

            if (note.modifier) {
                var modifierDiv = $('<span>').addClass('modifier-name');
                modifierDiv.text(note.modifier);
                modifiedDiv.append(modifierDiv);
            }

            noteDiv.append(modifiedDiv);

            if (note.modified_at === note.created_at) {
                modifiedDiv.css('visibility', 'hidden');
            }
        }


        editNoticeBoardDiv.append(noteDiv);
    }
}

/**
* Reloads all of the notes to the notice board from the database.
* @memberof module:edit_notice_board
*/
function reloadEditNoticeBoard() {
    kepler.getNoticeBoardNotes(function (data) {
        notes = data.notes;
        initNoticeBoard(notes);
    });
}

/**
* 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:edit_notice_board
*/
function doc_ready(){
    $("#navEditNotificationBoard").addClass("active");

    $("#showHidden").change(function () {
        if (notes) {
            initNoticeBoard(notes);
        }
    });

    editNoticeBoardDiv = $('#editNoticeBoard');
    reloadEditNoticeBoard();

    $("#addNote").click(addNote);
}

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