js: replace <meta> tags with <body> data attributes.

Refactor things to store information about the current user as data
attributes on the <body> tag rather than as <meta> tags. These <meta>
tags are now deprecated and will be eventually removed.

* Store all of the current user's API attributes as data attributes on
  the <body> tag.

* Add `CurrentUser.data` for getting data from the <body> 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.
This commit is contained in:
evazion
2019-10-02 15:59:22 -05:00
parent b492b5de9c
commit d64236813a
10 changed files with 46 additions and 34 deletions

View File

@@ -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

View File

@@ -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';

View File

@@ -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") {

View File

@@ -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();
});
});

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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();
}
}

View File

@@ -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('<a href="/session/new">Login</a> to disable tooltips permanently');
if (CurrentUser.data("is-anonymous")) {
Utility.notice('You must <a href="/session/new">login</a> 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);

View File

@@ -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(

View File

@@ -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);
}
};