From 63564921e5400ae007af0aeac3dd4984c6779b71 Mon Sep 17 00:00:00 2001 From: albert Date: Mon, 15 Aug 2011 11:30:14 -0400 Subject: [PATCH] js work --- app/assets/javascripts/favorites.js | 30 +++ app/assets/javascripts/post_mode_menu.js | 85 +++++++ app/assets/javascripts/posts.js | 231 ++++++-------------- app/assets/javascripts/tag_script.js | 56 +++++ app/assets/javascripts/utility.js | 4 + app/assets/stylesheets/application.css.scss | 66 ++++++ app/helpers/application_helper.rb | 2 +- 7 files changed, 305 insertions(+), 169 deletions(-) create mode 100644 app/assets/javascripts/post_mode_menu.js create mode 100644 app/assets/javascripts/tag_script.js diff --git a/app/assets/javascripts/favorites.js b/app/assets/javascripts/favorites.js index 3a88f0860..6441ff5cf 100644 --- a/app/assets/javascripts/favorites.js +++ b/app/assets/javascripts/favorites.js @@ -20,6 +20,36 @@ $("a#remove-from-favorites").hide(); } } + + Danbooru.Favorite.create = function(post_id) { + Danbooru.Post.notice_update("inc"); + + $.ajax({ + type: "POST", + url: "/favorites", + data: { + post_id: post_id + }, + complete: function() { + Danbooru.Post.notice_update("dec"); + }, + error: function(data, status, xhr) { + Danbooru.j_alert("Error: " + data.reason); + } + }); + } + + Danbooru.Favorite.destroy = function(post_id) { + Danbooru.Post.notice_update("inc"); + + $.ajax({ + type: "DELETE", + url: "/favorites/" + post_id, + complete: function() { + Danbooru.Post.notice_update("dec"); + } + }); + } })(); $(document).ready(function() { diff --git a/app/assets/javascripts/post_mode_menu.js b/app/assets/javascripts/post_mode_menu.js new file mode 100644 index 000000000..881eefb43 --- /dev/null +++ b/app/assets/javascripts/post_mode_menu.js @@ -0,0 +1,85 @@ +(function() { + Danbooru.PostModeMenu = {}; + + Danbooru.PostModeMenu.initialize = function() { + this.initialize_selector(); + this.initialize_preview_link(); + } + + Danbooru.PostModeMenu.initialize_selector = function() { + if (Danbooru.Cookie.get("mode") === "") { + Danbooru.Cookie.put("mode", "view"); + $("#mode-box select").val("view"); + } else { + $("#mode-box select").val(Danbooru.Cookie.get("mode")); + } + + $("#mode-box select").click(Danbooru.PostModeMenu.change); + } + + Danbooru.PostModeMenu.initialize_preview_link = function() { + $(".post-preview a").click(Danbooru.PostModeMenu.click); + } + + Danbooru.PostModeMenu.change = function() { + var s = $("#mode-box select").val(); + var $body = $(document.body); + $body.removeClass(); + $body.addClass("mode-" + s); + Danbooru.Cookie.put("mode", s, 7); + + if (s === "edit-tag-script") { + var script = Danbooru.Cookie.get("tag-script"); + script = prompt("Enter a tag script", script); + + if (script) { + Cookie.put("tag-script", script); + $("#mode-box select").val("apply-tag-script"); + } else { + $("#mode-box select").val("view"); + } + + this.change(); + } + } + + Danbooru.PostModeMenu.click = function(e) { + var s = $("#mode-box select").val(); + var post_id = $(e.target).closest("article").data("id"); + + if (s === "view") { + return; + } else if (s === "add-fav") { + Danbooru.Favorite.create(post_id); + } else if (s === "remove-fav") { + Danbooru.Favorite.destroy(post_id); + } else if (s === "edit") { + // TODO + } else if (s === 'vote-down') { + Danbooru.Post.vote("down", post_id); + } else if (s === 'vote-up') { + Danbooru.Post.vote("up", post_id); + } else if (s === 'rating-q') { + Danbooru.Post.update(post_id, {"post[rating]": "q"}); + } else if (s === 'rating-s') { + Danbooru.Post.update(post_id, {"post[rating]": "s"}); + } else if (s === 'rating-e') { + Danbooru.Post.update(post_id, {"post[rating]": "e"}); + } else if (s === 'lock-rating') { + Danbooru.Post.update(post_id, {"post[is_rating_locked]": "1"}); + } else if (s === 'lock-note') { + Danbooru.Post.update(post_id, {"post[is_note_locked]": "1"}); + } else if (s === 'add-to-pool') { + Pool.add_post(post_id, 0); + } else if (s === "apply-tag-script") { + var tag_script = Cookie.get("tag-script"); + TagScript.run(post_id, tag_script); + } + + e.preventDefault(); + } +})(); + +$(function() { + Danbooru.PostModeMenu.initialize(); +}); diff --git a/app/assets/javascripts/posts.js b/app/assets/javascripts/posts.js index a7235bafa..2f05527b5 100644 --- a/app/assets/javascripts/posts.js +++ b/app/assets/javascripts/posts.js @@ -1,170 +1,8 @@ -// PostModeMenu = { -// init: function() { -// this.original_background_color = $(document.body).css("background-color") -// -// if (Cookie.get("mode") == "") { -// Cookie.put("mode", "view"); -// $("#mode-box select").val("view"); -// } else { -// $("#mode-box select").val(Cookie.get("mode")); -// } -// -// // this.change(); -// }, -// -// change: function() { -// var s = $("#mode-box select").val(); -// Cookie.put("mode", s, 7); -// -// if (s == "view") { -// $(document.body).css({"background-color": this.original_background_color}); -// } else if (s == "edit") { -// $(document.body).css({"background-color": "#3A3"}); -// } else if (s == "add-fav") { -// $(document.body).css({"background-color": "#FFA"}); -// } else if (s == "remove-fav") { -// $(document.body).css({"background-color": "#FFA"}); -// } else if (s == "rating-q") { -// $(document.body).css({"background-color": "#AAA"}); -// } else if (s == "rating-s") { -// $(document.body).css({"background-color": "#6F6"}); -// } else if (s == "rating-e") { -// $(document.body).css({"background-color": "#F66"}); -// } else if (s == "vote-down") { -// $(document.body).css({"background-color": "#FAA"}); -// } else if (s == "vote-up") { -// $(document.body).css({"background-color": "#AFA"}); -// } else if (s == "lock-rating") { -// $(document.body).css({"background-color": "#AA3"}); -// } else if (s == "lock-note") { -// $(document.body).css({"background-color": "#3AA"}); -// } else if (s == "approve") { -// $(document.body).css({"background-color": "#26A"}); -// } else if (s == "unapprove") { -// $(document.body).css({"background-color": "#F66"}); -// } else if (s == "add-to-pool") { -// $(document.body).css({"background-color": "#26A"}); -// } else if (s == "apply-tag-script") { -// $(document.body).css({"background-color": "#A3A"}); -// } else if (s == "edit-tag-script") { -// $(document.body).css({"background-color": "#FFF"}); -// -// var script = Cookie.get("tag-script"); -// script = prompt("Enter a tag script", script); -// -// if (script) { -// Cookie.put("tag-script", script); -// $("#mode-box select").val("apply-tag-script"); -// } else { -// $("#mode-box select").val("view"); -// } -// -// this.change(); -// } else { -// $(document.body).css({"background-color": "#AFA"}); -// } -// }, -// -// click: function(post_id) { -// var s = $("#mode-box select").val(); -// -// if (s.value == "view") { -// return true; -// } else if (s.value == "add-fav") { -// Favorite.create(post_id); -// } else if (s.value == "remove-fav") { -// Favorite.destroy(post_id); -// } else if (s.value == "edit") { -// // TODO -// } else if (s.value == 'vote-down') { -// PostVote.create("down", post_id); -// } else if (s.value == 'vote-up') { -// PostVote.create("up", post_id); -// } else if (s.value == 'rating-q') { -// Post.update(post_id, {"post[rating]": "questionable"}); -// } else if (s.value == 'rating-s') { -// Post.update(post_id, {"post[rating]": "safe"}); -// } else if (s.value == 'rating-e') { -// Post.update(post_id, {"post[rating]": "explicit"}); -// } else if (s.value == 'lock-rating') { -// Post.update(post_id, {"post[is_rating_locked]": "1"}); -// } else if (s.value == 'lock-note') { -// Post.update(post_id, {"post[is_note_locked]": "1"}); -// } else if (s.value == 'unapprove') { -// Unapproval.create(post_id); -// } else if (s.value == "approve") { -// Post.update(post_id, {"post[is_pending]": "0"}); -// } else if (s.value == 'add-to-pool') { -// Pool.add_post(post_id, 0); -// } else if (s.value == "apply-tag-script") { -// var tag_script = Cookie.get("tag-script"); -// TagScript.run(post_id, tag_script); -// } -// -// return false; -// } -// } -// -// TagScript = { -// parse: function(script) { -// return script.match(/\[.+?\]|\S+/g); -// }, -// -// test: function(tags, predicate) { -// var split_pred = predicate.match(/\S+/g); -// var is_true = true; -// -// split_pred.each(function(x) { -// if (x[0] == "-") { -// if (tags.include(x.substr(1, 100))) { -// is_true = false; -// throw $break; -// } -// } else { -// if (!tags.include(x)) { -// is_true = false; -// throw $break; -// } -// } -// }) -// -// return is_true -// }, -// -// process: function(tags, command) { -// if (command.match(/^\[if/)) { -// var match = command.match(/\[if\s+(.+?)\s*,\s*(.+?)\]/) -// if (TagScript.test(tags, match[1])) { -// return TagScript.process(tags, match[2]); -// } else { -// return tags; -// } -// } else if (command == "[reset]") { -// return []; -// } else if (command[0] == "-") { -// return tags.reject(function(x) {return x == command.substr(1, 100)}) -// } else { -// tags.push(command) -// return tags; -// } -// }, -// -// run: function(post_id, tag_script) { -// var commands = TagScript.parse(tag_script); -// var post = Post.posts.get(post_id); -// var old_tags = post.tags.join(" "); -// -// commands.each(function(x) { -// post.tags = TagScript.process(post.tags, x); -// }) -// -// Post.update(post_id, {"post[old_tags]": old_tags, "post[tags]": post.tags.join(" ")}); -// } -// } - (function() { Danbooru.Post = {}; + Danbooru.Post.pending_update_count = 0; + Danbooru.Post.initialize_all = function() { this.initialize_post_sections(); this.initialize_wiki_page_excerpt(); @@ -192,7 +30,7 @@ Danbooru.Cookie.put("hide-wiki-page-excerpt", "0"); }); } - + Danbooru.Post.initialize_post_sections = function() { $("#post-sections li a").click(function(e) { $("#comments").hide(); @@ -209,11 +47,68 @@ $("#notes").hide(); $("#edit").hide(); } + + Danbooru.Post.notice_update = function(x) { + if (x === "inc") { + Danbooru.Post.pending_update_count += 1; + Danbooru.notice("Updating posts (" + Danbooru.Post.pending_update_count + " pending)..."); + } else { + Danbooru.Post.pending_update_count -= 1; + + if (Danbooru.Post.pending_update_count < 1) { + Danbooru.notice("Posts updated"); + } else { + Danbooru.notice("Updating posts (" + Post.pending_update_count + " pending)..."); + } + } + } + + Danbooru.Post.update_data = function(data) { + var $post = $("#post_" + data.id); + $post.data("tags", data.tags); + } + + Danbooru.Post.vote = function(score, id) { + Danbooru.Post.notice_update("inc"); + + $.ajax({ + type: "POST", + url: "/posts/" + id + "/votes", + data: { + score: score + }, + complete: function() { + Danbooru.Post.notice_update("dec"); + }, + success: function(data, status, xhr) { + $("post-score-" + data.post_id).html(data.score); + }, + error: function(data, status, xhr) { + Danbooru.notice("Error: " + data.reason); + } + }); + } + + Danbooru.Post.update = function(post_id, params) { + Danbooru.Post.notice_update("inc"); + + $.ajax({ + type: "PUT", + url: "/posts/" + post_id + ".json", + data: params, + complete: function() { + Danbooru.Post.notice_update("dec"); + }, + success: function(data, status, xhr) { + Danbooru.Post.update_data(data); + }, + error: function(data, status, xhr) { + Danbooru.j_alert("Error: " + data.reason); + } + }); + } })(); $(document).ready(function() { - // $("#mode-box select").click(PostModeMenu.change); - // PostModeMenu.init(); - Danbooru.Post.initialize_all(); }); diff --git a/app/assets/javascripts/tag_script.js b/app/assets/javascripts/tag_script.js new file mode 100644 index 000000000..20e8ffa46 --- /dev/null +++ b/app/assets/javascripts/tag_script.js @@ -0,0 +1,56 @@ +TagScript = { + parse: function(script) { + return script.match(/\[.+?\]|\S+/g); + }, + + test: function(tags, predicate) { + var split_pred = predicate.match(/\S+/g); + var is_true = true; + + split_pred.each(function(x) { + if (x[0] == "-") { + if (tags.include(x.substr(1, 100))) { + is_true = false; + throw $break; + } + } else { + if (!tags.include(x)) { + is_true = false; + throw $break; + } + } + }) + + return is_true + }, + + process: function(tags, command) { + if (command.match(/^\[if/)) { + var match = command.match(/\[if\s+(.+?)\s*,\s*(.+?)\]/) + if (TagScript.test(tags, match[1])) { + return TagScript.process(tags, match[2]); + } else { + return tags; + } + } else if (command == "[reset]") { + return []; + } else if (command[0] == "-") { + return tags.reject(function(x) {return x == command.substr(1, 100)}) + } else { + tags.push(command) + return tags; + } + }, + + run: function(post_id, tag_script) { + var commands = TagScript.parse(tag_script); + var post = Post.posts.get(post_id); + var old_tags = post.tags.join(" "); + + commands.each(function(x) { + post.tags = TagScript.process(post.tags, x); + }) + + Post.update(post_id, {"post[old_tags]": old_tags, "post[tags]": post.tags.join(" ")}); + } +} \ No newline at end of file diff --git a/app/assets/javascripts/utility.js b/app/assets/javascripts/utility.js index bae2db7cf..712f58bbc 100644 --- a/app/assets/javascripts/utility.js +++ b/app/assets/javascripts/utility.js @@ -3,6 +3,10 @@ return $("meta[name=" + key + "]").attr("content"); } + Danbooru.notice = function(msg) { + $('#notice').html(msg).show(); + } + Danbooru.j_alert = function(title, msg) { $('
').html(msg).dialog(); } diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss index 098fea73d..9a805d5ab 100644 --- a/app/assets/stylesheets/application.css.scss +++ b/app/assets/stylesheets/application.css.scss @@ -1108,3 +1108,69 @@ div#news-ticker { float: right; } } + + +/*** post mode menus ***/ +body#mode-view { + background-color: "#FFF"; +} + +body#mode-edit { + background-color: "#3A3"; +} + +body#mode-add-fav { + background-color: "#FFA"; +} + +body#mode-remove-fav { + background-color: "#FFA"; +} + +body#mode-rating-q { + background-color: "#AAA"; +} + +body#mode-rating-s { + background-color: "#6F6"; +} + +body#mode-rating-e { + background-color: "#F66"; +} + +body#mode-vote-down { + background-color: "#FAA"; +} + +body#mode-vote-up { + background-color: "#AFA"; +} + +body#mode-lock-rating { + background-color: "#AA3"; +} + +body#mode-lock-note { + background-color: "#3AA"; +} + +body#mode-approve { + background-color: "#26A"; +} + +body#mode-unapprove { + background-color: "#F66"; +} + +body#mode-add-to-pool { + background-color: "#26A"; +} + +body#mode-apply-tag-script { + background-color: "#A3A"; +} + +body#mode-edit-tag-script { + background-color: "#FFF"; +} diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 7c3425f44..95304d0cc 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -62,7 +62,7 @@ protected when "uploads" /^\/post/ - when "post_versions" + when "post_versions", "explore/posts" /^\/post/ when "pool_versions"