From 3f0e6c2b8065473491349f47600ed41c31614e74 Mon Sep 17 00:00:00 2001 From: Zaran Date: Sat, 5 May 2012 17:16:04 -0700 Subject: Basic message receive/send feature Note: the tab directive is not needed anymore because the ngClick directive already calls preventDefault() --- alias-angular/app/js/controllers.js | 65 ++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 9 deletions(-) (limited to 'alias-angular/app/js/controllers.js') diff --git a/alias-angular/app/js/controllers.js b/alias-angular/app/js/controllers.js index 7e68e41..979a4aa 100644 --- a/alias-angular/app/js/controllers.js +++ b/alias-angular/app/js/controllers.js @@ -105,26 +105,73 @@ function RosterCtl($scope, StropheSrv, $log, $rootScope) { RosterCtl.$inject = ['$scope','StropheSrv','$log', '$rootScope']; -function MsgCtl($scope, $log) { +function MsgCtl($scope, $log, StropheSrv, $rootScope) { - $scope.conversations = []; + $scope.conversations = {}; $scope.activeConversation = ''; - $scope.$on('msgrequest', function(event, contact) { - $log.log(contact); - if (_.indexOf($scope.conversations, contact)==-1) { - $scope.conversations.push(contact); + function addTab(contact) { + if (!_.has($scope.conversations, contact)) { + $scope.conversations[contact] = []; + } + }; + + function addMessage(conv,from,body) { + $scope.conversations[conv].push({from:from, body:body}); + }; + + function onMessage(message) { + var full_jid = $(message).attr('from'); + var jid = Strophe.getBareJidFromJid(full_jid); + addTab(jid); + var body = $(message).find("html > body"); + + if (body.length === 0) { + body = $(message).find('body'); + if (body.length > 0) { + body = body.text(); + } else { + body = null; + } + } else { + body = body.contents(); + var span = $(""); + body.each(function () { + if (document.importNode) { + $(document.importNode(this, true)).appendTo(span); + } else { + // IE workaround + span.append(this.xml); + } + }); + body = span; } + if (body) { + addMessage(jid,jid,body); + } + }; + + $scope.sendMessage = function(message,to) { + var msg = $msg({to: to, "type": "chat"}).c('body').t(message); + addMessage(to,$rootScope.self.jid,message); + StropheSrv.send(msg); + }; + + $scope.$on('connected', function() { + StropheSrv.addHandler(onMessage, 'message', 'chat'); + }); + + $scope.$on('msgrequest', function(event, contact) { + addTab(contact); $scope.activeConversation = contact; }); $scope.delete = function(conversation) { - $scope.conversations.splice(_.indexOf($scope.conversations, conversation),1); + delete $scope.conversations[conversation]; }; $scope.activate = function(conversation) { - $log.log(conversation+' was clicked'); $scope.activeConversation = conversation; }; @@ -133,4 +180,4 @@ function MsgCtl($scope, $log) { }; } -MsgCtl.$inject = ['$scope','$log']; \ No newline at end of file +MsgCtl.$inject = ['$scope','$log','StropheSrv', '$rootScope']; \ No newline at end of file -- cgit v1.2.3-70-g09d2