summaryrefslogtreecommitdiffstats
path: root/interfaces-update-html.lsp
blob: da0cde20a47835fd5cb44e62aedc24800fe66c5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<% local form, viewlibrary, page_info, session = ... 
htmlviewfunctions = require("htmlviewfunctions")
html = require("acf.html")
json = 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">
	if (typeof jQuery == 'undefined') {
		document.write('<script type="text/javascript" src="<%= html.html_escape(page_info.wwwprefix) %>/js/jquery-latest.js"><\/script>');
	}
</script>

<script type="text/javascript">
	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 optionalarray = [];
		if (methodoptions[family] && methodoptions[family][method])
			optionalarray = methodoptions[family][method];
		
		// show / hide all of the optional inputs
		$(".optional").each(function() {
			if (jQuery.inArray($(this).attr('name'), optionalarray)==-1) {
				$(this).parents(".item").hide();
			} else {
				$(this).parents(".item").show();
			}
		});
	}
	function methodchange() {
		$(this).removeClass('error').siblings().remove();
		$(this).parent().prev().removeClass('error');
		$(this).find("option").remove("*:contains('[')");
		showoptionalfields();
	}
	function familychange() {
		$(this).removeClass('error').siblings().remove();
		$(this).parent().prev().removeClass('error');
		$(this).find("option").remove("*:contains('[')");
		var method = $("select[name='method']");
		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>');
			});
		}
		method.append('<option selected value="">[]</option>');
		$(".optional").parents(".item").hide();
	}
	$(function(){
		$("select[name='family']").change(familychange);
		$("select[name='method']").change(methodchange);
		// 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>

<%
if page_info.action == "update" then form.label = form.label.." - "..form.value.name.value end
htmlviewfunctions.displayitem(form, page_info)
%>