src/main/ChatController.js
/**
 * For each chat to open, uses the loaded data to build chat list at left side of screen, showing last messages.
 */
For each chat to open, uses the loaded data to build chat list at left side of screen, showing last messages.
For each chat to open uses the loaded data to build chat list at left side of screen showing last messages .
async function loadChats() {
    semanticChats.sort(function(a, b) {
        var x = a.getLastMessage().time;
        var y = b.getLastMessage().time;
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    });
    if (semanticChats.length > 0) {
        $(".contact-list").html("");
    }
    semanticChats.forEach(async chat => {
        contactsWithChat.splice(semanticChats.indexOf(chat), 0, chat.interlocutorWebId);
        var lastMsg = chat.getLastMessage().messagetext;
        var lastHr = "";
        if (!lastMsg) {
            lastMsg = "Sin mensajes";
        } else {
            if (lastMsg.includes("data:image")) {
                lastMsg = "<img alt = 'uploaded'  src = '" + lastMsg + "'" + "/>";
            } else if (lastMsg.includes("data:video")) {
                lastMsg = "<video width='20' height='20'> <source src= '" + lastMsg + "'> Your browser does not support HTML5 video. </video>";
            } else if (lastMsg.includes("data:text")) {
                lastMsg = "<a class='disable' href='" + lastMsg + "'>" +
                    "Text File</a>";
            } else {
                lastMsg = lastMsg.replace(/\:(.*?)\:/g, "<img src='main/resources/static/img/$1.gif' alt='$1'></img>");
            }
            lastHr = chat.getHourOfMessage(chat.getNumberOfMsgs() - 1);
        }
        const newmsg = 0;
        if (newmsg == 0) {
            var html = "<div style='cursor: pointer;' class='contact' id='chatwindow" + chatCounter +
                "'><img src='" + chat.photo + "' alt='!' onerror='this.onerror=null;this.src='http://www.philosophica.info/voces/aquino/Aquino.jpg';'><div class='contact-preview'><div class='contact-text'><h1 class='font-name'>" + chat.interlocutorName +
                "</h1><p class=font-preview' id='lastMsg" + chatCounter + "'>" +
                lastMsg +
                "</p></div></div><div class='contact-time'><p>" + lastHr + "</p></div></div>";
        } else {
            var html = $("<div class='contact new-message-contact' id='" + chatCounter + "'><img src='" + chat.photo +
                "' alt='!' onerror='this.onerror=null;this.src='http://www.philosophica.info/voces/aquino/Aquino.jpg';'><div class='contact-preview'><div class='contact-text'><h1 class='font-name'>" + chat.interlocutorName + "</h1><p class='font-preview' id='lastMsg" + chatCounter + "'>" +
                lastMsg + "</p></div></div><div class='contact-time'><p>" + "?" + "</p><div class='new-message' id='nm" + lastHr + "'><p>" + "1" + "</p></div></div></div>");
        }
        $(".contact-list").prepend(html);
        document.getElementById("chatwindow" + chatCounter).addEventListener("click", loadMessagesToWindow, false);
        chatCounter += 1;
    });
    console.log(semanticChats);
    console.log(contactsWithChat);
}
async function loadChats ( ) { semanticChats . sort ( function ( a , b ) { var x = a . getLastMessage ( ) . time ; var y = b . getLastMessage ( ) . time ; return ( ( x < y ) ? - 1 : ( ( x > y ) ? 1 : 0 ) ) ; } ) ; if ( semanticChats . length > 0 ) { $ ( ".contact-list" ) . html ( "" ) ; } semanticChats . forEach ( async chat => { contactsWithChat . splice ( semanticChats . indexOf ( chat ) , 0 , chat . interlocutorWebId ) ; var lastMsg = chat . getLastMessage ( ) . messagetext ; var lastHr = "" ; if ( ! lastMsg ) { lastMsg = "Sin mensajes" ; } else { if ( lastMsg . includes ( "data:image" ) ) { lastMsg = "<img alt = 'uploaded'  src = '" + lastMsg + "'" + "/>" ; } else if ( lastMsg . includes ( "data:video" ) ) { lastMsg = "<video width='20' height='20'> <source src= '" + lastMsg + "'> Your browser does not support HTML5 video. </video>" ; } else if ( lastMsg . includes ( "data:text" ) ) { lastMsg = "<a class='disable' href='" + lastMsg + "'>" + "Text File</a>" ; } else { lastMsg = lastMsg . replace ( / \:(.*?)\: / g , "<img src='main/resources/static/img/$1.gif' alt='$1'></img>" ) ; } lastHr = chat . getHourOfMessage ( chat . getNumberOfMsgs ( ) - 1 ) ; } const newmsg = 0 ; if ( newmsg == 0 ) { var html = "<div style='cursor: pointer;' class='contact' id='chatwindow" + chatCounter + "'><img src='" + chat . photo + "' alt='!' onerror='this.onerror=null;this.src='http://www.philosophica.info/voces/aquino/Aquino.jpg';'><div class='contact-preview'><div class='contact-text'><h1 class='font-name'>" + chat . interlocutorName + "</h1><p class=font-preview' id='lastMsg" + chatCounter + "'>" + lastMsg + "</p></div></div><div class='contact-time'><p>" + lastHr + "</p></div></div>" ; } else { var html = $ ( "<div class='contact new-message-contact' id='" + chatCounter + "'><img src='" + chat . photo + "' alt='!' onerror='this.onerror=null;this.src='http://www.philosophica.info/voces/aquino/Aquino.jpg';'><div class='contact-preview'><div class='contact-text'><h1 class='font-name'>" + chat . interlocutorName + "</h1><p class='font-preview' id='lastMsg" + chatCounter + "'>" + lastMsg + "</p></div></div><div class='contact-time'><p>" + "?" + "</p><div class='new-message' id='nm" + lastHr + "'><p>" + "1" + "</p></div></div></div>" ) ; } $ ( ".contact-list" ) . prepend ( html ) ; document . getElementById ( "chatwindow" + chatCounter ) . addEventListener ( "click" , loadMessagesToWindow , false ) ; chatCounter += 1 ; } ) ; console . log ( semanticChats ) ; console . log ( contactsWithChat ) ; }
For each chat to open, uses the loaded data to build chat list at left side of screen, showing last messages.
For each chat to open uses the loaded data to build chat list at left side of screen showing last messages .