Created
December 14, 2015 23:49
-
-
Save villander/4aa6e574ea092b896233 to your computer and use it in GitHub Desktop.
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
| import Ember from 'ember'; | |
| export default Ember.Component.extend(Ember.Evented, { | |
| classNames: ['chat'], | |
| audio: Ember.inject.service('audio'), | |
| pubnub: Ember.inject.service('pubnub'), | |
| allChannels: [], | |
| activeChannel: null, | |
| notificationChannels: [], | |
| new_message: '', | |
| users: [], | |
| messages: [], | |
| countMessages: 0, | |
| lastMessage: {}, | |
| channel: function(){ | |
| var m = this.get('model'); | |
| //changin channel in pubNub | |
| var channel = []; | |
| channel.pushObject(m.me.get('profileId')); | |
| channel.pushObject(m.other.get('profileId')); | |
| this.set('activeChannel', channel.sort().join('::')); | |
| return channel.sort().join('::'); | |
| }.property('model'), | |
| channelChanged: function() { | |
| var chan = this.get('channel'); | |
| this.set('messages',[]); | |
| Ember.Logger.info(chan); | |
| this.set('lastMessage', ''); | |
| for (var i = 0, length = this.get('allChannels').length; i < length; i++ ) { | |
| if( this.get('allChannels')[i] === chan ) { | |
| this.getStorage(chan); | |
| return true; | |
| } | |
| } | |
| this.get('allChannels').push(chan); | |
| this.subscribe(chan); | |
| }.observes('channel'), | |
| scroll: function(){ | |
| var $chatMessageList = this.$('.chat_area'); | |
| $chatMessageList.animate({scrollTop:$chatMessageList.prop('scrollHeight')}, 'slow'); | |
| }, | |
| setup: function() { | |
| var channel = this.get('channel'); | |
| this.get('allChannels').push(channel); | |
| this.subscribe(channel); | |
| }.on('init'), | |
| subscribe: function(theChannel) { | |
| var pn = this.get('pubnub'); | |
| var self = this; | |
| pn.emSubscribe({ | |
| channel: theChannel, | |
| message : function( message ) { | |
| // RECEIVED A MESSAGE. | |
| if(message[0].receiver === self.get('model').me.get('profileId') ) { | |
| setTimeout(function(){ | |
| self.get('audio').getAudio().audio('audio_receiver-messages').play(); | |
| }, 1500); | |
| } | |
| self.set('lastMessage', message[0].sender); | |
| self.scroll(); | |
| }, | |
| presence: function(m) { | |
| Ember.Logger.info(m, 'presence'); | |
| }, | |
| connect: function(){ | |
| Ember.Logger.info("Connected"); | |
| }, | |
| disconnect: function(){ | |
| Ember.Logger.info("Disconnected"); | |
| }, | |
| reconnect: function(){ | |
| Ember.Logger.info("Reconnected"); | |
| }, | |
| error: function(){ | |
| Ember.Logger.info("Network Error"); | |
| } | |
| }); | |
| // Event the new messages | |
| pn.on(pn.emMsgEv(theChannel), function (payload) { | |
| // want no get storage | |
| if(!payload.numberMessages) { | |
| var other = self.get('model').other.get('profileId'); | |
| if( (payload.message.sender === other) ) { | |
| payload.message.readReceiver = true; | |
| } | |
| } | |
| var currentChannel = self.get('activeChannel'); | |
| // for get Storage | |
| self.countMessages ++; | |
| if( payload.numberMessages && (self.countMessages === payload.numberMessages) ) { | |
| self.set('lastMessage', payload.message.sender); | |
| self.countMessages = 0; | |
| Ember.run.scheduleOnce('afterRender', this, function(){ | |
| // perform your jQuery logic here | |
| var $chatMessageList = $('.chat_area'); | |
| $chatMessageList.scrollTop($chatMessageList.prop('scrollHeight')); | |
| }); | |
| } | |
| // Show message just if is same channel | |
| if( currentChannel === theChannel ){ | |
| self.get('messages').pushObject(payload.message); | |
| } else { | |
| // If notifications are empty | |
| if( self.get('notificationChannels').length > 0 ) { | |
| for (var i = 0, length = self.get('notificationChannels').length; i < length; i++) { | |
| // Notifications online | |
| if (self.get('notificationChannels')[i].channel === theChannel) { | |
| self.get('notificationChannels')[i].messages ++ ; | |
| //Ember.Logger.info(self.get('notificationChannels')); | |
| self.fireNotification(self.get('notificationChannels'), payload.message.receiver); | |
| return true; | |
| } | |
| } | |
| // create notifications from channels that have not been notified | |
| self.get('notificationChannels').push({ channel: theChannel, messages : 1 }); | |
| self.fireNotification(self.get('notificationChannels'), payload.message.receiver); | |
| } else { // create primary notification | |
| self.get('notificationChannels').push({ channel: theChannel, messages : 1 }); | |
| self.fireNotification(self.get('notificationChannels'), payload.message.receiver); | |
| } | |
| } | |
| }); | |
| this.getStorage(theChannel); | |
| }, | |
| getStorage: function(theChannel) { | |
| var pn = this.get('pubnub'); | |
| var self = this; | |
| pn.emHistory({ | |
| channel: theChannel, | |
| count: 500, | |
| callback: function(payload) { | |
| $.each(payload[0], function( index, message ) { | |
| self.get('messages').pushObject(message); | |
| }); | |
| } | |
| }); | |
| }, | |
| unsubscribe: function(theChannel) { | |
| this.get('pubnub').emUnsubscribe({ channel: theChannel }); | |
| }, | |
| formatAMPM : function(date) { | |
| var hours = date.getHours(); | |
| var minutes = date.getMinutes(); | |
| var ampm = hours >= 12 ? ' PM' : ' AM'; | |
| hours = hours % 12; // return '0' ou different of '0' | |
| hours = hours ? hours : 12; // the hour '0' should be '12' | |
| minutes = minutes < 10 ? '0'+ minutes : minutes; | |
| var timeCurrent = hours + ':' + minutes + ' ' + ampm; | |
| return timeCurrent; | |
| }, | |
| actions: { | |
| publishMessage: function(content){ | |
| // send message just if itself not null | |
| if(this.get('new_message').trim() !== ''){ | |
| var date = new Date(); | |
| var alias = ''; | |
| var name = ''; | |
| // If last message is the sender not are some send Avatar and name | |
| if(this.get('lastMessage') !== this.get('model').me.get('profileId')) { | |
| alias = this.get('model').me.get('alias'); | |
| name = this.get('model').me.get('name'); | |
| } | |
| var message = { | |
| sender: this.get('model').me.get('profileId'), | |
| receiver: this.get('model').other.get('profileId'), | |
| readSender: true, | |
| readReceiver: false, | |
| type: this.get('channel'), | |
| alias: alias, | |
| name: name, | |
| hour: this.formatAMPM(date), | |
| date: ( date.getMonth() + 1 ) + '/' + date.getUTCDate() + '/' + date.getFullYear(), | |
| content: content | |
| }; | |
| this.get('pubnub').emPublish({ | |
| channel: this.get('channel'), | |
| message: message | |
| }); | |
| this.scroll(); | |
| this.get('audio').getAudio().audio('audio-send-messages').play(); | |
| } | |
| this.set('new_message', ''); | |
| return true; | |
| } | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment