Repository navigation
Expand file tree
/
Copy pathchatCtrl.js
More file actions
90 lines (67 loc) · 2.08 KB
/
Copy pathchatCtrl.js
File metadata and controls
90 lines (67 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
angular.module('chatiNG').controller('chatCtrl', ['$scope', 'rooms', 'chat', '$window', function ($scope, rooms, chat, $window) {
$scope.rooms = [];
$scope.activeRoom = undefined;
function roomCallback(msg, room) {
if (!msg.name) {
msg.name = "Anonymous";
}
room.messages.push(msg);
}
rooms.get()
.success(function (data) {
var i;
$scope.rooms = data.results;
for (i = 0; i < $scope.rooms.length; i++) {
$scope.rooms[i].messages = [];
(function (ii) {
chat.join({
roomName: $scope.rooms[ii].name,
callback: function (m) {
roomCallback(m, $scope.rooms[ii]);
}
});
})(i);
}
if ($scope.rooms.length > 0) {
$scope.activeRoom = $scope.rooms[0];
}
})
.error(function () {
alert('error');
});
$scope.say = function () {
if (!$scope.toSay || !$scope.activeRoom) {
return;
}
chat.say({
roomName: $scope.activeRoom.name,
message: {
name: $scope.name,
text: $scope.toSay
}
});
$scope.toSay = '';
};
$scope.changeRoom = function (room) {
$scope.activeRoom = room;
};
$scope.addRoom = function () {
var roomName = $window.prompt('Enter a room name');
if (roomName) {
rooms.open(roomName)
.success(function (room) {
room.name = roomName;
room.messages = [];
$scope.rooms.push(room);
chat.join({
roomName: room.name,
callback: function (m) {
roomCallback(m, room);
}
});
}).error(function () {
});
}
};
} ]);