Advertisement
Guest User

Untitled

a guest
Jan 4th, 2015
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $('div.body a:not([rel="nofollow"])').off()
  2.  
  3. var getPost = (function () {
  4.         var cache = {};
  5.  
  6.         return function (targetId, url) {
  7.             var deferred = $.Deferred();
  8.             var data, post;
  9.  
  10.             var findPost = function (targetId, data) {
  11.                 var arr = data.posts;
  12.                 for (var i=0; i<arr.length; i++) {
  13.                     if (arr[i].no == targetId)
  14.                         return arr[i];
  15.                 }
  16.                 return false;
  17.             };
  18.             var get = function (targetId, url) {
  19.                 $.ajax({
  20.                     url: url,
  21.                     success: function (response) {
  22.                         cache[url] = response;
  23.                         var post = findPost(targetId, response);
  24.                         deferred.resolve(post);
  25.                     }
  26.                 });
  27.             };
  28.  
  29.             //  check for cached response and check if it's stale
  30.             if ((data = cache[url]) !== undefined && (post = findPost(targetId, data))) {
  31.                 deferred.resolve(post);
  32.             } else {
  33.                 get(targetId, url);
  34.             }
  35.  
  36.             return deferred.promise();
  37.         };
  38.     })();
  39.  
  40.     init_hover = function() {
  41.         var link = $(this);
  42.  
  43.         var id;
  44.         var matches;
  45.  
  46.         if (link.is('[data-thread]')) {
  47.                 id = link.attr('data-thread');
  48.         }
  49.         else if(matches = link.text().match(/^>>(?:>\/([^\/]+)\/)?(\d+)$/)) {
  50.             id = matches[2];
  51.         }
  52.         else {
  53.             return;
  54.         }
  55.        
  56.         var board = $(this);
  57.         while (board.data('board') === undefined) {
  58.             board = board.parent();
  59.         }
  60.         var threadid;
  61.         if (link.is('[data-thread]')) threadid = 0;
  62.         else threadid = board.attr('id').replace("thread_", "");
  63.  
  64.         board = board.data('board');
  65.  
  66.         var parentboard = board;
  67.        
  68.         if (link.is('[data-thread]')) parentboard = $('form[name="post"] input[name="board"]').val();
  69.         else if (matches[1] !== undefined) board = matches[1];
  70.  
  71.         var post = false;
  72.         var hovering = false;
  73.         link.hover(function(e) {
  74.             hovering = true;
  75.             var start_hover = function(link) {
  76.                 if(post.is(':visible') &&
  77.                         post.offset().top >= $(window).scrollTop() &&
  78.                         post.offset().top + post.height() <= $(window).scrollTop() + $(window).height()) {
  79.                     // post is in view
  80.                     post.addClass('highlighted');
  81.                 } else {
  82.                     var newPost = post.clone();
  83.                     newPost.find('>.reply, >br').remove();
  84.                     newPost.find('a.post_anchor').remove();
  85.  
  86.                     newPost
  87.                         .attr('id', 'post-hover-' + id)
  88.                         .attr('data-board', board)
  89.                         .addClass('post-hover')
  90.                         .css('border-style', 'solid')
  91.                         .css('box-shadow', '1px 1px 1px #999')
  92.                         .css('display', 'block')
  93.                         .css('position', 'absolute')
  94.                         .css('font-style', 'normal')
  95.                         .css('z-index', '100')
  96.                         .css('left', '0')
  97.                         .css('margin-left', '')
  98.                         .addClass('reply').addClass('post')
  99.                         .appendTo(link.closest('div.post'));
  100.                        
  101.                     // shrink expanded images
  102.                     newPost.find('div.file a[data-expanded="true"]').each(function() {
  103.                         var thumb = $(this).data('src');
  104.                         $(this).find('img.post-image').attr('src', thumb);
  105.                     });
  106.                    
  107.                     // Highlight references to the current post
  108.                     if (link.hasClass('mentioned-'+id)) {
  109.                         var postLinks = newPost.find('div.body a:not([rel="nofollow"])');
  110.                         if (postLinks.length > 1) {
  111.                             var originalPost = link.closest('div.post').attr('id').replace("reply_", "").replace("inline_", "");
  112.                             postLinks.each(function() {
  113.                                 if ($(this).text() == ">>"+originalPost) {
  114.                                     $(this).addClass('dashed-underline');
  115.                                 }
  116.                             });
  117.                         }
  118.                     }
  119.                    
  120.                     var previewWidth = newPost.outerWidth(true);
  121.                     var widthDiff = previewWidth - newPost.width();
  122.                     var linkLeft = link.offset().left;
  123.                     var left, top;
  124.                    
  125.                     if (linkLeft < $(document).width() * 0.7) {
  126.                         left = linkLeft + link.width();
  127.                         if (left + previewWidth > $(window).width()) {
  128.                             newPost.css('width', $(window).width() - left - widthDiff);
  129.                         }
  130.                     } else {
  131.                         if (previewWidth > linkLeft) {
  132.                             newPost.css('width', linkLeft - widthDiff);
  133.                             previewWidth = linkLeft;
  134.                         }
  135.                         left = linkLeft - previewWidth;
  136.                     }
  137.                     newPost.css('left', left);
  138.                    
  139.                     top = link.offset().top - 10;
  140.                    
  141.                     var scrollTop = $(window).scrollTop();
  142.                     if (link.is("[data-thread]")) {
  143.                         scrollTop = 0;
  144.                         top -= $(window).scrollTop();  
  145.                     }
  146.                    
  147.                     if(top < scrollTop + 15) {
  148.                         top = scrollTop;
  149.                     } else if(top > scrollTop + $(window).height() - newPost.height() - 15) {
  150.                         top = scrollTop + $(window).height() - newPost.height() - 15;
  151.                     }
  152.                    
  153.                     if (newPost.height() > $(window).height()) {
  154.                         top = scrollTop;
  155.                     }
  156.                    
  157.                     newPost.css('top', top);
  158.                 }
  159.             };
  160.            
  161.            
  162.             post = $('[data-board="' + board + '"] div.post#reply_' + id + ', [data-board="' + board + '"]div#thread_' + id);
  163.             if(post.length > 0) {
  164.                 start_hover($(this));
  165.             } else {
  166.                 var url = link.attr('href').replace(/#.*$/, '').replace('.html', '.json');
  167.                 var dataPromise = getPost(id, url);
  168.  
  169.                 dataPromise.done(function (data) {
  170.                     //  reconstruct post from json response
  171.                     var file_array = [];
  172.                     var multifile = false;
  173.  
  174.                     var add_info = function (data) {
  175.                         var file = {
  176.                             'thumb_h': data.tn_h,
  177.                             'thumb_w': data.tn_w,
  178.                             'fsize': data.fsize,
  179.                             'filename': data.filename,
  180.                             'ext': data.ext,
  181.                             'tim': data.tim
  182.                         };
  183.  
  184.                         if ('h' in data) {
  185.                             file.isImage = true; //(or video)
  186.                             file.h = data.h;
  187.                             file.w = data.w;
  188.                         } else {
  189.                             file.isImage = false;
  190.                         }
  191.                         // since response doens't indicate spoilered files,
  192.                         // just make do by assuming any image with 128*128px thumbnail is spoilered. which is probably 99% of the cases anyway.
  193.                         file.isSpoiler = (data.tn_h == 128 && data.tn_w == 128);
  194.  
  195.                         file_array.push(file);
  196.                     };
  197.  
  198.                     var bytesToSize = function (bytes) {
  199.                         var sizes = ['Bytes', 'KB', 'MB'];
  200.                         var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  201.  
  202.                         return (i === 0) ? bytes +' '+ sizes[i] : (bytes / Math.pow(1024, i)).toFixed(2) +' ' +sizes[i];
  203.                     };
  204.  
  205.                     //  in case no subject
  206.                     if (!data.sub) data.sub = '';
  207.  
  208.                     var $post = $('<div class="post reply hidden" id="reply_'+ data.no +'">')
  209.                                 .append($('<p class="intro"></p>')
  210.                                     .append('<span class="subject">'+ data.sub +'</span>')
  211.                                     .append('<span class="name">'+ data.name +'</span> ')
  212.                                     .append('<a class="post_no">No.'+ data.no +'</a>')
  213.                                 )
  214.                                 .append($('<div class="body"></div>')
  215.                                     .html(data.com)
  216.                                 )
  217.                                 .css('display', 'none');
  218.  
  219.                     if ('filename' in data) {
  220.                         var $files = $('<div class="files">');
  221.  
  222.                         add_info(data);
  223.                         if ('extra_files' in data) {
  224.                             multifile = true;
  225.                             $.each(data.extra_files, function () {
  226.                                 add_info(this);
  227.                             });
  228.                         }
  229.  
  230.                         $.each(file_array, function () {
  231.                             var thumb_url;
  232.  
  233.                             if (this.isImage && !this.isSpoiler) {
  234.                                 // video files uses jpg for thumbnail
  235.                                 if (this.ext === '.webm' || this.ext === '.mp4') this.ext = '.jpg';
  236.                                 thumb_url = '/'+ board +'/thumb/' + this.tim + this.ext;
  237.                             } else {
  238.                                 thumb_url = (this.isSpoiler) ? '/static/spoiler.png' : '/static/file.png';
  239.                             }
  240.                             // file infos
  241.                             var $ele = $('<div class="file">')
  242.                                         .append($('<p class="fileinfo">')
  243.                                             .append('<span>File: </span>')
  244.                                             .append('<a>'+ this.filename + this.ext +'</a>')
  245.                                             .append('<span class="unimportant"> ('+ bytesToSize(this.fsize) +', '+ this.w +'x'+ this.h +')</span>')
  246.                                         );
  247.                             //if (multifile) $ele.addClass('multifile').css('width', '168px');
  248.                             if (multifile) $ele.addClass('multifile');
  249.                             // image
  250.                             var $img = $('<img class="post-image">')
  251.                                                 .css('width', this.thumb_w)
  252.                                                 .css('height', this.thumb_h)
  253.                                                 .attr('src', thumb_url);
  254.  
  255.                             $ele.append($img);
  256.                             $files.append($ele);
  257.                         });
  258.                        
  259.                         $post.children('p.intro').after($files);
  260.                     }
  261.  
  262.  
  263.                     var mythreadid = (data.resto !== 0) ? data.resto : data.no;
  264.  
  265.                     if (mythreadid != threadid || parentboard != board) {
  266.                         // previewing post from external thread/board
  267.                         if ($('div#thread_'+ mythreadid +'[data-board="'+ board +'"]').length === 0) {
  268.                             $('form[name="postcontrols"]').prepend('<div class="thread" id="thread_'+ mythreadid +'" data-board="'+ board +'" style="display: none;"></div>');
  269.                         }
  270.                     }
  271.                     if ($('div#thread_'+ mythreadid +'[data-board="'+ board +'"]').children('#reply_'+ data.no).length === 0) {
  272.                         $('div#thread_'+ mythreadid +'[data-board="'+ board +'"]').prepend($post);
  273.                     }
  274.  
  275.                     post = $('[data-board="' + board + '"] div.post#reply_' + id + ', [data-board="' + board + '"]div#thread_' + id);
  276.                     if (hovering && post.length > 0) {
  277.                         start_hover(link);
  278.                     }
  279.                 });
  280.             }
  281.         }, function() {
  282.             hovering = false;
  283.             if(!post)
  284.                 return;
  285.            
  286.             post.removeClass('highlighted');
  287.             if(post.hasClass('hidden'))
  288.                 post.css('display', 'none');
  289.             $('.post-hover').remove();
  290.         });
  291.     };
  292.    
  293.     $('div.body a:not([rel="nofollow"])').each(init_hover);
  294.    
  295.     // allow to work with auto-reload.js, etc.
  296.     $(document).on('new_post', function(e, post) {
  297.         $(post).find('div.body a:not([rel="nofollow"])').each(init_hover);
  298.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement