samhuri.net/public/tweets/js/simplebird.min.js

2 lines
No EOL
5.4 KiB
JavaScript

/*! Simplebird 18-03-2013 */
function tmpl(e,t){return Templates[e]=Templates[e]||Hogan.compile($("#"+e).html()),Templates[e].render(t)}function init(){Grailbird={cur_page:0,payload_details:payload_details,user_details:user_details,tweet_index:tweet_index,data:{},date:function(){var e=this.tweet_index[this.cur_page],t=e.month||0,a=e.year;return CalendarMonths[t-1]+" "+a},tweet_count:function(){var e=this.tweet_index[this.cur_page];return e.tweet_count},tweet_history:function(){for(var e=[],t=0,a=Grailbird.tweet_index[0].year,n=1;0!=n;){var r=$.grep(Grailbird.tweet_index,function(e){return t=Math.max(t,e.tweet_count),e.calendar_month=CalendarMonths[e.month-1],e.year==a});if(r.reverse(),n=r.length,0==n)break;for(var o=r[0].month;r.length>0&&12>r.length;)1==o?r.push({year:a,tweet_count:0,month:r.length+1,calendar_month:CalendarMonths[r.length]}):(r.unshift({year:a,tweet_count:0,month:o,calendar_month:CalendarMonths[o]}),o+=1);e.push({year:a,months:r,max_tweet_count:t}),a--}return e},hasPrev:function(){return!!(this.cur_page<this.tweet_index.length-1)},hasNext:function(){return!(0==this.cur_page)}},$("header").html(tmpl("tmpl_header",Grailbird.user_details)),$(".newtweet").on("click",openTweetActionInWindow),refresh(),setTimeout(function(){$("#tweet_history").html(tmpl("tmpl_history",Grailbird)),drawTweetHistory(),refreshActiveHistory()},10)}function prev(){$("#prev").off("click"),loadPage(Math.min(Grailbird.tweet_index.length-1,Grailbird.cur_page+1))}function next(){$("#next").off("click"),loadPage(Math.max(0,Grailbird.cur_page-1))}function loadPage(e){$("#tweet_list").addClass("hidden"),scrollTo("#main",150),setTimeout(function(){Grailbird.cur_page=e,refresh()},150)}function openTweetActionInWindow(e){e.preventDefault(),window.open(e.target.getAttribute("href"),"","width=520,height=360,menubar=no,toolbar=no")}function toggleTweetHistory(e,t){e.preventDefault();var a=$("#main"),t=void 0===t?!a.hasClass("menu_open"):t;t?scrollTo("#main",150,function(){a.addClass("menu_open");var e=Math.min(parseInt($("#tweet_history").css("height")),parseInt($("#tweet_history").css("max-height")));$(".menu_open section").css({"-webkit-transform":"translateY("+e+"px)","-moz-transform":"translateY("+e+"px)","-ms-transform":"translateY("+e+"px)",transform:"translateY("+e+"px)"})}):($(".menu_open section").css({"-webkit-transform":"","-moz-transform":"","-ms-transform":"",transform:""}),a.removeClass("menu_open"))}function refresh(){$("nav").html(tmpl("tmpl_nav",Grailbird)),$("#prev").on("click",prev),$("#next").on("click",next),$("#toggle_history").on("click",toggleTweetHistory),refreshActiveHistory(),loadTweets(tweet_index[Grailbird.cur_page],function(){})}function drawTweetHistory(){var e=parseInt($("#tweet_history ol.tweet_months").data("max-tweet-count"));$("#tweet_history a .count_bar").each(function(t,a){var n=parseInt($(a).data("tweet-count"))/e;$(a).height(100-100*n+"%")}),$("#tweet_history .bar a").on("click",function(e){var t=$(e.target).parents(".bar").data("var-name");loadHistoryFromVarName(t)})}function refreshActiveHistory(){$("#tweet_history .active").removeClass("active").parents(".tweet_year").removeClass("active"),$("#tweet_history .bar[data-var-name="+Grailbird.tweet_index[Grailbird.cur_page].var_name+"]").addClass("active").parents(".tweet_year").addClass("active")}function loadHistoryFromVarName(e){if(e){var t;$.each(Grailbird.tweet_index,function(a,n){t||n.var_name!=e||(t=a)}),loadPage(t)}}function loadTweets(e){var t=Grailbird.data[e.var_name];t?drawTweets(t):$.getScript(e.file_name,function(){drawTweets(Grailbird.data[e.var_name])})}function drawTweets(e){var t,a="";dataset=e,$.each(e,function(e,n){t=n.retweeted_status||n,n.formatted_content=t.formatted_content=n.formatted_content||getFormattedTweetContent(t),n.formatted_date=t.formatted_date=n.formatted_date||getFormattedDate(t.created_at),n.retweeted_status&&(t.retweeter=n.user.screen_name),a+=tmpl("tmpl_tweet",t)}),setTimeout(function(){$("#tweet_list").html(a).removeClass("hidden"),$(".tweet_actions a").on("click",openTweetActionInWindow)},1)}function scrollTo(e,t,a){$("html, body").animate({scrollTop:$(e).offset().top},t,"swing",a)}function getFormattedTweetContent(e){var t=e.text,a=e.entities;return $.each(a.hashtags,function(e){t=t.replace(RegExp("#"+e.text,"g"),'<a href="http://twitter.com/search/'+escape(e.text)+'">#'+e.text+"</a>")}),$.each(a.urls,function(e,a){t=t.replace(RegExp(a.url,"g"),'<a title="'+a.expanded_url+'" href="'+a.expanded_url+'">'+a.display_url+"</a>")}),$.each(a.user_mentions,function(e,a){t=t.replace(RegExp("@"+a.screen_name,"g"),'<a class="atname" href="http://twitter.com/'+a.screen_name+'">'+a.screen_name+"</a>")}),t}function getFormattedDate(e){return new Date(e).toDateString()}(function(e){e.fn.noClickDelay=function(){var t=this,a=this,n=!1;0!="ontouchstart"in window&&t.bind("touchstart mousedown",function(r){r.preventDefault(),n=!1,a=e(r.target),3==a.nodeType&&(a=e(a.parent())),a.addClass("pressed"),t.bind("touchmove mousemove",function(){n=!0,a.removeClass("pressed")}),t.bind("touchend mouseup",function(){t.unbind("mousemove touchmove"),t.unbind("mouseup touchend"),!n&&a.length&&(a.removeClass("pressed"),a.trigger("click"))})})}})(jQuery);var Grailbird={},Templates={},CalendarMonths=["January","February","March","April","May","June","July","August","September","October","November","December"];$.when($.getScript("data/js/payload_details.js"),$.getScript("data/js/user_details.js"),$.getScript("data/js/tweet_index.js")).then(init);