/**
* Share.js
*
* @author overtrue
* @license MIT
*
* @example
*
* $('.share-components').share();
*
* // or
*
* $('.share-bar').share({
* sites: ['qzone', 'qq', 'weibo','wechat'],
* // ...
* });
*
*/
;(function($){
/**
* Initialize a share bar.
*
* @param {Object} $options globals (optional).
*
* @return {Void}
*/
$.fn.share = function ($options) {
var $head = $(document.head);
var $defaults = {
url: location.href,
site_url: location.origin,
source: $head.find('[name=site], [name=Site]').attr('content') || document.title,
title: $head.find('[name=title], [name=Title]').attr('content') || document.title,
description: $head.find('[name=description], [name=Description]').attr('content') || '',
image: $('img:first').prop('src') || '',
imageSelector: undefined,
weiboKey: '',
wechatQrcodeTitle: '',
wechatQrcodeHelper: '
Scan the WeChat QR code to share
', wechatQrcodeSize: 100, mobileSites: [], sites: ['weibo','qq','wechat','tencent','douban','qzone','linkedin','diandian','facebook','twitter','google'], disabled: [], initialized: false }; var $globals = $.extend({}, $defaults, $options); var $templates = { qzone : 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url={{URL}}&title={{TITLE}}&desc={{DESCRIPTION}}&summary={{SUMMARY}}&site={{SOURCE}}&pics={{IMAGE}}', qq : 'http://connect.qq.com/widget/shareqq/index.html?url={{URL}}&title={{TITLE}}&source={{SOURCE}}&desc={{DESCRIPTION}}&pics={{IMAGE}}', tencent : 'http://share.v.t.qq.com/index.php?c=share&a=index&title={{TITLE}}&url={{URL}}&pic={{IMAGE}}', weibo : 'https://service.weibo.com/share/share.php?url={{URL}}&title={{TITLE}}&pic={{IMAGE}}&appkey={{WEIBOKEY}}', wechat : 'javascript:;', douban : 'http://shuo.douban.com/!service/share?href={{URL}}&name={{TITLE}}&text={{DESCRIPTION}}&image={{IMAGE}}&starid=0&aid=0&style=11', diandian : 'http://www.diandian.com/share?lo={{URL}}&ti={{TITLE}}&type=link', linkedin : 'http://www.linkedin.com/shareArticle?mini=true&ro=true&title={{TITLE}}&url={{URL}}&summary={{SUMMARY}}&source={{SOURCE}}&armin=armin', facebook : 'https://www.facebook.com/sharer/sharer.php?u={{URL}}&title={{TITLE}}&description={{DESCRIPTION}}&caption={{SUBHEAD}}&link={{URL}}&picture={{IMAGE}}', twitter : 'https://twitter.com/intent/tweet?text={{TITLE}}&url={{URL}}&via={{SITE_URL}}', google : 'https://plus.google.com/share?url={{URL}}' }; var $ariaLabels = { qzone: "QQ空间", qq: "QQ", tencent: "腾讯微博", weibo: "微博", wechat: "微信", douban: "豆瓣", diandian: "点点", linkedin: "LinkedIn", facebook: "Facebook", twitter: "Twitter", google: "Google" }; this.each(function() { if ($(this).data('initialized')) { return true; } var $data = $.extend({}, $globals, $(this).data()); if ($data.imageSelector) { $data.image = $($data.imageSelector).map(function() { return $(this).prop('src'); }).get().join('||'); } var $container = $(this).addClass('share-component social-share'); createIcons($container, $data); createWechat($container, $data); $(this).data('initialized', true); }); /** * Create site icons * * @param {Object|String} $container * @param {Object} $data */ function createIcons ($container, $data) { var $sites = getSites($data); $data.mode == 'prepend' ? $sites.reverse() : $sites if (!$sites.length) {return;} $.each($sites, function (i, $name) { var $url = makeUrl($name, $data); var $link = $data.initialized ? $container.find('.icon-'+$name) : $(''); if (!$link.length) { return true; } $link.prop('aria-label', "分享到 "+$ariaLabels[$name]); $link.prop('href', $url); if ($name === 'wechat') { $link.prop('tabindex', -1); } else { $link.prop('target', '_blank'); } if (!$data.initialized) { $data.mode == 'prepend' ? $container.prepend($link) : $container.append($link); } }); } /** * Create the wechat icon and QRCode. * * @param {Object|String} $container * @param {Object} $data */ function createWechat ($container, $data) { var $wechat = $container.find('a.icon-wechat'); if (!$wechat.length) {return;} $wechat.append('