-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
200 lines (157 loc) · 5.43 KB
/
popup.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
"use strict"
function openTab(isNewTab) {
var element = document.getElementsByClassName("mdl-tabs__tab is-active")[0];
var url, baseURL, company;
baseURL = document.getElementById('base_url').value;
company = document.getElementById('company').value;
switch (element.hash) {
case '#table-browser-panel':
var searchParams = new URLSearchParams({
cmp: company,
mi: "SysTableBrowser",
tableName: document.getElementById('table_name').value
});
break;
case '#class-runner-panel':
var searchParams = new URLSearchParams({
cmp: company,
mi: "SysClassRunner",
cls: document.getElementById('class_name').value
});
break;
case '#form-panel':
var searchParams = new URLSearchParams({
cmp: company,
f: document.getElementById('form_name').value
});
break;
}
url = `${baseURL}/?${searchParams.toString()}`;
if (isNewTab) {
chrome.tabs.create({ url: url }, null);
}
else {
chrome.tabs.update(null, { url: url });
}
chrome.storage.sync.get(['base_url'], (result) => {
var resultCopy;
if (result.base_url && !result.base_url.includes(baseURL)) {
resultCopy = {
'base_url': [
...result.base_url,
baseURL
]
};
}
else {
resultCopy = {
'base_url': [
baseURL
]
};
}
console.log(resultCopy);
chrome.storage.sync.set(resultCopy);
});
}
function validateFields(fieldName, fieldValue) {
var baseURL, company, message = "";
baseURL = document.getElementById('base_url').value;
company = document.getElementById('company').value;
if (!baseURL) {
message += "Base URL is required. ";
}
if (!company) {
message += "Company is required. ";
}
var element = document.getElementsByClassName("mdl-tabs__tab is-active")[0];
switch (element.hash) {
case '#table-browser-panel':
if (!document.getElementById('table_name').value) {
message += `Table name is required. `;
}
break;
case '#class-runner-panel':
if (!document.getElementById('class_name').value) {
message += `Class name is required. `;
}
break;
case '#form-panel':
if (!document.getElementById('form_name').value) {
message += `Form name is required. `;
}
break;
}
if (message) {
var snackbarContainer = document.getElementById("validation-message-box");
var data = { message: message };
snackbarContainer.MaterialSnackbar.showSnackbar(data);
return false;
}
else {
return true;
}
}
window.addEventListener('DOMContentLoaded', function () {
chrome.tabs.query({
active: true,
currentWindow: true
}, ([currentTab]) => {
var url = new URL(currentTab.url);
let params = url.searchParams;
// set url and company from current tab URL
document.getElementById('base_url').value = url.origin;
document.getElementById('company').value = params.get('cmp');
});
// Base URL autocomplete
document.getElementById('base_url').addEventListener('keyup', (event) => {
// retrieve the base_url_list element
var base_url_list = document.getElementById('base_url_list');
// minimum number of characters before we start to generate suggestions
var min_characters = 0;
if (event.target.value.length < 0) {
return;
}
else {
base_url_list.innerHTML = "";
chrome.storage.sync.get(['base_url'], (result) => {
if (result.base_url) {
result.base_url.forEach((value, index, array) => {
// Create a new <option> element.
var option = document.createElement('option');
option.value = value;
// attach the option to the datalist element
base_url_list.appendChild(option);
});
}
});
}
});
// Button click listener
document.getElementById('open_new_tab_button').addEventListener('click', (event) => {
event.preventDefault();
if (validateFields()) {
openTab(true);
}
});
// Button click listener
document.getElementById('open_current_tab_button').addEventListener('click', (event) => {
event.preventDefault();
if (validateFields()) {
openTab(false);
window.close();
}
});
// attach listeners to respond on "Enter"
document.querySelectorAll('.mdl-textfield__input').forEach((value, index, array) => {
value.focus();
document.execCommand("Paste");
value.addEventListener("keyup", event => {
if (event.key !== "Enter") {
return;
}
document.querySelector("#open_new_tab_button").click(); // Things you want to do.
event.preventDefault(); // No need to `return false;`.
});
});
});