Valerii Vasin (valerii) wrote in changelog,
Valerii Vasin
valerii
changelog

[livejournal] r23238: LJSUP-14136: [Friends Feed] Optimize lik...

Committer: vvasin
LJSUP-14136: [Friends Feed] Optimize like buttons parsing. parseLikeButtons has been rewritten.
U   trunk/htdocs/js/livejournal.js
Modified: trunk/htdocs/js/livejournal.js
===================================================================
--- trunk/htdocs/js/livejournal.js	2012-11-01 11:53:27 UTC (rev 23237)
+++ trunk/htdocs/js/livejournal.js	2012-11-01 12:03:19 UTC (rev 23238)
@@ -407,102 +407,123 @@
 	}, 'json');
 };
 
-LiveJournal.parseLikeButtons = function(ctx) {
-	try {
-		window.FB.XFBML.parse( jQuery(ctx).find('[action=recommend]').parent().get(0) );
-	} catch (e) {
-		console.warn(e.message);
+LiveJournal.parseLikeButtons = (function ($) {
+	'use strict';
+
+	/**
+	 * Parse lj-like buttons
+	 * @param  {Object} context jQuery Node (with class .lj-like)
+	 */
+	function parse($node) {
+		parseFacebook($node);
+		parseGoogle($node);
+		parseTwitter($node);
+		parseTumblr($node);
+		parseSurfingbird($node);
+		parseRepost($node);
 	}
 
-	try {
-		if (jQuery.browser.msie) {
-			var replaceNode, attrs, j, node;
-			var nodes = document.body.getElementsByTagName('plusone');
+	/**
+	 * Parse facebook likes
+	 * @param  {jQuery} $node jQuery collection
+	 */
+	function parseFacebook($node) {
+		try {
+			window.FB.XFBML.parse( $node.find('.lj-like-item-facebook').get(0) );
+		} catch (e) {
+			console.warn(e.message);
+		}
+	}
 
-			for (var i = 0, l = nodes.length; i < l; ++i) {
-				replaceNode = document.createElement('g:plusone');
-				node = nodes[i];
-				attrs = node.attributes;
+	/**
+	 * Parse google +1 button
+	 * @param  {jQuery} $node jQuery node with likes in which we will search for google +1 button for parsing
+	 */
+	function parseGoogle($node) {
+		// gapi could
+		try {
+			gapi.plusone.go( $node.find('.lj-like-item-google').get(0) );
+		} catch (e) {
+			console.warn(e.message);
+		}
+	}
 
-				for (j = 0; j < attrs.length; ++j) {
-					if (attrs[j].specified) {
-						replaceNode.setAttribute(attrs[j].nodeName, attrs[j].nodeValue);
-					}
-				}
+	function parseTwitter($node) {
+		var params = null,
+			iframe = null,
+			link = $node.children('.lj-like-item-twitter')
+						.children()
+						.eq(0);
 
-				node.parentNode.replaceChild(replaceNode, node);
-			}
-		}
+		params = {
+			url: link.data('url'),
+			text: link.data('text'),
+			count: link.data('count'),
+			lang: link.data('lang')
+		};
 
-		gapi.plusone.go();
-	} catch(e) {}
+		iframe = $('<iframe />', {
+			frameborder: 0,
+			scrolling: 'no',
+			allowtransparency: true,
+			src: LiveJournal.constructUrl('http://platform.twitter.com/widgets/tweet_button.html', params)
+		})
+		.css({
+			width: 110,
+			height: 20
+		});
 
-	jQuery('a.twitter-share-button', ctx || document).each(function() {
-		if (this.href != 'http://twitter.com/share') {
-			return;
-		}
+		link.replaceWith(iframe);
+	}
 
-		var link = jQuery(this), params = {
-			url: link.attr('data-url'),
-			text: link.attr('data-text'),
-			count: link.attr('data-count'),
-			lang: link.attr('data-lang')
+	function parseSurfingbird($node) {
+		var link = $node.find('.surfinbird__like_button'),
+			iframe = null,
+			params = null;
+
+		params = {
+			url: link.data('url'),
+			caption: link.data('text'),
+			layout: 'common'
 		};
 
-		link.replaceWith(jQuery('<iframe frameborder="0" scrolling="no" allowtransparency="true" />').css({
-			width: '110px',
-			height: '20px'
+		iframe = $('<iframe />', {
+			frameborder: 0,
+			scrolling: 'no',
+			allowtransparency: true,
+			src: LiveJournal.constructUrl('http://surfingbird.ru/button', params)
 		})
-		.attr('src', LiveJournal.constructUrl('http://platform.twitter.com/widgets/tweet_button.html', params))
-		.insertBefore(link));
-	});
+		.css({
+			width: 110,
+			height: 20
+		});
 
-	jQuery('a.surfinbird__like_button', ctx || document).each(function () {
-		if (this.href !== 'http://surfingbird.ru/share') {
-			return;
-		}
-
-		var link = jQuery(this),
-			params = {
-				url: link.attr('data-url'),
-				caption: link.attr('data-text'),
-				layout: 'common'
-			},
-			iframe = jQuery('<iframe frameborder="0" scrollin="no" allowtransparency="true" />')
-				.css({
-					width: '110px',
-					height: '20px'
-				})
-				.attr('src', LiveJournal.constructUrl('http://surfingbird.ru/button', params))
-				.insertBefore(link)
-
 		link.replaceWith(iframe);
-	});
+	}
 
-	jQuery('a.tumblr-share-button', ctx || document).each(function () {
-		if (this.href !== 'http://tumblr.com/share/link') {
-			return;
-		}
-
-		var link = jQuery(this),
+	function parseTumblr($node) {
+		var link = $node.find('.tumblr-share-button'),
+			href = link.attr('href'),
 			params = {
-				url: link.attr('data-url'),
-				name: link.attr('data-title')
+				url: link.data('url'),
+				name: link.data('title')
 			};
 
-		link.attr('href', LiveJournal.constructUrl(link.attr('href'), params));
-	});
+		link.attr('href', LiveJournal.constructUrl(href, params));
+	}
 
-	jQuery('div.lj-like-item-repost > a', ctx || document).each(function() {
-		var link = jQuery(this),
+	function parseRepost($node) {
+		var link = $node.find('.lj-like-item-repost').find('a'),
 			url = link.data('url');
 
 		LJ.Api.call('repost.get_status', { url: url }, function (data) {
 			link.replaceWith(LiveJournal.renderRepostButton(url, data));
 		});
-	});
-};
+	}
 
+	return parse;
+}(jQuery));
+
 LiveJournal.renderRepostButton = function (url, data) {
 	data = data || {};
 

Tags: invis89, js, livejournal, vvasin
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments