From: Darren <darren@darrenwhitlen.com>
Date: Tue, 12 Aug 2014 13:14:30 +0000 (+0100)
Subject: Network components get/set
X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=a9b1c5d6e4d27b7e72c96338200abf90d0e4a757;p=KiwiIRC.git

Network components get/set
---

diff --git a/client/src/app.js b/client/src/app.js
index add00f1..afc76a0 100644
--- a/client/src/app.js
+++ b/client/src/app.js
@@ -94,22 +94,36 @@ _kiwi.global = {
         Network: function(connection_id) {
             var connection_event;
 
+            // If no connection id given, use all connections
             if (typeof connection_id !== 'undefined') {
                 connection_event = 'connection:' + connection_id.toString();
             } else {
                 connection_event = 'connection';
             }
 
+            // Helper to get the network object
+            var getNetwork = function() {
+                var network = typeof connection_id === 'undefined' ?
+                    _kiwi.app.connections.active_connection :
+                    _kiwi.app.connections.getByConnectionId(connection_id);
+
+                return network ?
+                    network :
+                    undefined;
+            };
+
+            // Create the return object (events proxy from the gateway)
             var obj = new this.EventComponent(_kiwi.gateway, connection_event);
+
+            // Proxy several gateway functions onto the return object
             var funcs = {
                 kiwi: 'kiwi', raw: 'raw', kick: 'kick', topic: 'topic',
                 part: 'part', join: 'join', action: 'action', ctcp: 'ctcp',
                 ctcpRequest: 'ctcpRequest', ctcpResponse: 'ctcpResponse',
                 notice: 'notice', msg: 'privmsg', changeNick: 'changeNick',
-                channelInfo: 'channelInfo', mode: 'mode', quit: 'quit'
+                channelInfo: 'channelInfo', mode: 'mode', quit: 'quit',
             };
 
-            // Proxy each gateway method
             _.each(funcs, function(gateway_fn, func_name) {
                 obj[func_name] = function() {
                     var fn_name = gateway_fn;
@@ -123,6 +137,25 @@ _kiwi.global = {
                 };
             });
 
+            // Add the networks getters/setters
+            obj.get = function() {
+                var network = getNetwork();
+                if (!network) {
+                    return;
+                }
+
+                return network.get.apply(network, arguments);
+            };
+
+            obj.set = function() {
+                var network = getNetwork();
+                if (!network) {
+                    return;
+                }
+
+                return network.set.apply(network, arguments);
+            };
+
             return obj;
         },