-
Notifications
You must be signed in to change notification settings - Fork 0
/
uribuilder.js
208 lines (172 loc) · 5.7 KB
/
uribuilder.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* on page load
check if array is already in storage
if Not, display nothing message
if so, display items from array
*/
/* on new entry
store new value into array
store new array into storage
clear previous messages/list items
display items
*/
/* on clear
Delete array from storage
Clear array
display nothing message
*/
//disable logging
//console.log = function() {}
var uriList = [];
var storageKey = "servers";
var maxItems = 5;
function initialise() {
try {
if (!localStorage.getItem(storageKey)) {
//array is empty
console.log("initialise: store is empty");
renderMessage();
}
else {
//display items from array
//getLocalStorage
//renderList
console.log("initialise: key found, getting value...")
uriList = getLocalStorage(storageKey);
renderHistory(uriList);
}
}
catch(err) {
console.log("initialise: " + err);
renderMessage("There was an error reading from storage. The history will populate for this session only.<br><span style=\"color:red;\">" + err + "<\span>");
}
}
function renderMessage(message) {
if (message === undefined) message = "No items have been saved or could not read storage.";
var list = document.getElementById("initMessage");
list.innerHTML = message;
}
function getLocalStorage(key) {
//try catch
try {
var value = JSON.parse(localStorage.getItem(key));
console.log("GetLocalStorage: Key " + key + " has value " + value);
return value;
}
catch(err) {
console.log("GetLocalStorage: " + err);
}
}
function setLocalStorage(key,value) {
try {
localStorage.removeItem(key);
localStorage.setItem(key, JSON.stringify(value));
console.log("setLocalStorage: saved: " + JSON.parse(localStorage.getItem(key)));
}
catch (err) {
console.log("setLocalStorage: " + err);
}
}
function stubStorage() {
//storage stuff
uriList = ["milk", "eggs", "frosted flakes", "salami", "juice"];
console.log("stubStorage: saving: " + uriList);
localStorage.removeItem(storageKey);
localStorage.setItem(storageKey, JSON.stringify(uriList));
console.log("stubStorage: saved: " + JSON.parse(localStorage.getItem(storageKey)));
}
function clearLocalStorage(key) {
if (key === undefined) key = storageKey;
localStorage.removeItem(key);
}
function renderHistory(items) {
//output array to html bullet list
var list = document.getElementById("ulFeatureList");
var i = "";
clearHistory();
for (i = 0; i < items.length; i++) {
var newItem = document.createElement("LI"); // Create a <li> node
var newLink = document.createElement("A"); // Create and populate link href
newLink.setAttribute('href', items[i]);
newLink.setAttribute('target', '_blank');
var textNode = document.createTextNode(items[i]); // Create a text node for link text
newLink.appendChild(textNode); // Append the text to <a>
newItem.appendChild(newLink); // Append a to li
list.appendChild(newItem);
}
}
function clearHistory() {
var list = document.getElementById("ulFeatureList");
while (list.hasChildNodes()) {
list.removeChild(list.firstChild);
}
}
function buildUri(jiraID){
//build URL
var txtURIPrefix = "https://feature-";
var txtURISuffux = ".inshur.com";
var uri = txtURIPrefix + jiraID + txtURISuffux;
console.log("buildUri: " + uri);
return uri;
}
function buildUriFromTextbox() {
var uri = buildUri(document.getElementById("txtJiraID").value);
console.log("buildUriFromTextbox: " + uri);
return uri;
}
function updateUriList(newUri) {
// arrays stuff
console.log("updateUriList: unmodified array: " + uriList);
//check to see if newUri has already been saved in array
var itemIndex = uriList.indexOf(newUri);
console.log("updateUriList: itemIndex of new item= " + itemIndex);
if (itemIndex < 0) {
//not used before, add to array at beginning
console.log("updateUriList: URI not in history.")
var itemCount = uriList.unshift(newUri);
}
else {
//already exists, move to top of list and de-duplicate
console.log("updateUriList: URI in history.");
uriList.splice(itemIndex, 1); //remove current entry
var itemCount = uriList.unshift(newUri); //add to front
}
while (itemCount > maxItems) {
uriList.pop();
itemCount--;
console.log("updateUriList: Removed excess items...");
}
console.log("updateUriList: modified array: " + uriList);
}
function builder() {
console.log("submit: button clicked")
updateUriList(buildUriFromTextbox());
setLocalStorage(storageKey,uriList);
renderHistory(uriList);
window.open(
buildUri(document.getElementById("txtJiraID").value),
'_blank' // <- _blank for new window. _self for same window.
);
}
function resetHistory() {
clearLocalStorage();
clearHistory();
uriList = [];
}
//detect click on a link
window.onclick = function (ev) {
if (ev.target.localName == 'a') {
console.log(ev.target.text + ' was clicked!');
updateUriList(ev.target.text);
setLocalStorage(storageKey,uriList);
renderHistory(uriList);
}
}
//detect enter key press
var input = document.getElementById("txtJiraID");
input.addEventListener("keyup", function (event) {
if (event.keyCode === 13) {
event.preventDefault();
//return false;
document.getElementById("btnSubmit").click();
}
});