aboutsummaryrefslogtreecommitdiffstats
path: root/alias-angular/app/js/controllers.js
diff options
context:
space:
mode:
Diffstat (limited to 'alias-angular/app/js/controllers.js')
-rw-r--r--alias-angular/app/js/controllers.js65
1 files changed, 56 insertions, 9 deletions
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 = $("<span></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