Skip to content

Commit 93403b0

Browse files
committed
Replace the share popup with a component
1 parent 2a25136 commit 93403b0

File tree

7 files changed

+200
-199
lines changed

7 files changed

+200
-199
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import { wantsNewWindow } from 'discourse/lib/intercept-click';
2+
import { longDateNoYear } from 'discourse/lib/formatter';
3+
import computed from 'ember-addons/ember-computed-decorators';
4+
import Sharing from 'discourse/lib/sharing';
5+
6+
export default Ember.Component.extend({
7+
elementId: 'share-link',
8+
classNameBindings: ['visible'],
9+
link: null,
10+
visible: null,
11+
12+
@computed
13+
sources() {
14+
return Sharing.activeSources(this.siteSettings.share_links);
15+
},
16+
17+
@computed('type', 'postNumber')
18+
shareTitle(type, postNumber) {
19+
if (type === 'topic') { return I18n.t('share.topic'); }
20+
if (postNumber) {
21+
return I18n.t('share.post', { postNumber });
22+
}
23+
return I18n.t('share.topic');
24+
},
25+
26+
@computed('date')
27+
displayDate(date) {
28+
return longDateNoYear(new Date(date));
29+
},
30+
31+
_focusUrl() {
32+
const link = this.get('link');
33+
if (!this.capabilities.touch) {
34+
const $linkInput = $('#share-link input');
35+
$linkInput.val(link);
36+
37+
// Wait for the fade-in transition to finish before selecting the link:
38+
window.setTimeout(() => $linkInput.select().focus(), 160);
39+
} else {
40+
const $linkForTouch = $('#share-link .share-for-touch a');
41+
$linkForTouch.attr('href', link);
42+
$linkForTouch.html(link);
43+
const range = window.document.createRange();
44+
range.selectNode($linkForTouch[0]);
45+
window.getSelection().addRange(range);
46+
}
47+
},
48+
49+
_showUrl($target, url) {
50+
const $currentTargetOffset = $target.offset();
51+
const $this = this.$();
52+
53+
if (Ember.isEmpty(url)) { return; }
54+
55+
// Relative urls
56+
if (url.indexOf("/") === 0) {
57+
url = window.location.protocol + "//" + window.location.host + url;
58+
}
59+
60+
const shareLinkWidth = $this.width();
61+
let x = $currentTargetOffset.left - (shareLinkWidth / 2);
62+
if (x < 25) { x = 25; }
63+
if (x + shareLinkWidth > $(window).width()) {
64+
x -= shareLinkWidth / 2;
65+
}
66+
67+
const header = $('.d-header');
68+
let y = $currentTargetOffset.top - ($this.height() + 20);
69+
if (y < header.offset().top + header.height()) {
70+
y = $currentTargetOffset.top + 10;
71+
}
72+
73+
$this.css({top: "" + y + "px"});
74+
75+
if (!this.site.mobileView) {
76+
$this.css({left: "" + x + "px"});
77+
}
78+
this.set('link', url);
79+
this.set('visible', true);
80+
81+
Ember.run.scheduleOnce('afterRender', this, this._focusUrl);
82+
},
83+
84+
didInsertElement() {
85+
this._super();
86+
87+
const $html = $('html');
88+
$html.on('mousedown.outside-share-link', e => {
89+
// Use mousedown instead of click so this event is handled before routing occurs when a
90+
// link is clicked (which is a click event) while the share dialog is showing.
91+
if (this.$().has(e.target).length !== 0) { return; }
92+
this.send('close');
93+
return true;
94+
});
95+
96+
$html.on('click.discoure-share-link', '[data-share-url]', e => {
97+
// if they want to open in a new tab, let it so
98+
if (wantsNewWindow(e)) { return true; }
99+
100+
e.preventDefault();
101+
102+
const $currentTarget = $(e.currentTarget);
103+
const url = $currentTarget.data('share-url');
104+
const postNumber = $currentTarget.data('post-number');
105+
const postId = $currentTarget.closest('article').data('post-id');
106+
const date = $currentTarget.children().data('time');
107+
108+
this.setProperties({ postNumber, date, postId });
109+
this._showUrl($currentTarget, url);
110+
return false;
111+
});
112+
113+
$html.on('keydown.share-view', e => {
114+
if (e.keyCode === 27) {
115+
this.send('close');
116+
}
117+
});
118+
119+
this.appEvents.on('share:url', (url, $target) => this._showUrl($target, url));
120+
},
121+
122+
willDestroyElement() {
123+
this._super();
124+
$('html').off('click.discoure-share-link')
125+
.off('mousedown.outside-share-link')
126+
.off('keydown.share-view');
127+
},
128+
129+
actions: {
130+
replyAsNewTopic() {
131+
const postStream = this.get("topic.postStream");
132+
const postId = this.get("postId") || postStream.findPostIdForPostNumber(1);
133+
const post = postStream.findLoadedPost(postId);
134+
this.sendAction('replyAsNewTopic', post);
135+
this.send("close");
136+
},
137+
138+
close() {
139+
this.setProperties({
140+
link: null,
141+
postNumber: null,
142+
postId: null,
143+
visible: false
144+
});
145+
},
146+
147+
share(source) {
148+
var url = source.generateUrl(this.get('link'), this.get('title'));
149+
if (source.shouldOpenInPopup) {
150+
window.open(url, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,width=600,height=' + (source.popupHeight || 315));
151+
} else {
152+
window.open(url, '_blank');
153+
}
154+
}
155+
}
156+
157+
});

app/assets/javascripts/discourse/controllers/share.js.es6

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<h3>{{shareTitle}}</h3>
2+
3+
{{#if date}}
4+
<span class="date">{{displayDate}}</span>
5+
{{/if}}
6+
7+
<div>
8+
<input type='text'>
9+
<div class="share-for-touch"><div class="overflow-ellipsis"><a></a></div></div>
10+
</div>
11+
12+
{{#each sources as |s|}}
13+
{{share-source source=s title=model.title action="share"}}
14+
{{/each}}
15+
16+
{{#if topic.details.can_reply_as_new_topic}}
17+
<div class='reply-as-new-topic'>
18+
<a href {{action "replyAsNewTopic"}} aria-label={{i18n 'post.reply_as_new_topic'}} title={{i18n 'post.reply_as_new_topic'}}>{{fa-icon "plus"}}{{i18n 'topic.create'}}</a>
19+
</div>
20+
{{/if}}
21+
22+
<div class='link'>
23+
<a href {{action "close"}} class="close-share" aria-label={{i18n 'share.close'}} title={{i18n 'share.close'}}>{{fa-icon "close"}}</a>
24+
</div>

app/assets/javascripts/discourse/templates/share.hbs

Lines changed: 0 additions & 26 deletions
This file was deleted.

app/assets/javascripts/discourse/templates/topic.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@
221221
</div>
222222
{{/if}}
223223

224-
{{render "share"}}
224+
{{share-popup topic=model replyAsNewTopic="replyAsNewTopic"}}
225225

226226
{{#if currentUser.enable_quoting}}
227227
{{render "quote-button"}}

app/assets/javascripts/discourse/views/share.js.es6

Lines changed: 0 additions & 116 deletions
This file was deleted.

0 commit comments

Comments
 (0)