summaryrefslogtreecommitdiffstats
path: root/tinydns-edit-html.lsp
blob: a4754e1a692004744e689978cc31a2a21dd642c0 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<% local form, viewlibrary, page_info = ... %>
<% require("viewfunctions") %>

<% local domain = string.gsub(form.value.filename.value, "^.*/", "") %>
<script type="text/javascript" src="/js/jquery-latest.js"></script>
<script type="text/javascript">
	var editEntry = '<td> \
		<a href="javascript:;"><img src="/skins/static/tango/16x16/actions/list-add.png" width="16" height="16" title="Insert record"></a> \
		<a href="javascript:;"><img src="/skins/static/tango/16x16/actions/list-remove.png" width="16" height="16" title="Remove record"></a> \
		<a href="javascript:;"><img src="/skins/static/tango/16x16/actions/document-properties.png" width="16" height="16" title="Edit record"></a> \
		</td>';

	function Entry(entryType,descr,num,descr0,descr1,descr2,descr3,descr4,descr5,descr6,descr7,descr8,descr9,descr10){
		this.entryType=entryType;
		this.descr=descr;
		this.num=num;this.descriptions=new Array(11);
		this.descriptions[0]=descr0;this.descriptions[1]=descr1;this.descriptions[2]=descr2;this.descriptions[3]=descr3;this.descriptions[4]=descr4;this.descriptions[5]=descr5;this.descriptions[6]=descr6;this.descriptions[7]=descr7;this.descriptions[8]=descr8;this.descriptions[9]=descr9;this.descriptions[10]=descr10;
	}

	var entryTypes = new Array(12);
	entryTypes[0]=new Entry("","Comment line",1,"Comment");
	entryTypes[1]=new Entry(".","Name server",6,"Domain","IP address","Name server","Time to live","Timestamp","Location");
	entryTypes[2]=new Entry("&","Delegate subdomain",6,"Domain","IP address","Name server","Time to live","Timestamp","Location");
	entryTypes[3]=new Entry("=","Host",5,"Host","IP address","Time to live","Timestamp","Location");
	entryTypes[4]=new Entry("+","Alias",5,"Alias","IP address","Time to live","Timestamp","Location");
	entryTypes[5]=new Entry("@","Mail exchanger",7,"Domain","IP address","Mail exchanger","Distance","Time to live","Timestamp","Location"); 
	entryTypes[6]=new Entry("'","Text record",5,"Domain","Text Record","Time to live","Timestamp","Location");
	entryTypes[7]=new Entry("^","Reverse record",5,"PTR","Domain name","Time to live","Timestamp","Location");
	entryTypes[8]=new Entry("C","Canonical name",5,"Domain","Canonical name","Time to live","Timestamp","Location");
	entryTypes[9]=new Entry("Z","SOA record",11,"Domain","Primary name server","Contact address","Serial number","Refresh time","Retry time","Expire time","Minimum time","Time to live","Timestamp","Location");
	entryTypes[10]=new Entry(":","Generic record",6,"Domain","Record type","Record data","Time to live","Timestamp","Location");
	entryTypes[11]=new Entry("%","Client location",2,"Location","IP prefix");

	function finishForm(entry){
		var entrytext = entry.find("select").val() + entry.find("input:not(.submit)").map(function(){
			if (this.name == "fixme") {
				if ($(this).val() != "")
					return $(this).val() + ".<%= domain %>";
				else
					return "<%= domain %>";
			} else {
				return $(this).val();
			}
		}).get().join(":");
		entry.empty().append(entrytext);
	}
	function createForm(entry){
		var entrytext = entry.text();
		var entryType = entryTypes[0];
		var form = '<select>';
		var typechar = entrytext.charAt(0);
		if (typechar === null) { typechar = ""; }
		for (i=0; i<12; i++) {
			form = form + '<option ';
			if (typechar == entryTypes[i].entryType){
				entryType=entryTypes[i];
				form = form + 'selected ';
			}
			form = form + 'value="' + entryTypes[i].entryType + '">' + entryTypes[i].descr + '</option>';
		}
		form = form + "</select><br><dl>";
		var entries;
		if (entryType.entryType == ""){
			entries = new Array(1);
			entries[0] = entrytext;
		} else {
			entrytext = entrytext.substring(1,entrytext.length) + "::::::::::";
			entries = entrytext.split(":");
		}
		for (i=0; i<entryType.num; i++){
			if (entries[i] === null) { entries[i] = ""; }
			if (entryType.descriptions[i] == "Domain" || entryType.descriptions[i] == "Alias" || entryType.descriptions[i] == "Host" )
				form = form + '<dt>' + entryType.descriptions[i] + '</dt><dd><input type="text" value="' + entries[i].replace(/\.?<%= string.gsub(domain, "[\\%[%]%.%*%?%+%{%^%$]", "\\%1") %>$/i, "") + '" name="fixme"> . <%= domain %></dd>';
			else
				form = form + '<dt>' + entryType.descriptions[i] + '</dt><dd><input type="text" value="' + entries[i] + '"></dd>';
		}
		form = form + '\n<DT></DT><DD><input class="submit" type="submit" value="<%= form.option %>"></DD>';
		form = form + "\n</dl>";
		entry.empty().append(form);
		entry.find("select").change(function(){
			var entry = $(this).parent();
			finishForm(entry);
			createForm(entry);
		});
		entry.find("input.submit").click(function() {
			$("input[name='<%= form.option %>']").click();
		});
	}
	function editLine(){
		var parent = $(this).parent();
		if (parent.is("td")) {
			parent = parent.parent();
		}
		var entry = parent.find("td:eq(1)");
		if (entry.find("select").is("select")){
			finishForm(entry);
		} else {
			createForm(entry);
		}
	}
	function deleteLine(){
		$(this).parent().parent().replaceWith();
	}
	function addLine(){
		addLinks($(this).parent().parent().before('<tr><td></td></tr>').prev());
	}
	function addLinks(rows){
		rows.each(function(){
			$(this).prepend(editEntry);
			$(this).find("td > a:eq(0)").click(addLine);
			$(this).find("td > a:eq(1)").click(deleteLine);
			$(this).find("td > a:eq(2)").click(editLine);
			$(this).find("td:eq(1)").dblclick(editLine);
		});
	}
	function submitFile(){
		var file = "";
		var lines = $("#entries").find("tr").find("td:eq(1)");
		lines.each(function(){
			var elem = jQuery(this);
			if (elem.find("select").is("select")){
				finishForm(elem);
			}
			file = file + elem.text() + "\n";
		});
		$("input:eq(1)").val(file);
	}

	$(function(){
		addLinks($("#entries").find("tr"));
		$("input.submit").click(submitFile);
		<% if form.value.filecontent.linenumber then %>
			$("#entries tr:eq(<%= form.value.filecontent.linenumber - 1 %>)").find("a:eq(2)").click();
			var top = $("#entries tr:eq(<%= form.value.filecontent.linenumber - 1 %>)").offset().top;
			$("html,body").scrollTop(top);
		<% end %>
	});
</script>

<%
--[[ DEBUG INFORMATION
io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>")
io.write(html.cfe_unpack(form))
io.write("</span>")
--]]
%>

<H1>Configuration</H1>
<H2>Expert Configuration</H2>
<H3>File Details</H3>
<DL>
<% 
displayitem(form.value.filename)
displayitem(form.value.filesize)
displayitem(form.value.mtime)
%>
</DL>

<H3>File Entries</H3>
<% if form.descr then %><P CLASS='descr'><%= string.gsub(form.descr, "\n", "<BR>") %></P><% end %>
<% if form.errtxt then %><P CLASS='error'><%= string.gsub(form.errtxt, "\n", "<BR>") %></P><% end %>
<TABLE id="entries">
<% for line in string.gmatch(html.html_escape(form.value.filecontent.value).."\n", "([^\n]*)\n") do %>
	<TR>
	<TD><%= line %></TD>
	</TR>
<% end %>
</TABLE>
<% if form.value.filecontent.errtxt then %><P CLASS='error'><%= string.gsub(form.value.filecontent.errtxt, "\n", "<BR>") %></P><% end %>

<form action="<%= page_info.script .. page_info.prefix .. page_info.controller .. "/" .. page_info.action %>" method="POST">
<input type="hidden" name="filename" value="<%= form.value.filename.value %>">
<input type="hidden" name="filecontent" value="<%= html.html_escape(form.value.filecontent.value) %>">
<H2>Save and Apply Above Settings</H2>
<DL><DT></DT><DD><input class="submit" type="submit" name="<%= form.option %>" value="<%= form.option %>"></DD></DL>
</form>