Tabs + panels + core code working with multi-connections
[KiwiIRC.git] / client / assets / dev / model_panellist.js
1 _kiwi.model.PanelList = Backbone.Collection.extend({
2 model: _kiwi.model.Panel,
3
4 comparator: function (chan) {
5 return chan.get('name');
6 },
7 initialize: function (elements, network) {
8 var that = this;
9
10 // The network this PanelList is associated with
11 this.network = network;
12
13 this.view = new _kiwi.view.Tabs({model: this});
14
15 // Holds the active panel
16 this.active = null;
17
18 // Keep a tab on the active panel
19 this.bind('active', function (active_panel) {
20 this.active = active_panel;
21 }, this);
22
23 this.bind('add', function(panel) {
24 panel.set('panel_list', this);
25 });
26 },
27
28
29
30 getByName: function (name) {
31 if (typeof name !== 'string') return;
32
33 return this.find(function (c) {
34 return name.toLowerCase() === c.get('name').toLowerCase();
35 });
36 }
37 });
38
39
40
41 _kiwi.model.NetworkPanelList = Backbone.Collection.extend({
42 model: _kiwi.model.Network,
43
44 initialize: function() {
45 this.view = new _kiwi.view.NetworkTabs({model: this});
46
47 this.on('add', this.onNetworkAdd, this);
48 this.on('remove', this.onNetworkRemove, this);
49
50 // Current active connection / panel
51 this.active_connection = undefined;
52 this.active_panel = undefined;
53
54 // TODO: Remove this - legacy
55 this.active = undefined;
56 },
57
58 getByConnectionId: function(id) {
59 return this.find(function(connection){
60 return connection.get('connection_id') == id;
61 });
62 },
63
64 panels: function() {
65 var panels = [];
66
67 this.each(function(network) {
68 panels = panels.concat(network.panels.models);
69 });
70
71 return panels;
72 },
73
74
75 onNetworkAdd: function(network) {
76 network.panels.on('active', this.onPanelActive, this);
77
78 // if it's our first connection, set it active
79 if (this.models.length === 1) {
80 this.active_connection = network;
81 this.active_panel = network.panels.server;
82
83 // TODO: Remove this - legacy
84 this.active = this.active_panel;
85 }
86 },
87
88 onNetworkRemove: function(network) {
89 network.panels.off('active', this.onPanelActive, this);
90 },
91
92 onPanelActive: function(panel) {
93 var connection = this.getByConnectionId(panel.tab.data('connection_id'));
94 this.trigger('active', panel, connection);
95
96 this.active_connection = connection;
97 this.active_panel = panel;
98
99 // TODO: Remove this - legacy
100 this.active = panel;
101 }
102 });