diff --git a/app.js b/app.js index 18037b3..f22928d 100644 --- a/app.js +++ b/app.js @@ -88,24 +88,7 @@ let kara = new Vue({ this.scrollDown(); }, methods: { - addMessage(body, bot, me = false) { - this.messages.push({ - body: body, - bot: bot, - command: body.search('/') === 0, - me: me, - time: Date.now() - }) - }, - botMessage(body) { - this.addMessage(body, true); - }, - userMessage(body) { - this.addMessage(body, false); - this.lastMessage = body; - - this.updateStorage(); - }, + // Initial initialGreeting() { this.botMessage( this.oneOf( @@ -129,6 +112,54 @@ let kara = new Vue({ ) ); }, + + // Name + askForName() { + this.botMessage( + this.oneOf([ + 'May i ask for your name?', + 'Whats your name? :)', + 'How can i call you?', + 'How did your developers call you? :)' + ]) + ); + + this.askedForName = true; + }, + setName(message) { + this.username = message.trim(); + this.settingsModal.username = this.username; + this.askedForName = false; + this.updateStorage(); + + this.botMessage( + this.oneOf([ + "That's a beautiful name!", + "Okay, i'll call you " + this.username + " from now on :)", + "Nice to meet you, " + this.username + ". :D" + ]) + ) + }, + + // Messages / Chat + addMessage(body, bot, me = false) { + this.messages.push({ + body: body, + bot: bot, + command: body.search('/') === 0, + me: me, + time: Date.now() + }) + }, + botMessage(body) { + this.addMessage(body, true); + }, + userMessage(body) { + this.addMessage(body, false); + this.lastMessage = body; + + this.updateStorage(); + }, sendMessage() { if (this.chatbox.trim() === '') { return false; @@ -141,6 +172,10 @@ let kara = new Vue({ this.react(this.chatbox); this.chatbox = ''; }, + meMessage(message) { + this.addMessage(this.username + ' ' + message, false, true); + this.react(message, true); + }, react(message, recursive = false) { this.isTyping = true; @@ -159,7 +194,7 @@ let kara = new Vue({ } else if (this.takeNote === true) { this.saveNote(message); } else { - let answer = this.getAnswer(message); + let answer = this.getReaction(message); if (answer) { this.botMessage(answer); } @@ -172,116 +207,7 @@ let kara = new Vue({ }, 1800); } }, - processCommands(message) { - if (this.checkForCommands(message, 'note')) { - let noteToSave = this.checkForCommands(message, 'note'); - this.saveNote(noteToSave); - } - - if (this.checkForCommands(message, 'clear')) { - this.clearChat(); - } - - if (this.checkForCommands(message, 'weather')) { - this.checkWeather() - } - - if (this.checkForCommands(message, 'joke')) { - this.tellJoke(this.checkForCommands(message, 'joke')) - } - - if (this.checkForCommands(message, 'me')) { - this.meMessage(this.checkForCommands(message, 'me')) - } - }, - checkForCommands(message, commands) { - if (!Array.isArray(commands)) { - commands = [commands]; - } - - let commandFound = false; - let parameter = false; - - commands.forEach((command) => { - if (commandFound) { - return; - } - let commandString = '/' + command; - - if (message.search(commandString) === 0) { - parameter = message.replace(commandString, '').trim(); - commandFound = true; - } - }); - - return parameter ? parameter : commandFound; - }, - scrollDown() { - $('#chat-box').stop().animate({ - scrollTop: ($('#chat-box')[0].scrollHeight * 10) - }, 800); - }, - oneOf(answers) { - let amountOfAnswers = answers.length; - let randomIndex = Math.floor(Math.random() * (amountOfAnswers)); - - return this.convertWildcards(answers[randomIndex]); - }, - convertWildcards(message) { - message = message.replace('$name$', this.username); - message = message.replace('$botname$', this.name); - - return message; - }, - includesOneOf(phrases, wordsToSearch) { - let includes = false; - - wordsToSearch.forEach((searchWord) => { - if (phrases.includes(searchWord)) { - includes = true; - } - }) - - return includes; - }, - includesAllOf(phrases, wordsToSearch) { - let includesAllPhrases = true; - - wordsToSearch.forEach((searchWord) => { - if (!phrases.includes(searchWord)) { - includesAllPhrases = false; - } - }) - - return includesAllPhrases; - }, - cleanupMessage(message) { - message = message.toLowerCase(); - return message.replace('?', '') - .replace('!', '') - .replace('.', '') - .replace(',', '') - .replace('-', '') - .replace('_', '') - .replace('#', '') - .replace('\'', '') - .replace('"', '') - .replace('+', '') - .replace('*', '') - .replace('ยง', '') - .replace('$', '') - .replace('%', '') - .replace('&', '') - .replace('/', '') - .replace('(', '') - .replace(')', '') - .replace('=', '') - .replace('\\', '') - .replace('@', '') - .replace('~', '') - .replace('โ€ฆ', ''); - }, - getAnswer(message) { + getReaction(message) { message = this.cleanupMessage(message); let phrases = message.split(' '); @@ -372,9 +298,6 @@ let kara = new Vue({ return 'I don\'t know what to say..'; }, - addResponseToInput() { - this.addModal.responses.push(''); - }, addNewResponseContainer() { let includeAll = this.addModal.includeAll; let keywords = this.cleanupMessage(this.addModal.keywords).split(','); @@ -392,120 +315,65 @@ let kara = new Vue({ this.updateStorage(); }, - updateStorage() { - localStorage.setItem('name', this.name); - localStorage.setItem('username', this.username); - localStorage.setItem('activeTheme', this.activeTheme); - localStorage.setItem('messages', JSON.stringify(this.messages)); - localStorage.setItem('answers', JSON.stringify(this.answers)); - localStorage.setItem('notes', JSON.stringify(this.notes)); - localStorage.setItem('lastMessage', JSON.stringify(this.lastMessage)); - localStorage.setItem('location', JSON.stringify(this.location)); - }, - getSavedData() { - let savedName = localStorage.getItem('name'); - - this.name = savedName ? savedName : this.name; - - let savedUsername = localStorage.getItem('username'); - this.username = savedUsername ? savedUsername : null; - let savedActiveTheme = localStorage.getItem('activeTheme'); - this.activeTheme = savedActiveTheme ? savedActiveTheme : 'slate'; - - let savedMessages = JSON.parse(localStorage.getItem('messages')); - this.messages = savedMessages ? savedMessages : []; - - let savedAnswers = JSON.parse(localStorage.getItem('answers')); - this.answers = savedAnswers !== [] ? savedAnswers : this.answers; - - let savedNotes = JSON.parse(localStorage.getItem('notes')); - this.notes = savedNotes ? savedNotes : []; - - let savedLastMessage = JSON.parse(localStorage.getItem('lastMessage')); - this.lastMessage = savedLastMessage ? savedLastMessage : null; - - let savedLocation = JSON.parse(localStorage.getItem('location')); - this.location = savedLocation ? savedLocation : null; + // Forms + saveSettings() { + this.name = this.settingsModal.name; + this.username = this.settingsModal.username; + this.location = this.settingsModal.location; + this.updateStorage(); + // this.botMessage('Settings saved! :)'); this.scrollDown(); }, - askForName() { - this.botMessage( - this.oneOf([ - 'May i ask for your name?', - 'Whats your name? :)', - 'How can i call you?', - 'How did your developers call you? :)' - ]) - ); - - this.askedForName = true; + addResponseToInput() { + this.addModal.responses.push(''); }, - setName(message) { - this.username = message.trim(); - this.settingsModal.username = this.username; - this.askedForName = false; - this.updateStorage(); - this.botMessage( - this.oneOf([ - "That's a beautiful name!", - "Okay, i'll call you " + this.username + " from now on :)", - "Nice to meet you, " + this.username + ". :D" - ]) - ) - }, - saveNote(message) { - this.takeNote = false; + // Commands + processCommands(message) { + if (this.checkForCommands(message, 'note')) { + let noteToSave = this.checkForCommands(message, 'note'); + this.saveNote(noteToSave); + } - this.notes.push({ - time: moment(), - body: message, - checked: false - }); + if (this.checkForCommands(message, 'clear')) { + this.clearChat(); + } - this.updateStorage(); + if (this.checkForCommands(message, 'weather')) { + this.checkWeather() + } - this.botMessage( - this.oneOf([ - "Saved! :)", - "You can read and check your notes in the clipboard-section. :)" - ]) - ) + if (this.checkForCommands(message, 'joke')) { + this.tellJoke(this.checkForCommands(message, 'joke')) + } + + if (this.checkForCommands(message, 'me')) { + this.meMessage(this.checkForCommands(message, 'me')) + } }, - clearNotes() { - this.notes = []; + checkForCommands(message, commands) { + if (!Array.isArray(commands)) { + commands = [commands]; + } - this.botMessage( - this.oneOf([ - "Notes cleared. ๐Ÿšฎ" - ]) - ); + let commandFound = false; + let parameter = false; - this.updateStorage(); - }, - saveSettings() { - this.name = this.settingsModal.name; - this.username = this.settingsModal.username; - this.location = this.settingsModal.location; - this.updateStorage(); + commands.forEach((command) => { + if (commandFound) { + return; + } + let commandString = '/' + command; - // this.botMessage('Settings saved! :)'); - this.scrollDown(); - }, - clearChat() { - this.messages = []; - this.botMessage(this.oneOf([ - 'Chat cleared.', - 'Evidence destroyed.', - 'Mind cleared.', - 'Uhm.. i think forgot everything we ever talked about.. oops.', - 'A fresh start!', - "You've got something to hide? Nevermind, gotcha fam. Chat is cleared now. ๐Ÿฑโ€๐Ÿ‘ค" - ])); + if (message.search(commandString) === 0) { + parameter = message.replace(commandString, '').trim(); + commandFound = true; + } + }); - this.updateStorage(); + return parameter ? parameter : commandFound; }, checkWeather() { let vue = this; @@ -571,14 +439,163 @@ let kara = new Vue({ this.updateStorage(); }, - meMessage(message) { - this.addMessage(this.username + ' ' + message, false, true); - this.react(message, true); + + // Notes + saveNote(message) { + this.takeNote = false; + + this.notes.push({ + time: moment(), + body: message, + checked: false + }); + + this.updateStorage(); + + this.botMessage( + this.oneOf([ + "Saved! :)", + "You can read and check your notes in the clipboard-section. :)" + ]) + ) + }, + clearNotes() { + this.notes = []; + + this.botMessage( + this.oneOf([ + "Notes cleared. ๐Ÿšฎ" + ]) + ); + + this.updateStorage(); + }, + + // LocalStorage + getSavedData() { + let savedName = localStorage.getItem('name'); + + this.name = savedName ? savedName : this.name; + + let savedUsername = localStorage.getItem('username'); + this.username = savedUsername ? savedUsername : null; + + let savedActiveTheme = localStorage.getItem('activeTheme'); + this.activeTheme = savedActiveTheme ? savedActiveTheme : 'slate'; + + let savedMessages = JSON.parse(localStorage.getItem('messages')); + this.messages = savedMessages ? savedMessages : []; + + let savedAnswers = JSON.parse(localStorage.getItem('answers')); + this.answers = savedAnswers !== [] ? savedAnswers : this.answers; + + let savedNotes = JSON.parse(localStorage.getItem('notes')); + this.notes = savedNotes ? savedNotes : []; + + let savedLastMessage = JSON.parse(localStorage.getItem('lastMessage')); + this.lastMessage = savedLastMessage ? savedLastMessage : null; + + let savedLocation = JSON.parse(localStorage.getItem('location')); + this.location = savedLocation ? savedLocation : null; + + this.scrollDown(); + }, + updateStorage() { + localStorage.setItem('name', this.name); + localStorage.setItem('username', this.username); + localStorage.setItem('activeTheme', this.activeTheme); + localStorage.setItem('messages', JSON.stringify(this.messages)); + localStorage.setItem('answers', JSON.stringify(this.answers)); + localStorage.setItem('notes', JSON.stringify(this.notes)); + localStorage.setItem('lastMessage', JSON.stringify(this.lastMessage)); + localStorage.setItem('location', JSON.stringify(this.location)); }, clearStorage() { localStorage.clear(); location.reload(); }, + + // Utility + scrollDown() { + $('#chat-box').stop().animate({ + scrollTop: ($('#chat-box')[0].scrollHeight * 10) + }, 800); + }, + oneOf(answers) { + let amountOfAnswers = answers.length; + let randomIndex = Math.floor(Math.random() * (amountOfAnswers)); + + return this.convertWildcards(answers[randomIndex]); + }, + convertWildcards(message) { + message = message.replace('$name$', this.username); + message = message.replace('$botname$', this.name); + + return message; + }, + cleanupMessage(message) { + message = message.toLowerCase(); + return message.replace('?', '') + .replace('!', '') + .replace('.', '') + .replace(',', '') + .replace('-', '') + .replace('_', '') + .replace('#', '') + .replace('\'', '') + .replace('"', '') + .replace('+', '') + .replace('*', '') + .replace('ยง', '') + .replace('$', '') + .replace('%', '') + .replace('&', '') + .replace('/', '') + .replace('(', '') + .replace(')', '') + .replace('=', '') + .replace('\\', '') + .replace('@', '') + .replace('~', '') + .replace('โ€ฆ', ''); + }, + clearChat() { + this.messages = []; + this.botMessage(this.oneOf([ + 'Chat cleared.', + 'Evidence destroyed.', + 'Mind cleared.', + 'Uhm.. i think forgot everything we ever talked about.. oops.', + 'A fresh start!', + "You've got something to hide? Nevermind, gotcha fam. Chat is cleared now. ๐Ÿฑโ€๐Ÿ‘ค" + ])); + + this.updateStorage(); + }, + includesOneOf(phrases, wordsToSearch) { + let includes = false; + + wordsToSearch.forEach((searchWord) => { + if (phrases.includes(searchWord)) { + includes = true; + } + }) + + return includes; + }, + includesAllOf(phrases, wordsToSearch) { + let includesAllPhrases = true; + + wordsToSearch.forEach((searchWord) => { + if (!phrases.includes(searchWord)) { + includesAllPhrases = false; + } + }) + + return includesAllPhrases; + }, + + // Ajax calls for saving/receiving responses & themes getBootswatchThemes() { let vue = this;