/
share.js.es6
125 lines (102 loc) · 3.92 KB
/
share.js.es6
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
export default Ember.View.extend({
templateName: 'share',
elementId: 'share-link',
classNameBindings: ['hasLink'],
title: function() {
if (this.get('controller.type') === 'topic') return I18n.t('share.topic');
var postNumber = this.get('controller.postNumber');
if (postNumber) {
return I18n.t('share.post', {postNumber: this.get('controller.postNumber')});
} else {
return I18n.t('share.topic');
}
}.property('controller.type', 'controller.postNumber'),
hasLink: function() {
if (!Ember.isEmpty(this.get('controller.link'))) return 'visible';
return null;
}.property('controller.link'),
linkChanged: function() {
const self = this;
if (!Ember.isEmpty(this.get('controller.link'))) {
Em.run.next(function() {
if (!self.capabilities.touch) {
var $linkInput = $('#share-link input');
$linkInput.val(self.get('controller.link'));
// Wait for the fade-in transition to finish before selecting the link:
window.setTimeout(function() {
$linkInput.select().focus();
}, 160);
} else {
var $linkForTouch = $('#share-link .share-for-touch a');
$linkForTouch.attr('href',self.get('controller.link'));
$linkForTouch.html(self.get('controller.link'));
var range = window.document.createRange();
range.selectNode($linkForTouch[0]);
window.getSelection().addRange(range);
}
});
}
}.observes('controller.link'),
didInsertElement: function() {
var self = this,
$html = $('html');
$html.on('mousedown.outside-share-link', function(e) {
// Use mousedown instead of click so this event is handled before routing occurs when a
// link is clicked (which is a click event) while the share dialog is showing.
if (self.$().has(e.target).length !== 0) { return; }
self.get('controller').send('close');
return true;
});
function showPanel($target, url, postNumber, date) {
const $currentTargetOffset = $target.offset();
const $shareLink = $('#share-link');
// Relative urls
if (url.indexOf("/") === 0) {
url = window.location.protocol + "//" + window.location.host + url;
}
const shareLinkWidth = $shareLink.width();
let x = $currentTargetOffset.left - (shareLinkWidth / 2);
if (x < 25) {
x = 25;
}
if (x + shareLinkWidth > $(window).width()) {
x -= shareLinkWidth / 2;
}
const header = $('.d-header');
let y = $currentTargetOffset.top - ($shareLink.height() + 20);
if (y < header.offset().top + header.height()) {
y = $currentTargetOffset.top + 10;
}
$shareLink.css({top: "" + y + "px"});
if (!self.site.mobileView) {
$shareLink.css({left: "" + x + "px"});
}
self.set('controller.link', url);
self.set('controller.postNumber', postNumber);
self.set('controller.date', date);
}
this.appEvents.on('share:url', (url, $target) => showPanel($target, url));
$html.on('click.discoure-share-link', '[data-share-url]', function(e) {
// if they want to open in a new tab, let it so
if (e.shiftKey || e.metaKey || e.ctrlKey || e.which === 2) { return true; }
e.preventDefault();
const $currentTarget = $(e.currentTarget),
url = $currentTarget.data('share-url'),
postNumber = $currentTarget.data('post-number'),
date = $currentTarget.children().data('time');
showPanel($currentTarget, url, postNumber, date);
return false;
});
$html.on('keydown.share-view', function(e){
if (e.keyCode === 27) {
self.get('controller').send('close');
}
});
},
willDestroyElement: function() {
this.get('controller').send('close');
$('html').off('click.discoure-share-link')
.off('mousedown.outside-share-link')
.off('keydown.share-view');
}
});