forked from cloudspokes/GLIB-ChromeExt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
239 lines (220 loc) · 8.37 KB
/
options.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Copyright (c) 2016 TopCoder, Inc. All rights reserved.
*/
/**
* Represents the script for options page
*
*
* @author TCSASSEMBLER
* @version 1.0
*/
var TOKEN_KEY_GITHUB = 'glib::github_token';
var TOKEN_KEY_TOPCODER = 'glib::topcoder_token';
var ADD_MASS_DELIMETER = "###";
var ENVIRONMENT = 'glib::environment';
/**
* Set the value in Chrome Storage
*/
function setChromeStorage(key, val) {
var obj = {};
obj[key] = val;
chrome.storage.local.set(obj);
}
/**
* Remove a key from Chrome Storage
*/
function removeChromeStorage(key) {
chrome.storage.local.remove(key);
}
$(document).ready(function() {
/* initialization for vex library */
vex.defaultOptions.className = 'vex-theme-os';
/* Onload populate mappings from Chrome Storage */
populateRepoMaps();
/* Populate GitHub and TopCoder tokens */
chrome.storage.local.get(TOKEN_KEY_GITHUB, function(result) {
if (result[TOKEN_KEY_GITHUB] == undefined) {
$(".delete[type='github']").prop('disabled', true);
} else {
$("#githubToken").val(result[TOKEN_KEY_GITHUB]);
}
});
chrome.storage.local.get(TOKEN_KEY_TOPCODER, function(result) {
if (result[TOKEN_KEY_TOPCODER] == undefined) {
$(".delete[type='topcoder']").prop('disabled', true);
} else {
$("#topCoderToken").val(result[TOKEN_KEY_TOPCODER]);
}
});
//CWD-- set checkbox status
chrome.storage.local.get(ENVIRONMENT, function(result) {
if (result[ENVIRONMENT] == undefined) {
$("#environment").prop('checked', false);
} else {
$("#environment").prop('checked', result[ENVIRONMENT]);
}
});
//CWD-- bind event on checkbox
$("#environment").click(function() {
setChromeStorage(ENVIRONMENT, $("#environment").prop('checked'));
});
/* Delete GitHub and TopCoder tokens */
$(".delete").click(function() {
var self = this;
vex.dialog.confirm({
message: 'Are you sure to delete the token ?',
callback: function(value) {
if (value) {
if ($(self).attr("type") == "github") {
$(".delete[type='github']").prop('disabled', true);
removeChromeStorage(TOKEN_KEY_GITHUB);
$("#githubToken").val("");
} else {
$(".delete[type='topcoder']").prop('disabled', true);
removeChromeStorage(TOKEN_KEY_TOPCODER);
$("#topCoderToken").val("");
}
}
}
});
});
/* Delete mappings on click of delete icons */
$(document).on("click", ".remove", function(e) {
var index = $(this).attr("index");
var self = this;
chrome.storage.local.get("repoMap", function(result) {
/* Remove a specfic index from the existing data array */
result.repoMap.splice(index, 1);
setChromeStorage("repoMap", result.repoMap);
$(self).parent().fadeOut(300, function() {
$(self).parent().remove();
});
/* Change each row's index after delete */
$("#mapWrapper div").each(function(index) {
$(this).find(".project-id,.repo-url,.remove").attr("index", index);
});
if ($("#mapWrapper div").length == 1) {
$(".delete-all").prop("disabled", true);
}
});
});
/* Add a new mapping. Store the data in Chrome Storage. */
$("#add").click(function() {
chrome.storage.local.get("repoMap", function(result) {
var mapObj = {
"projectId": $("#pid").val(),
"repoURL": $("#repoUrl").val()
};
if (result.repoMap == undefined || result.repoMap.length == 0) {
setChromeStorage("repoMap", [mapObj]);
} else {
/* Push to existing data */
result.repoMap.push(mapObj);
setChromeStorage("repoMap", result.repoMap);
}
populateRepoMaps();
});
});
/* Delete all mappings. */
$(".delete-all").click(function() {
var self = this;
vex.dialog.confirm({
message: 'Are you sure to delete all mappings ?',
callback: function(value) {
if (value) {
removeChromeStorage("repoMap");
populateRepoMaps();
}
}
});
});
/**
* Mass addition of mappings. The default separator is ###.
* One mapping on each line
*/
$("#addMass").click(function() {
var self = this;
vex.dialog.open({
message: 'Enter TopCoder project id and GitHub repo separated by ' +
ADD_MASS_DELIMETER + ' (one mapping on each line)',
input: "<textarea rows=\"7\" placeholder=\"PROJECT_ID " + ADD_MASS_DELIMETER +
" REPO_URL\nPROJECT_ID " + ADD_MASS_DELIMETER +
" REPO_URL\" name=\"mappings\"></textarea>",
buttons: [
$.extend({}, vex.dialog.buttons.YES, {
text: 'Add mass'
}), $.extend({}, vex.dialog.buttons.NO, {
text: 'Cancel'
})
],
callback: function(data) {
if (data === false) {
console.log("canceled");
} else {
var mapArr = [];
/* Split each new line */
var lines = data.mappings.split("\n");
for (var i = 0; i < lines.length; i++) {
var projectId = lines[i].split(ADD_MASS_DELIMETER)[0];
var gitRepo = lines[i].split(ADD_MASS_DELIMETER)[1];
mapArr.push({
"projectId": projectId,
"repoURL": gitRepo
});
}
/* Read from Chrome Storage and push to existing array */
chrome.storage.local.get("repoMap", function(result) {
var mapsInStorage = result.repoMap;
if (mapsInStorage == undefined || mapsInStorage.length == 0) {
mapsInStorage = [];
}
/* Push to existing data */
for (var i = 0; i < mapArr.length; i++) {
mapsInStorage.push(mapArr[i]);
}
setChromeStorage("repoMap", mapsInStorage);
populateRepoMaps();
});
}
}
});
});
/* Mapping fields are editable. Save the data while user edits the fields */
$(document).on('keyup', '.project-id,.repo-url', function() {
var index = $(this).attr("index");
chrome.storage.local.get("repoMap", function(result) {
result.repoMap[index] = {
"projectId": $(".project-id[index='" + index + "']").val(),
"repoURL": $(".repo-url[index='" + index + "']").val()
};
setChromeStorage("repoMap", result.repoMap);
});
});
});
/**
* Read values from Chrome Storage and populate the mapping list
*/
function populateRepoMaps() {
$("#mapWrapper").empty();
chrome.storage.local.get("repoMap", function(result) {
if (result.repoMap != undefined) {
if (result.repoMap == "" || result.repoMap.length == 0) {
$(".delete-all").prop("disabled", true);
} else {
$(".delete-all").prop("disabled", false);
}
for (i = 0; i < result.repoMap.length; i++) {
var html = "<input type='text' class='project-id' index=" + i + ">" +
"<input type='text' class='repo-url' index=" + i + ">" +
"<img class='remove' src='images/delete.png' index=" + i + ">";
var div = $("<div>")
.appendTo("#mapWrapper")
.append(html);
$(div).find(".project-id").val(result.repoMap[i].projectId);
$(div).find(".repo-url").val(result.repoMap[i].repoURL);
}
} else {
$(".delete-all").prop("disabled", true);
}
});
}