Skip to content

Instantly share code, notes, and snippets.

@felipap
Last active August 29, 2015 14:15
Show Gist options
  • Save felipap/abc9b9a23aa4f57f1e3d to your computer and use it in GitHub Desktop.
Save felipap/abc9b9a23aa4f57f1e3d to your computer and use it in GitHub Desktop.
Plugins! :)
$.fn.sshare = function (options) {
// Prevent binding multiple times.
if (this.find('.sn-share-btns').length)
return;
var defOptions = {
trigger: 'hover',
duration: 'fast',
text: undefined,
url: 'http://google.com',
};
// Get options from default < element datset < arguments.
var options = $.extend($.extend(defOptions, this.data()), options);
var funcs = {
twitter: function (e) {
if (options.text || options.url)
var url = 'http://twitter.com/share?'+(options.text && '&text='+encodeURIComponent(options.text))||('url='+encodeURIComponent(options.url));
else throw "No url or text specified";
window.open(url,'','width=500,height=350,toolbar=0,menubar=0,location=0','modal=yes');
},
facebook: function (e) {
if (options.url)
var url = 'http://www.facebook.com/sharer.php?u='+encodeURIComponent(options.url);
else throw "No url or text specified";
window.open(url,'','width=500,height=350,toolbar=0,menubar=0,location=0','modal=yes');
},
gplus: function (e) {
if (options.url)
var url = 'https://plusone.google.com/_/+1/confirm?hl=pt&url='+encodeURIComponent(options.url);
else throw "No url or text specified";
window.open(url,'','width=500,height=350,toolbar=0,menubar=0,location=0','modal=yes');
},
};
this.addClass('sn-share');
var html = $('<div class="sn-share-btns"><div class="btn-group"><button class="btn btn-xs btn-info btn-twitter"><i class="fa fa-twitter"></i></button><button class="btn btn-xs btn-info btn-facebook">&nbsp;<i class="fa fa-facebook"></i>&nbsp;</button><button class="btn btn-xs btn-info btn-gplus"><i class="fa fa-google-plus"></i></button></div><div class="arrow"></div></div>');
html.find('.btn-twitter').click(funcs.twitter);
html.find('.btn-facebook').click(funcs.facebook);
html.find('.btn-gplus').click(funcs.gplus);
html.appendTo(this);
this.click(function(evt){
evt.stopPropagation();
evt.preventDefault();
return false;
})
if (options.now === true) {
html.fadeIn();
this.on('click '+(options.trigger === 'hover'?'mouseenter':''), function (e) {
html.fadeIn(options.duration);
});
} else {
this.on('click '+(options.trigger === 'hover'?'mouseenter':''), function (e) {
html.fadeIn(options.duration);
});
}
this.on('mouseleave', function (e) {
html.fadeOut(options.duration);
});
};
$.fn.xdialog = function (options) {
if (!this[0]) return;
var anchor = this[0].hash || '#'+this[0].dataset.hash,
$dialogEl = $(anchor),
hideUrl = !!this.data('hide-url');
this.click(function (evt) {
evt.preventDefault();
$dialogEl.addClass('active');
if (!hideUrl)
history.pushState({}, '', anchor)
});
$dialogEl.find('[data-action=close-dialog]').click(function (evt) {
evt.preventDefault();
console.log('clicked');
$dialogEl.removeClass('active');
if (!hideUrl)
location.hash = '';
return false;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment