initial commit
[namibia] / public / js / vendor / ape-source / Pipe / PipeMulti.js
1 APE.PipeMulti = new Class({
2
3         Extends: APE.Pipe,
4
5         initialize: function(core, options) {
6                 this.parent(core, options);
7
8                 this.type = 'multi';
9                 this.name = options.pipe.properties.name;
10
11                 //Test if pipe have users before sending event
12                 //because this.users need to be defined
13                 if (options.users) {
14                         this.users = new $H;
15                         var users = options.users;
16                 }
17
18                 this.ape.fireEvent('multiPipeCreate', [this, options]);
19
20                 if (options.users) {
21                         var l = users.length;
22                         for (var i=0; i < l; i++) {
23                                 this.addUser(users[i].pubid, users[i]);
24                         }
25                 }
26                 this.onRaw('left', this.rawLeft);
27                 this.onRaw('join', this.rawJoin);
28         },
29
30         rawJoin: function(raw, pipe) {
31                 this.addUser(raw.data.user.pubid, raw.data.user);
32         },
33
34         rawLeft: function(raw, pipe) {
35                 if (pipe.name.charAt(0) != '*') this.delUser(raw.data.user.pubid);
36                 if (raw.data.user.pubid == this.ape.user.pubid) this.ape.delPipe(pipe.pipe.pubid);
37         },
38
39         left: function() {
40                 this.ape.left(this.pipe.pubid);
41         },
42
43         addUser: function(pubid, updatedUser) {
44                 var user;
45                 if (!this.ape.users.has(pubid)) {
46                         user = updatedUser;
47                         user.pipes = new $H;
48                         this.ape.users.set(pubid, updatedUser);
49                 } else {
50                         user = this.ape.users.get(pubid);
51                 }
52                 user.pipes.set(this.pipe.pubid, this);
53                 var u = {'pipes':user.pipes ,'casttype': user.casttype, 'pubid': user.pubid, 'properties': updatedUser.properties};
54                 this.users.set(pubid, u);
55                 this.fireGlobalEvent('userJoin', [u, this]);
56                 return u;
57         },
58
59         delUser: function(pubid) {
60                 var u = this.users.get(pubid);
61                 this.users.erase(pubid);
62                 u.pipes.erase(this.pipe.pubid)
63                 if (u.pipes.getLength() == 0) {
64                         this.ape.users.erase(u.pubid);
65                 }
66                 this.fireGlobalEvent('userLeft', [u, this]);
67                 return u;
68         },
69
70         getUser: function(pubid) {
71                 return this.users.get(pubid);
72         },
73         
74         getUserPipe: function(user) {
75                 if (typeof user == 'string') user = this.users.get(users.pubid);
76                 return this.ape.newPipe('uni', {'pipe':user});
77         }
78 });