Files
danbooru/app/javascript/src/javascripts/posts.js.erb

679 lines
20 KiB
Plaintext

/* global addthis */
import Utility from './utility'
import Hammer from 'hammerjs'
import RelatedTag from './related_tag.js.erb'
import Cookie from './cookie'
import Note from './notes'
import SavedSearch from './saved_searches'
let Post = {};
Post.pending_update_count = 0;
Post.initialize_all = function() {
if ($("#c-posts").length) {
this.initialize_shortcuts();
this.initialize_saved_searches();
}
if ($("#c-posts").length && $("#a-index").length) {
this.initialize_excerpt();
this.initialize_gestures();
}
if ($("#c-posts").length && $("#a-show").length) {
this.initialize_links();
this.initialize_post_relationship_previews();
this.initialize_favlist();
this.initialize_post_sections();
this.initialize_post_image_resize_links();
this.initialize_post_image_resize_to_window_link();
this.initialize_similar();
this.initialize_replace_image_dialog();
this.initialize_gestures();
if ((Utility.meta("always-resize-images") === "true") || (Utility.meta("viewport") && (window.screen.width <= 660))) {
$("#image-resize-to-window-link").click();
}
}
if ($("#image").length) {
this.initialize_edit_dialog();
}
$(window).on('danbooru:initialize_saved_seraches', () => {
Post.initialize_saved_searches();
});
}
Post.initialize_gestures = function() {
if (Utility.meta("disable-mobile-gestures") === "true") {
return;
}
var $body = $("body");
if ($body.data("hammer")) {
return;
}
if (!Utility.test_max_width(660)) {
return;
}
$("#image-container").css({overflow: "visible"});
var hasPrev = $("#a-show").length || $(".paginator a[rel~=prev]").length;
var hasNext = $("#a-index").length && $(".paginator a[rel~=next]").length;
var hammer = new Hammer($body[0], {touchAction: 'pan-y', recognizers: [[Hammer.Swipe, { threshold: 20, velocity: 0.4, direction: Hammer.DIRECTION_HORIZONTAL }]], inputClass: Hammer.TouchInput});
$body.data("hammer", hammer);
if (hasPrev) {
hammer.on("swiperight", function(e) {
$("body").css({"transition-timing-function": "ease", "transition-duration": "0.2s", "opacity": "0", "transform": "translateX(150%)"});
$.timeout(200).done(function() {Post.swipe_prev(e)});
});
}
if (hasNext) {
hammer.on("swipeleft", function(e) {
$("body").css({"transition-timing-function": "ease", "transition-duration": "0.2s", "opacity": "0", "transform": "translateX(-150%)"});
$.timeout(200).done(function() {Post.swipe_next(e)});
});
}
}
Post.initialize_edit_dialog = function() {
$("#open-edit-dialog").button().show().click(function(e) {
$(window).scrollTop($("#image").offset().top);
Post.open_edit_dialog();
e.preventDefault();
});
$("#toggle-related-tags-link").click(function(e) {
RelatedTag.toggle();
e.preventDefault();
});
}
Post.open_edit_dialog = function() {
if ($("#edit-dialog").length === 1) {
return;
}
$("#edit").show();
$("#comments").hide();
$("#share").hide();
$("#post-sections li").removeClass("active");
$("#post-edit-link").parent("li").addClass("active");
$("#related-tags-container").show();
var $tag_string = $("#post_tag_string,#upload_tag_string");
$("div.input").has($tag_string).prevAll().hide();
$("#open-edit-dialog").hide();
$("#toggle-related-tags-link").show().click();
var dialog = $("<div/>").attr("id", "edit-dialog");
$("#form").appendTo(dialog);
dialog.dialog({
title: "Edit tags",
width: $(window).width() * 0.6,
position: {
my: "right",
at: "right-20",
of: window
},
drag: function(e, ui) {
if (Utility.meta("enable-auto-complete") === "true") {
$tag_string.data("uiAutocomplete").close();
}
},
close: Post.close_edit_dialog
});
dialog.dialog("widget").draggable("option", "containment", "none");
var pin_button = $("<button/>").button({icons: {primary: "ui-icon-pin-w"}, label: "pin", text: false});
pin_button.css({width: "20px", height: "20px", position: "absolute", right: "28.4px"});
dialog.parent().children(".ui-dialog-titlebar").append(pin_button);
pin_button.click(function(e) {
var dialog_widget = $('.ui-dialog:has(#edit-dialog)');
var pos = dialog_widget.offset();
if (dialog_widget.css("position") === "absolute") {
pos.left -= $(window).scrollLeft();
pos.top -= $(window).scrollTop();
dialog_widget.offset(pos).css({ position: "fixed" });
dialog.dialog("option", "resize", function() { dialog_widget.css({ position: "fixed" }); });
pin_button.button("option", "icons", {primary: "ui-icon-pin-s"});
} else {
pos.left += $(window).scrollLeft();
pos.top += $(window).scrollTop();
dialog_widget.offset(pos).css({ position: "absolute" });
dialog.dialog("option", "resize", function() { /* do nothing */ });
pin_button.button("option", "icons", {primary: "ui-icon-pin-w"});
}
});
dialog.parent().mouseout(function(e) {
dialog.parent().css({"opacity": 0.6, "transition": "opacity .2s ease"});
}).mouseover(function(e) {
dialog.parent().css({"opacity": 1});
});
$tag_string.css({"resize": "none", "width": "100%"});
$tag_string.focus().selectEnd().height($tag_string[0].scrollHeight);
}
Post.close_edit_dialog = function(e, ui) {
$("#form").appendTo($("#c-posts #edit,#c-uploads #a-new"));
$("#edit-dialog").remove();
RelatedTag.show();
$("#toggle-related-tags-link").hide();
var $tag_string = $("#post_tag_string,#upload_tag_string");
$("div.input").has($tag_string).prevAll().show();
$("#open-edit-dialog").show();
$tag_string.css({"resize": "", "width": ""});
}
Post.initialize_similar = function() {
$("#similar-button").click(function(e) {
$.get("/iqdb_queries", {"url": $("#post_source").val()}).done(function(html) {$("#iqdb-similar").html(html).show()});
e.preventDefault();
});
}
Post.swipe_prev = function(e) {
if ($("#a-show").length) {
window.history.back();
} if ($(".paginator a[rel~=prev]").length) {
location.href = $("a[rel~=prev]").attr("href");
}
e.preventDefault();
}
Post.nav_prev = function(e) {
var href = "";
if ($("#search-seq-nav").length) {
href = $("#search-seq-nav a[rel~=prev]").attr("href");
if (href) {
location.href = href;
}
} else if ($(".paginator a[rel~=prev]").length) {
location.href = $("a[rel~=prev]").attr("href");
} else {
href = $("#pool-nav a.active[rel~=prev], #favgroup-nav a.active[rel~=prev]").attr("href");
if (href) {
location.href = href;
}
}
e.preventDefault();
}
Post.nav_next = function(e) {
var href = "";
if ($("#search-seq-nav").length) {
href = $("#search-seq-nav a[rel~=next]").attr("href");
location.href = href;
} else if ($(".paginator a[rel~=next]").length) {
location.href = $(".paginator a[rel~=next]").attr("href");
} else {
href = $("#pool-nav a.active[rel~=next], #favgroup-nav a.active[rel~=next]").attr("href");
if (href) {
location.href = href;
}
}
e.preventDefault();
}
Post.swipe_next = function(e) {
if ($(".paginator a[rel~=next ]").length) {
location.href = $(".paginator a[rel~=next]").attr("href");
}
e.preventDefault();
}
Post.initialize_shortcuts = function() {
if ($("#a-show").length) {
Utility.keydown("a", "prev_page", Post.nav_prev);
Utility.keydown("d", "next_page", Post.nav_next);
}
}
Post.initialize_links = function() {
$("#copy-notes").click(function(e) {
var current_post_id = $("meta[name=post-id]").attr("content");
var other_post_id = parseInt(prompt("Enter the ID of the post to copy all notes to:"), 10);
if (other_post_id !== null) {
$.ajax("/posts/" + current_post_id + "/copy_notes", {
type: "PUT",
data: {
other_post_id: other_post_id
},
success: function(data) {
$(window).trigger("danbooru:notice", "Successfully copied notes to <a href='" + other_post_id + "'>post #" + other_post_id + "</a>");
},
error: function(data) {
if (data.status === 404) {
$(window).trigger("danbooru:error", "Error: Invalid destination post");
} else if (data.responseJSON && data.responseJSON.reason) {
$(window).trigger("danbooru:error", "Error: " + data.responseJSON.reason);
} else {
$(window).trigger("danbooru:error", "There was an error copying notes to <a href='" + other_post_id + "'>post #" + other_post_id + "</a>");
}
}
});
}
e.preventDefault();
});
$(".unvote-post-link").hide();
}
Post.initialize_post_relationship_previews = function() {
var current_post_id = $("meta[name=post-id]").attr("content");
$("[id=post_" + current_post_id + "]").addClass("current-post");
if (Cookie.get("show-relationship-previews") === "0") {
this.toggle_relationship_preview($("#has-children-relationship-preview"), $("#has-children-relationship-preview-link"));
this.toggle_relationship_preview($("#has-parent-relationship-preview"), $("#has-parent-relationship-preview-link"));
}
$("#has-children-relationship-preview-link").click(function(e) {
Post.toggle_relationship_preview($("#has-children-relationship-preview"), $(this));
e.preventDefault();
});
$("#has-parent-relationship-preview-link").click(function(e) {
Post.toggle_relationship_preview($("#has-parent-relationship-preview"), $(this));
e.preventDefault();
});
}
Post.toggle_relationship_preview = function(preview, preview_link) {
preview.toggle();
if (preview.is(":visible")) {
preview_link.html("&laquo; hide");
Cookie.put("show-relationship-previews", "1");
} else {
preview_link.html("show &raquo;");
Cookie.put("show-relationship-previews", "0");
}
}
Post.initialize_favlist = function() {
$("#favlist").hide();
$("#hide-favlist-link").hide();
var fav_count = $("#show-favlist-link").prev().text();
if (fav_count === "0") {
$("#show-favlist-link").hide();
}
$("#show-favlist-link").click(function(e) {
$("#favlist").show();
$(this).hide();
$("#hide-favlist-link").show();
e.preventDefault();
});
$("#hide-favlist-link").click(function(e) {
$("#favlist").hide();
$(this).hide();
$("#show-favlist-link").show();
e.preventDefault();
});
}
Post.expand_image = function(e) {
if (Utility.test_max_width(660)) {
// just do the default behavior
return;
}
var $link = $(e.target);
var $image = $("#image");
var $notice = $("#image-resize-notice");
$image.attr("src", $link.attr("href"));
$image.css("opacity", "0.25");
$image.width($image.data("original-width"));
$image.height($image.data("original-height"));
$image.on("load", function() {
$image.css("opacity", "1");
$notice.hide();
});
$notice.children().eq(0).hide();
$notice.children().eq(1).show(); // Loading message
Note.Box.scale_all();
$image.data("scale-factor", 1);
e.preventDefault();
}
Post.initialize_post_image_resize_links = function() {
$("#image-resize-link").click(Post.expand_image);
if ($("#image-resize-notice").length) {
Utility.keydown("v", "resize", function(e) {
if ($("#image-resize-notice").is(":visible")) {
$("#image-resize-link").click();
} else {
var $image = $("#image");
var $notice = $("#image-resize-notice");
$image.attr("src", $("#image-container").data("large-file-url"));
$image.css("opacity", "0.25");
$image.width($image.data("large-width"));
$image.height($image.data("large-height"));
$notice.children().eq(0).show();
$notice.children().eq(1).hide(); // Loading message
$image.on("load", function() {
$image.css("opacity", "1");
$notice.show();
});
Note.Box.scale_all();
$image.data("scale-factor", 1);
e.preventDefault();
}
});
}
}
Post.resize_image_to_window = function($img) {
var sidebar_width = 0;
var client_width = 0;
if (($img.data("scale-factor") === 1) || ($img.data("scale-factor") === undefined)) {
if ($(window).width() > 660) {
sidebar_width = $("#sidebar").width() || 0;
client_width = $(window).width() - sidebar_width - 75;
} else {
client_width = $(window).width() - 2;
}
if ($img.width() > client_width) {
var ratio = client_width / $img.data("original-width");
$img.data("scale-factor", ratio);
$img.css("width", $img.data("original-width") * ratio);
$img.css("height", $img.data("original-height") * ratio);
Post.resize_ugoira_controls();
}
} else {
$img.data("scale-factor", 1);
$img.width($img.data("original-width"));
$img.height($img.data("original-height"));
Post.resize_ugoira_controls();
}
Note.Box.scale_all();
}
Post.initialize_post_image_resize_to_window_link = function() {
$("#image-resize-to-window-link").click(function(e) {
Post.resize_image_to_window($("#image"));
e.preventDefault();
});
}
Post.initialize_excerpt = function() {
$("#excerpt").hide();
$("#show-posts-link").click(function(e) {
$("#show-posts-link").parent("li").addClass("active");
$("#show-excerpt-link").parent("li").removeClass("active");
$("#posts").show();
$("#excerpt").hide();
e.preventDefault();
});
$("#show-excerpt-link").click(function(e) {
if ($(this).parent("li").hasClass("active")) {
return;
}
$("#show-posts-link").parent("li").removeClass("active");
$("#show-excerpt-link").parent("li").addClass("active");
$("#posts").hide();
$("#excerpt").show();
e.preventDefault();
});
if (!$(".post-preview").length && /Nobody here but us chickens/.test($("#posts").html())) {
$("#show-excerpt-link").click();
}
}
Post.initialize_post_sections = function() {
$("#post-sections li a").click(function(e) {
if (e.target.hash === "#comments") {
$("#comments").show();
$("#edit").hide();
$("#share").hide();
$("#recommended").hide();
} else if (e.target.hash === "#edit") {
$("#edit").show();
$("#comments").hide();
$("#share").hide();
$("#post_tag_string").focus().selectEnd().height($("#post_tag_string")[0].scrollHeight);
$("#related-tags-button").trigger("click");
$("#find-artist-button").trigger("click");
$("#recommended").hide();
} else if (e.target.hash === "#recommended") {
$("#comments").hide();
$("#edit").hide();
$("#share").hide();
$("#recommended").show();
$.get("/recommended_posts", {context: "post", post_id: Utility.meta("post-id")}, function(data) {
$("#recommended").html(data);
});
} else {
$("#edit").hide();
$("#comments").hide();
$("#share").show();
addthis.init();
$("#recommended").hide();
}
$("#post-sections li").removeClass("active");
$(e.target).parent("li").addClass("active");
e.preventDefault();
});
$("#notes").hide();
$("#edit").hide();
$("#recommended").hide();
$("#post-sections li:first-child").addClass("active");
}
Post.resize_ugoira_controls = function() {
var $img = $("#image");
var width = Math.max($img.width(), 350);
$("#ugoira-control-panel").css("width", width);
$("#seek-slider").css("width", width - 81);
}
Post.notice_update = function(x) {
if (x === "inc") {
Post.pending_update_count += 1;
$(window).trigger("danbooru:notice", "Updating posts (" + Post.pending_update_count + " pending)...", true);
} else {
Post.pending_update_count -= 1;
if (Post.pending_update_count < 1) {
$(window).trigger("danbooru:notice", "Posts updated");
} else {
$(window).trigger("danbooru:notice", "Updating posts (" + Post.pending_update_count + " pending)...", true);
}
}
}
Post.update_data = function(data) {
var $post = $("#post_" + data.id);
$post.attr("data-tags", data.tag_string);
$post.data("rating", data.rating);
$post.removeClass("post-status-has-parent post-status-has-children");
if (data.parent_id) {
$post.addClass("post-status-has-parent");
}
if (data.has_visible_children) {
$post.addClass("post-status-has-children");
}
}
Post.vote = function(score, id) {
$(window).trigger("danbooru:notice", "Voting...");
$.post("/posts/" + id + "/votes.js", {
score: score
});
}
Post.update = function(post_id, params) {
Post.notice_update("inc");
$.ajax({
type: "PUT",
url: "/posts/" + post_id + ".json",
data: params,
success: function(data) {
Post.notice_update("dec");
Post.update_data(data);
},
error: function(data) {
Post.notice_update("dec");
$(window).trigger("danbooru:error", 'There was an error updating <a href="/posts/' + post_id + '">post #' + post_id + '</a>');
}
});
}
Post.ban = function(post_id) {
$.ajax({
type: "POST",
url: "/moderator/post/posts/" + post_id + "/ban.js",
data: {
commit: "Ban"
},
success: function(data) {
$("#post_" + post_id).remove();
},
error: function(data) {
$(window).trigger("danbooru:error", 'There was an error updating <a href="/posts/' + post_id + '">post #' + post_id + '</a>');
}
});
}
Post.approve = function(post_id) {
$.post(
"/moderator/post/approval.json",
{"post_id": post_id}
).fail(function(data) {
var message = $.map(data.responseJSON.errors, function(msg, attr) { return msg; }).join("; ");
$(window).trigger("danbooru:error", "Error: " + message);
}).done(function(data) {
var $post = $("#post_" + post_id);
if ($post.length) {
$post.data("flags", $post.data("flags").replace(/pending/, ""));
$post.removeClass("post-status-pending");
$(window).trigger("danbooru:notice", "Approved post #" + post_id);
}
});
}
Post.initialize_saved_searches = function() {
$("#new_saved_search #saved_search_label_string").autocomplete({
search: function() {
$(this).data("ui-autocomplete").menu.bindings = $();
},
source: function(req, resp) {
SavedSearch.labels(req.term).then(function(labels) {
resp(labels.map(function(label) {
return {
label: label.replace(/_/g, " "),
value: label
};
}));
});
}
});
$("#save-search-dialog").dialog({
width: 500,
modal: true,
autoOpen: false,
buttons: {
"Submit": function() {
$("#save-search-dialog form").submit();
$(this).dialog("close");
},
"Cancel": function() {
$(this).dialog("close");
}
}
});
$("#save-search").click(function(e) {
$("#save-search-dialog #saved_search_query").val($("#tags").val());
if (Utility.meta("disable-labeled-saved-searches") === "false") {
$("#save-search-dialog").dialog("open");
} else {
$.post(
"/saved_searches.js",
{
"saved_search": {
"query": $("#tags").val()
}
}
);
}
e.preventDefault();
});
$("#search-dropdown #wiki-search").click(function(e) {
window.location.href = "/wiki_pages?search%5Btitle%5D=" + encodeURIComponent($("#tags").val());
e.preventDefault();
});
$("#search-dropdown #artist-search").click(function(e) {
window.location.href = "/artists?search%5Bname%5D=" + encodeURIComponent($("#tags").val());
e.preventDefault();
});
}
Post.initialize_replace_image_dialog = function() {
$("#replace-image-dialog").dialog({
autoOpen: false,
width: 700,
modal: true,
buttons: {
"Submit": function() {
$("#replace-image-dialog form").submit();
$(this).dialog("close");
},
"Cancel": function() {
$(this).dialog("close");
}
}
});
$('#replace-image-dialog form').submit(function() {
$('#replace-image-dialog').dialog('close');
});
$("#replace-image").click(function(e) {
e.preventDefault();
$("#replace-image-dialog").dialog("open");
});
};
$(document).ready(function() {
Post.initialize_all();
});
export default Post