148 lines
5.2 KiB
JavaScript
148 lines
5.2 KiB
JavaScript
/**
|
|
* jQuery prettySocial: Use custom social share buttons
|
|
* Author: Sonny T. <hi@sonnyt.com>, sonnyt.com
|
|
*/
|
|
|
|
(function ($) {
|
|
'use strict';
|
|
|
|
$.fn.prettySocial = function () {
|
|
/**
|
|
* Supported social sites
|
|
* @type {Object}
|
|
*/
|
|
var _sites = {
|
|
pinterest: {
|
|
url: 'http://pinterest.com/pin/create/button/?url={{url}}&media={{media}}&description={{description}}',
|
|
popup: {
|
|
width: 685,
|
|
height: 500
|
|
}
|
|
},
|
|
facebook: {
|
|
url: 'https://www.facebook.com/sharer/sharer.php?s=100&p[title]={{title}}&p[summary]={{description}}&p[url]={{url}}&p[images][0]={{media}}',
|
|
popup: {
|
|
width: 626,
|
|
height: 436
|
|
}
|
|
},
|
|
twitter: {
|
|
url: 'https://twitter.com/share?url={{url}}&via={{via}}&text={{description}}',
|
|
popup: {
|
|
width: 685,
|
|
height: 500
|
|
}
|
|
},
|
|
googleplus: {
|
|
url: 'https://plus.google.com/share?url={{url}}',
|
|
popup: {
|
|
width: 600,
|
|
height: 600
|
|
}
|
|
},
|
|
linkedin: {
|
|
url: 'https://www.linkedin.com/shareArticle?mini=true&url={{url}}&title={{title}}&summary={{description}}+&source={{via}}',
|
|
popup: {
|
|
width: 600,
|
|
height: 600
|
|
}
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Pop-up window
|
|
* This method is only used on desktop browsers
|
|
* @param {Object} site Selected social site
|
|
* @param {String} url Fixed URL to open
|
|
*/
|
|
_popup = function (site, url) {
|
|
// center window
|
|
var left = (window.innerWidth/2) - (site.popup.width/2),
|
|
top = (window.innerHeight/2) - (site.popup.height/2);
|
|
|
|
return window.open(url, '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + site.popup.width + ', height=' + site.popup.height + ', top=' + top + ', left=' + left);
|
|
},
|
|
|
|
/**
|
|
* Prepare link based on social sites
|
|
* @param {Object} site Selected social site
|
|
* @param {Object} link Link with variables
|
|
* @return {String} Polished link based on the social site template
|
|
*/
|
|
_linkFix = function (site, link) {
|
|
// replace template url
|
|
var url = site.url.replace(/{{url}}/g, encodeURIComponent(link.url))
|
|
.replace(/{{title}}/g, encodeURIComponent(link.title))
|
|
.replace(/{{description}}/g, encodeURIComponent(link.description))
|
|
.replace(/{{media}}/g, encodeURIComponent(link.media))
|
|
.replace(/{{via}}/g, encodeURIComponent(link.via));
|
|
|
|
return url;
|
|
};
|
|
|
|
return this.each(function() {
|
|
|
|
// declare $(this) as variable
|
|
var $this = $(this);
|
|
|
|
// link type
|
|
var type = $this.data('type'),
|
|
|
|
// set site
|
|
site = _sites[type] || null;
|
|
|
|
// check if social site is selected
|
|
if (!site) {
|
|
$.error('Social site is not set.');
|
|
}
|
|
|
|
// gather link info
|
|
var link = {
|
|
url: $this.data('url') || '',
|
|
title: $this.data('title') || '',
|
|
description: $this.data('description') || '',
|
|
media: $this.data('media') || '',
|
|
via: $this.data('via') || ''
|
|
};
|
|
|
|
// prepare link
|
|
var url = _linkFix(site, link);
|
|
|
|
// if not, set click trigger
|
|
if (navigator.userAgent.match(/Android|IEMobile|BlackBerry|iPhone|iPad|iPod|Opera Mini/i)) {
|
|
$this
|
|
.bind('touchstart', function (e) {
|
|
if(e.originalEvent.touches.length > 1) {
|
|
return;
|
|
}
|
|
|
|
$this.data('touchWithoutScroll', true);
|
|
})
|
|
.bind('touchmove', function () {
|
|
$this.data('touchWithoutScroll', false);
|
|
|
|
return;
|
|
}).bind('touchend', function (e) {
|
|
e.preventDefault();
|
|
|
|
var touchWithoutScroll = $this.data('touchWithoutScroll');
|
|
|
|
if (e.originalEvent.touches.length > 1 || !touchWithoutScroll) {
|
|
return;
|
|
}
|
|
|
|
// call popup window
|
|
_popup(site, url);
|
|
});
|
|
} else {
|
|
$this.bind('click', function (e) {
|
|
e.preventDefault();
|
|
|
|
// call popup window
|
|
_popup(site, url);
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
})(jQuery);
|