summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2012-12-06 00:53:52 +0000
committerTed Trask <ttrask01@yahoo.com>2012-12-06 00:53:52 +0000
commit50f946418c3fbd5fd32f5218b177920a065a53ac (patch)
treebb7f51df2dccf9b2842c7975c39e5a0e483851d3
parentbf240e45183c7a1aff68a39b59b09638cae7e7e3 (diff)
downloadacf-alpine-baselayout-50f946418c3fbd5fd32f5218b177920a065a53ac.tar.bz2
acf-alpine-baselayout-50f946418c3fbd5fd32f5218b177920a065a53ac.tar.xz
Interfaces: Move iface structure to a separate file and use it in view to determine which options to display
The previous method meant that each option was defined in both the model and in the view. Now, everything is defined in the model, and the view just hides / shows as desired.
-rw-r--r--interfaces-definitions.lua61
-rw-r--r--interfaces-model.lua71
-rw-r--r--interfaces-update-html.lsp156
3 files changed, 108 insertions, 180 deletions
diff --git a/interfaces-definitions.lua b/interfaces-definitions.lua
new file mode 100644
index 0000000..9b6704a
--- /dev/null
+++ b/interfaces-definitions.lua
@@ -0,0 +1,61 @@
+module (..., package.seeall)
+-- Definitions of interface cfe's / options
+ required = {
+ comment = {type="longtext", label="Comments", seq=2},
+ auto = {type="boolean", value=false, label="Auto bring-up", seq=3},
+ name = {label="Interface Name", seq=1},
+ family = {type="select", label="Address Family", option={"inet", "ipx", "inet6"}, seq=4},
+ method = {type="select", label="Method", option={"loopback", "static", "manual", "dhcp", "bootp", "ppp", "wvdial", "dynamic", "v4tunnel"}, seq=5},
+ ['pre-up'] = {type="longtext", label="'pre-up' actions", seq=100},
+ up = {type="longtext", label="'up' actions", seq=101},
+ down = {type="longtext", label="'down' actions", seq=102},
+ ['post-down'] = {type="longtext", label="'post-down' actions", seq=103},
+ }
+ family_methods = {
+ inet = {"loopback", "static", "manual", "dhcp", "bootp", "ppp", "wvdial"},
+ ipx = {"static", "dynamic"},
+ inet6 = {"loopback", "static", "manual", "v4tunnel"},
+ }
+ method_options = {
+ inet = {
+ static = {"address", "netmask", "broadcast", "network", "metric", "gateway", "pointopoint", "media", "hwaddress", "mtu"},
+ dhcp = {"hostname", "leasehours", "leasetime", "vendor", "client", "hwaddress"},
+ bootp = {"bootfile", "server", "hwaddr"},
+ ppp = {"provider"},
+ wvdial = {"provider"},
+ },
+ ipx = {
+ static = {"frame", "netnum"},
+ dynamic = {"frame"},
+ },
+ inet6 ={
+ static = {"address", "netmask", "gateway", "media", "hwaddress", "mtu"},
+ v4tunnel = {"address", "netmask", "endpoint", "local", "gateway", "ttl"},
+ },
+ }
+ optional = {
+ address = {label="Address", seq=6},
+ netmask = {label="Netmask", seq=7},
+ endpoint = {label="Endpoint address", seq=8},
+ ['local'] = {label="Local address", seq=9},
+ broadcast = {label="Broadcast address", seq=10},
+ network = {label="Network address", seq=11},
+ metric = {label="Routing metric", seq=12},
+ gateway = {label="Default gateway", seq=13},
+ pointopoint = {label="Point-to-point address", seq=14},
+ media = {label="Medium type", seq=15},
+ hostname = {label="Hostname", seq=16},
+ leasehours = {label="Preferred lease time (hours)", seq=17},
+ leasetime = {label="Preferred lease time (seconds)", seq=18},
+ vendor = {label="Vendor class identifier", seq=19},
+ client = {label="Client identifier", seq=20},
+ hwaddress = {label="Hardware address", seq=21},
+ mtu = {label="MTU size", seq=22},
+ bootfile = {label="Boot file", seq=23},
+ server = {label="Server address", seq=24},
+ hwaddr = {label="Hardware address", seq=25},
+ provider = {label="Provider name", seq=26},
+ frame = {label="Ethernet frame type", seq=27},
+ netnum = {label="Network number", seq=28},
+ ttl = {label="TTL setting", seq=29},
+ }
diff --git a/interfaces-model.lua b/interfaces-model.lua
index 59e16ba..1e86b8c 100644
--- a/interfaces-model.lua
+++ b/interfaces-model.lua
@@ -14,67 +14,7 @@ local array
-- iface is a local table with cfes for the various parts of interface definitions
-- Depending on the address family and corresponding method, different options are valid
-local iface = {
- required = {
- comment = {type="longtext", label="Comments", seq=2},
- auto = {type="boolean", value=false, label="Auto bring-up", seq=3},
- name = {label="Interface Name", seq=1},
- family = {type="select", label="Address Family", option={"inet", "ipx", "inet6"}, seq=4},
- method = {type="select", label="Method", option={"loopback", "static", "manual", "dhcp", "bootp", "ppp", "wvdial", "dynamic", "v4tunnel"}, seq=5},
- ['pre-up'] = {type="longtext", label="'pre-up' actions", seq=100},
- up = {type="longtext", label="'up' actions", seq=101},
- down = {type="longtext", label="'down' actions", seq=102},
- ['post-down'] = {type="longtext", label="'post-down' actions", seq=103},
- },
- family_methods = {
- inet = {"loopback", "static", "manual", "dhcp", "bootp", "ppp", "wvdial"},
- ipx = {"static", "dynamic"},
- inet6 = {"loopback", "static", "manual", "v4tunnel"},
- },
- method_options = {
- inet = {
- static = {"address", "netmask", "broadcast", "network", "metric", "gateway", "pointopoint", "media", "hwaddress", "mtu"},
- dhcp = {"hostname", "leasehours", "leasetime", "vendor", "client", "hwaddress"},
- bootp = {"bootfile", "server", "hwaddr"},
- ppp = {"provider"},
- wvdial = {"provider"},
- },
- ipx = {
- static = {"frame", "netnum"},
- dynamic = {"frame"},
- },
- inet6 ={
- static = {"address", "netmask", "gateway", "media", "hwaddress", "mtu"},
- v4tunnel = {"address", "netmask", "endpoint", "local", "gateway", "ttl"},
- },
- },
- optional = {
- address = {label="Address", seq=6},
- netmask = {label="Netmask", seq=7},
- endpoint = {label="Endpoint address", seq=8},
- ['local'] = {label="Local address", seq=9},
- broadcast = {label="Broadcast address", seq=10},
- network = {label="Network address", seq=11},
- metric = {label="Routing metric", seq=12},
- gateway = {label="Default gateway", seq=13},
- pointopoint = {label="Point-to-point address", seq=14},
- media = {label="Medium type", seq=15},
- hostname = {label="Hostname", seq=16},
- leasehours = {label="Preferred lease time (hours)", seq=17},
- leasetime = {label="Preferred lease time (seconds)", seq=18},
- vendor = {label="Vendor class identifier", seq=19},
- client = {label="Client identifier", seq=20},
- hwaddress = {label="Hardware address", seq=21},
- mtu = {label="MTU size", seq=22},
- bootfile = {label="Boot file", seq=23},
- server = {label="Server address", seq=24},
- hwaddr = {label="Hardware address", seq=25},
- provider = {label="Provider name", seq=26},
- frame = {label="Ethernet frame type", seq=27},
- netnum = {label="Network number", seq=28},
- ttl = {label="TTL setting", seq=29},
- },
-}
+local iface = require("alpine-baselayout/interfaces-definitions")
-- Create an interface structure with all options
local get_blank_iface = function ()
@@ -297,15 +237,18 @@ end
get_iface_by_name = function(self, clientdata)
-- if the name is blank, then return a blank iface with error
+ local ret
unpack_interfaces()
local rc, idx = arrayindex(clientdata.name or "")
if rc == false then
- local ret = get_blank_iface()
+ ret = get_blank_iface()
ret.value.name.value = name
ret.value.name.errtxt = "Interface does not exist"
- return ret
+ else
+ ret = array[idx]
end
- return array[idx]
+ ret.value.name.readonly = true
+ return ret
end
get_iface = function(self, clientdata)
diff --git a/interfaces-update-html.lsp b/interfaces-update-html.lsp
index 06c88e0..579b9b2 100644
--- a/interfaces-update-html.lsp
+++ b/interfaces-update-html.lsp
@@ -1,89 +1,35 @@
<% local form, viewlibrary, page_info = ...
require("htmlviewfunctions")
html = require("acf.html")
+require("json")
+
+-- iface is a local table with cfes for the various parts of interface definitions
+-- Depending on the address family and corresponding method, different options are valid
+local iface = require("alpine-baselayout/interfaces-definitions")
+for name,value in pairs(iface.optional) do
+ form.value[name].class = "optional"
+end
%>
<script type="text/javascript" src="<%= html.html_escape(page_info.wwwprefix) %>/js/jquery-latest.js"></script>
<script type="text/javascript">
- // Here are the optional form entries
- <% -- I'm going to temporarily override the io.write function so I can remove \n from the form items
- local tmpwrite = io.write
- local buffer = {}
- io.write = function(...)
- for i,item in ipairs{...} do
- buffer[#buffer+1] = item
- end
- end
- function outputbuffer()
- print((table.concat(buffer,""):gsub("\n","")))
- buffer = {}
- end
- %>
- var _address = '<% htmlviewfunctions.displayformitem({ name="address", label="Address", value="", type="text" }) %>';<% outputbuffer() %>
- var _netmask = '<% htmlviewfunctions.displayformitem({ name="netmask", label="Netmask", value="", type="text" }) %>';<% outputbuffer() %>
- var _broadcast = '<% htmlviewfunctions.displayformitem({ name="broadcast", label="Broadcast address", value="", type="text" }) %>';<% outputbuffer() %>
- var _network = '<% htmlviewfunctions.displayformitem({ name="network", label="Network address", value="", type="text" }) %>';<% outputbuffer() %>
- var _metric = '<% htmlviewfunctions.displayformitem({ name="metric", label="Routing metric", value="", type="text" }) %>';<% outputbuffer() %>
- var _gateway = '<% htmlviewfunctions.displayformitem({ name="gateway", label="Default gateway", value="", type="text" }) %>';<% outputbuffer() %>
- var _pointopoint = '<% htmlviewfunctions.displayformitem({ name="pointopoint", label="Point-to-point address", value="", type="text" }) %>';<% outputbuffer() %>
- var _media = '<% htmlviewfunctions.displayformitem({ name="media", label="Medium type", value="", type="text" }) %>';<% outputbuffer() %>
- var _hwaddress = '<% htmlviewfunctions.displayformitem({ name="hwaddress", label="Hardware address", value="", type="text" }) %>';<% outputbuffer() %>
- var _mtu = '<% htmlviewfunctions.displayformitem({ name="mtu", label="MTU size", value="", type="text" }) %>';<% outputbuffer() %>
- var _hostname = '<% htmlviewfunctions.displayformitem({ name="hostname", label="Hostname", value="", type="text" }) %>';<% outputbuffer() %>
- var _leasehours = '<% htmlviewfunctions.displayformitem({ name="leasehours", label="Preferred lease time (hours)", value="", type="text" }) %>';<% outputbuffer() %>
- var _leasetime = '<% htmlviewfunctions.displayformitem({ name="leasetime", label="Preferred lease time (seconds)", value="", type="text" }) %>';<% outputbuffer() %>
- var _vendor = '<% htmlviewfunctions.displayformitem({ name="vendor", label="Vendor class identifier", value="", type="text" }) %>';<% outputbuffer() %>
- var _client = '<% htmlviewfunctions.displayformitem({ name="client", label="Client identifier", value="", type="text" }) %>';<% outputbuffer() %>
- var _bootfile = '<% htmlviewfunctions.displayformitem({ name="bootfile", label="Boot file", value="", type="text" }) %>';<% outputbuffer() %>
- var _server = '<% htmlviewfunctions.displayformitem({ name="server", label="Server address", value="", type="text" }) %>';<% outputbuffer() %>
- var _hwaddr = '<% htmlviewfunctions.displayformitem({ name="hwaddr", label="Hardware address", value="", type="text" }) %>';<% outputbuffer() %>
- var _provider = '<% htmlviewfunctions.displayformitem({ name="provider", label="Provider name", value="", type="text" }) %>';<% outputbuffer() %>
- var _frame = '<% htmlviewfunctions.displayformitem({ name="frame", label="Ethernet frame type", value="", type="text" }) %>';<% outputbuffer() %>
- var _netnum = '<% htmlviewfunctions.displayformitem({ name="netnum", label="Network number", value="", type="text" }) %>';<% outputbuffer() %>
- var _endpoint = '<% htmlviewfunctions.displayformitem({ name="endpoint", label="Endpoint address", value="", type="text" }) %>';<% outputbuffer() %>
- var _local = '<% htmlviewfunctions.displayformitem({ name="local", label="Local address", value="", type="text" }) %>';<% outputbuffer() %>
- var _ttl = '<% htmlviewfunctions.displayformitem({ name="ttl", label="TTL setting", value="", type="text" }) %>';<% outputbuffer() %>
- // Finally, add the different types of method inputs<% outputbuffer() %>
- var _inetmethod = '<% htmlviewfunctions.displayformitem({ name="method", type="select", value="", label="Method", errtxt="Must define method", option={"loopback", "static", "manual", "dhcp", "bootp", "ppp", "wvdial"} }) %>';<% outputbuffer() %>
- var _ipxmethod = '<% htmlviewfunctions.displayformitem({ name="method", type="select", value="", label="Method", errtxt="Must define method", option={"static", "dynamic"} }) %>';<% outputbuffer() %>
- var _inet6method = '<% htmlviewfunctions.displayformitem({ name="method", type="select", value="", label="Method", errtxt="Must define method", option={"loopback", "static", "manual", "v4tunnel"} }) %>';<% outputbuffer() %>
- <% io.write = tmpwrite %>
-
- var optionalfields = "address,netmask,broadcast,network,metric,gateway,pointopoint,media,hwaddress,mtu,hostname,leasehours,leasetime,vendor,client,bootfile,server,hwaddr,provider,frame,netnum,endpoint,local,ttl";
- var inetstaticfields = "address,netmask,broadcast,network,metric,gateway,pointopoint,media,hwaddress,mtu";
- var inetdhcpfields = "hostname,leasehours,leasetime,vendor,client,hwaddress";
- var inetbootpfields = "bootfile,server,hwaddr";
- var inetpppfields = "provider";
- var inetwvdialfields = "provider";
- var ipxstaticfields = "frame,netnum";
- var ipxdynamicfields = "frame";
- var inet6staticfields = "address,netmask,gateway,media,hwaddress,mtu";
- var inet6v4tunnelfields = "address,netmask,endpoint,local,gateway,ttl";
-
+ var methodoptions = <%= json.encode(iface.method_options) %>;
+
+ var familymethods = <%= json.encode(iface.family_methods) %>;
+
function showoptionalfields() {
var family = $("select[name='family']").val();
var method = $("select[name='method']").val();
- var fieldstable = ""
- if (family == "inet" && method == "static") fieldstable = inetstaticfields;
- if (family == "inet" && method == "dhcp") fieldstable = inetdhcpfields;
- if (family == "inet" && method == "bootp") fieldstable = inetbootpfields;
- if (family == "inet" && method == "ppp") fieldstable = inetpppfields;
- if (family == "inet" && method == "wvdial") fieldstable = inetwvdialfields;
- if (family == "ipx" && method == "static") fieldstable = ipxstaticfields;
- if (family == "ipx" && method == "dynamic") fieldstable = ipxdynamicfields;
- if (family == "inet6" && method == "static") fieldstable = inet6staticfields;
- if (family == "inet6" && method == "v4tunnel") fieldstable = inet6v4tunnelfields;
+ var optionalarray = [];
+ if (methodoptions[family] && methodoptions[family][method])
+ optionalarray = methodoptions[family][method];
// show / hide all of the optional inputs
- var visiblearray = fieldstable.split(',');
- var optionalarray = optionalfields.split(',');
- $("input").each(function() {
- if (jQuery.inArray($(this).attr('name'), optionalarray)!=-1) {
- if (jQuery.inArray($(this).attr('name'), visiblearray)==-1) {
- $(this).parent().hide().prev().hide();
- } else {
- $(this).parent().show().prev().show();
- }
+ $(".optional").each(function() {
+ if (jQuery.inArray($(this).attr('name'), optionalarray)==-1) {
+ $(this).parent().hide().prev().hide();
+ } else {
+ $(this).parent().show().prev().show();
}
});
}
@@ -91,64 +37,42 @@ html = require("acf.html")
$(this).removeClass('error').siblings().remove();
$(this).parent().prev().removeClass('error');
$(this).find("option").remove("*:contains('[')");
- //$(this).remove("option:contains('[')");
showoptionalfields();
}
function familychange() {
$(this).removeClass('error').siblings().remove();
$(this).parent().prev().removeClass('error');
$(this).find("option").remove("*:contains('[')");
- //$(this).remove("option:contains('[')");
var method = $("select[name='method']");
- var newmethod = ""
- if ($(this).val() == "inet") { newmethod = _inetmethod; }
- if ($(this).val() == "ipx") { newmethod = _ipxmethod; }
- if ($(this).val() == "inet6") { newmethod = _inet6method; }
- if (method.length == 1) {
- method.parent().prev().remove().end().replaceWith(newmethod);
- } else {
- $(this).parent().after(newmethod);
+ method.find("option").remove(); // this also ensures that nothing is selected
+ if (familymethods[$(this).val()]) {
+ jQuery.each(familymethods[$(this).val()], function(index, value) {
+ method.append('<option selected value="'+value+'">'+value+'</option>');
+ });
}
- $("select[name='method']").change(methodchange);
- showoptionalfields();
+ method.append('<option selected value="">[]</option>');
+ $(".optional").parent().hide().prev().hide();
}
$(function(){
$("select[name='family']").change(familychange);
$("select[name='method']").change(methodchange);
- var end = $(".submit").parent().prev();
- if($("input[name='address']").length==0) end.before(_address);
- if($("input[name='netmask']").length==0) end.before(_netmask);
- if($("input[name='broadcast']").length==0) end.before(_broadcast);
- if($("input[name='network']").length==0) end.before(_network);
- if($("input[name='metric']").length==0) end.before(_metric);
- if($("input[name='gateway']").length==0) end.before(_gateway);
- if($("input[name='pointopoint']").length==0) end.before(_pointopoint);
- if($("input[name='media']").length==0) end.before(_media);
- if($("input[name='hwaddress']").length==0) end.before(_hwaddress);
- if($("input[name='mtu']").length==0) end.before(_mtu);
- if($("input[name='hostname']").length==0) end.before(_hostname);
- if($("input[name='leasehours']").length==0) end.before(_leasehours);
- if($("input[name='leasetime']").length==0) end.before(_leasetime);
- if($("input[name='vendor']").length==0) end.before(_vendor);
- if($("input[name='client']").length==0) end.before(_client);
- if($("input[name='bootfile']").length==0) end.before(_bootfile);
- if($("input[name='server']").length==0) end.before(_server);
- if($("input[name='hwaddr']").length==0) end.before(_hwaddr);
- if($("input[name='provider']").length==0) end.before(_provider);
- if($("input[name='frame']").length==0) end.before(_frame);
- if($("input[name='netnum']").length==0) end.before(_netnum);
- if($("input[name='endpoint']").length==0) end.before(_endpoint);
- if($("input[name='local']").length==0) end.before(_local);
- if($("input[name='ttl']").length==0) end.before(_ttl);
+ // Remove the method options that aren't valid for this family
+ var familyval = $("select[name='family']").val();
+ var method = $("select[name='method']");
+ if (familymethods[familyval]) {
+ method.find("option").each(function() {
+ if (jQuery.inArray($(this).val(), familymethods[familyval])==-1)
+ $(this).remove();
+ });
+ } else {
+ method.find("option").remove();
+ method.append('<option selected value="">[]</option>');
+ }
showoptionalfields();
});
</script>
<h1><%= html.html_escape(form.label) %><% if page_info.action == "update" then io.write(html.html_escape(" - "..form.value.name.value)) end %></h1>
<%
- if page_info.action == "update" then
- form.value.name.readonly = true
- end
- local order = {"name", "comment", "auto", "family", "method", "pre-up", "up", "down", "post-down"}
- htmlviewfunctions.displayform(form, order)
+ htmlviewfunctions.displayform(form, nil, nil, page_info, 2)
%>