//= require jquery //= require jquery_ujs //= require_tree . "use strict"; var _Lobsters = Class.extend({ curUser: null, storyDownvoteReasons: { <%= Vote::STORY_REASONS.map{|k,v| "#{k.inspect}: #{v.inspect}" }.join(", ") %> }, commentDownvoteReasons: { <%= Vote::COMMENT_REASONS.map{|k,v| "#{k.inspect}: #{v.inspect}" }.join(", ") %> }, upvote: function(voterEl) { Lobsters.vote("story", voterEl, 1); }, downvote: function(voterEl) { Lobsters._showDownvoteWhyAt("story", voterEl, function(k) { Lobsters.vote("story", voterEl, -1, k); }); }, upvoteComment: function(voterEl) { Lobsters.vote("comment", voterEl, 1); }, downvoteComment: function(voterEl) { Lobsters._showDownvoteWhyAt("comment", voterEl, function(k) { Lobsters.vote("comment", voterEl, -1, k); }); }, _showDownvoteWhyAt: function(thingType, voterEl, onChooseWhy) { if (!Lobsters.curUser) return Lobsters.bounceToLogin(); var li = $(voterEl).parents("li").first(); if (li.hasClass("downvoted")) { /* already upvoted, neutralize */ Lobsters.vote(thingType, voterEl, -1, null); return; } if ($("#downvote_why")) $("#downvote_why").remove(); if ($("#downvote_why_shadow")) $("#downvote_why_shadow").remove(); var sh = $("
"); $(voterEl).after(sh); sh.click(function() { $("#downvote_why_shadow").remove(); $("#downvote_why").remove(); }); var d = $("
"); var reasons; if (thingType == "comment") reasons = Lobsters.commentDownvoteReasons; else reasons = Lobsters.storyDownvoteReasons; $.each(reasons, function(k, v) { var a = $("" + v + ""); a.click(function() { $("#downvote_why").remove(); $("#downvote_why_shadow").remove(); if (k != "") onChooseWhy(k); return false; }); d.append(a); }); $(voterEl).after(d); d.position({ my: "left top", at: "left bottom", offset: "-2 -2", of: $(voterEl), collision: "none", }); }, vote: function(thingType, voterEl, point, reason) { if (!Lobsters.curUser) return Lobsters.bounceToLogin(); var li = $(voterEl).parents("li").first(); var scoreDiv = li.find("div.score").get(0); var score = parseInt(scoreDiv.innerHTML); var action = ""; if (li.hasClass("upvoted") && point > 0) { /* already upvoted, neutralize */ li.removeClass("upvoted"); score--; action = "unvote"; } else if (li.hasClass("downvoted") && point < 0) { /* already downvoted, neutralize */ li.removeClass("downvoted"); score++; action = "unvote"; } else if (point > 0) { if (li.hasClass("downvoted")) /* flip flop */ score++; li.removeClass("downvoted").addClass("upvoted"); score++; action = "upvote"; } else if (point < 0) { if (li.hasClass("upvoted")) /* flip flop */ score--; li.removeClass("upvoted").addClass("downvoted"); score--; action = "downvote"; } scoreDiv.innerHTML = score; if (action == "upvote" || action == "unvote") li.find(".reason").html(""); else if (action == "downvote" && thingType == "comment") li.find(".reason").html("(" + Lobsters.commentDownvoteReasons[reason].toLowerCase() + ")"); $.post("/" + (thingType == "story" ? "stories" : thingType + "s") + "/" + $(voterEl).parents("li").first().attr("data-shortid") + "/" + action, { reason: reason }); }, postComment: function(form) { $(form).parent().load($(form).attr("action"), $(form).serializeArray()); }, previewComment: function(form) { $(form).parent().load($(form).attr("action").replace(/(post|update)/, "preview"), $(form).serializeArray()); }, previewStory: function(form) { $("#inside").load("/stories/preview", $(form).serializeArray()); }, fetchURLTitle: function(button, url_field, title_field) { if (url_field.val() == "") return; var old_value = button.val(); button.prop("disabled", true); button.val("Fetching..."); $.post("/stories/fetch_url_title", { fetch_url: url_field.val(), }) .success(function(data) { if (data && data.title) title_field.val(data.title.substr(0, title_field.maxLength)); button.val(old_value); button.prop("disabled", false); }) .error(function() { button.val(old_value); button.prop("disabled", false); }); }, bounceToLogin: function() { document.location = "/login?return=" + encodeURIComponent(document.location); }, }); var Lobsters = new _Lobsters(); $(document).ready(function() { $("li.comment a.downvoter").click(function() { Lobsters.downvoteComment(this); return false; }); $("li.comment a.upvoter").click(function() { Lobsters.upvoteComment(this); return false; }); $("li.story a.downvoter").click(function() { Lobsters.downvote(this); return false; }); $("li.story a.upvoter").click(function() { Lobsters.upvote(this); return false; }); $("a.comment_replier").live("click", function() { if (!Lobsters.curUser) { Lobsters.bounceToLogin(); return false; } var box = $(this).parents("li.comment").first().find("div.comment_reply"). first(); box.html($("#comment_form").clone()); box.find("ol").remove(); box.find("button.comment-preview").after(" \n "); box.find("textarea").focus(); var el = $(""); box.find("form").append(el); return false; }); $("button.comment-cancel").live("click", function() { $(this).parents("div.comment_reply form").remove(); }); $("a.comment_editor").live("click", function() { var li = $(this).parents("li.comment").first(); li.load("/comments/" + $(li).attr("data-shortid") + "/edit", { "edit": 1 }); }); $("a.comment_deletor").live("click", function() { if (confirm("Are you sure you want to delete this comment?")) { var li = $(this).parents("li.comment").first(); $.post("/comments/" + $(li).attr("data-shortid") + "/delete", function(d) { $(li).replaceWith(d); }); } }); $("a.comment_undeletor").live("click", function() { if (confirm("Are you sure you want to undelete this comment?")) { var li = $(this).parents("li.comment").first(); $.post("/comments/" + $(li).attr("data-shortid") + "/undelete", function(d) { $(li).replaceWith(d); }); } }); $("#story_tags_a").select2({ formatSelection: function(what) { return what.id; } }); /* TODO: jquery says live() is slow and bad, what is supposed to be used? */ $("div.markdown_help_toggler .markdown_help_label").live("click", function() { $(this).parents("div.markdown_help_toggler").first(). children(".markdown_help").toggle(); }); $("button.comment-post").live("click", function() { Lobsters.postComment($(this).parents("form").first()); }); $("button.comment-preview").live("click", function() { Lobsters.previewComment($(this).parents("form").first()); }); });