-
Notifications
You must be signed in to change notification settings - Fork 0
/
launchd.js
134 lines (121 loc) · 4.54 KB
/
launchd.js
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
/*
-----------------------------------------------------------------------------
Functions to open and close menu
-----------------------------------------------------------------------------
*/
function menu_open() {
document.getElementById("mySidebar").style.display = "block";
document.getElementById("myOverlay").style.display = "block";
}
function menu_close() {
document.getElementById("mySidebar").style.display = "none";
document.getElementById("myOverlay").style.display = "none";
}
function menu_list(item) {
var x = document.getElementById(item);
if (x.className.indexOf("w3-show") == -1) {
x.className += " w3-show";
x.previousElementSibling.className += " w3-green";
} else {
x.className = x.className.replace(" w3-show", "");
x.previousElementSibling.className =
x.previousElementSibling.className.replace(" w3-green", "");
}
}
/*
-----------------------------------------------------------------------------
Functions to adjust file
-----------------------------------------------------------------------------
*/
function element(name,content,attributes){
var att_str = ''
if (attributes) { // tests false if this arg is missing!
att_str = formatAttributes(attributes)
}
var xml
if (!content){
xml='<' + name + att_str + '/>'
}
else {
xml='<' + name + att_str + '>' + content + '</'+name+'>'
}
return xml
}
var APOS = "'"; QUOTE = '"'
var ESCAPED_QUOTE = { }
ESCAPED_QUOTE[QUOTE] = '"'
ESCAPED_QUOTE[APOS] = '''
/*
Format a dictionary of attributes into a string suitable
for inserting into the start tag of an element. Be smart
about escaping embedded quotes in the attribute values.
*/
function formatAttributes(attributes) {
var att_value
var apos_pos, quot_pos
var use_quote, escape, quote_to_escape
var att_str
var re
var result = ''
for (var att in attributes) {
att_value = attributes[att]
// Find first quote marks if any
apos_pos = att_value.indexOf(APOS)
quot_pos = att_value.indexOf(QUOTE)
// Determine which quote type to use around
// the attribute value
if (apos_pos == -1 && quot_pos == -1) {
att_str = ' ' + att + "='" + att_value + "'"
result += att_str
continue
}
// Prefer the single quote unless forced to use double
if (quot_pos != -1 && quot_pos < apos_pos) {
use_quote = APOS
}
else {
use_quote = QUOTE
}
// Figure out which kind of quote to escape
// Use nice dictionary instead of yucky if-else nests
escape = ESCAPED_QUOTE[use_quote]
// Escape only the right kind of quote
re = new RegExp(use_quote,'g')
att_str = ' ' + att + '=' + use_quote +
att_value.replace(re, escape) + use_quote
result += att_str
}
return result
}
function test() {
var atts = {att1:"a1",
att2:"This is in \"double quotes\" and this is " +
"in 'single quotes'",
att3:"This is in 'single quotes' and this is in " +
"\"double quotes\""}
// Basic XML example
// alert(element('elem','This is a test'))
// Nested elements
var xml = element('p', 'This is ' +
element('strong','Bold Text') + 'inline')
// alert(xml)
// Attributes with all kinds of embedded quotes
// alert(element('elem','This is a test', atts))
// Empty element version
// alert(element('elem','', atts))
}
/*
-----------------------------------------------------------------------------
Functions to create files
-----------------------------------------------------------------------------
*/
function createFile(){
var filename = document.getElementById("fileName").value
var net = {file:filename + ".net.xml"}
var rou = {file:filename + ".rou.xml"}
var add = {file:filename + ".add.xml"}
var settings = {file:filename + ".settings.xml"}
var sumocfg = {file:filename + ".sumocfg", type:"config"}
var file = new File([element('launch', ["\n\t" + element('copy', null, net) + "\n\t" + element('copy', null, rou) + "\n\t" + element('copy', null, add) + "\n\t" + element('copy', null, settings) + "\n\t" + element('copy', null, sumocfg) + "\n"], null)], {type: "application/xml;charset==utf-8"});
saveAs(file, filename + ".launchd.xml");
}