Add controls for copying commentary from source or another post.

This commit is contained in:
evazion
2016-12-21 03:36:05 -06:00
parent be379cc4a2
commit 68744bc24a
2 changed files with 95 additions and 0 deletions

View File

@@ -53,6 +53,91 @@
e.preventDefault();
$("#add-commentary-dialog").dialog("open");
});
$('#fetch-commentary select[name="commentary_source_type"]').change(function() {
$("#fetch-commentary input").toggle();
});
$('#fetch-commentary button[type="submit"]').click(Danbooru.ArtistCommentary.fetch_commentary);
}
Danbooru.ArtistCommentary.fetch_commentary = function() {
Danbooru.notice("Fetching artist commentary...");
var type = $('#fetch-commentary select[name="commentary_source_type"]').val();
if (type === "Source") {
var source = $('#fetch-commentary input[name="commentary_source"]').val();
var commentary = Danbooru.ArtistCommentary.from_source(source);
} else if (type === "Post") {
var id = $('#fetch-commentary input[name="commentary_post_id"]').val();
var commentary = Danbooru.ArtistCommentary.from_post_id(id);
}
commentary.then(Danbooru.ArtistCommentary.fill_commentary).done(function (success) {
var message = success ? "Artist commentary copied." : "Artist commentary copied; conflicting fields ignored.";
Danbooru.notice(message);
}).fail(function () {
Danbooru.notice("Fetching artist commentary failed.");
});
return false;
};
Danbooru.ArtistCommentary.from_source = function(source) {
return $.get("/source.json?url=" + encodeURIComponent(source)).then(function(data) {
return {
original_title: data.artist_commentary.title,
original_description: data.artist_commentary.description,
source: source,
};
});
};
Danbooru.ArtistCommentary.from_post_id = function(post_id) {
return $.get("/posts/" + encodeURIComponent(post_id) + "/artist_commentary.json").then(function(commentary) {
commentary.source = "post #" + post_id;
return commentary;
});
};
Danbooru.ArtistCommentary.fill_commentary = function(commentary) {
var description = Danbooru.ArtistCommentary.merge_commentaries($("#artist_commentary_original_description").val().trim(), commentary);
$("#artist_commentary_original_description").val(description);
// Update the other fields if they're blank. Return success if none conflict.
return [
Danbooru.ArtistCommentary.update_field($("#artist_commentary_original_title"), commentary.original_title),
Danbooru.ArtistCommentary.update_field($("#artist_commentary_translated_title"), commentary.translated_title),
Danbooru.ArtistCommentary.update_field($("#artist_commentary_translated_description"), commentary.translated_description),
].every(function (i) { return i; });
};
// If the new description conflicts with the current description, merge them
// by appending the new description onto the old one.
Danbooru.ArtistCommentary.merge_commentaries = function(description, commentary) {
if ((commentary.original_description && description) &&
(commentary.original_description != description)) {
return description
+ "\n\n[tn]\nSource: " + $("#post_source").val() + "\n[/tn]"
+ "\n\nh6. " + (commentary.original_title || "Untitled")
+ "\n\n" + commentary.original_description
+ "\n\n[tn]\nSource: " + commentary.source + "\n[/tn]";
} else {
return commentary.original_description || description;
}
};
// Update commentary field if it's blank, signal an error if there's a conflict.
Danbooru.ArtistCommentary.update_field = function($field, value) {
if ($field.val().trim() === "") {
$field.val(value);
return true;
} else if ($field.val().trim() !== value) {
$field.effect("shake", { direction: "up", distance: 5 });
return false;
} else {
return true;
}
}
})();

View File

@@ -1,5 +1,15 @@
<p>If the artist of this image posted some interesting additional information about this work, you can copy it here. <%= link_to "View help.", wiki_pages_path(:search => {:title => "help:artist_commentary"}) %></p>
<form id="fetch-commentary" class="simple_form">
<div class="input">
<label>Copy from</label>
<%= select_tag :commentary_source_type, options_for_select(%w[Source Post]) %>
<%= text_field_tag :commentary_source, @post.source %>
<%= text_field_tag :commentary_post_id, (@post.parent.try(&:id) || @post.children.first.try(&:id)), :style => "display: none;" %>
<%= button_tag "Fetch" %>
</div>
</form>
<%= form_tag(create_or_update_artist_commentaries_path(:format => :js), :remote => true, :class => "simple_form", :method => :put) do %>
<%= hidden_field :artist_commentary, :post_id, :value => @post.id %>