aboutsummaryrefslogtreecommitdiffstats
path: root/alias-angular/app/js
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@gmail.com>2012-04-18 15:18:35 -0400
committerGuillaume Horel <guillaume.horel@gmail.com>2012-04-18 15:18:35 -0400
commit1cb203c4e3d2114fca928729f049b259d4b05a66 (patch)
treeebdb2a6bcc8d1809cdd0dc2d84893a94648654c6 /alias-angular/app/js
parente61de2b221181abacdc162cdb46c0f79eee70ca0 (diff)
downloadalias-1cb203c4e3d2114fca928729f049b259d4b05a66.tar.gz
Small restructuring and cleanup
- the $rootScope object contains an is_connected() method and a username value, which all the other controllers have access to. - added a get_contacts() method so that we don't need to write a custom filter. - now sort by status as well
Diffstat (limited to 'alias-angular/app/js')
-rw-r--r--alias-angular/app/js/controllers.js62
-rw-r--r--alias-angular/app/js/services.js2
2 files changed, 40 insertions, 24 deletions
diff --git a/alias-angular/app/js/controllers.js b/alias-angular/app/js/controllers.js
index 4288a10..144592a 100644
--- a/alias-angular/app/js/controllers.js
+++ b/alias-angular/app/js/controllers.js
@@ -2,23 +2,27 @@
/* App Controllers */
function ConnectCtl($scope, StropheSrv, $log, $rootScope) {
- $scope.status = "";
+ $rootScope.username = $scope.username;
+ $rootScope.is_connected = function () {
+ return $rootScope.status == Strophe.Status.CONNECTED;
+ };
function connect_callback(status){
- if ( status == Strophe.Status.CONNECTING ) {
+ $rootScope.status = status;
+ switch(status) {
+ case Strophe.Status.CONNECTING:
$log.log('Strophe is connecting.');
- $scope.status = "Connecting";
- } else if ( status == Strophe.Status.CONNFAIL ) {
+ break;
+ case Strophe.Status.CONNFAIL:
$log.log('Strophe failed to connect.');
- $scope.status = "Failed";
- } else if ( status == Strophe.Status.DISCONNECTING ) {
+ break;
+ case Strophe.Status.DISCONNECTING:
$log.log('Strophe is disconnecting.');
- $scope.status = "Disconnecting";
- } else if ( status == Strophe.Status.DISCONNECTED ) {
+ break;
+ case Strophe.Status.DISCONNECTED:
$log.log('Strophe is disconnected.');
- $scope.status = "Disconnected";
- } else if ( status == Strophe.Status.CONNECTED ) {
+ break;
+ case Strophe.Status.CONNECTED:
$log.log('Strophe is connected.');
- $scope.status = "Connected";
$rootScope.$broadcast('connected', true);
}
};
@@ -34,43 +38,55 @@ ConnectCtl.$inject = ['$scope', 'StropheSrv', '$log', '$rootScope'];
function RosterCtl($scope, StropheSrv, $log) {
$scope.contacts = {};
+ $scope.get_contacts = function() {
+ return _.toArray($scope.contacts);
+ };
function onRoster(iq) {
var elems = iq.getElementsByTagName('query');
var query = elems[0];
Strophe.forEachChild(query, 'item', function(item){
var jid = item.getAttribute('jid');
var name = item.getAttribute('name');
- $scope.contacts[jid] = {name: name};
+ //contacts are offline by default
+ $scope.contacts[jid] = {jid: jid, name: name, status: 'offline'};
});
+ //make sure the roster is populated before sending presence request
+ StropheSrv.send($pres());
};
+
function onPresence(presence) {
var who = $(presence).attr('from');
var jid = Strophe.getBareJidFromJid(who);
var type = $(presence).attr('type');
- if (type !== 'error') {
- if (type === 'unavailable') {
- $scope.contacts[jid]['status']='offline';
- }
- else {
- var show = $(presence).find('show').text();
- if (show === '') {
- $scope.contacts[jid]['status'] = 'online';
+ // if (jid in $scope.contacts) {
+ if(_.has($scope.contacts, jid)){
+ if (type !== 'error') {
+ if (type === 'unavailable') {
+ $log.log(jid +' is offline');
+ $scope.contacts[jid]['status'] = 'offline';
}else{
- $scope.contacts[jid]['status'] = 'away';
+ var show = $(presence).find('show').text();
+ if (show === '') {
+ $scope.contacts[jid]['status'] = 'online';
+ }else{
+ $log.log(jid +' is away');
+ $scope.contacts[jid]['status'] = 'away';
+ }
}
}
}
}
+
$scope.$on('connected', function() {
StropheSrv.addHandler(onPresence, 'presence');
$scope.getRoster();
- StropheSrv.send($pres());
});
+
$scope.getRoster = function () {
var query = $iq({type : 'get'}).c('query', {xmlns : Strophe.NS.ROSTER});
StropheSrv.sendIQ(query, onRoster);
};
-}
+ }
RosterCtl.$inject = ['$scope','StropheSrv','$log'];
diff --git a/alias-angular/app/js/services.js b/alias-angular/app/js/services.js
index 9cfeb81..906fe18 100644
--- a/alias-angular/app/js/services.js
+++ b/alias-angular/app/js/services.js
@@ -14,7 +14,7 @@ angular.module('Alias.services', [], function($provide) {
});
},
addHandler: function(callback, name) {
- connection.addHandler(function(stanza){
+ connection.addHandler(function (stanza) {
$rootScope.$apply(callback(stanza));
return true;
}, null, name, null, null, null, null);