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
|
// Copyright (c) 2021, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import $ from 'jquery';
import GoldenLayout from 'golden-layout';
import _ from 'underscore';
import ClipboardJS from 'clipboard';
import {set as localStorageSet} from './local.js';
import {ga} from './analytics.js';
import * as url from './url.js';
import {options} from './options.js';
import ClickEvent = JQuery.ClickEvent;
import TriggeredEvent = JQuery.TriggeredEvent;
import {Settings, SiteSettings} from './settings.js';
import {SentryCapture} from './sentry.js';
const cloneDeep = require('lodash.clonedeep');
enum LinkType {
Short,
Full,
Embed,
}
const shareServices = {
twitter: {
embedValid: false,
logoClass: 'fab fa-twitter',
cssClass: 'share-twitter',
getLink: (title, url) => {
return (
'https://twitter.com/intent/tweet' +
`?text=${encodeURIComponent(title)}` +
`&url=${encodeURIComponent(url)}` +
'&via=CompileExplore'
);
},
text: 'Tweet',
},
reddit: {
embedValid: false,
logoClass: 'fab fa-reddit',
cssClass: 'share-reddit',
getLink: (title, url) => {
return (
'http://www.reddit.com/submit' +
`?url=${encodeURIComponent(url)}` +
`&title=${encodeURIComponent(title)}`
);
},
text: 'Share on Reddit',
},
};
export class Sharing {
private layout: GoldenLayout;
private lastState: any;
private share: JQuery;
private shareShort: JQuery;
private shareFull: JQuery;
private shareEmbed: JQuery;
private settings: SiteSettings;
private clippyButton: ClipboardJS | null;
constructor(layout: any) {
this.layout = layout;
this.lastState = null;
this.share = $('#share');
this.shareShort = $('#shareShort');
this.shareFull = $('#shareFull');
this.shareEmbed = $('#shareEmbed');
this.settings = Settings.getStoredSettings();
this.clippyButton = null;
this.initButtons();
this.initCallbacks();
}
private initCallbacks(): void {
this.layout.eventHub.on('displaySharingPopover', () => {
this.openShareModalForType(LinkType.Short);
});
this.layout.eventHub.on('copyShortLinkToClip', () => {
this.copyLinkTypeToClipboard(LinkType.Short);
});
this.layout.on('stateChanged', this.onStateChanged.bind(this));
$('#sharelinkdialog')
.on('show.bs.modal', this.onOpenModalPane.bind(this))
.on('hidden.bs.modal', this.onCloseModalPane.bind(this));
this.layout.eventHub.on('settingsChange', (newSettings: SiteSettings) => {
this.settings = newSettings;
});
$(window).on('blur', async () => {
localStorageSet('gl', JSON.stringify(this.layout.toConfig()));
if (this.settings.keepMultipleTabs) {
try {
const link = await this.getLinkOfType(LinkType.Full);
window.history.replaceState(null, '', link);
} catch (e) {
// This is probably caused by a link that is too long
SentryCapture(e, 'url update');
}
}
});
}
private onStateChanged(): void {
const config = Sharing.filterComponentState(this.layout.toConfig());
this.ensureUrlIsNotOutdated(config);
if (options.embedded) {
const strippedToLast = window.location.pathname.substr(0, window.location.pathname.lastIndexOf('/') + 1);
$('a.link').prop('href', strippedToLast + '#' + url.serialiseState(config));
}
}
private ensureUrlIsNotOutdated(config: any): void {
const stringifiedConfig = JSON.stringify(config);
if (stringifiedConfig !== this.lastState) {
if (this.lastState != null && window.location.pathname !== window.httpRoot) {
window.history.replaceState(null, '', window.httpRoot);
}
this.lastState = stringifiedConfig;
}
}
private static bindToLinkType(bind: string): LinkType {
switch (bind) {
case 'Full':
return LinkType.Full;
case 'Short':
return LinkType.Short;
case 'Embed':
return LinkType.Embed;
default:
return LinkType.Full;
}
}
private onOpenModalPane(event: TriggeredEvent<HTMLElement, undefined, HTMLElement, HTMLElement>): void {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore The property is added by bootstrap
const button = $(event.relatedTarget);
const currentBind = Sharing.bindToLinkType(button.data('bind'));
const modal = $(event.currentTarget);
const socialSharingElements = modal.find('.socialsharing');
const permalink = modal.find('.permalink');
const embedsettings = modal.find('#embedsettings');
const updatePermaLink = () => {
socialSharingElements.empty();
const config = this.layout.toConfig();
Sharing.getLinks(config, currentBind, (error: any, newUrl: string, extra: string, updateState: boolean) => {
permalink.off('click');
if (error || !newUrl) {
permalink.prop('disabled', true);
permalink.val(error || 'Error providing URL');
SentryCapture(error, 'Error providing url');
} else {
if (updateState) {
Sharing.storeCurrentConfig(config, extra);
}
permalink.val(newUrl);
permalink.on('click', () => {
permalink.trigger('focus').trigger('select');
});
if (options.sharingEnabled) {
Sharing.updateShares(socialSharingElements, newUrl);
// Disable the links for every share item which does not support embed html as links
if (currentBind === LinkType.Embed) {
socialSharingElements.children('.share-no-embeddable').hide().on('click', false);
}
}
}
});
};
const clippyElement = modal.find('button.clippy').get(0);
if (clippyElement != null) {
this.clippyButton = new ClipboardJS(clippyElement);
this.clippyButton.on('success', e => {
this.displayTooltip(permalink, 'Link copied to clipboard');
e.clearSelection();
});
this.clippyButton.on('error', e => {
this.displayTooltip(permalink, 'Error copying to clipboard');
});
}
if (currentBind === LinkType.Embed) {
embedsettings.show();
embedsettings
.find('input')
// Off any prev click handlers to avoid multiple events triggering after opening the modal more than once
.off('click')
.on('click', () => updatePermaLink());
} else {
embedsettings.hide();
}
updatePermaLink();
ga.proxy('send', {
hitType: 'event',
eventCategory: 'OpenModalPane',
eventAction: 'Sharing',
});
}
private onCloseModalPane(): void {
if (this.clippyButton) {
this.clippyButton.destroy();
this.clippyButton = null;
}
}
private initButtons(): void {
const shareShortCopyToClipBtn = this.shareShort.find('.clip-icon');
const shareFullCopyToClipBtn = this.shareFull.find('.clip-icon');
const shareEmbedCopyToClipBtn = this.shareEmbed.find('.clip-icon');
shareShortCopyToClipBtn.on('click', e => this.onClipButtonPressed(e, LinkType.Short));
shareFullCopyToClipBtn.on('click', e => this.onClipButtonPressed(e, LinkType.Full));
shareEmbedCopyToClipBtn.on('click', e => this.onClipButtonPressed(e, LinkType.Embed));
if (options.sharingEnabled) {
Sharing.updateShares($('#socialshare'), window.location.protocol + '//' + window.location.hostname);
}
}
private onClipButtonPressed(event: ClickEvent, type: LinkType): void {
// Don't let the modal show up.
// We need this because the button is a child of the dropdown-item with a data-toggle=modal
if (Sharing.isNavigatorClipboardAvailable()) {
event.stopPropagation();
this.copyLinkTypeToClipboard(type);
// As we prevented bubbling, the dropdown won't close by itself. We need to trigger it manually
this.share.dropdown('hide');
}
}
private getLinkOfType(type: LinkType): Promise<string> {
const config = this.layout.toConfig();
return new Promise<string>((resolve, reject) => {
Sharing.getLinks(config, type, (error: any, newUrl: string, extra: string, updateState: boolean) => {
if (error || !newUrl) {
this.displayTooltip(this.share, 'Oops, something went wrong');
SentryCapture(error, 'Getting short link failed');
reject();
} else {
if (updateState) {
Sharing.storeCurrentConfig(config, extra);
}
resolve(newUrl);
}
});
});
}
private copyLinkTypeToClipboard(type: LinkType): void {
const config = this.layout.toConfig();
Sharing.getLinks(config, type, (error: any, newUrl: string, extra: string, updateState: boolean) => {
if (error || !newUrl) {
this.displayTooltip(this.share, 'Oops, something went wrong');
SentryCapture(error, 'Getting short link failed');
} else {
if (updateState) {
Sharing.storeCurrentConfig(config, extra);
}
this.doLinkCopyToClipboard(type, newUrl);
}
});
}
private displayTooltip(where: JQuery, message: string): void {
where.tooltip('dispose');
where.tooltip({
placement: 'bottom',
trigger: 'manual',
title: message,
});
where.tooltip('show');
// Manual triggering of tooltips does not hide them automatically. This timeout ensures they do
setTimeout(() => where.tooltip('hide'), 1500);
}
private openShareModalForType(type: LinkType): void {
switch (type) {
case LinkType.Short:
this.shareShort.trigger('click');
break;
case LinkType.Full:
this.shareFull.trigger('click');
break;
case LinkType.Embed:
this.shareEmbed.trigger('click');
break;
}
}
private doLinkCopyToClipboard(type: LinkType, link: string): void {
if (Sharing.isNavigatorClipboardAvailable()) {
navigator.clipboard
.writeText(link)
.then(() => this.displayTooltip(this.share, 'Link copied to clipboard'))
.catch(() => this.openShareModalForType(type));
} else {
this.openShareModalForType(type);
}
}
public static getLinks(config: any, currentBind: LinkType, done: CallableFunction): void {
const root = window.httpRoot;
ga.proxy('send', {
hitType: 'event',
eventCategory: 'CreateShareLink',
eventAction: 'Sharing',
});
switch (currentBind) {
case LinkType.Short:
Sharing.getShortLink(config, root, done);
return;
case LinkType.Full:
done(null, window.location.origin + root + '#' + url.serialiseState(config), false);
return;
case LinkType.Embed: {
const options = {};
$('#sharelinkdialog input:checked').each((i, element) => {
options[$(element).prop('class')] = true;
});
done(null, Sharing.getEmbeddedHtml(config, root, false, options), false);
return;
}
default:
// Hmmm
done('Unknown link type', null);
}
}
private static getShortLink(config: any, root: string, done: CallableFunction): void {
const useExternalShortener = options.urlShortenService !== 'default';
const data = JSON.stringify({
config: useExternalShortener ? url.serialiseState(config) : config,
});
$.ajax({
type: 'POST',
url: window.location.origin + root + 'api/shortener',
dataType: 'json', // Expected
contentType: 'application/json', // Sent
data: data,
success: (result: any) => {
const pushState = useExternalShortener ? null : result.url;
done(null, result.url, pushState, true);
},
error: err => {
// Notify the user that we ran into trouble?
done(err.statusText, null, false);
},
cache: true,
});
}
private static getEmbeddedHtml(config, root, isReadOnly, extraOptions): string {
const embedUrl = Sharing.getEmbeddedUrl(config, root, isReadOnly, extraOptions);
// The attributes must be double quoted, the full url's rison contains single quotes
return `<iframe width="800px" height="200px" src="${embedUrl}"></iframe>`;
}
private static getEmbeddedUrl(config: any, root: string, readOnly: boolean, extraOptions: object): string {
const location = window.location.origin + root;
const parameters = _.reduce(
extraOptions,
(total, value, key): string => {
if (total === '') {
total = '?';
} else {
total += '&';
}
return total + key + '=' + value;
},
'',
);
const path = (readOnly ? 'embed-ro' : 'e') + parameters + '#';
return location + path + url.serialiseState(config);
}
private static storeCurrentConfig(config: any, extra: string): void {
window.history.pushState(null, '', extra);
}
private static isNavigatorClipboardAvailable(): boolean {
return (navigator.clipboard as Clipboard | undefined) !== undefined;
}
public static filterComponentState(config: any, keysToRemove: [string] = ['selection']): any {
function filterComponentStateImpl(component: any) {
if (component.content) {
for (let i = 0; i < component.content.length; i++) {
filterComponentStateImpl(component.content[i]);
}
}
if (component.componentState) {
Object.keys(component.componentState)
.filter(e => keysToRemove.includes(e))
.forEach(key => delete component.componentState[key]);
}
}
config = cloneDeep(config);
filterComponentStateImpl(config);
return config;
}
private static updateShares(container: JQuery, url: string): void {
const baseTemplate = $('#share-item');
_.each(shareServices, (service, serviceName) => {
const newElement = baseTemplate.children('a.share-item').clone();
if (service.logoClass) {
newElement.prepend(
$('<span>').addClass('dropdown-icon mr-1').addClass(service.logoClass).prop('title', serviceName),
);
}
if (service.text) {
newElement.children('span.share-item-text').text(service.text);
}
newElement
.prop('href', service.getLink('Compiler Explorer', url))
.addClass(service.cssClass)
.toggleClass('share-no-embeddable', !service.embedValid)
.appendTo(container);
});
}
}
|