MediaWiki:Gadget-Social.js

From Uncyclopedia, the content-free encyclopedia
Jump to navigation Jump to search

Note: After saving, you have to bypass your browser's cache to see the changes.

  • Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button, or press Ctrl+F5.
  • Firefox: hold down the Shift key while clicking Reload; alternatively press Ctrl+F5 or Ctrl-Shift-R.
  • Opera, Konqueror and Safari users can just click the Reload button.
  • Chrome: press Ctrl+F5 or Shift+F5
/** Uncyclopedia share buttons **********************************************
 * This gadget is useable cross-wiki WITH SOME QUICK CHANGE(S):
 * 	1) var twitterHandle should be set to YOUR official twitter handle
 * 	2) var shareBoxTab is the image to use for the "share" tab
 * 	3) var redditTargetCommunity - what it says on the tin
 * To add a badge:
 * 	1) if the badge includes script(s), use mw.loader.load() in
 * 		the main function
 * 	2) add any jQuery build-code inside a $( document ).ready() call,
 * 		as your badge code runs *before* document.onready
 * 	3) please keep your badge and badge-script calls grouped together
 *
 * @name		MediaWiki:Gadget-Sharing.js
 * @dependencies	jQuery
 * @source
 * @revision		1.7
 * @revision date	December 8, 2013
 * @author		[[User:Bizzeebeever]]
 * @license		GPL-whatever
*/

if ( !location.href.match( /mediawiki:|action=(edit|submit|move|delete|protect)/i ) ) {
	( function( $ ) {
		// site-specific variables
		var twitterHandle = 'Uncyclopaedia',
			shareBoxTab = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAABOCAYAAAAkaeahAAAABmJLR0QAAAAAAAD5Q7t/AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3QwIEyoDijLgBAAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAACk0lEQVRYw+2XT2gTQRTGv4bCngYR6r9c7CE57CFUBZVAjYkgIRRy0YOgpWRPVjBeImpKexF6aXsOQpFKbi0qpEgJQpbdBkpDsQ01FGElLZQmagTlndZD6GHZmKjJxt3Ug8xAyDLD/ObNt+99eekjIhzFcOGIBgdzMAdzMAdzMAf/CzDTdQ9TEWcq4gDAgLBjMAPCSOyXxJFXEczmZwAA7vyEeYj9iKPFzMxCYrRAYzHAaG5q3ojUOMS+FAe4S68XAaqaM4JCmnmIbbAoV3IsXU+ZujIgzNL1lChXcp329Vn3buz0CEtOquyKD9gFMIgArW6/oemnzbf4dfRbvzwaKpfZY3YDp8w5eolPQOeILCNm0aJOmSHhb/PYMmIo2ecs7UvhrGsHAXxoXiIgaz/i63kVpV0AxwAcb1mjg+GA7YhrPyISvO1Wyb4UgkLaH6tsry4SMG4/K+YqOpSPEOXPuZ3QyWvmd0BencfoEweVp2yB5PtBs6QLNBa7I6cn1OnbPue2SZtrP4uBqil6Nou3pcvObTNdTwEANr6uMxXxntgmycmgeG9h0HC1cQm3lm8iWswE5K15h15h+IUphX6VeYR3nhO1svZ+YKD95q7A7a7eqfL6u9J3aVvCBq3/btXD9sFYykokJ4P6Be1LTyvPTDehF+7Woun3F1E2d07HxTMPHbsbc+fV1pkeuVvNG5G6uzA5y2Om6x4SBK1Zpk4ydNewqIgjsV9qpF60mEG0mLFqWEBEHT8IregEj994XjSe4fEjtKJ32ufqSkfaXGtxOsPtHP6YmnLs1UWzSbGUoauXx877L2FyCviGAj2IAVRl7mWVaOpRy03su5th8g2HU0jrgW3yvwoczMEczMEczMH/D/gQDDFPJaeGDmgAAAAASUVORK5CYII=',
			redditTargetCommunity = 'humor',
			//badges load in the order they are defined here. You can leave some out, too!
			enabled = [ 'facebook', 'twitter', 'googleplus', 'reddit', 'stumbleupon' ],

		// don't touch anything below this line
			$outer,
			$shareBox,
			shareboxClosed = true,
			forceSecure, // not implemented yet
			cookieName = 'enable-sharing',
			isSecure = location.protocol == 'https',
			title = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' ),
			pageName = mw.config.get( 'wgPageName' ),
			pageUrl = mw.config.get( 'wgServer' )
				+ mw.config.get( 'wgArticlePath' ).replace( '$1', pageName ),
			apiPath = mw.config.get( 'wgScriptPath' ) + '/api.php',
			wrapper = '<div class="un-share-wrapper"></div>';

		var badges = {
			facebook: function(){
				// load facebutt script IMMEDIATELY
				//mw.loader.load( '//connect.facebook.net/en_US/all.js#xfbml=1' );
				// ...or not.
				// onready: build facebook badge
				$( function() {
					// what the following element is for, God and his devils only know
					$( '<div>' ).attr( 'id', 'fb-root' ).prependTo( 'body' );
					// facebook
					//<fb:like href="http://wiki.example.org/wiki/Article_name"
					//	send="false" layout="box_count" width="60" show_faces="false"></fb:like>
					badge( $( '<div>' ).attr({
						'class': 'fb-like',
						'data-href': pageUrl,
						'data-layout': 'box_count',
						'data-action': 'like',
						'data-show-faces': 'false',
						'data-share': 'false',
						'data-width': 55
					}) );
					// try loading *after* the proper elements have been created...
					mw.loader.load( '//connect.facebook.net/en_US/all.js#xfbml=1' );
				});
			},

			twitter: function() {
				// load twitter script IMMEDIATELY
				mw.loader.load( '//platform.twitter.com/widgets.js' );
				// onready: build badge
				$( function() {
					badge( $( '<a>' ).attr({
						'href': 'https://twitter.com/share',
						'class': 'twitter-share-button',
						'data-url': pageUrl,
						'data-text': title,
						'data-via': twitterHandle,
						'data-count': 'vertical'
					}) );
				});
			},

			googleplus: function() {
				// load google+ script IMMEDIATELY
				mw.loader.load( 'https://apis.google.com/js/plusone.js' );
				// onready: build badge
				$( function() {
					//googleplus
					//<g:plusone annotation="bubble" size="tall"
					//	url="http://wiki.example.org/wiki/Article_name"></g:plusone>
					badge( $( '<g:plusone>' ).attr({
						'annotation': 'bubble', // choices: I forget
						'size': 'tall', 		// choices: I forget
						'url': pageUrl, 		// page URL
						'title': title,			// page title
						'href': pageUrl
					}) );
				});
			},

			// derived from http://www.reddit.com/static/button/button3.js
			// probably shouldn't do it this way, but Reddit's code uses a
			// document.write() AND global variables
			// And people think Reddit doesn't suck.
			reddit: function() {
				// onload: insert IFRAME badge (wtf? lame)
				$( function() {
					var redditUnsecure = 'http://www.reddit.com/static',
						redditSecure = 'https://redditstatic.s3.amazonaws.com',
						redditButton = '/button/button3.html?',
						redditUrl = ( isSecure ? redditSecure : redditUnsecure ) + redditButton
							+ $.param({
								url: pageUrl,
								title: title,
								sr: redditTargetCommunity, // target community
								//bgcolor: specs.bgcolor, // optional
								//bordercolor: specs.bordercolor, // optional
								//css: '', // optional - CSS string
								newwindow: 1 // open a new window on click
							});
					badge( $( '<iframe src="' + redditUrl + '" scrolling="no">' )
						.css({
							height: 52,	// not changeable (for button3)
							width: 69, // ditto
							border: 'none'
						})
					);
				});
			},

			stumbleupon: function() {
				// load stumbleupon script IMMEDIATELY
				mw.loader.load( '//platform.stumbleupon.com/1/widgets.js' );
				// onready: build badge
				$( function() {
					badge( $( '<su:badge layout="5" location="' + pageUrl + '">' ) );
				});
			}
		};

		function init() {
			// needs to be done ondocumentready, not before
			$( addShareBox );
			// needs to be done immediately. Note: badges will definitely be added to sharebox
			// *after* sharebox is created.
			addBadges();
		}

		function addShareBox() {
			$outer = $( '<div id="un-share">' ).appendTo( 'body' );
			$shareBox = $( '<div id="un-share-box">' ).appendTo( $outer );
			addTab();
			addCloseButton();
		}

		function addTab() {
			$( '<img>' )
				.attr({
					id: 'un-share-tab',
					src: shareBoxTab,
					title: 'Click to open the sharing box: share via Twitter, Facebook, and more'
				})
				.click( toggle )
				.prependTo( $outer );
		}

		function addCloseButton() {
			$( '<div class="link" title="Get rid of this widget">[disable]</div>' )
				.appendTo( $shareBox )
				.click( disable );
		}

		function addBadges() {
			//should not be done until onready
			$( addCloseButton );
			$.each( enabled, function( i, val ) {
				if ( $.isFunction( badges[val] ) ) {
					badges[val]();
				}
			} );
		}

		function badge( $badge ) {
			$badge.appendTo( $shareBox ).wrap( wrapper );
		}

		function toggle() {
			var openDim = '-' + $( '#un-share-box' ).width() + 'px';
			if ( shareboxClosed ) {
				shareboxClosed = false;
				$( '#un-share' ).animate({ 'margin-left': openDim })
			} else {
				shareboxClosed = true;
				$( '#un-share' ).animate({ 'margin-left': 0 })
			}
		}

		function disable() {
			$( '#un-share .link' ).unbind( 'click' ).text( '[Please wait]' );
			fetchPrefToken();
		}

		function fetchPrefToken() {
			$.get( apiPath, {
				action: 'tokens',
				type: 'options',
				format: 'xml'
			})
				.done( onFetchPrefs )
				.fail( onPrefError );
		}

		function onFetchPrefs( xml ) {
			var token = $( xml ).find( 'tokens' ).attr( 'optionstoken' );
			if ( token )
				$.post( apiPath, {
					action: 'options',
					optionname: 'gadget-Sharing',
					optionvalue: 0,
					format: 'xml',
					token: token
				})
				.done( onPrefReply )
				.fail( onPrefError )
			else
				onPrefError();
		}

		function onPrefError() {
			alert( 'There was an error setting your preference. To disable the sharing tab, go to your preferences > Gadgets and un-tick the "social media" box.' );
		}

		function onPrefReply( xml ) {
			if ( ! $( xml ).find( 'error' ).length ) {
				$( '#un-share' ).remove();
				onPrefSuccess();
			} else {
				onPrefError();
			}
		}

		function onPrefSuccess() {
			alert( 'The sharing tab has been disabled. To re-enable, go to your preferences > Gadgets and tick the "social media" box.' );
		}

		// onready: build $outer, $tab, $shareBox
		init();

		// end $outer function
	})( jQuery );
}