forked from jarrodek/ChromeRestClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
603 lines (569 loc) · 16.1 KB
/
Copy pathmain.js
File metadata and controls
603 lines (569 loc) · 16.1 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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
(function(document, window) {
'use strict';
let app = document.querySelector('#app');
app.pageTitle = 'Request';
/**
* Selected request database ID
*/
app.selectedRequest = null;
app.gaCustomDimensions = [];
app.appId = chrome.runtime && chrome.runtime.id ? chrome.runtime.id : 'not-in-chrome-app';
app.analyticsDisabled = false;
app.initialized = false;
// True if the user is signed in to Chrome browser and can authorize the app.
app.chromeSignedIn = false;
app.appVersion = arc.app.utils.appVer;
app.observers = [
'_routeChanged(route, params.*)'
];
app._routeChanged = function(route, paramsRecord) {
var params = paramsRecord && paramsRecord.base;
switch (route) {
case 'request':
app.pageTitle = 'Request';
if (!params || !params.type) {
return;
}
var id;
switch (params.type) {
case 'history': id = params.historyId; break;
case 'saved': id = params.savedId; break;
case 'restore':
case 'latest':
case 'current':
app.selectedRequest = '';
return;
case 'drive':
app.selectedRequest = '';
return app.openDriveItem(params.driveId);
default:
app.selectedRequest = '';
console.error('ID not handled!', params);
throw new Error('ID not handled!');
}
app.selectedRequest = decodeURIComponent(id);
return;
case 'project':
if (!params) {
return;
}
app.fire('selected-project-changed', {
value: params.projectId
});
app.pageTitle = 'Project details';
break;
case 'socket':
app.pageTitle = 'Socket';
app.selectedRequest = undefined;
break;
case 'settings':
app.pageTitle = 'Settings';
app.selectedRequest = undefined;
break;
case 'history':
app.pageTitle = 'History';
app.selectedRequest = undefined;
break;
case 'saved':
app.pageTitle = 'Saved request';
app.selectedRequest = undefined;
break;
default:
app.selectedRequest = undefined;
}
};
// Event fired when all components has been initialized.
app.addEventListener('dom-change', function() {
app.updateBranding();
});
// Called when current request changed.
window.addEventListener('selected-request', (e) => {
app.selectedRequest = e.detail.id ? e.detail.id : null;
});
// event fired when the app is initialized and can remove loader.
window.addEventListener('ArcInitialized', function() {
document.querySelector('arc-loader-screen').opened = false;
app.initialized = true;
});
window.addEventListener('WebComponentsReady', function() {
app.initAnalytics();
app.initRouting();
});
app.initRouting = () => {
if (app.routingInitialized) {
console.warn('Routing is already initialized.');
return;
}
app.routingInitialized = true;
let event = new Event('initializeRouting');
window.dispatchEvent(event);
};
//When changin route this will scroll page top. This is called from router.
app.scrollPageToTop = function() {
app.$.headerPanelMain.scrollToTop(true);
};
//called by the router to close a drawer (in mobile view) when changing route.
app.closeDrawer = function() {
app.$.paperDrawerPanel.closeDrawer();
};
// called when any component want to change request link.
document.body.addEventListener('action-link-change', (e) => {
var url = e.detail.url;
var setUrl = function(url) {
let panel = document.querySelector('request-panel');
panel.set('request.url', url);
};
var getUrl = function() {
return document.querySelector('request-panel').request.url;
};
var currentUrl = getUrl();
if (currentUrl && url.indexOf('/') === 0) {
var parser;
try {
parser = new URL(currentUrl);
url = parser.origin + url;
setUrl(url);
} catch (e) {
console.log('URL parse error', e);
this.fire('app-log', {
message: e
});
setUrl(url);
}
} else {
setUrl(url);
}
app.scrollPageToTop();
});
app.onSave = (e, detail) => {
var ctrl = document.querySelector('arc-request-panel');
ctrl.onSave({
source: 'shortcut',
shift: detail.keyboardEvent.shiftKey
});
var label = 'Save';
if (detail.keyboardEvent.shiftKey) {
label += ' (shift)';
}
app.fire('send-analytics', {
type: 'event',
category: 'Shortcats usage',
action: 'Called',
label: label
});
};
app.onSaveShift = (e, detail) => app.onSave(e, detail);
app.onOpen = () => {
page('/saved');
app.fire('send-analytics', {
type: 'event',
category: 'Shortcats usage',
action: 'Called',
label: 'Open'
});
};
// Current "height" of the top header.
app.mainHeaderTop = '64px';
// Called when ctrl/command + F combination has been pressed.
app.onSearch = () => {
if (app.route !== 'request') {
return;
}
var searchBar = document.getElementById('content-search-bar');
if (searchBar.opened) {
searchBar.focusInput();
} else {
searchBar.style.top = app.mainHeaderTop;
searchBar.opened = true;
app.fire('send-analytics', {
type: 'event',
category: 'Shortcats usage',
action: 'Called',
label: 'Search'
});
}
};
// Called when ctrl/command + n called to open new window.
app.onNewWindow = () => {
chrome.runtime.getBackgroundPage(function(bg) {
bg.arc.bg.openWindow();
});
};
app.onSend = () => {
if (app.route !== 'request') {
return;
}
var panel = document.querySelector('arc-request-panel');
if (!panel) {
console.warn('The request panel is undefined');
return;
}
panel.sendRequest();
};
app.textSearchBarOpened = () => {
var searchBar = document.getElementById('content-search-bar');
if (!searchBar) {
console.warn('Search bar was not available in document.');
return;
}
searchBar.style.top = app.mainHeaderTop;
};
window.addEventListener('paper-header-transform', function(e) {
var searchBar = document.getElementById('content-search-bar');
if (!searchBar.opened) {
return;
}
var detail = e.detail;
var top = detail.height - detail.y;
if (top < 0) {
top = 0;
}
if (searchBar.style.top === top + 'px') {
return;
}
app.mainHeaderTop = top + 'px';
searchBar.style.top = top;
});
/**
* Updates body class depending on a channel release.
* TODO: update dev and beta branding.
*/
app.updateBranding = () => {
if (!chrome.runtime.getManifest) {
//tests
return;
}
var channel = arc.app.utils.releaseChannel;
var cls = null;
if (channel === 'canary') {
cls = 'canary-channel';
} else if (channel === 'dev') {
cls = 'dev-channel';
} else if (channel === 'beta') {
cls = 'beta-channel';
}
if (cls) {
document.body.classList.add(cls);
Polymer.updateStyles();
}
};
/**
* Used by elements to open a browser window/tab.
* Element must have data-href attribute set with value of the URL to open.
*
* @param {ClickEvent} e A click event.
*/
app.followLink = (e) => {
if (e.target.dataset.href) {
window.open(e.target.dataset.href);
}
};
/**
* Handler called to network state change event.
* This will display toase then the device is offline and close the message when it
* comes back online.
*/
app._networkStateChanged = (e) => {
var online = e.detail.online;
if (online) {
app.$.offlineToast.opened = false;
} else {
app.$.offlineToast.opened = true;
}
};
window.addEventListener('error', (e) => {
if (!e.detail || !e.detail.message) {
return;
}
var message = '[Window]' + e.detail.message;
app.fire('send-analytics', {
type: 'exception',
description: message,
fatal: false
});
});
window.addEventListener('unhandledrejection', (event) => {
event.preventDefault();
let reason = event.reason;
console.error('Unhandled promise rejection: ' + (reason && (reason.stack || reason)));
app.fire('send-analytics', {
type: 'exception',
description: (reason && (reason.stack || reason)),
fatal: false
});
});
app._computeA11yButtons = (key, hasShift) => {
var isMac = navigator.platform.indexOf('Mac') !== -1;
var cmd = '';
if (isMac) {
cmd += 'meta+';
} else {
cmd += 'ctrl+';
}
if (hasShift) {
cmd += 'shift+';
}
cmd += key;
return cmd;
};
function openDriveSelector() {
if (!app.chromeSignedIn) {
return app.openChromeSigninInfo();
}
page('/drive');
}
window.addEventListener('open-drive-selector', openDriveSelector);
window.addEventListener('pick-google-drive-item', openDriveSelector);
app.initAnalytics = function() {
app.push('gaCustomDimensions', {
index: 1,
value: arc.app.utils.chromeVersion
});
app.push('gaCustomDimensions', {
index: 2,
value: arc.app.utils.appVer
});
app.push('gaCustomDimensions', {
index: 5,
value: arc.app.utils.releaseChannel
});
};
window.addEventListener('analytics-permitted-changed', (e) => {
var permitted = e.detail.permitted;
app.set('analyticsDisabled', !permitted);
});
window.addEventListener('app-new-window', function() {
chrome.runtime.getBackgroundPage(function(bg) {
bg.arc.bg.openWindow();
});
});
window.addEventListener('app-version', function(e) {
e.detail.version = arc.app.utils.appVer;
});
app.notifyError = function(message) {
app.$.errorToast.text = message;
app.$.errorToast.opened = true;
};
function isSingleRequest(data) {
if (!data.requests || !data.requests.length) {
return false;
}
if (data.requests.length !== 1) {
return false;
}
if (data.projects && data.projects.length === 0) {
delete data.projects;
}
if (Object.keys(data).length === 4) {
return true;
}
return false;
}
function processIcomingData(data, opts) {
opts = opts || {};
var event = app.fire('import-normalize', {
content: data
}, {
cancelable: true
});
event.detail.result.then((data) => {
if (isSingleRequest(data)) {
let obj = data.requests[0];
if (opts.diveId) {
obj.type = 'google-drive';
obj.driveId = opts.diveId;
}
document.body.querySelector('arc-request-panel').request = obj;
page('/request/current');
} else {
let panel = document.body.querySelector('data-import-export-panel');
panel.importObject = data;
panel.importPage = 3;
app.openImportExport();
}
})
.catch(cause => app.notifyError(cause.message));
}
window.addEventListener('on-process-incoming-data', function(e) {
processIcomingData(e.detail.data);
});
app.openChromeSigninInfo = function() {
var node = document.querySelector('chrome-not-signedin-view');
if (!node) {
node = document.createElement('chrome-not-signedin-view');
document.body.appendChild(node);
}
node.opened = true;
};
/**
* Opens a request from the Google Drive.
* This action support integration with Drive UI, action "open with".
*/
app.openDriveItem = function(id) {
if (!app.chromeSignedIn) {
return app.openChromeSigninInfo();
}
Polymer.RenderStatus.afterNextRender(app, function() {
app.fire('navigate', {
base: 'drive'
});
var picker = document.querySelector('google-drive-browser');
picker._isOpened = true;
picker._downloadFile(id);
});
};
/**
* Handles opening a file from Google Drive.
* Normalizes content to the import object and opens import panel
* if the data is import data or a request if import data contains single
* request.
*/
app._openDriveRequest = function(e) {
var opts = {
diveId: e.detail.diveId
};
processIcomingData(e.detail.content, opts);
};
app._chromeSignin = function(e) {
app.fire('google-signin-success', {
scope: e.target.scope,
token: e.detail.token
});
};
app._chromeSignout = function(e) {
app.fire('google-signout', {
scope: e.target.scope
});
};
window.addEventListener('google-autorize', function(e) {
var aware = app.$.signInAware;
var scope = e.detail.scope;
if (!aware.signedIn) {
app.fire('google-signout', {
scope: scope
});
return app.openChromeSigninInfo();
}
aware.scope = scope;
if (aware.needAdditionalAuth) {
aware.signIn();
} else {
app.fire('google-signin-success', {
scope: scope,
token: aware.accessToken
});
}
});
/* Navigation from main app chrome */
app.openLogs = function() {
var logViewer = document.querySelector('app-log-viewer');
if (!logViewer) {
throw new Error('Log viewer not available.');
}
logViewer.open();
};
document.body.addEventListener('logs-requested', app.openLogs);
/**
* Opens about app page
*/
app.openAbout = function() {
app.fire('navigate', {
base: 'about'
});
};
/**
* Opens an issue tracker - new issue report.
*/
app.openIssueReport = function() {
var appVersion = arc.app.utils.appVer;
var message = 'Your description here\n\n';
message += '## Expected outcome\nWhat should happen?\n\n';
message += '## Actual outcome\nWhat happened?\n\n';
message += `## Versions\nApp: ${appVersion}\n`;
message += `Platform: ${navigator.appVersion}\n\n`;
message += '## Steps to reproduce\n1. \n2. \n3. ';
message = encodeURIComponent(message);
window.open('https://github.com/jarrodek/ChromeRestClient/issues/new?body=' + message);
};
/**
* Opens the import / export panel
*/
app.openImportExport = function() {
app.fire('navigate', {
base: 'dataimport'
});
};
/**
* Opens the settings panel.
*/
app.openSettings = function() {
app.fire('navigate', {
base: 'settings'
});
};
app.openLicense = function() {
var dialog = document.querySelector('arc-license-dialog');
dialog.opened = true;
};
document.body.addEventListener('display-license', app.openLicense);
app._unreadMessagesChanged = function(e) {
var state = !!(e.detail.value && e.detail.value.length > 0);
app.set('newMessages', state);
};
app._openInfoCenter = function() {
var service = document.querySelector('arc-messages-service');
service.readMessages();
app.fire('navigate', {
base: 'messages'
});
window.setTimeout(function() {
service.unread.forEach((item, i) => {
service.set(['unread', i, 'read'], 1);
});
}, 5000);
};
app._closeInfoCenter = function() {
app.fire('navigate', {
base: 'default'
});
};
app._openTerminationMessage = function() {
window
.open('https://install.advancedrestclient.com/');
};
var clipboard = {
write: function(data) {
chrome.permissions.contains({
permissions: ['clipboardWrite']
}, (status) => {
if (status) {
clipboard._write(data);
} else {
chrome.permissions.request({
permissions: ['clipboardWrite']
}, (granted) => {
if (granted) {
clipboard._write(data);
}
});
}
});
},
_write: function(data) {
var clipboardholder = document.createElement('textarea');
document.body.appendChild(clipboardholder);
clipboardholder.value = data;
clipboardholder.select();
document.execCommand('copy');
clipboardholder.parentNode.removeChild(clipboardholder);
}
};
window.addEventListener('content-copy', function(e) {
clipboard.write(e.detail.value);
e.preventDefault();
});
// variables editor drawer
window.addEventListener('open-variables-editor', function() {
app.$.environmentsDrawer.opened = true;
});
})(document, window);