From ccbca1a0c4943a4ed0e9495e645f7231428257c4 Mon Sep 17 00:00:00 2001 From: albert Date: Tue, 13 Sep 2011 13:15:57 -0400 Subject: [PATCH] Fixes #32: Setting a default images size in the user profile does not seem to work --- app/assets/javascripts/notes.js | 89 +++++++---------- app/assets/javascripts/posts.js | 63 +++++++----- app/assets/stylesheets/application.css.scss | 4 + app/logical/pixiv_proxy.rb | 103 +++++++++----------- app/views/layouts/default.html.erb | 2 +- app/views/posts/show.html.erb | 2 + 6 files changed, 126 insertions(+), 137 deletions(-) diff --git a/app/assets/javascripts/notes.js b/app/assets/javascripts/notes.js index f4821a1fc..9341e1f8b 100644 --- a/app/assets/javascripts/notes.js +++ b/app/assets/javascripts/notes.js @@ -78,42 +78,54 @@ Danbooru.Note = { scale: function($note_box) { var $image = $("#image"); - var original_width = parseFloat($image.data("width")); - var original_height = parseFloat($image.data("height")); + var original_width = parseFloat($image.data("original-width")); var ratio = $image.width() / original_width; if (ratio < 1) { - var scaled_width = original_width * ratio; - var scaled_height = original_height * ratio; - var scaled_top = $note_box.offset().top * ratio; - var scaled_left = $note_box.offset().left * ratio; + var scaled_width = Math.round($note_box.width() * ratio); + var scaled_height = Math.round($note_box.height() * ratio); + var scaled_top = Math.round($note_box.position().top * ratio); + var scaled_left = Math.round($note_box.position().left * ratio); $note_box.css({ top: scaled_top, left: scaled_left, width: scaled_width, height: scaled_height }); + Danbooru.Note.Box.resize_inner_border($note_box); } }, + scale_all: function() { + $(".note-box").each(function(i, v) { + Danbooru.Note.Box.scale($(v)); + }); + }, + descale: function($note_box) { var $image = $("#image"); - var original_width = parseFloat($image.data("width")); - var original_height = parseFloat($image.data("height")); - var ratio = $image.width() / original_width; + var original_width = parseFloat($image.data("original-width")); + var ratio = original_width / $image.width(); - if (ratio < 1) { - var scaled_width = original_width * ratio; - var scaled_height = original_height * ratio; - var scaled_top = $note_box.offset().top * ratio; - var scaled_left = $note_box.offset().left * ratio; + if (ratio > 1) { + var scaled_width = Math.round($note_box.width() * ratio); + var scaled_height = Math.round($note_box.height() * ratio); + var scaled_top = Math.round($note_box.position().top * ratio); + var scaled_left = Math.round($note_box.position().left * ratio); $note_box.css({ top: scaled_top, left: scaled_left, width: scaled_width, height: scaled_height }); + Danbooru.Note.Box.resize_inner_border($note_box); } + }, + + descale_all: function() { + $(".note-box").each(function(i, v) { + Danbooru.Note.Box.descale($(v)); + }); } }, @@ -214,10 +226,12 @@ Danbooru.Note = { Danbooru.Note.Body.hide($note_body_inner.data("id")); }); - $note_body.click(function(e) { - var $note_body_inner = $(e.currentTarget); - Danbooru.Note.Edit.show($note_body_inner); - }) + if (Danbooru.meta("current-user-name") !== "Anonymous") { + $note_body.click(function(e) { + var $note_body_inner = $(e.currentTarget); + Danbooru.Note.Edit.show($note_body_inner); + }) + } } }, @@ -345,7 +359,6 @@ Danbooru.Note = { history: function() { var $this = $(this); var id = $this.data("id"); - console.log("this=%o id=%o", $this, id); if (id.match(/\d/)) { window.location.href = "/note_versions?search[note_id_eq]=" + id; } @@ -355,6 +368,7 @@ Danbooru.Note = { TranslationMode: { start: function() { + $("#original-file-link").click(); $("#note-container").click(Danbooru.Note.TranslationMode.create_note); $("#translate").one("click", Danbooru.Note.TranslationMode.stop).html("Click on image"); }, @@ -371,38 +385,12 @@ Danbooru.Note = { } }, - Image: { - resize: function() { - var $image = $("#image"); - var max_width = parseInt($("meta[name=max-image-width]").attr("content")); - var current_width = $image.width(); - var current_height = $image.height(); - if (current_width > max_width) { - var ratio = max_width / current_width; - $image.attr("ratio", ratio); - $image.width(current_width * ratio).height(current_height * ratio).attr("resized", "1"); - } - }, - - reset_size: function() { - var $image = $("#image"); - $image.width($image.data("width")).height($image.data("height")).attr("resized", "0"); - } - }, - id: "x", dragging: false, editing: false, timeouts: [], pending: {}, - scale: function() { - var $image = $("#image"); - if ($image.attr("ratio")) { - // TODO: implement - } - }, - add: function(id, x, y, w, h, text) { var $note_box = Danbooru.Note.Box.create(id); var $note_body = Danbooru.Note.Body.create(id); @@ -462,20 +450,9 @@ Danbooru.Note = { $(function() { if ($("#c-posts #a-show").size() > 0) { - Danbooru.Note.Image.resize(); $("#translate").one("click", Danbooru.Note.TranslationMode.start); $("#note-container").width($("#image").width()).height($("#image").height()); $(document).bind("keydown", "ctrl+n", Danbooru.Note.TranslationMode.start); - - $("#toggle-resize").click(function() { - var $image = $("#image"); - if ($image.attr("resized") === "1") { - Danbooru.Note.Image.reset_size(); - } else { - Danbooru.Note.Image.resize(); - } - }); - Danbooru.Note.load_all(); } }); diff --git a/app/assets/javascripts/posts.js b/app/assets/javascripts/posts.js index 2834cd770..793e9616d 100644 --- a/app/assets/javascripts/posts.js +++ b/app/assets/javascripts/posts.js @@ -7,33 +7,48 @@ this.initialize_post_sections(); this.initialize_wiki_page_excerpt(); this.initialize_post_image_resize_links(); + this.initialize_image_resize(); + } + + Danbooru.Post.initialize_image_resize = function() { + var default_image_size = Danbooru.meta("default-image-size"); + var original_width = parseInt($("#image").data("original-width")); + var medium_width = parseInt(Danbooru.meta("config-medium-width")); + var large_width = parseInt(Danbooru.meta("config-large-width")); + + console.log("original-width=%o medium-width=%o", original_width, medium_width); + + if ((default_image_size === "medium") && (original_width > medium_width)) { + $("#medium-file-link").trigger("click"); + } else if ((default_image_size === "large") && (original_width > large_width)) { + $("#large-file-link").trigger("click"); + } else { + $("#original-file-link").trigger("click"); + } + } + + Danbooru.Post.build_resize_function = function(size) { + return function(e) { + console.log("clicked " + size); + Danbooru.Note.Box.descale_all(); + var $link = $(e.target); + var $image = $("#image"); + $("#medium-file-link").removeClass("active"); + $("#large-file-link").removeClass("active"); + $("#original-file-link").removeClass("active"); + $link.addClass("active"); + $image.attr("src", $link.attr("href")); + $image.width($image.data(size + "-width")); + $image.height($image.data(size + "-height")); + Danbooru.Note.Box.scale_all(); + e.preventDefault(); + } } Danbooru.Post.initialize_post_image_resize_links = function() { - $("#medium-file-link").click(function(e) { - var $link = $(e.target); - var $image = $("#image"); - $image.attr("src", $link.attr("href")); - $image.width($image.data("medium-width")); - $image.height($image.data("medium-height")); - e.preventDefault(); - }); - $("#large-file-link").click(function(e) { - var $link = $(e.target); - var $image = $("#image"); - $image.attr("src", $link.attr("href")); - $image.width($image.data("large-width")); - $image.height($image.data("large-height")); - e.preventDefault(); - }); - $("#original-file-link").click(function(e) { - var $link = $(e.target); - var $image = $("#image"); - $image.attr("src", $link.attr("href")); - $image.width($image.data("original-width")); - $image.height($image.data("original-height")); - e.preventDefault(); - }); + $("#medium-file-link").click(Danbooru.Post.build_resize_function("medium")); + $("#large-file-link").click(Danbooru.Post.build_resize_function("large")); + $("#original-file-link").click(Danbooru.Post.build_resize_function("original")); } Danbooru.Post.initialize_wiki_page_excerpt = function() { diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss index 067ae2b7d..1f9deb973 100644 --- a/app/assets/stylesheets/application.css.scss +++ b/app/assets/stylesheets/application.css.scss @@ -87,6 +87,10 @@ a:active { text-decoration: none; } +a.active { + font-weight: bold; +} + abbr[title=required] { display: none; } diff --git a/app/logical/pixiv_proxy.rb b/app/logical/pixiv_proxy.rb index 5db0dcdc9..ba0657aee 100644 --- a/app/logical/pixiv_proxy.rb +++ b/app/logical/pixiv_proxy.rb @@ -1,4 +1,4 @@ -class PixivProxy +class PixivProxy < ActiveRecord::Base def self.is_pixiv?(url) url =~ /pixiv\.net/ end @@ -9,24 +9,47 @@ class PixivProxy get_single(url) elsif url =~ /member_illust\.php/ && url =~ /illust_id=/ get_single(url) - # elsif url =~ /member_illust\.php/ && url =~ /id=/ - # get_listing(url) - # elsif url =~ /member\.php/ && url =~ /id=/ - # get_profile(url) else {} end end - def self.get_profile(url) - url = URI.parse(url).request_uri - mech = create_mechanize - hash = {} - mech.get(url) do |page| - hash[:artist] = page.search("a.avatar_m").attr("title").value - hash[:listing_url] = "/member_illust.php?id=" + url[/id=(\d+)/, 1] + def self.get_profile_from_page(page) + links = page.search("div.front-subContent a").find_all do |node| + node["href"] =~ /member\.php/ + end + + if links.any? + profile_url = links[0]["href"] + children = links[0].children + artist = children[0]["alt"] + return [artist, profile_url] + else + return [] + end + end + + def self.get_image_url_from_page(page) + meta = page.search("meta[property=\"og:image\"]").first + if meta + meta.attr("content").sub(/_m\./, ".") + else + nil + end + end + + def self.get_jp_tags_from_page(page) + links = page.search("div.pedia li a").find_all do |node| + node["href"] =~ /tags\.php/ + end + + if links.any? + links.map do |node| + [node.inner_text, node.attr("href")] + end + else + [] end - hash end def self.get_single(url) @@ -34,58 +57,26 @@ class PixivProxy mech = create_mechanize hash = {} mech.get(url) do |page| - if page.search("a.avatar_m") - hash[:artist] = page.search("a.avatar_m").attr("title").value - hash[:image_url] = page.search("div.works_display/a/img").attr("src").value.sub("_m.", ".") - hash[:profile_url] = page.search("a.avatar_m").attr("href").value - hash[:jp_tags] = page.search("span#tags/a").map do |node| - [node.inner_text, node.attribute("href").to_s] - end.reject {|x| x[0].empty?} - else - hash[:artist] = "?" - hash[:image_url] = "?" - hash[:profile_url] = "?" - hash[:jp_tags] = [] - end + artist, profile_url = get_profile_from_page(page) + image_url = get_image_url_from_page(page) + jp_tags = get_jp_tags_from_page(page) + + hash[:artist] = artist + hash[:profile_url] = profile_url + hash[:image_url] = image_url + hash[:jp_tags] = jp_tags end hash end - def self.get_listing(url) - mech = create_mechanize - p = 1 - url = URI.parse(url).request_uri.sub(/&p=\d+/, "") + "&p=1" - more = true - images = [] - - while more - mech.get(url) do |page| - links = page.search("div#illust_c4/ul/li/a") - - if links.empty? - more = false - else - images += links.map do |node| - image_src = node.child.attribute("src").to_s - [image_src, image_src.sub("_s.", "."), node.attribute("href").to_s] - end - end - - p += 1 - url.sub!(/&p=\d+/, "&p=#{p}") - end - end - - images - end - def self.create_mechanize mech = Mechanize.new mech.get("http://www.pixiv.net") do |page| page.form_with(:action => "/login.php") do |form| - form.pixiv_id = "uroobnad" - form.pass = "uroobnad556" + form['mode'] = "login" + form['login_pixiv_id'] = "uroobnad" + form['pass'] = "uroobnad556" end.click_button end diff --git a/app/views/layouts/default.html.erb b/app/views/layouts/default.html.erb index aba793616..10e204a08 100644 --- a/app/views/layouts/default.html.erb +++ b/app/views/layouts/default.html.erb @@ -14,7 +14,7 @@ <% if flash[:notice] =~ /error/ %> <% end %> - + <%= auto_discovery_link_tag :atom, posts_path(:format => "atom", :tags => params[:tags]) %> <%= stylesheet_link_tag "application", :media => "screen" %> <%= javascript_include_tag "application" %> diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 708c0f54f..d7a810449 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -91,6 +91,8 @@ + + <% end %> <%= render "posts/partials/common/secondary_links" %>