Advertisement
Guest User

Untitled

a guest
Dec 19th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Post = {
  2.   posts: new Hash(),
  3.   blacklist_options: {replace: true},  
  4.  
  5.   register: function(post) {
  6.     post.tags = post.tags.match(/\S+/g)
  7.    
  8.     if (!post.tags)
  9.       post.tags = []
  10.    
  11.     post.match_tags = post.tags.clone()
  12.     post.match_tags.push("rating:" + post.rating.charAt(0))
  13.     post.match_tags.push("status:" + post.status)
  14.     post.match_tags.push("user:" + post.author.toLowerCase())
  15.     post.match_tags.push("id:"+post.id)
  16.     post.match_tags.push("type:"+post.file_ext)
  17.    
  18.     // These won't actually work for blacklisting until it's rewritten to take into account comparison/ranges (score:<0)
  19.     post.match_tags.push("score:"+post.score)
  20.     post.match_tags.push("width:"+post.width)
  21.     post.match_tags.push("height:"+post.height)
  22.    
  23.     this.posts.set(post.id, post)
  24.   },
  25.  
  26.   blacklists: [],
  27.  
  28.   is_blacklisted: function(post_id) {  // you can't have side effects like ++b.hits in a method called "is_blacklisted", ffs
  29.     var post = this.posts.get(post_id)
  30.     var has_tag = post.match_tags.member.bind(post.match_tags)
  31.     return Post.blacklists.any(function(b) {
  32.       return (b.require.all(has_tag) && !b.exclude.any(has_tag))
  33.     })
  34.   },
  35.  
  36.   apply_blacklists: function() {
  37.     Post.blacklists.each(function(b) { b.hits = 0 })
  38.    
  39.     var count = 0
  40.    
  41.     Post.posts.each(function(pair) {
  42.       var thumbs = $$("#p" + pair.key)
  43.      
  44.       if (thumbs.length == 0)
  45.         return;
  46.      
  47.       var post = pair.value
  48.       var has_tag = post.match_tags.member.bind(post.match_tags)
  49.       post.blacklisted = []
  50.      
  51.       Post.blacklists.each(function(b) {
  52.         if (b.require.all(has_tag) && !b.exclude.any(has_tag)) {
  53.           b.hits++
  54.          
  55.           if (!b.disabled)
  56.             post.blacklisted.push(b)
  57.         }
  58.       })
  59.      
  60.       bld = post.blacklisted.length > 0
  61.       count += bld
  62.      
  63.       thumbs.each(function(thumb) {
  64.         if (Post.blacklist_options.replace) {
  65.           var img = thumb.down('img')
  66.           var isavatar = (img.parentNode.parentNode.className.indexOf("thumb_avatar") != -1)
  67.          
  68.           if (bld && ((isavatar && (Cookie.get("blacklist_avatars") == "true")) || !isavatar)) {
  69.             img.src   = "/images/blacklisted-preview.png"
  70.             img.setAttribute('data-original', "/images/blacklisted-preview.png");
  71.             img.width = img.height = 150
  72.           }
  73.           else {
  74.             img.setAttribute('data-original', post.preview_url);
  75.             img.src    = post.preview_url
  76.             img.width  = post.preview_width
  77.             img.height = post.preview_height
  78.           }
  79.          
  80.           thumb.removeClassName('blacklisted');
  81.         }
  82.         else {
  83.           if (bld)
  84.             thumb.addClassName('blacklisted');
  85.           else
  86.             thumb.removeClassName('blacklisted');
  87.         }
  88.       });
  89.     })
  90.    
  91.     if (Post.countText)
  92.       $j('#blacklist-count').html(count);
  93.    
  94.     return count
  95.   },
  96.  
  97.   init_blacklisted: function (options) {
  98.     Post.blacklisted=[];
  99.     Post.blacklists=[];
  100.     Post.blacklist_options = Object.extend(Post.blacklist_options, options);
  101.     var bl_entries = Cookie.get("blacklisted_tags").split(/[&,]/);
  102.    
  103.     bl_entries.each(function (val) {
  104.       var s = Cookie.unescape(val).replace(/(rating:[qes])\w+/, "$1");
  105.       var tags = s.match(/\S+/g);
  106.      
  107.       if (!tags)
  108.         return;
  109.      
  110.       var b = {tags: tags, require: [], exclude: [], disabled: false, hits: 0};
  111.      
  112.       tags.each(function (tag) {
  113.         Post.blacklisted_user(tag);
  114.        
  115.         if (tag.charAt(0) == "-")
  116.           b.exclude.push(tag.slice(1));
  117.         else
  118.           b.require.push(tag);
  119.       });
  120.      
  121.       Post.blacklists.push(b);});
  122.    
  123.     var sidebar = $("blacklisted-sidebar");
  124.    
  125.     if (!sidebar) {
  126.       Post.apply_blacklists();
  127.       return;
  128.     };
  129.    
  130.     var blacklist_count = $("blacklist-count");
  131.    
  132.     if (blacklist_count && !Post.countText)
  133.       Post.countText = blacklist_count.appendChild(document.createTextNode(""));
  134.    
  135.     if (!Post.apply_blacklists()) {
  136.       sidebar.hide();
  137.       return;
  138.     }
  139.    
  140.     sidebar.show();
  141.     sidebar.observe("mousedown", function (event) {event.stop();});
  142.     var list = $("blacklisted-list");
  143.    
  144.     while (list.childNodes.length>1) {
  145.       list.removeChild(list.childNodes[1]);
  146.     }
  147.    
  148.     Post.blacklists.each(function (b) {
  149.       if (!b.hits)
  150.         return;
  151.      
  152.       var li = list.appendChild(document.createElement("li"));
  153.       li.className = "blacklisted-tags";
  154.       var a = li.appendChild(document.createElement("a"));
  155.       a.href = "#";
  156.       var expand = a.appendChild(document.createTextNode("\xBB"));
  157.      
  158.       a.observe("click", function (event) {
  159.         b.disabled = !b.disabled;
  160.         a.className = b.disabled ? "blacklisted-tags-disabled" : "blacklisted-tags";
  161.         Post.apply_blacklists();
  162.         event.stop();
  163.       });
  164.      
  165.       a.appendChild(document.createTextNode(" "));
  166.       var tags = a.appendChild(document.createTextNode(b.tags.join(" ")));
  167.       li.appendChild(document.createTextNode(" "));
  168.       var span = li.appendChild(document.createElement("span"));
  169.       span.className = "post-count";
  170.       span.appendChild(document.createTextNode(b.hits));
  171.     });
  172.   },
  173.  
  174.   blacklisted_user: function(tag) {
  175.     if (Cookie.get("blacklist_users") == "true") {
  176.       var pattern=/user:/i;
  177.       if (pattern.test(tag)) {
  178.         var username = tag.replace(pattern, "").toLowerCase();
  179.         jQuery("div[data-author='" + username +"'], tr[data-author='" + username +"']").each(function(){
  180.           jQuery(this).hide();
  181.         });
  182.       }
  183.     }
  184.   }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement