Skip to content

Instantly share code, notes, and snippets.

@arvi
Created August 12, 2016 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arvi/0eb4a2def34126fa2a54d4f3fa8174f5 to your computer and use it in GitHub Desktop.
Save arvi/0eb4a2def34126fa2a54d4f3fa8174f5 to your computer and use it in GitHub Desktop.
myApp.onPageInit('conversations-chat', function (page) {
var myMessages = myApp.messages('.messages', {
autoLayout: true //adds all required additional classes (like "message-pic", "message-with-avatar", "message-with-tail", etc)
});
//initialize message bar
var myMessagebar = myApp.messagebar('.messagebar');
var conversationStarted = false;
//triggered when message is sent
$$('.messagebar .app-send-message-icon').on('click', function () {
//message content
var messageText = myMessagebar.value().trim();
//exit if empy message
if (messageText.length === 0) return;
//empty messagebar
//myMessagebar.clear()
//random message type for simulation purpose only - use real messaging through sockets
var messageType = (['sent', 'received'])[Math.round(Math.random())];
//avatar and name for received message
var avatar, name;
if(messageType === 'received') {
avatar = 'img/sample-profile-pic-3.jpg';
name = 'Kate';
}
//add message in UI
myMessages.addMessage({
text: messageText, //message text
type: messageType, //random message type
avatar: avatar, //avatar
name: name, //name
day: !conversationStarted ? 'Today' : false, //day
time: !conversationStarted ? (new Date()).getHours() + ':' + (new Date()).getMinutes() : false
}, 'append');
//update conversation flag
conversationStarted = true;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment