Skip to content

Instantly share code, notes, and snippets.

@jcowher
Last active August 27, 2019 18:35
Show Gist options
  • Save jcowher/db67dfcdbaf7737acefa541fa9bae0b4 to your computer and use it in GitHub Desktop.
Save jcowher/db67dfcdbaf7737acefa541fa9bae0b4 to your computer and use it in GitHub Desktop.
<ul class="menu share-tools"
data-url="{{ url('entity.node.canonical', {'node': node.id}) }}"
data-title="{{ node.title.value }}">
<li class="share-tools__item"><a class="share-tools__button share-tools__button--twitter" target="_blank" href="#twitter" rel="nofollow noopener"></a></li>
<li class="share-tools__item"><a class="share-tools__button share-tools__button--facebook" target="_blank" href="#facebook" rel="nofollow noopener"></a></li>
<li class="share-tools__item"><a class="share-tools__button share-tools__button--email" target="_blank" href="#email" rel="nofollow noopener"></a></li>
</ul>
$('.share-tools', context).on('click', '.share-tools__button', function (e) {
e.preventDefault();
const $this = $(this);
const $shareTools = $this.closest('.share-tools');
const $window = $(window);
const url = $shareTools.attr('data-url');
const title = $shareTools.attr('data-title');
const type = $this.attr('href').replace('#','');
const width = 600;
const height = 400;
const left = ($window.width() - width) / 2;
const top = ($window.height() - height) / 2;
const opts = 'status=1' + ',width=' + width + ',height=' + height + ',top=' + top + ',left=' + left;
if (url && title && type) {
switch (type) {
case 'facebook':
window.open('https://www.facebook.com/sharer.php?u='+encodeURIComponent(url),'sharer',opts);
break;
case 'twitter':
window.open('https://twitter.com/share?url='+url+'&text='+encodeURIComponent(title),'twitter',opts);
break;
case 'email':
window.open('mailto:?subject='+encodeURIComponent(title)+'&body='+encodeURIComponent(url), '_blank');
break;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment