luci2: add intial LuCI2.firewall abstraction class
[project/luci2/ui.git] / luci2 / htdocs / luci2 / luci2.js
index d2ad812..983e3d2 100644 (file)
@@ -1188,6 +1188,57 @@ function LuCI2()
                }
        };
 
+       this.firewall = {
+               getZoneColor: function(zone)
+               {
+                       if ($.isPlainObject(zone))
+                               zone = zone.name;
+
+                       if (zone == 'lan')
+                               return '#90f090';
+                       else if (zone == 'wan')
+                               return '#f09090';
+
+                       for (var i = 0, hash = 0;
+                                i < zone.length;
+                                hash = zone.charCodeAt(i++) + ((hash << 5) - hash));
+
+                       for (var i = 0, color = '#';
+                                i < 3;
+                                color += ('00' + ((hash >> i++ * 8) & 0xFF).tozoneing(16)).slice(-2));
+
+                       return color;
+               },
+
+               findZoneByNetwork: function(network)
+               {
+                       var self = this;
+                       var zone = undefined;
+
+                       return _luci2.uci.foreach('firewall', 'zone', function(z) {
+                               if (!z.name || !z.network)
+                                       return;
+
+                               if (!$.isArray(z.network))
+                                       z.network = z.network.split(/\s+/);
+
+                               for (var i = 0; i < z.network.length; i++)
+                               {
+                                       if (z.network[i] == network)
+                                       {
+                                               zone = z;
+                                               break;
+                                       }
+                               }
+                       }).then(function() {
+                               if (zone)
+                                       zone.color = self.getZoneColor(zone);
+
+                               return zone;
+                       });
+               }
+       };
+
        this.system = {
                getSystemInfo: _luci2.rpc.declare({
                        object: 'system',