Committer: dpetrov
LJSUP-9176: LJ-Like: Problems with loading buttons in LJ-like ?\226?\128?\148 the 2nd stepU trunk/htdocs/js/journal.js U trunk/htdocs/js/livejournal.js
Modified: trunk/htdocs/js/journal.js =================================================================== --- trunk/htdocs/js/journal.js 2011-07-22 08:23:25 UTC (rev 19511) +++ trunk/htdocs/js/journal.js 2011-07-22 08:32:41 UTC (rev 19512) @@ -181,23 +181,34 @@ } }; +(function() { + var prev_page_start, have_prev, list; + + function getList( html ) { + var answer = jQuery( html ), + node = answer.find( '.b-friendstimes' ); + + return node + .add( answer.filter( 'script' ) ); + } + FriendsTimes = { - prev_page_start: null, - have_prev: null, initTime: +new Date(), - init: function() { - jQuery(function(){ - FriendsTimes.checkUnreaded({ - timeout: 5000 - }); + init: function( node ) { + prev_page_start = node.attr( 'data-prev-page-start' ); + have_prev = parseInt( node.attr( 'data-have-prev' ), 10 ); + list = node; - if( FriendsTimes.have_prev ) { - FriendsTimes.bindLoadMore({ - max_load: 4 - }); - } + FriendsTimes.checkUnreaded({ + timeout: 5000 }); + + if( have_prev ) { + FriendsTimes.bindLoadMore({ + max_load: 4 + }); + } }, checkUnreaded: function(conf) { @@ -220,6 +231,26 @@ }, conf.timeout); }, + fetchContent: function( data, success, error ) { + jQuery.ajax({ + url: LiveJournal.getAjaxUrl("ft_more"), + data: data, + dataType: "html", + success: function(html) { + if( html ) { + var node = getList( html ), + li = node.children(), + scripts = node.filter( 'script' ); + + success( node, li, scripts ); + } else { + success; + } + }, + error: error + }); + }, + /** * Fetch new posts that have appeared since page load. * Function loads new posts with ajax if the user is on the first page and there are ten @@ -229,24 +260,20 @@ var blockFunction = false, content; - function fetchContent() { - var ftList = jQuery( '.b-friendstimes' ); - jQuery.get( LiveJournal.getAjaxUrl( 'ft_more' ), { - boundary: ftList.attr( 'data-firstitem' ) - }, function( html ) { - html = html.replace( /<script[^>]+>(.*?)<\/script>/, ''); - - var node = jQuery( html ).find( '.b-friendstimes' ), - newBoundary = node.attr( 'data-firstitem' ), + function loadContent() { + FriendsTimes.fetchContent( { + boundary: list.attr( 'data-firstitem' ) + }, function( node, li, scripts ) { + var newBoundary = node.attr( 'data-firstitem' ), newTs = +new Date(); - content = node.children(); - if( content.length ) { - ftList.attr( 'data-firstitem', newBoundary ); + if( li.length ) { + list.attr( 'data-firstitem', newBoundary ); Site.server_time += Math.floor( ( newTs - FriendsTimes.initTime ) / 1000 ); FriendsTimes.initTime = newTs; + content = li.add( scripts ); } - } ) + } ); } function renderContent() { @@ -256,8 +283,10 @@ content .css( 'opacity', 0.01 ) - .prependTo( '.b-friendstimes' ); + .prependTo( list ); + LiveJournal.parseLikeButtons(); + setTimeout( function() { if( jQuery.browser.msie && +jQuery.browser.version <= 8 ) { content @@ -292,7 +321,7 @@ animateComplete = true; } } ); - fetchContent(); + loadContent(); if( event ) { event = jQuery.event.fix( event ); @@ -306,36 +335,38 @@ var $window = jQuery(window), more_node = jQuery(".b-friendstimes-loading"), loaded_count = 0; + function loading_more() { // preload if ((jQuery(document).height() - 1000) <= ($window.scrollTop() + $window.height())) { $window.unbind("scroll", loading_more); - jQuery.ajax({ - url: LiveJournal.getAjaxUrl("ft_more"), - data: { - to: FriendsTimes.prev_page_start - }, - dataType: "html", - success: function(html) { - if (html) { - loaded_count++; - more_node.before(html); + FriendsTimes.fetchContent( { to: prev_page_start }, + function( node, li, scripts ) { + if( li ) { + list.append( li ).append( scripts ); + LiveJournal.parseLikeButtons(); + prev_page_start = node.attr( 'data-prev-page-start' ); + have_prev = parseInt( node.attr( 'data-have-prev' ), 10 ); + if (loaded_count < conf.max_load) { - jQuery(".b-friendstimes-pages").remove(); - $window.scroll(loading_more); + if( !have_prev ) { + $window.scroll(loading_more); + } else { + more_node.remove(); + } } else { - jQuery(".b-friendstimes-pages").show(); + node.nextAll( '.b-friendstimes-pages' ) + .insertAfter( list ) + .show(); more_node.remove(); } } else { more_node.remove(); } - }, - error: function() { + }, function() { // retry setTimeout(loading_more, 5000); - } - }); + } ); } } @@ -344,9 +375,12 @@ } }; +} )(); + jQuery(function($) { - if ($("#friendstimes").length) { - FriendsTimes.init(); + var ft = $( '#friendstimes' ) + if ( ft.length) { + FriendsTimes.init( ft.find( '.b-friendstimes' ) ); } }); Modified: trunk/htdocs/js/livejournal.js =================================================================== --- trunk/htdocs/js/livejournal.js 2011-07-22 08:23:25 UTC (rev 19511) +++ trunk/htdocs/js/livejournal.js 2011-07-22 08:32:41 UTC (rev 19512) @@ -212,12 +212,12 @@ * @param {Object} args Object with arguments, that have to be passed with the url. * @return {String} */ -LiveJournal.constructUrl = function( base, args ) { +LiveJournal.constructUrl = function( base, args, escapeArgs ) { var queryStr = base + ( base.indexOf( '?' ) === -1 ? '?' : '&' ), queryArr = []; for( var i in args ) { - queryArr.push( i + '=' + args[i] ); + queryArr.push( i + '=' + ( ( escapeArgs ) ? encodeURIComponent( args[i] ) : args[i] ) ); } return queryStr + queryArr.join( '&' ); @@ -267,3 +267,32 @@ } }, 'json'); } + +LiveJournal.parseLikeButtons = function() { + try { + FB.XFBML.parse(); + } catch(e) {}; + + try { + gapi.plusone.go(); + } catch(e) {}; + + jQuery( 'a.twitter-share-button' ).each( function() { + if( this.href != 'http://twitter.com/share' ) { return; } + + 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' ) + }; + + link.replaceWith( jQuery( '<iframe frameborder="0" scrolling="no" allowtransparency="true" />' ) + .css( { + width: "110px", + height: "20px" } ) + .attr( 'src', LiveJournal.constructUrl( 'http://platform.twitter.com/widgets/tweet_button.html', params ) ) + .insertBefore( link ) ); + } ); +}