diff options
author | Ted Trask <ttrask01@yahoo.com> | 2012-12-06 00:53:52 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2012-12-06 00:53:52 +0000 |
commit | 50f946418c3fbd5fd32f5218b177920a065a53ac (patch) | |
tree | bb7f51df2dccf9b2842c7975c39e5a0e483851d3 /interfaces-update-html.lsp | |
parent | bf240e45183c7a1aff68a39b59b09638cae7e7e3 (diff) | |
download | acf-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.
Diffstat (limited to 'interfaces-update-html.lsp')
-rw-r--r-- | interfaces-update-html.lsp | 156 |
1 files changed, 40 insertions, 116 deletions
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) %> |