-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpopup.html
392 lines (314 loc) · 9.58 KB
/
popup.html
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<!-- Copyright Mike Anderson 2011 -->
<html>
<head>
<link href="styles.css" media="screen" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="oauth2/oauth2.js"></script>
<script type="text/javascript" src="githubapi.js"></script>
<script type="text/javascript" src="scripts/utilities.js"></script>
<script>
jQuery.ajaxSetup({
'beforeSend': function(xhr) {
xhr.setRequestHeader("Accept", "application/json")
console.log("Outgoing ajax request");
console.log(xhr);
}
})
// Handler for response from checkAuthorized running in background.html
checkAuthorizedHandler = function(response){
if(!response.authorized)
{
// Reset the file upload field
console.log("not authorized");
$('.status').show();
$('.status').html('Please grant Github permissions');
$('.status').addClass('error');
setTimeout(function(){
$('.status').fadeOut('slow', function(){ $('.status').removeClass('error')});
},5000)
}
else
gh = new GitHub(response.authorized); // This will return the access key
}
buildRepoDropDown = function(){
dropdown = $(".repo-dropdown");
repoMap = JSON.parse(localStorage["repoMap"]);
// Build the
for (var key in repoMap) {
if (repoMap.hasOwnProperty(key)) {
repo_name = key
repo_url = repoMap[key]["url"]
var opt = $("<option>")
opt.val(repo_url)
opt.html(repo_name)
// TODO: set as selected if it was used last
if(localStorage["defaultRepo"] == repo_url)
opt.attr("selected", "true")
dropdown.append(opt)
}
}
}
buildAssignedDropDown = function(){
repoKey = $('.repo-dropdown option:selected').html()
dropdown = $(".assigned-dropdown");
// Reset the dropdown
dropdown.html('')
var blank = $("<option>")
blank.val("null")
blank.html("Nobody assigned")
dropdown.append(blank)
repoMap = JSON.parse(localStorage["repoMap"]);
contributorList = repoMap[repoKey]["contributors"]
// Build the dropdown
$.map(contributorList, function(contributor){
name = contributor.login
var opt = $("<option>")
opt.val(name)
opt.html(name)
dropdown.append(opt)
})
}
buildMilestoneDropDown = function(){
repoKey = $('.repo-dropdown option:selected').html()
dropdown = $(".milestone-dropdown");
// Reset the dropdown
dropdown.html('')
var blank = $("<option>")
blank.val("null")
blank.html("Select")
dropdown.append(blank)
repoMap = JSON.parse(localStorage["repoMap"]);
milestoneList = repoMap[repoKey]["milestones"]
// Build the dropdown
$.map(milestoneList, function(milestone){
name = milestone.title
var opt = $("<option>")
opt.val(milestone.number)
opt.html(name)
dropdown.append(opt)
})
}
//dataUrl = canvas.toDataURL('image/png');
submitIssue = function(crop){
s3_proxy_url = "http://afternoon-earth-714.heroku.com/s3_files"
options = {}
crop = crop || false
title = $(".title").val()
body = $(".body").val()
assigned = $(".assigned-dropdown").val()
milestone = $(".milestone-dropdown").val()
repoUrl = $(".repo-dropdown").val()
options["repo"] = repoUrl;
options["title"] = title;
options["body"] = body;
if(assigned != "null")
options["assignee"] = assigned;
if(milestone != "null")
options["milestone"] = milestone;
bodyAppend = []
ssFilename = ""
psFilename = ""
// Empty defer object
submitDefer = $.when();
if($('#pagesource').attr('checked'))
{
psFilename = randomUUID() + ".html";
bodyAppend.push("View [page source](" + getAwsPath(psFilename) + ")");
// Get the current URL
submitDefer = submitDefer.pipe(fetchUrl).pipe(function(url){
bodyAppend.push("Url: " + url);
return $.when();
});
// We should make sure we wait to submit the page source before we close the window for cropping
submitDefer = submitDefer.pipe(function(){
return uploadPageSource({filename: psFilename, s3_proxy_url: s3_proxy_url});
});
}
// User wants a full page screenshot
if($('#pagescreenshot').attr('checked') && !crop)
{
ssFilename = randomUUID() + ".jpeg";
bodyAppend.push("View [screenshot](" + getAwsPath(ssFilename) + ")");
// User the background.html to capture the window
var bg = chrome.extension.getBackgroundPage();
bg.screenshot.filename = ssFilename;
bg.screenshot.s3_proxy_url = s3_proxy_url;
bg.screenshot.captureWindow();
}
else if($('#pagescreenshot').attr('checked'))
{
ssFilename = randomUUID() + ".jpeg";
bodyAppend.push("View [screenshot](" + getAwsPath(ssFilename) + ")");
// We show the image cropper after we submit the Issue
}
// Append the links and Submit the new issue
submitDefer = submitDefer.pipe(function(){
console.log("submit time ")
options["body"] += "\n\n";
options["body"] += bodyAppend.join("\n");
return gh.newIssue(options)
})
// After the issue is submitted, show the complete status or close window and crop
if($('#pagescreenshot').attr('checked') && crop)
{
submitDefer.done(function(){
showCropSelector({filename: ssFilename, s3_proxy_url: s3_proxy_url});
})
}
else
{
submitDefer.done(showStatusSuccess)
}
}
fetchUrl = function(){
var deferred = new jQuery.Deferred();
chrome.tabs.getSelected(null,function(tab){
console.log("got URL from tab")
deferred.resolve(tab.url);
});
setTimeout(function(){
deferred.resolve("");
}, 200); // Timeout after 200 milliseconds
return deferred.promise();
}
showCropSelector = function(data){
setTimeout(function(){
var bg = chrome.extension.getBackgroundPage();
bg.screenshot.filename = data.filename;
bg.screenshot.s3_proxy_url = data.s3_proxy_url;
bg.screenshot.githubData = {issueUrl: gh.issueUrl, issueNumber: gh.issueNumber}
bg.screenshot.showSelectionArea();
window.close();
}, 100)
}
uploadPageSource = function(data){
console.log("getting page source")
var deferred = new jQuery.Deferred();
chrome.tabs.getSelected(null,function(tab){
console.log("sending page source request to tab.")
chrome.tabs.sendRequest(tab.id, {msg:"get_page_source"}, function(response){
console.log("got response from tab")
$.ajax({
url: data.s3_proxy_url,
type: 'post',
dataType: 'json',
data: {s3_file: {file_contents: response.source, filename: data.filename}},
// Whether or not this is a success, lets resolve the deffered
complete: function(data){ console.log("ajax completed from page.js"); deferred.resolve(data) }
});
});
});
setTimeout(function(){
deferred.resolve(false);
}, 1000); // Only wait 1 second
return deferred.promise();
}
function getAwsPath(filename){
path = "http://gis-e6xaz.s3.amazonaws.com/" + filename;
return path;
}
resetForm = function(){
$(".title").val("")
$(".body").val("")
$(".assigned-dropdown").val("null")
$(".milestone-dropdown").val("null")
}
setCropSubmitButton = function(){
if($("#pagescreenshot").attr('checked')) {
$('.crop-submit').fadeIn('fast');
} else {
$('.crop-submit').fadeOut('fast');
}
}
showStatusSuccess = function(){
$('.status').html('Issue number <a target="_blank" href="' + gh.issueUrl + '">' + gh.issueNumber + '</a> was created.');
$('.status').removeClass('spinner');
setTimeout(function(){
$('.status').fadeOut('slow')
},5000);
resetForm();
}
$(document).ready(function(){
// Reset background page
var bg = chrome.extension.getBackgroundPage();
bg.screenshot.data = null;
bg.screenshot.filename = "";
bg.screenshot.githubData = {}
bg.screenshot.captureType = ""
chrome.extension.sendRequest({msg: 'check_authorized'}, checkAuthorizedHandler)
buildRepoDropDown();
buildAssignedDropDown();
buildMilestoneDropDown();
$(".submit").click(function(){
submitIssue();
})
$(".crop-submit").click(function(){
submitIssue(true);
})
$(".repo-dropdown").change(function(){
selectedRepo = $(this).val();
localStorage["defaultRepo"] = selectedRepo;
// Rebuild the assignee list
buildAssignedDropDown();
buildMilestoneDropDown();
})
$("#pagescreenshot").change(function(){
setCropSubmitButton();
});
if($("#pagescreenshot").attr('checked')) {
$('.crop-submit').show();
} else {
$('.crop-submit').hide();
}
$('.status').ajaxSend(function() {
$(this).show();
$(this).html('Submitting...');
$(this).addClass('spinner');
});
$('.status').ajaxError(function(){
$(this).html('There was an error, please try again.');
$(this).removeClass('spinner');
$(this).addClass('error')
setTimeout(function(){
$('.status').fadeOut('slow')
},5000)
})
$('.refresh').click(function(){
$.when(gh.fetchRepos()).done(function(){
window.localStorage.setItem("repoMap", JSON.stringify(gh.repos))
location.reload(true);
}
);
})
})
</script>
</head>
<body>
<div class="header">
<a class="refresh" href="#">Refresh</a>
</div>
<div class="content">
<div class="tools">
<h1>Create a new issue</h1>
<select class="repo-dropdown"></select>
</div>
<div class="comment-wrapper">
<div class="comment-inner">
<form id="form">
<label>Title</label><br><input type="text" class="title"/><br>
<div class="infobar">
<p>Assign to: <select class="assigned-dropdown"></select></p>
<p>Milestone: <select class="milestone-dropdown"></select></p>
</div>
<textarea class="body" rows="10"></textarea>
<input type="checkbox" id="pagesource" name="source" value="true" checked="true" />Include page source<br>
<input type="checkbox" id="pagescreenshot" name="screenshot" value="true"/>Include screenshot<br>
</form>
</div>
</div>
<a href="#" class="submit button" style="margin-left: 15px"><span>Submit</span></a>
<a href="#" class="crop-submit button"><span>Crop & Submit</span></a>
<span class="status"></span>
</div>
</body>
</html>