From 1e15133b9bb80d95d06cb654b935cb03b7aeb528 Mon Sep 17 00:00:00 2001 From: Markerov Date: Mon, 5 Jan 2015 13:27:39 +0800 Subject: [PATCH] Fetch external posts with json --- js/post-hover.js | 189 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 150 insertions(+), 39 deletions(-) diff --git a/js/post-hover.js b/js/post-hover.js index 94841cc5..fe99ccb9 100644 --- a/js/post-hover.js +++ b/js/post-hover.js @@ -14,16 +14,16 @@ */ onready(function(){ - var dont_fetch_again = []; + init_hover = function() { var link = $(this); - + var id; var matches; - if (link.is('[data-thread]')) { - id = link.attr('data-thread'); - } + if (link.is('[data-thread]')) { + id = link.attr('data-thread'); + } else if(matches = link.text().match(/^>>(?:>\/([^\/]+)\/)?(\d+)$/)) { id = matches[2]; } @@ -50,8 +50,7 @@ onready(function(){ var hovering = false; link.hover(function(e) { hovering = true; - - var start_hover = function(link) { + var start_hover = function(link) { if(post.is(':visible') && post.offset().top >= $(window).scrollTop() && post.offset().top + post.height() <= $(window).scrollTop() + $(window).height()) { @@ -75,7 +74,7 @@ onready(function(){ .css('left', '0') .css('margin-left', '') .addClass('reply').addClass('post') - .appendTo(link.closest('div.post')) + .appendTo(link.closest('div.post')); // shrink expanded images newPost.find('div.file a[data-expanded="true"]').each(function() { @@ -137,47 +136,124 @@ onready(function(){ } }; + post = $('[data-board="' + board + '"] div.post#reply_' + id + ', [data-board="' + board + '"]div#thread_' + id); if(post.length > 0) { start_hover($(this)); } else { - var url = link.attr('href').replace(/#.*$/, ''); - - if($.inArray(url, dont_fetch_again) != -1) { - return; - } - dont_fetch_again.push(url); - - $.ajax({ - url: url, - context: document.body, - success: function(data) { - var mythreadid = $(data).find('div[id^="thread_"]').attr('id').replace("thread_", ""); + var url = link.attr('href').replace(/#.*$/, '').replace('.html', '.json'); + var dataPromise = getPost(id, url); - if (mythreadid == threadid && parentboard == board) { - $(data).find('div.post.reply').each(function() { - if($('[data-board="' + board + '"] #' + $(this).attr('id')).length == 0) { - $('[data-board="' + board + '"]#thread_' + threadid + " .post.reply:first").before($(this).hide().addClass('hidden')); - } + dataPromise.done(function (data) { + // reconstruct post from json response + var file_array = []; + var multifile = false; + + var add_info = function (data) { + var file = { + 'thumb_h': data.tn_h, + 'thumb_w': data.tn_w, + 'fsize': data.fsize, + 'filename': data.filename, + 'ext': data.ext, + 'tim': data.tim + }; + + if ('h' in data) { + file.isImage = true; //(or video) + file.h = data.h; + file.w = data.w; + } else { + file.isImage = false; + } + // since response doens't indicate spoilered files, + // we'll just make do by assuming any image with 128*128px thumbnail is spoilered. + // which is probably 99% of the cases anyway. + file.isSpoiler = (data.tn_h == 128 && data.tn_w == 128); + + file_array.push(file); + }; + + var bytesToSize = function (bytes) { + var sizes = ['Bytes', 'KB', 'MB']; + var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); + + return (i === 0) ? bytes +' '+ sizes[i] : (bytes / Math.pow(1024, i)).toFixed(2) +' ' +sizes[i]; + }; + + // in case no subject + if (!data.sub) data.sub = ''; + + var $post = $('