2014-06-20 00:43:54 +08:00
|
|
|
export default Discourse.View.extend({
|
2013-02-23 04:41:12 +08:00
|
|
|
templateName: 'share',
|
|
|
|
elementId: 'share-link',
|
|
|
|
classNameBindings: ['hasLink'],
|
|
|
|
|
2013-06-08 00:13:46 +08:00
|
|
|
title: function() {
|
2013-07-09 07:32:16 +08:00
|
|
|
if (this.get('controller.type') === 'topic') return I18n.t('share.topic');
|
2013-09-12 09:47:41 +08:00
|
|
|
var postNumber = this.get('controller.postNumber');
|
|
|
|
if (postNumber) {
|
|
|
|
return I18n.t('share.post', {postNumber: this.get('controller.postNumber')});
|
|
|
|
} else {
|
|
|
|
return I18n.t('share.topic');
|
|
|
|
}
|
2013-09-07 05:34:31 +08:00
|
|
|
}.property('controller.type', 'controller.postNumber'),
|
2013-02-23 04:41:12 +08:00
|
|
|
|
2013-06-08 00:13:46 +08:00
|
|
|
hasLink: function() {
|
2013-02-23 04:41:12 +08:00
|
|
|
if (this.present('controller.link')) return 'visible';
|
|
|
|
return null;
|
2013-06-08 00:13:46 +08:00
|
|
|
}.property('controller.link'),
|
2013-02-23 04:41:12 +08:00
|
|
|
|
2013-06-08 00:13:46 +08:00
|
|
|
linkChanged: function() {
|
2014-12-11 12:46:08 +08:00
|
|
|
var self=this;
|
2013-02-23 04:41:12 +08:00
|
|
|
if (this.present('controller.link')) {
|
2014-12-11 12:46:08 +08:00
|
|
|
Em.run.next(function(){
|
|
|
|
if (!self.capabilities.touch) {
|
|
|
|
var $linkInput = $('#share-link input');
|
|
|
|
$linkInput.val(self.get('controller.link'));
|
2014-12-06 05:21:07 +08:00
|
|
|
|
2014-12-11 12:46:08 +08:00
|
|
|
// 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'));
|
|
|
|
}
|
|
|
|
});
|
2013-02-23 04:41:12 +08:00
|
|
|
}
|
2013-06-08 00:13:46 +08:00
|
|
|
}.observes('controller.link'),
|
2013-02-23 04:41:12 +08:00
|
|
|
|
|
|
|
didInsertElement: function() {
|
2014-03-19 09:27:15 +08:00
|
|
|
var shareView = this,
|
|
|
|
$html = $('html');
|
2014-06-25 22:20:06 +08:00
|
|
|
|
2014-03-19 09:27:15 +08:00
|
|
|
$html.on('mousedown.outside-share-link', function(e) {
|
2013-03-28 07:43:37 +08:00
|
|
|
// 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.
|
2013-06-08 00:13:46 +08:00
|
|
|
if (shareView.$().has(e.target).length !== 0) { return; }
|
2013-08-26 14:28:42 +08:00
|
|
|
|
2013-09-17 02:08:55 +08:00
|
|
|
shareView.get('controller').send('close');
|
2013-02-23 04:41:12 +08:00
|
|
|
return true;
|
|
|
|
});
|
2013-06-08 00:13:46 +08:00
|
|
|
|
2014-03-19 09:27:15 +08:00
|
|
|
$html.on('click.discoure-share-link', '[data-share-url]', function(e) {
|
2014-06-23 23:11:37 +08:00
|
|
|
// if they want to open in a new tab, let it so
|
|
|
|
if (e.shiftKey || e.metaKey || e.ctrlKey || e.which === 2) { return true; }
|
|
|
|
|
2013-02-23 04:41:12 +08:00
|
|
|
e.preventDefault();
|
2014-06-25 22:20:06 +08:00
|
|
|
|
2014-03-19 09:27:15 +08:00
|
|
|
var $currentTarget = $(e.currentTarget),
|
2014-04-09 22:01:09 +08:00
|
|
|
$currentTargetOffset = $currentTarget.offset(),
|
2014-06-25 22:20:06 +08:00
|
|
|
$shareLink = $('#share-link'),
|
|
|
|
url = $currentTarget.data('share-url'),
|
|
|
|
postNumber = $currentTarget.data('post-number'),
|
|
|
|
date = $currentTarget.children().data('time');
|
2013-02-23 04:41:12 +08:00
|
|
|
|
2014-06-10 03:14:03 +08:00
|
|
|
// Relative urls
|
2013-02-23 04:41:12 +08:00
|
|
|
if (url.indexOf("/") === 0) {
|
|
|
|
url = window.location.protocol + "//" + window.location.host + url;
|
2013-02-21 02:15:50 +08:00
|
|
|
}
|
2013-08-21 06:03:57 +08:00
|
|
|
|
2014-03-19 09:27:15 +08:00
|
|
|
var shareLinkWidth = $shareLink.width();
|
2014-04-09 22:01:09 +08:00
|
|
|
var x = $currentTargetOffset.left - (shareLinkWidth / 2);
|
2013-08-21 06:03:57 +08:00
|
|
|
if (x < 25) {
|
|
|
|
x = 25;
|
|
|
|
}
|
2013-10-12 12:38:57 +08:00
|
|
|
if (x + shareLinkWidth > $(window).width()) {
|
|
|
|
x -= shareLinkWidth / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
var header = $('.d-header');
|
2014-04-09 22:01:09 +08:00
|
|
|
var y = $currentTargetOffset.top - ($shareLink.height() + 20);
|
2013-10-12 12:38:57 +08:00
|
|
|
if (y < header.offset().top + header.height()) {
|
2014-04-09 22:01:09 +08:00
|
|
|
y = $currentTargetOffset.top + 10;
|
2013-10-12 12:38:57 +08:00
|
|
|
}
|
2013-08-21 06:03:57 +08:00
|
|
|
|
2014-12-06 05:21:07 +08:00
|
|
|
$shareLink.css({top: "" + y + "px"});
|
|
|
|
|
|
|
|
if (!Discourse.Mobile.mobileView) {
|
|
|
|
$shareLink.css({left: "" + x + "px"});
|
|
|
|
}
|
2014-06-10 03:14:03 +08:00
|
|
|
|
2013-08-21 06:03:57 +08:00
|
|
|
shareView.set('controller.link', url);
|
2013-09-07 05:34:31 +08:00
|
|
|
shareView.set('controller.postNumber', postNumber);
|
2014-06-10 03:14:03 +08:00
|
|
|
shareView.set('controller.date', date);
|
|
|
|
|
2013-02-23 04:41:12 +08:00
|
|
|
return false;
|
|
|
|
});
|
2013-06-08 00:13:46 +08:00
|
|
|
|
2014-03-19 09:27:15 +08:00
|
|
|
$html.on('keydown.share-view', function(e){
|
2013-03-28 04:28:53 +08:00
|
|
|
if (e.keyCode === 27) {
|
2013-09-17 02:08:55 +08:00
|
|
|
shareView.get('controller').send('close');
|
2013-03-28 04:28:53 +08:00
|
|
|
}
|
|
|
|
});
|
2013-02-23 04:41:12 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement: function() {
|
2014-11-19 06:29:46 +08:00
|
|
|
this.get('controller').send('close');
|
|
|
|
|
2014-06-25 22:20:06 +08:00
|
|
|
$('html').off('click.discoure-share-link')
|
|
|
|
.off('mousedown.outside-share-link')
|
|
|
|
.off('keydown.share-view');
|
2013-02-23 04:41:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|