Instantly share code, notes, and snippets.
Created
April 1, 2018 09:54
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save kusyka911/352a619611c6786b0fd86877e01dc858 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function social_window(url, width, height, closeCallback) { | |
var modalDiv, | |
dialogPrefix = window.showModalDialog ? 'dialog' : '', | |
unit = 'px', | |
maximized = width === true || height === true, | |
w = width || 600, | |
h = height || 500, | |
border = 5, | |
taskbar = 40, // windows taskbar | |
header = 20, | |
x, | |
y; | |
if (maximized) { | |
x = 0; | |
y = 0; | |
w = screen.width; | |
h = screen.height; | |
} else { | |
x = window.screenX + (screen.width / 2) - (w / 2) - (border * 2); | |
y = window.screenY + (screen.height / 2) - (h / 2) - taskbar - border; | |
} | |
var features = [ | |
'toolbar=no', | |
'location=no', | |
'directories=no', | |
'status=no', | |
'menubar=no', | |
'scrollbars=no', | |
'resizable=no', | |
'copyhistory=no', | |
'center=yes', | |
dialogPrefix + 'width=' + w + unit, | |
dialogPrefix + 'height=' + h + unit, | |
dialogPrefix + 'top=' + y + unit, | |
dialogPrefix + 'left=' + x + unit | |
], | |
showModal = function (context) { | |
if (context) { | |
modalDiv = context.document.createElement('div'); | |
modalDiv.style.cssText = 'top:0;right:0;bottom:0;left:0;position:absolute;z-index:50000;'; | |
modalDiv.onclick = function () { | |
if (context.focus) { | |
context.focus(); | |
} | |
return false; | |
} | |
window.top.document.body.appendChild(modalDiv); | |
} | |
}, | |
removeModal = function () { | |
if (modalDiv) { | |
modalDiv.onclick = null; | |
modalDiv.parentNode.removeChild(modalDiv); | |
modalDiv = null; | |
} | |
}; | |
// IE | |
if (window.showModalDialog) { | |
window.showModalDialog(url, null, features.join(';') + ';'); | |
if (closeCallback) { | |
closeCallback(); | |
} | |
// Other browsers | |
} else { | |
var win = window.open(url, '', features.join(',')); | |
if (maximized) { | |
win.moveTo(0, 0); | |
} | |
// When charging the window. | |
var onLoadFn = function () { | |
showModal(this); | |
}, | |
// When you close the window. | |
unLoadFn = function () { | |
window.clearInterval(interval); | |
if (closeCallback) { | |
closeCallback(); | |
} | |
removeModal(); | |
}, | |
// When you refresh the context that caught the window. | |
beforeUnloadAndCloseFn = function () { | |
try { | |
unLoadFn(); | |
} | |
finally { | |
win.close(); | |
} | |
}; | |
if (win) { | |
// Create a task to check if the window was closed. | |
var interval = window.setInterval(function () { | |
try { | |
if (win == null || win.closed) { | |
unLoadFn(); | |
} | |
} catch (e) { } | |
}, 500); | |
if (win.addEventListener) { | |
win.addEventListener('load', onLoadFn, false); | |
} else { | |
win.attachEvent('load', onLoadFn); | |
} | |
window.addEventListener('beforeunload', beforeUnloadAndCloseFn, false); | |
} | |
} | |
// return window.showModalDialog(url, name, "dialogWidth:500px;dialogHeight:500px"); | |
} | |
$('.social-quote-icon').on('click', function(event) { | |
event.preventDefault(); | |
var postUrl = $('meta[property="og:url"]').attr('content'); | |
var name = $('meta[property="og:title"]').attr('content'); | |
var text = $(this).next().text(); | |
var url = 'https://twitter.com/intent/tweet?url='+postUrl+'&text='+text+':'; | |
var res = social_window(url, 500, 500); | |
}); | |
$('.pop_up').on('click', function(event) { | |
var socialNetwork = $(this).find('span').data('social'); | |
switch (socialNetwork) { | |
case 'twitter': | |
var postUrl = $('meta[property="og:url"]').attr('content'); | |
var name = $('meta[property="og:title"]').attr('content'); | |
var url = 'https://twitter.com/intent/tweet?url='+postUrl; | |
break; | |
case 'facebook': | |
var postUrl = $('meta[property="og:url"]').attr('content'); | |
var name = $('meta[property="og:title"]').attr('content'); | |
var url = 'https://www.facebook.com/sharer/sharer.php?u='+postUrl; | |
break; | |
case 'googleplus': | |
var postUrl = $('meta[itemprop="url"]').attr('content'); | |
var name = $('meta[itemprop="title"]').attr('content'); | |
var url = 'https://plus.google.com/share?url='+postUrl; | |
break; | |
case 'vkontakte': | |
var postUrl = $('meta[property="url"]').attr('content'); | |
var name = $('meta[property="title"]').attr('content'); | |
var url = 'https://vk.com/share.php?url='+postUrl; | |
break; | |
case 'pocket': | |
var postUrl = $('meta[property="og:url"]').attr('content'); | |
var name = $('meta[property="og:title"]').attr('content'); | |
var url = 'https://getpocket.com/edit?url='+postUrl; | |
break; | |
} | |
var value = $(this).parents('.with-share').data('value'); | |
var res = social_window(url, 500, 500, function(j){ | |
//cb counter | |
$.ajax({ | |
url: '/blog/ajax/get-socials-share/', | |
type:'POST', | |
data: {socialNetwork: socialNetwork, postUrl: postUrl, postSlug: postSlug}, | |
success: function(data) { | |
if(data.status = 'success'){ | |
$('.'+data.socialNetwork).find('span').text(data.countShares); | |
$('.post-share-like span, .icon-like').text(data.countTotal); | |
socialInteraction(data.socialNetwork, 'share', window.location.href); | |
} | |
} | |
}); | |
}); | |
return false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment