Repository navigation
Expand file tree
/
Copy patheditor.js
More file actions
143 lines (136 loc) · 4.12 KB
/
Copy patheditor.js
File metadata and controls
143 lines (136 loc) · 4.12 KB
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
if (localStorage.getItem("secrets")===null) {
var secrets = {};
} else {
var secrets =JSON.parse(localStorage.getItem("secrets"));
}
var codedisplays = document.getElementById("codedisplays");
var len = null
function displaysecrets() {
if (Object.keys(secrets).length === 0){
codedisplays.textContent="No codes set up yet, add some!";
codedisplays.innerHTML+=("<p><button id='service0' onclick='newentry();'>New</button></p>"); //the "New" button is called "service#" # being the amount of services, so it gets selected when the user gets to the bottom
} else {
len = Object.keys(secrets).length;
codedisplays.textContent="";
for (var i = 0; i < len; i++) {
service=Object.keys(secrets)[i];
codedisplays.innerHTML+=("<p><input type=text id='service"+String(i)+"'value='"+service+"'><button id='delete"+String(i)+"' onclick='delentry(\""+String(i)+"\");'>Delete</button></p>");
codedisplays.innerHTML+=("<p>Secret:<input type=text onclick='newentry();' id='secret"+String(i)+"'value='"+secrets[service]["secret"]+"'></p>");
}
codedisplays.innerHTML+=("<p><button id='service"+String(len)+"' onclick='newentry();'>New</button></p>"); //the "New" button is called "service#" # being the amount of services, so it gets selected when the user gets to the bottom
}
}
displaysecrets();
document.getElementById("service0").focus();
var selectedType = "service";
var selectedServiceId=0;
function save() {
if (confirm("Save changes and exit?")===true) {
len = Object.keys(secrets).length;
secrets = {};
for (var i = 0; i<len; i++) {
secrets[document.getElementById("service"+i).value] = {"secret":(document.getElementById("secret"+i).value)}
}
localStorage.setItem("secrets", JSON.stringify(secrets));
window.location.href = "index.html";
}
}
function exitwithoutsaving() {
if (confirm("Exit without saving?")===true) {
window.location.href = "index.html";
}
}
function newentry() {
len = Object.keys(secrets).length;
secrets = {};
for (var i = 0; i<len; i++) {
secrets[document.getElementById("service"+i).value] = {"secret":(document.getElementById("secret"+i).value)}
console.log(document.getElementById("service"+i).value)
}
selectedType = "service";
selectedServiceId = len;
var newentryid=len+1;
secrets["Entry "+newentryid] = {"secret":""};
displaysecrets();
document.getElementById(selectedType+selectedServiceId).focus();
}
function delentry(entry) {
if (confirm("Delete entry "+(entry+1)+"?") === true) {
delete secrets[Object.keys(secrets)[entry]]
}
displaysecrets();
}
function moveup() {
var el=null;
deleteselected=false;
if (selectedType==="secret") {
el=document.getElementById("delete"+selectedServiceId)
if (el) {
el.focus();
selectedType="delete";
}
}else if (selectedType==="delete") {
el=document.getElementById("service"+selectedServiceId)
if (el) {
el.focus();
selectedType="service";
}
} else {
if (selectedServiceId === 0) {
selectedType = "savebtn";
document.getElementById("savebtn").focus();
} else{
el = document.getElementById("secret"+(selectedServiceId-1))
if (el) {
el.focus();
selectedServiceId--;
selectedType="secret";
}
}
}
}
function movedown() {
var el=null;
deleteselected=false;
if (selectedType === "savebtn") {
selectedType = "service";
selectedServiceId = 0;
document.getElementById("service"+selectedServiceId).focus();
} else {
if (selectedType==="service") {
el=document.getElementById("delete"+selectedServiceId)
if (el) {
el.focus();
selectedType="delete";
}
} else if (selectedType==="delete") {
el = document.getElementById("secret"+selectedServiceId)
if (el) {
el.focus();
selectedType="secret";
}
}
else {
el = document.getElementById("service"+(selectedServiceId+1))
if (el) {
el.focus();
selectedServiceId++;
selectedType="service";
}
}
}
}
window.addEventListener("load", () => {
document.addEventListener("keydown", evt => {
console.log("Pressed:", evt.key);
if (evt.key === "ArrowUp") {
moveup();
}
if (evt.key === "ArrowDown") {
movedown();
}
if (evt.key === "Backspace") {
exitwithoutsaving();
}
});
});