diff options
1 files changed, 49 insertions, 0 deletions
| -rw-r--r-- | scripts/core/home.js | 49 |
diff --git a/scripts/core/home.js b/scripts/core/home.js index b9c4ea8..df46b87 100644 --- a/scripts/core/home.js +++ b/scripts/core/home.js @@ -34,6 +34,7 @@ const supportedCommands = { class Home { constructor() { this.commands = supportedCommands; + this.cachedMessages = []; } isCommand(command) { @@ -44,6 +45,44 @@ class Home { this.commands[command].invoke(); } + initEvents() { + app.eventService.addEventHandler('LoggedIn', () => { + app.htmlService.clearElement('.history-messages'); + }); + + app.eventService.addEventHandler('OnLoggedIn', (users) => { + users.forEach(user => { + if(!app.htmlService.getElement(`#${user}`)) { + const userNode = app.htmlService + .getElementFromTemplate('user', { + user: user, + }); + + app.htmlService.attachElement(app.htmlService.createElement(userNode), '.participants'); + } + }); + }); + + app.eventService.addEventHandler('OnLoggedOut', (users) => { + users.forEach(user => { + app.htmlService.getElement(`#${user}`).remove(); + }); + }); + + app.eventService.addEventHandler('OnReceiveMessage', ([user, message]) => { + const messageNode = app.htmlService + .getElementFromTemplate('message', { + user: user, + message: message + }); + + this.cachedMessages.push(messageNode); + + app.htmlService.attachElement(app.htmlService.createElement(messageNode) + , '.history-messages'); + }); + } + init() { app.htmlService.addEventHandler('#main-chat-input', 'keyup', (e) => { if (e.key === 'Enter' || e.keyCode === 13) { @@ -64,6 +103,16 @@ class Home { } } }); + + if(app.userService.isLoggedIn()) { + app.htmlService.clearElement('.history-messages'); + this.cachedMessages.forEach(cachedMessage => { + app.htmlService.attachElement(app.htmlService.createElement(cachedMessage) + , '.history-messages'); + }); + } + + this.initEvents(); } } |