subway /assets/js/collections.js

Language Javascript Lines 96
MD5 Hash 485babd5e68fb21cb60ffcdf67a81cf1
Repository https://github.com/predominant/subway.git View Raw File
| Open JSFiddle
 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
91
92
93
94
// Collection of messages that belong to a frame
var Stream = Backbone.Collection.extend({
  model: Message,

  unread: function() {
    return this.filter(function(msg) { return msg.get('unread'); });
  },

  unreadMentions: function() {
    return this.filter(function(msg) { return msg.get('unreadMention'); });
  }
});

// All channels/private message chats a user has open
var WindowList = Backbone.Collection.extend({
  model: ChatWindow,

  initialize: function() {
    this.bind('add', this.setActive, this);
  },

  getByName: function(name) {
    return this.find(function(chat) {
      return chat.get('name') === name;
    });
  },

  getActive: function() {
    return this.find(function(chat) {
      return chat.get('active') === true;
    });
  },

  setActive: function(selected) {
    //This is here for private messages
    var name = selected.get('name');
    if((name[0] !== '#' && name !== 'status') && selected.stream.models.length < 1){
      selected.set({active: false});
      return;
    }
    console.log(name + ' set as active chat!');
    this.each(function(chat) {
      chat.set({active: false});
    });
    selected.set({active: true});
    selected.view.render();
  },

  // Restrict to a certain type of chat window
  byType: function(type) {
    return this.filter(function(chat) {
      return chat.get('type') === type;
    });
  },

  // Unread private messages and mentions
  unreadCount: function() {
    var channels = this.byType('channel');
    var pms = this.byType('pm');

    var count = 0;
    count = channels.reduce(function(prev, chat) {
      return prev + chat.get('unreadMentions');
    }, 0);
    count += pms.reduce(function(prev, chat) {
      return prev + chat.get('unread');
    }, 0);

    return count;
  }

});

var UserList = Backbone.Collection.extend({
  model: User,

  initialize: function(channel) {
    this.channel = channel;
    this.view = new UserListView({collection:this});
  },

  getByNick: function(nick) {
    return this.detect(function(user) {
      return user.get('nick') == nick;
    });
  },

  getUsers: function() {
    var users = this.map(function(user) {
      return user.get('nick');
    });
    return users;
  }
});
Back to Top