forked from mozilla/friendlycode
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mozilla#9 from Pomax/gh-pages
re-enabled the share button
- Loading branch information
Showing
6 changed files
with
155 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* A tiny library for load-loading social media share buttons. | ||
* If we don't do this, social media will track users even before | ||
* they click the like button and we don't like that kind of | ||
* monitoring behaviour. | ||
*/ | ||
var SocialMedia = function(options) { | ||
var $ = options.jQuery; | ||
var targetSelector = options.selector; | ||
var urlPlaceHolder = "__URL__PLACE__HOLDER__"; | ||
|
||
/** | ||
* The various social media all have the same API. | ||
*/ | ||
var socialMedia = { | ||
|
||
facebook: { | ||
class: "facebook", | ||
id: "facebook-jssdk", | ||
src: "http://connect.facebook.net/en_US/all.js#xfbml=1&appId=1", | ||
html: "<div id='fb-root'></div><div class='fb-like' data-href='"+urlPlaceHolder+"' data-send='false' data-show-faces='false' data-font='tahoma'></div>" }, | ||
|
||
google: { | ||
class: "google", | ||
id: "google-plus", | ||
src: "https://apis.google.com/js/plusone.js", | ||
html: "<g:plusone annotation='none' href='"+urlPlaceHolder+"'></g:plusone>" }, | ||
|
||
twitter: { | ||
class: "twitter", | ||
id: "twitter-wjs", | ||
src: "http://platform.twitter.com/widgets.js", | ||
html: "<a href='https://twitter.com/share'class='twitter-share-button' data-url='"+urlPlaceHolder+"' data-via='Mozilla' data-count='none'>Tweet</a>" } | ||
} | ||
|
||
/** | ||
* Hot-load a social medium's button by first | ||
* injecting the necessary HTML for the medium | ||
* to perform its own iframe replacements, and | ||
* then late-loading the script required for | ||
* the medium to load up its functionality. | ||
*/ | ||
var hotLoad = function($, element, socialMedium, targetSelector) { | ||
element.innerHTML = socialMedium.html.replace(urlPlaceHolder, $(targetSelector).text()); | ||
(function(document, id, src, url) { | ||
var script = document.createElement("script"); | ||
script.type = "text/javascript"; | ||
script.id = id; | ||
script.src = src; | ||
document.head.appendChild(script); | ||
}(document, socialMedium.id, socialMedium.src)); | ||
} | ||
|
||
/** | ||
* This is a separate function because the | ||
* for/in loop is a weird JavaScript thing; | ||
* it does late variable binding, somehow, | ||
* so dynamic functions that involve "medium" | ||
* end up all being evaluated for the last | ||
* object in the socialMedia object. Effectively | ||
* it makes every button a twitter button. | ||
*/ | ||
var setupHotLoading = function($, targetSelector, parent, medium) { | ||
if (medium.src) { | ||
$("." + medium.class, parent).click(function() { | ||
hotLoad($, this, medium, targetSelector); | ||
// prevent default click behaviour | ||
return false; | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Set up the onclick bindings. Content will | ||
* be injected into elements inside a master | ||
* container with id "share-container", and | ||
* content replacements are based on each | ||
* social medium's "class" property. | ||
*/ | ||
var parent = $("#share-container"); | ||
for (medium in socialMedia) { | ||
// call an actual function to force | ||
// immediate evaluation of {medium} | ||
setupHotLoading($, targetSelector, parent, socialMedia[medium]); | ||
} | ||
}; |
This file contains 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
This file contains 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
This file contains 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