/** * XO Group Inc. sharing tools loader * 0.0.6 20130523 * Techwiki: http://techwiki.intranet.theknot.com/display/tw/sharingInit.js */ /*jslint plusplus: true, maxerr: 50, indent: 4 */ /*global FB, jQuery, $ */ (function (win) { 'use strict'; // Private vars var fbKeys = { dev: { tk: '179731445796', tn: '153976482927', tb: '286205545050', wc: '190720133641', bl: '394454603954155', // theblush gc: '', // getcrush bf: '330655050375883' }, prod: { tk: '25371745544', tn: '5304144263', tb: '44019285167', wc: '6751238267', bl: '426763974032441', // theblush gc: '255101957928612', // getcrush bf: '102400723261912' } }, subdomains = [ 'webdev', 'webqa', 'stg', 'local', 'local6', '127.0.0.1', 'test', 'qa' ], // Dev subdomains used by FB init part length = subdomains.length, state = 'prod', // App state used by FB init part d = win.document || {}, l = d.location || {}, url = l.href || false, protocol = l.protocol || false, host = l.host || '', domain = host.match(/[a-z]{1,32}\.com$/i) || false, fbRoot = (d && d.getElementById) ? d.getElementById('fb-root') : false, id = null, // fbKey to use site = null, // Site name used by Twitter sharing tools sharingClass = win.tkSharingClassName || '.sharing', // Share bar class name xo_share_template = { text: 'Check out "{{title}}"', title: d.title, url: url, via: false }, $o, // Store wrapped sets tt, // Store old Twitter template text i, // Counter var s, // Script DOM element index, // More for old Twitter template text // Private utility methods extend = function (outDoor, inDoor) { var p; if (outDoor && inDoor) { for (p in inDoor) { if (inDoor.hasOwnProperty(p) && !outDoor.hasOwnProperty(p)) outDoor[p] = inDoor[p]; } } return outDoor; }; // Check for reqs if (!url) { return; } // If the FB SDK has already been loaded, no reason to proceed with loading FB. if (!win.FB) { // If an element with the id fb-root doesn't exist, create it. if (!fbRoot) { fbRoot = d.createElement('div'); fbRoot.id = 'fb-root'; d.getElementsByTagName('body')[0].appendChild(fbRoot); } // Includes http protocol, just in case user uses webdev, webqa or stg as a dir/folder name in the URL. for (i = 0; i < length; i++) { if (url.indexOf(protocol + '//' + subdomains[i]) !== -1) { state = 'dev'; break; } } // Set our FB key based on domain and state. if (domain && domain.length) { switch (domain[0]) { case 'theknot.com': id = fbKeys[state].tk; break; case 'thenest.com': id = fbKeys[state].tn; break; case 'thebump.com': id = fbKeys[state].tb; break; case 'weddingchannel.com': id = fbKeys[state].wc; break; case 'theblush.com': id = fbKeys[state].bl; break; case 'breastfeeding.com': id = fbKeys[state].bf; break; case 'getcrush.com': id = fbKeys[state].gc; break; } } // No domain match? No go. if (id) { // Initialize the FB win.fbAsyncInit = function () { FB.init({ appId: id, status: true, cookie: true, xfbml: true }); /* Optionally execute a function that will populate the user data. We only want to do this on some of the pages where the data is actually used */ if (typeof (populateFBInfo) == "function") { populateFBInfo(); } }; var js, jsId = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(jsId)) { return; }; js = d.createElement('script'); js.id = jsId; js.async = true; js.src = "//connect.facebook.net/en_US/all.js"; ref.parentNode.insertBefore(js, ref); } } // Do we have a sharing widget on the page? If not, no reason to proceed. if (win.jQuery) { $(function () { var win = window; if (jQuery(sharingClass).length !== 0) { // Determine the current site name for Twitter. if (domain && domain.length) { switch (domain[0]) { case 'theknot.com': site = 'TheKnot'; break; case 'thenest.com': site = 'TheNest'; break; case 'thebump.com': site = 'TheBump'; break; case 'weddingchannel.com': site = 'WeddingChannel'; break; case 'theblush.com': site = 'TheBlush'; break; case 'breastfeeding.com': site = 'BreastFeeding'; break; } } if (site) { xo_share_template.via = site; } // Attemp backwards compatibility if (win.addthis_share_template && win.addthis_share_template.templates && win.addthis_share_template.templates.twitter && !win.xo_share_template) { win.xo_share_template = {}; tt = win.addthis_share_template.templates.twitter.replace('{{url}} ', ''); index = tt.indexOf('via'); if (index !== -1) { if (tt.substr(index - 1, 1) === '(') { index = index - 1; } site = tt.substr(index).match(/TheKnot|TheNest|TheBump|WeddingChannel/i); if (site && site.length) { win.xo_share_template.via = site[0]; } } win.xo_share_template.text = tt.substr(0, index); } // Is there a user-defined template that should override ours? if (win.xo_share_template) { xo_share_template = extend(win.xo_share_template, xo_share_template); } // Replace {{title}} placeholder xo_share_template.text = xo_share_template.text.replace('{{title}}', xo_share_template.title); // Twitter $o = $('.addthis_button_twitter, .addthis_button_tweet'); // Iterate for backwards compatibility $o.each(function () { var $this = $(this); if ($this.attr('tw:text')) { xo_share_template.text = $this.attr('tw:text'); } if ($this.attr('tw:via')) { xo_share_template.via = $this.attr('tw:via'); } }); if ($o) { $o.attr({ "data-text": xo_share_template.text, "data-url": xo_share_template.url, "data-via": xo_share_template.via, "data-count": 'none' }).addClass('twitter-share-button').html('Tweet'); s = d.createElement('script'); s.async = true; s.src = '//platform.twitter.com/widgets.js'; d.getElementsByTagName('body')[0].appendChild(s); } // Google Plus $o = $('.addthis_button_google_plusone'); if ($o) { $o.parent().html('
'); s = d.createElement('script'); s.async = true; s.src = '//apis.google.com/js/plusone.js'; d.getElementsByTagName('body')[0].appendChild(s); } } }); // We do. Let's try and add Twitter and Google Plus. } } (window));