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

[livejournal] r23226: LJSUP-14127: Optimize $.fn.ljLikes() plu...

Committer: vvasin
LJSUP-14127: Optimize $.fn.ljLikes() plugin to perform parsing only likes that are currently on the page at once. Replace previous lazy loader.
U   trunk/htdocs/js/journal.js
Modified: trunk/htdocs/js/journal.js
===================================================================
--- trunk/htdocs/js/journal.js	2012-10-31 11:23:30 UTC (rev 23225)
+++ trunk/htdocs/js/journal.js	2012-10-31 11:31:56 UTC (rev 23226)
@@ -1,12 +1,12 @@
 ILikeThis = {
 	dialog: jQuery(),
-	
+
 	dialogRemove: function()
 	{
 		this.dialog.remove();
 		jQuery(document).unbind('click', this.document_click);
 	},
-	
+
 	// inline click
 	rate: function(e, node, itemid, username)
 	{
@@ -15,7 +15,7 @@
 		// has undorate node
 		var action = jQuery('.i_like_this_'+itemid+' .i_like_this_already').remove().length ? 'undorate' : 'rate';
 		jQuery(node).parent().removeClass('i_dont_like_this');
-		
+
 		jQuery.ajax({
 			url: LiveJournal.getAjaxUrl('eventrate'),
 			data: {
@@ -44,18 +44,18 @@
 							append_node = jQuery(context);
 						}
 						append_node.append('<i class="i_like_this_already">/</i>');
-					} 
+					}
 				}
 			}
 		});
 		return false;
 	},
-	
+
 	// inline click
 	showList: function(e, node, itemid, username)
 	{
 		this.ajax && this.ajax.abort();
-		
+
 		this.ajax = jQuery.ajax({
 			url: LiveJournal.getAjaxUrl('eventrate'),
 			data: {
@@ -88,39 +88,39 @@
 									+'</a></p>' : ''
 								+'</div>')
 					}).ljAddContextualPopup();
-					
+
 					ILikeThis.dialog.appendTo(document.body);
-					
+
 					//calc with viewport
 					var ele_offset = $node.offset(),
 						left = ele_offset.left,
 						top = ele_offset.top + $node.height() + 0, // TODO: 4 is fix markup height
 						$window = jQuery(window);
-					
+
 					left = Math.min(left,  $window.width() + $window.scrollLeft() - ILikeThis.dialog.outerWidth(true));
 					top = Math.min(top, $window.height() + $window.scrollTop() - ILikeThis.dialog.outerHeight(true));
-					
+
 					jQuery(document).click(ILikeThis.document_click);
-						
+
 					ILikeThis.dialog.css({
 						left: left,
 						top: top,
 						visibility: 'visible'
 					});
-					
+
 					var append_node = jQuery('.we_like_this_'+itemid+' span>span>span');
 					if (!append_node.length) { // s1
 						append_node = jQuery('.we_like_this_'+itemid);
 					}
-					
+
 					append_node.text(data.total);
 				}
 			}
 		});
-		
+
 		return false;
 	},
-	
+
 	document_click: function(e)
 	{
 		if (!jQuery(e.target).parents('.b-popup-ilikethis').length) {
@@ -158,7 +158,7 @@
 
 			if( ev.data && ev.data.message === "updateWallet" ) {
 				LiveJournal.run_hook( 'update_wallet_balance' );
-				jQuery.getJSON( LiveJournal.getAjaxUrl( 'give_tokens' ) + "?" + url_data + "&mode=js", 
+				jQuery.getJSON( LiveJournal.getAjaxUrl( 'give_tokens' ) + "?" + url_data + "&mode=js",
 					function( result ) {
 						if( result.html ) {
 							$node = jQuery( link ).closest( '.lj-button' );
@@ -291,7 +291,7 @@
 			loading: 'b-mediaplaceholder-processing',
 			init: function() {
 				var self = this;
-				doc.on('click', this.selector, function(ev) { 
+				doc.on('click', this.selector, function(ev) {
 					self.handler(this, ev);
 				});
 			},
@@ -399,53 +399,10 @@
 
 
 /**
-* delayed like buttons loader
-*/
+ * Delayed like buttons loader
+ */
+LiveJournal.register_hook('page_load', function () {
+	'use strict';
 
-(function() {
-	var likePos = [];
-
-	LiveJournal.register_hook('page_load', function() {
-		
-		likePos = jQuery('.lj-like').map(function() {
-			return {
-				el: this,
-				top: jQuery(this).offset().top,
-				init: false
-			};
-		}).toArray();
-
-		fullInit();
-
-		if (likePos.length > 0) jQuery(window).scroll(fullInit);
-	});
-
-	function fullInit() {
-		if (likePos.length > 0) {
-			
-			var scrollTop = jQuery(window).scrollTop(),
-				windowHeight = jQuery(window).height(),
-
-				toInit = likePos.filter(function(like) {
-					return (!like.init &&
-							 like.top > scrollTop - 100 &&
-							 like.top < scrollTop + windowHeight + 200);
-				});
-
-			toInit.forEach(function(like) {
-				var jEl = jQuery(like.el),
-					internal = (jEl.get(0) || {}).childNodes;
-
-				if (!internal || !internal[0]) {
-					return;
-				}
-				
-				jEl.html(internal[0].nodeValue); // strip '<!--' and '-->'
-				LiveJournal.parseLikeButtons(jEl);
-
-				like.init = true;
-			});
-		}
-	}
-	
-})();
\ No newline at end of file
+	jQuery(document.body).ljLikes();
+});
\ No newline at end of file

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