From d64236813a63879f3b6a1755f279fedcd33f8d07 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 2 Oct 2019 15:59:22 -0500 Subject: [PATCH] js: replace tags with data attributes. Refactor things to store information about the current user as data attributes on the tag rather than as tags. These tags are now deprecated and will be eventually removed. * Store all of the current user's API attributes as data attributes on the tag. * Add `CurrentUser.data` for getting data from the tag, and use it instead of `Utility.meta`. * Add `CurrentUser.update` for updating the current user's settings. * Fix a bug with the user named "Anonymous" not being able to edit notes. --- app/helpers/application_helper.rb | 3 ++- app/javascript/packs/application.js | 1 + .../src/javascripts/autocomplete.js.erb | 6 +++--- app/javascript/src/javascripts/common.js | 14 ++++---------- .../src/javascripts/current_user.js | 14 ++++++++++++++ app/javascript/src/javascripts/notes.js | 5 +++-- .../src/javascripts/post_mode_menu.js | 7 ++++--- .../src/javascripts/post_tooltips.js | 19 ++++++++----------- app/javascript/src/javascripts/posts.js.erb | 7 ++++--- app/javascript/src/javascripts/utility.js | 4 +++- 10 files changed, 46 insertions(+), 34 deletions(-) create mode 100644 app/javascript/src/javascripts/current_user.js diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 0904ba41e..2de4ff84f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -175,7 +175,8 @@ module ApplicationHelper end def body_attributes(user = CurrentUser.user) - attributes = [:id, :name, :level, :level_string, :theme, :can_approve_posts?, :can_upload_free?] + attributes = user.api_attributes + attributes -= [:custom_style, :blacklisted_tags, :favorite_tags] attributes += User::Roles.map { |role| :"is_#{role}?" } controller_param = params[:controller].parameterize.dasherize diff --git a/app/javascript/packs/application.js b/app/javascript/packs/application.js index da34763a3..1ed58eb97 100644 --- a/app/javascript/packs/application.js +++ b/app/javascript/packs/application.js @@ -31,6 +31,7 @@ importAll(require.context('../src/styles', true, /\.s?css(?:\.erb)?$/)); export { default as Autocomplete } from '../src/javascripts/autocomplete.js.erb'; export { default as Blacklist } from '../src/javascripts/blacklists.js'; export { default as Comment } from '../src/javascripts/comments.js'; +export { default as CurrentUser } from '../src/javascripts/current_user.js'; export { default as Dtext } from '../src/javascripts/dtext.js'; export { default as Note } from '../src/javascripts/notes.js'; export { default as Post } from '../src/javascripts/posts.js.erb'; diff --git a/app/javascript/src/javascripts/autocomplete.js.erb b/app/javascript/src/javascripts/autocomplete.js.erb index c4a07a83e..be27667c0 100644 --- a/app/javascript/src/javascripts/autocomplete.js.erb +++ b/app/javascript/src/javascripts/autocomplete.js.erb @@ -1,4 +1,4 @@ -import Utility from './utility' +import CurrentUser from './current_user' import SavedSearch from './saved_searches' let Autocomplete = {}; @@ -14,7 +14,7 @@ Autocomplete.METATAGS_REGEX = Autocomplete.METATAGS.concat(Object.keys(Autocompl Autocomplete.TERM_REGEX = new RegExp(`([-~]*)(?:(${Autocomplete.METATAGS_REGEX}):)?(\\S*)$`, "i"); Autocomplete.initialize_all = function() { - if (Utility.meta("enable-auto-complete") === "true") { + if (CurrentUser.data("enable-auto-complete")) { $.widget("ui.autocomplete", $.ui.autocomplete, { options: { delay: 0, @@ -355,7 +355,7 @@ Autocomplete.render_item = function(list, item) { } else if (item.type === "user") { var level_class = "user-" + item.level.toLowerCase(); $link.addClass(level_class); - if (Utility.meta("style-usernames") === "true") { + if (CurrentUser.data("style-usernames")) { $link.addClass("with-style"); } } else if (item.type === "pool") { diff --git a/app/javascript/src/javascripts/common.js b/app/javascript/src/javascripts/common.js index 6d611bd3d..c03552164 100644 --- a/app/javascript/src/javascripts/common.js +++ b/app/javascript/src/javascripts/common.js @@ -1,5 +1,5 @@ import Cookie from './cookie' -import Utility from './utility' +import CurrentUser from './current_user' $(function() { $("#hide-upgrade-account-notice").on("click.danbooru", function(e) { @@ -21,16 +21,10 @@ $(function() { e.preventDefault(); }); - $("#desktop-version-link a").on("click.danbooru", function(e) { + $("#desktop-version-link a").on("click.danbooru", async function(e) { e.preventDefault(); - $.ajax("/users/" + Utility.meta("current-user-id") + ".json", { - method: "PUT", - data: { - "user[disable_responsive_mode]": "true" - } - }).then(function() { - location.reload(); - }); + await CurrentUser.update({ disable_responsive_mode: true }); + location.reload(); }); }); diff --git a/app/javascript/src/javascripts/current_user.js b/app/javascript/src/javascripts/current_user.js new file mode 100644 index 000000000..286253e03 --- /dev/null +++ b/app/javascript/src/javascripts/current_user.js @@ -0,0 +1,14 @@ +let CurrentUser = {}; + +CurrentUser.data = function(key) { + return $("body").data(`user-${key}`); +}; + +CurrentUser.update = function(settings) { + return $.ajax(`/users/${CurrentUser.data("id")}.json`, { + method: "PUT", + data: { user: settings } + }); +}; + +export default CurrentUser; diff --git a/app/javascript/src/javascripts/notes.js b/app/javascript/src/javascripts/notes.js index c6131e8fc..615de3c50 100644 --- a/app/javascript/src/javascripts/notes.js +++ b/app/javascript/src/javascripts/notes.js @@ -1,3 +1,4 @@ +import CurrentUser from './current_user' import Utility from './utility' let Note = { @@ -348,7 +349,7 @@ let Note = { e.stopPropagation(); }); - if (Utility.meta("current-user-name") !== "Anonymous") { + if (CurrentUser.data("is-anonymous") === false) { $note_body.on("click.danbooru", function(e) { if (e.target.tagName !== "A") { var $note_body_inner = $(e.currentTarget); @@ -573,7 +574,7 @@ let Note = { start: function(e) { e.preventDefault(); - if (Utility.meta("current-user-id") === "") { + if (CurrentUser.data("is-anonymous")) { Utility.notice("You must be logged in to edit notes"); return; } diff --git a/app/javascript/src/javascripts/post_mode_menu.js b/app/javascript/src/javascripts/post_mode_menu.js index f98732605..b1f456b1a 100644 --- a/app/javascript/src/javascripts/post_mode_menu.js +++ b/app/javascript/src/javascripts/post_mode_menu.js @@ -1,7 +1,8 @@ -import Utility from './utility' import Cookie from './cookie' -import Post from './posts.js.erb' +import CurrentUser from './current_user' import Favorite from './favorites' +import Post from './posts.js.erb' +import Utility from './utility' let PostModeMenu = {}; @@ -91,7 +92,7 @@ PostModeMenu.initialize_edit_form = function() { PostModeMenu.close_edit_form = function() { $("#quick-edit-div").slideUp("fast"); - if (Utility.meta("enable-auto-complete") === "true") { + if (CurrentUser.data("enable-auto-complete")) { $("#post_tag_string").data("uiAutocomplete").close(); } } diff --git a/app/javascript/src/javascripts/post_tooltips.js b/app/javascript/src/javascripts/post_tooltips.js index d504adbab..58a1e4577 100644 --- a/app/javascript/src/javascripts/post_tooltips.js +++ b/app/javascript/src/javascripts/post_tooltips.js @@ -1,3 +1,4 @@ +import CurrentUser from './current_user' import Utility from './utility' require('qtip2'); @@ -99,25 +100,21 @@ PostTooltip.hide = function (event) { }; PostTooltip.disabled = function (event) { - return PostTooltip.isTouching || Utility.meta("disable-post-tooltips") === "true"; + return PostTooltip.isTouching || CurrentUser.data("disable-post-tooltips"); }; -PostTooltip.on_disable_tooltips = function (event) { +PostTooltip.on_disable_tooltips = async function (event) { event.preventDefault(); $(event.target).parents(".qtip").qtip("hide"); - if (Utility.meta("current-user-id") === "") { - Utility.notice('Login to disable tooltips permanently'); + if (CurrentUser.data("is-anonymous")) { + Utility.notice('You must login to disable tooltips'); return; } - $.ajax("/users/" + Utility.meta("current-user-id") + ".json", { - method: "PUT", - data: { "user[disable_post_tooltips]": "true" }, - }).then(function() { - Utility.notice("Tooltips disabled; check your account settings to re-enable."); - location.reload(); - }); + await CurrentUser.update({ disable_post_tooltips: true }); + Utility.notice("Tooltips disabled; check your account settings to re-enable."); + location.reload(); }; $(document).ready(PostTooltip.initialize); diff --git a/app/javascript/src/javascripts/posts.js.erb b/app/javascript/src/javascripts/posts.js.erb index 7334e8cc8..d7fad487b 100644 --- a/app/javascript/src/javascripts/posts.js.erb +++ b/app/javascript/src/javascripts/posts.js.erb @@ -1,3 +1,4 @@ +import CurrentUser from './current_user' import Utility from './utility' import Hammer from 'hammerjs' import Cookie from './cookie' @@ -49,7 +50,7 @@ Post.initialize_all = function() { } Post.initialize_gestures = function() { - if (Utility.meta("disable-mobile-gestures") === "true") { + if (CurrentUser.data("disable-mobile-gestures")) { return; } var $body = $("body"); @@ -117,7 +118,7 @@ Post.open_edit_dialog = function() { of: window }, drag: function(e, ui) { - if (Utility.meta("enable-auto-complete") === "true") { + if (CurrentUser.data("enable-auto-complete")) { $tag_string.data("uiAutocomplete").close(); } }, @@ -591,7 +592,7 @@ Post.initialize_saved_searches = function() { $("#save-search").on("click.danbooru", function(e) { $("#save-search-dialog #saved_search_query").val($("#tags").val()); - if (Utility.meta("disable-labeled-saved-searches") === "false") { + if (CurrentUser.data("disable-categorized-saved-searches") === false) { $("#save-search-dialog").dialog("open"); } else { $.post( diff --git a/app/javascript/src/javascripts/utility.js b/app/javascript/src/javascripts/utility.js index 20dfb54ec..72bb4325b 100644 --- a/app/javascript/src/javascripts/utility.js +++ b/app/javascript/src/javascripts/utility.js @@ -1,3 +1,5 @@ +import CurrentUser from "./current_user"; + let Utility = {}; Utility.delay = function(milliseconds) { @@ -66,7 +68,7 @@ Utility.dialog = function(title, html) { } Utility.keydown = function(keys, namespace, handler) { - if (Utility.meta("enable-js-navigation") === "true") { + if (CurrentUser.data("enable-post-navigation")) { $(document).on("keydown.danbooru." + namespace, null, keys, handler); } };