Created
January 1, 2018 15:23
-
-
Save reynaldobarrosjr/36e291ca20a91ab519a77911843f55f2 to your computer and use it in GitHub Desktop.
Chat Live Vue tutorial - Page 59 of Book Vue Step by Step - Kindle Unlimited
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Chat Live example</title> | |
| <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css"> | |
| <style type="text/css"> | |
| body{margin:20px;} | |
| </style> | |
| </head> | |
| <body> | |
| <div id="root"> | |
| <div> | |
| <h1>{{appName}}</h1> | |
| <messages :removemessage = "removemessage" :chat="messages"></messages> | |
| <chatform :totalmessages="messages.lenght" :save="save"></chatform> | |
| </div> | |
| </div> | |
| <!-- templates --> | |
| <template id="messages-list"> | |
| <div> | |
| <ul class="list-group"> | |
| <li class="list-group-item" v-for="m,index in chat"> | |
| <messageitem :message="m"></messageitem> | |
| <removemessage :removemessagefunction="removemessage"></removemessage> | |
| <span class="pull-right"> Date: {{getRelativeTime(m.time)}} | |
| </span> | |
| </li> | |
| <div v-if="chat.lenght <= 0"> | |
| <div class="alert alert-info"> | |
| <p>No Messages</p> | |
| </div> | |
| </ul> | |
| </div> | |
| </template> | |
| <template id="remove_message"> | |
| <span> | |
| <a v-on:click="removemessagefunction()" class="close">x</a> | |
| </span> | |
| </template> | |
| <template id="message-item"> | |
| <span>{{message.text}}</span> | |
| </template> | |
| <template id="chat-form"> | |
| <div> | |
| <form v-on:submit.prevent="save(message,empty()"> | |
| <label>Your message:</label> | |
| <input v-model="message" type="text" class="form-control"/> | |
| <div v-if="hasErros" class="alert alert-danger"> | |
| <p> | |
| Please enter a message | |
| </p> | |
| </div> | |
| <button class="btn btn-info">Send</button> | |
| <span class="pull-right badge badge-info">{{totalmessages}}</span> | |
| </form> | |
| </div> | |
| </template> | |
| <!--templates--> | |
| </body> | |
| <script src="https://cdn.jsdelivr.net/npm/vue"></script> | |
| <script type="text/javascript"> | |
| var removemessage = Vue.component("removemessage",{ | |
| template:"#remove_message", | |
| props:["removemessagefunction"], | |
| }); | |
| var messageItem = Vue.component("messageitem",{ | |
| template:"#message-item", | |
| props:["message"] | |
| }); | |
| var messageList = Vue.component("messages",{ | |
| template:"#messages-list", | |
| props:['chat',"removemessage"], | |
| methods: { | |
| getRelativeTime: function (timeStamp){ | |
| var now = new Date(), | |
| secondsPast = (now.getTime() - timeStamp.getTime()) / 1000; | |
| if(secondsPast < 60){ | |
| return parseInt(secondsPast) + 's'; | |
| } | |
| if(secondsPast < 3600){ | |
| return parseInt(secondsPast/60) + 'm'; | |
| } | |
| if(secondsPast <= 86400){ | |
| return parseInt(secondsPast/3600) + 'h'; | |
| } | |
| if(secondsPast > 86400){ | |
| day = timeStamp.getDate(); | |
| month = timeStamp.toDateString().match(/ [a-zA-Z]*/)[0].replace(" ",""); | |
| year = timeStamp.getFullYear() == now.getFullYear() ? "" : " "+timeStamp.getFullYear(); | |
| return day + " " + month + year; | |
| } | |
| } | |
| }, | |
| }); | |
| var chatForm = Vue.component("chatform",{ | |
| template: "#chat-form", | |
| props:["save","totalmessages"], | |
| data: function(){ | |
| return{ | |
| message:"", | |
| hasErrors:false, | |
| } | |
| }, | |
| methods:{ | |
| empty: function(){ | |
| if (this.message){ | |
| this.message=""; | |
| this.hasErrors = false; | |
| }else{ | |
| this.hasErrors = true; | |
| } | |
| } | |
| } | |
| }); | |
| var ChatApp = new Vue({ | |
| el:"#root", | |
| data:{ | |
| appName:"Welcome to chat App", | |
| messages:[ | |
| {text:"Hi are you?",time: new Date()}, | |
| {text:"I'm fine",time: new Date()}, | |
| {text:"I'm fine",time: new Date()} | |
| ], | |
| }, | |
| methods : { | |
| save: function (message){ | |
| if (message) | |
| this.messages.push({text: message, time: new Date()}); | |
| }, | |
| removemessage: function (index){ | |
| this.messages.splice(index,1); | |
| } | |
| } | |
| }); | |
| </script> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment