hotkeys: refactor to use .on(), namespaces.

* Use .on() instead of .bind() because bind is deprecated in jquery 3.0.
* Ensure enable-js-navigation is always respected.
* Namespace keybindings so they may be disabled by userscripts with
  e.g. $(document).off("keydown.danbooru") or $(document).off("next_page").
This commit is contained in:
evazion
2017-01-24 00:19:38 -06:00
parent 6430a9b0a4
commit c3fa653fc5
10 changed files with 61 additions and 85 deletions

View File

@@ -4,7 +4,7 @@
Danbooru.FavoriteGroup.initialize_all = function() {
if ($("#c-posts").length && $("#a-show").length) {
this.initialize_add_to_favgroup_dialog();
$(document).bind("keydown", "1 2 3 4 5 6 7 8 9 0", Danbooru.FavoriteGroup.add_to_favgroup);
Danbooru.keydown("1 2 3 4 5 6 7 8 9 0", "add_to_favgroup", Danbooru.FavoriteGroup.add_to_favgroup);
}
}
@@ -33,7 +33,7 @@
e.preventDefault();
}
$(document).bind("keydown", "g", open_favgroup_dialog);
Danbooru.keydown("g", "open_favgroup_dialog", open_favgroup_dialog);
$("#open-favgroup-dialog-link").click(open_favgroup_dialog);
}

View File

@@ -62,7 +62,7 @@ Danbooru.Note = {
},
bind_events: function($note_box) {
$note_box.bind(
$note_box.on(
"dragstart resizestart",
function(e) {
var $note_box_inner = $(e.currentTarget);
@@ -80,8 +80,7 @@ Danbooru.Note = {
}
);
$note_box.bind(
"resize",
$note_box.resize(
function(e) {
var $note_box_inner = $(e.currentTarget);
Danbooru.Note.Box.resize_inner_border($note_box_inner);
@@ -89,7 +88,7 @@ Danbooru.Note = {
}
);
$note_box.bind(
$note_box.on(
"dragstop resizestop",
function(e) {
Danbooru.Note.dragging = false;
@@ -105,7 +104,7 @@ Danbooru.Note = {
}
);
$note_box.bind(
$note_box.on(
"mouseover mouseout",
function(e) {
if (Danbooru.Note.dragging) {
@@ -407,7 +406,7 @@ Danbooru.Note = {
}
$dialog.dialog("option", "title", 'Edit note (<a href="/wiki_pages/help:notes">view help</a>)');
$dialog.bind("dialogclose", function() {
$dialog.on("dialogclose", function() {
Danbooru.Note.editing = false;
$(".note-box").resizable("enable");
$(".note-box").draggable("enable");
@@ -570,9 +569,9 @@ Danbooru.Note = {
Danbooru.Note.TranslationMode.active = true;
$(document.body).addClass("mode-translation");
$("#original-file-link").click();
$("#image").unbind("click", Danbooru.Note.Box.toggle_all);
$("#image").bind("mousedown", Danbooru.Note.TranslationMode.Drag.start);
$(window).bind("mouseup", Danbooru.Note.TranslationMode.Drag.stop);
$("#image").off("click", Danbooru.Note.Box.toggle_all);
$("#image").mousedown(Danbooru.Note.TranslationMode.Drag.start);
$(window).mouseup(Danbooru.Note.TranslationMode.Drag.stop);
$("#mark-as-translated-section").show();
Danbooru.notice('Translation mode is on. Drag on the image to create notes. <a href="#">Turn translation mode off</a> (shortcut is <span class="key">n</span>).');
@@ -584,9 +583,9 @@ Danbooru.Note = {
Danbooru.Note.TranslationMode.active = false;
$("#image").css("cursor", "auto");
$("#image").bind("click", Danbooru.Note.Box.toggle_all);
$("#image").unbind("mousedown", Danbooru.Note.TranslationMode.Drag.start);
$(window).unbind("mouseup", Danbooru.Note.TranslationMode.Drag.stop);
$("#image").click(Danbooru.Note.Box.toggle_all);
$("#image").off("mousedown", Danbooru.Note.TranslationMode.Drag.start);
$(window).mouseup(Danbooru.Note.TranslationMode.Drag.stop);
$(document.body).removeClass("mode-translation");
$("#close-notice-link").click();
$("#mark-as-translated-section").hide();
@@ -689,7 +688,7 @@ Danbooru.Note = {
if (Danbooru.Note.TranslationMode.Drag.dragStartX === 0) {
return; /* 'stop' is bound to window, don't create note if start wasn't triggered */
}
$(window).unbind("mousemove");
$(window).off("mousemove");
if (Danbooru.Note.TranslationMode.Drag.dragging) {
$('#note-preview').css({display:'none'});
@@ -782,11 +781,11 @@ Danbooru.Note = {
$(function() {
if ($("#c-posts").length && $("#a-show").length && $("#image").length && !$("video#image").length) {
if ($("#note-locked-notice").length == 0) {
$("#translate").bind("click", Danbooru.Note.TranslationMode.toggle);
$(document).bind("keydown", "n", Danbooru.Note.TranslationMode.toggle);
$("#translate").click(Danbooru.Note.TranslationMode.toggle);
Danbooru.keydown("n", "translation_mode", Danbooru.Note.TranslationMode.toggle);
}
Danbooru.Note.embed = (Danbooru.meta("post-has-embedded-notes") === "true");
Danbooru.Note.load_all();
$("#image").bind("click", Danbooru.Note.Box.toggle_all);
$("#image").click(Danbooru.Note.Box.toggle_all);
}
});

View File

@@ -17,9 +17,9 @@
})();
$(function() {
if ($(".paginator").length && (Danbooru.meta("enable-js-navigation") === "true")) {
$(document).bind("keydown", "d", Danbooru.Paginator.next_page);
$(document).bind("keydown", "a", Danbooru.Paginator.prev_page);
if ($(".paginator").length) {
Danbooru.keydown("d", "next_page", Danbooru.Paginator.next_page);
Danbooru.keydown("a", "prev_page", Danbooru.Paginator.prev_page);
}
});

View File

@@ -13,7 +13,7 @@
}
Danbooru.PostModeMenu.initialize_shortcuts = function() {
$(document).bind("keydown", "1 2 3 4 5 6 7 8 9 0", Danbooru.PostModeMenu.change_tag_script);
Danbooru.keydown("1 2 3 4 5 6 7 8 9 0", "change_tag_script", Danbooru.PostModeMenu.change_tag_script);
}
Danbooru.PostModeMenu.show_notice = function(i) {

View File

@@ -1,41 +1,36 @@
(function() {
Danbooru.PostPopular = {};
Danbooru.PostPopular.nav_prev = function() {
Danbooru.PostPopular.nav_prev = function(e) {
if ($("#popular-nav-links").length) {
var href = $("#popular-nav-links a[rel=prev]").attr("href");
if (href) {
location.href = href;
}
}
e.preventDefault();
}
Danbooru.PostPopular.nav_next = function() {
Danbooru.PostPopular.nav_next = function(e) {
if ($("#popular-nav-links").length) {
var href = $("#popular-nav-links a[rel=next]").attr("href");
if (href) {
location.href = href;
}
}
e.preventDefault();
}
Danbooru.PostPopular.initialize_all = function() {
if ($("#c-explore-posts").length) {
if (Danbooru.meta("enable-js-navigation") === "true") {
$(document).bind("keydown", "a", function(e) {
Danbooru.PostPopular.nav_prev();
e.preventDefault();
});
$(document).bind("keydown", "d", function(e) {
Danbooru.PostPopular.nav_next();
e.preventDefault();
});
}
Danbooru.keydown("a", "prev_page", Danbooru.PostPopular.nav_prev);
Danbooru.keydown("d", "next_page", Danbooru.PostPopular.nav_next);
}
}
})();
$(document).ready(function() {
Danbooru.PostPopular.initialize_all();
});
});

View File

@@ -7,9 +7,7 @@
this.initialize_post_previews();
if ($("#c-posts").length) {
if (Danbooru.meta("enable-js-navigation") === "true") {
this.initialize_shortcuts();
}
this.initialize_shortcuts();
}
if ($("#c-posts").length && $("#a-index").length) {
@@ -141,7 +139,7 @@
});
}
Danbooru.Post.nav_prev = function() {
Danbooru.Post.nav_prev = function(e) {
if ($("#search-seq-nav").length) {
var href = $("#search-seq-nav a[rel~=prev]").attr("href");
if (href) {
@@ -153,9 +151,11 @@
location.href = href;
}
}
e.preventDefault();
}
Danbooru.Post.nav_next = function() {
Danbooru.Post.nav_next = function(e) {
if ($("#search-seq-nav").length) {
var href = $("#search-seq-nav a[rel~=next]").attr("href");
location.href = href;
@@ -165,27 +165,22 @@
location.href = href;
}
}
e.preventDefault();
}
Danbooru.Post.initialize_shortcuts = function() {
if ($("#a-show").length) {
$(document).bind("keydown", "e", function(e) {
Danbooru.keydown("e", "edit", function(e) {
$("#post-edit-link").trigger("click");
$("#post_tag_string").focus();
e.preventDefault();
});
$(document).bind("keydown", "a", function(e) {
Danbooru.Post.nav_prev();
e.preventDefault();
});
Danbooru.keydown("a", "prev_page", Danbooru.Post.nav_prev);
Danbooru.keydown("d", "next_page", Danbooru.Post.nav_next);
$(document).bind("keydown", "d", function(e) {
Danbooru.Post.nav_next();
e.preventDefault();
});
$(document).bind("keydown", "f", function(e) {
Danbooru.keydown("f", "favorite", function(e) {
if ($("#add-to-favorites").is(":visible")) {
$("#add-to-favorites").click();
} else {
@@ -331,8 +326,8 @@
e.preventDefault();
});
if ($("#image-resize-notice").length && Danbooru.meta("enable-js-navigation") === "true") {
$(document).bind("keydown", "v", function(e) {
if ($("#image-resize-notice").length) {
Danbooru.keydown("v", "resize", function(e) {
if ($("#image-resize-notice").is(":visible")) {
$("#image-resize-link").click();
} else {

View File

@@ -2,21 +2,16 @@
Danbooru.Shortcuts = {};
Danbooru.Shortcuts.initialize = function() {
$(document).bind("keydown", "s", function(e) {
Danbooru.Shortcuts.nav_scroll_down();
});
Danbooru.keydown("s", "scroll_down", Danbooru.Shortcuts.nav_scroll_down);
Danbooru.keydown("w", "scroll_up", Danbooru.Shortcuts.nav_scroll_up);
$(document).bind("keydown", "w", function(e) {
Danbooru.Shortcuts.nav_scroll_up();
});
$(document).bind("keydown", "q", function(e) {
Danbooru.keydown("q", "focus_search", function(e) {
$("#tags, #search_name, #search_name_matches, #query").trigger("focus").selectEnd();
e.preventDefault();
});
if ($("#image").length) { // post page or bookmarklet upload page
$(document).bind("keydown", "shift+e", function(e) {
Danbooru.keydown("shift+e", "edit_dialog", function(e) {
if (Danbooru.meta("current-user-id") == "") { // anonymous
return;
}
@@ -36,25 +31,15 @@
}
if ($("#c-posts").length && $("#a-show").length) {
$(document).bind("keydown", "shift+o", function(e) {
Danbooru.keydown("shift+o", "approve", function(e) {
if (Danbooru.meta("current-user-can-approve-posts") === "true") {
Danbooru.Post.approve(Danbooru.meta("post-id"));
}
});
$(document).bind("keydown", "r", function(e) {
$("#random-post")[0].click();
});
}
if ($("#c-posts").length && $("#a-index").length) {
$(document).bind("keydown", "r", function(e) {
$("#random-post")[0].click();
});
}
if ($("#c-favorites").length && $("#a-index").length) {
$(document).bind("keydown", "r", function(e) {
if ($("#c-posts #a-index, #c-posts #a-show, #c-favorites #a-index").length) {
Danbooru.keydown("r", "random", function(e) {
$("#random-post")[0].click();
});
}
@@ -76,7 +61,5 @@
$(document).ready(function() {
if (Danbooru.meta("enable-js-navigation") === "true") {
Danbooru.Shortcuts.initialize();
}
Danbooru.Shortcuts.initialize();
});

View File

@@ -27,11 +27,11 @@
}
Danbooru.Upload.initialize_enter_on_tags = function() {
$("#upload_tag_string,#post_tag_string").bind("keydown", "return", function(e) {
$("#upload_tag_string,#post_tag_string").on("keydown.danbooru.submit", null, "return", function(e) {
if (!Danbooru.autocompleting) {
$("#form").trigger("submit");
$("#quick-edit-form").trigger("submit");
$("#upload_tag_string,#post_tag_string").unbind("keydown");
$("#upload_tag_string,#post_tag_string").off(".submit");
}
e.preventDefault();

View File

@@ -45,6 +45,12 @@
}
}
Danbooru.keydown = function(keys, namespace, handler) {
if (Danbooru.meta("enable-js-navigation") === "true") {
$(document).on("keydown" + ".danbooru." + namespace, null, keys, handler);
}
};
Danbooru.is_subset = function(array, subarray) {
var all = true;

View File

@@ -7,9 +7,7 @@
this.initialize_autocomplete();
}
if (Danbooru.meta("enable-js-navigation") === "true") {
this.initialize_shortcuts();
}
this.initialize_shortcuts();
}
}
@@ -51,7 +49,7 @@
Danbooru.WikiPage.initialize_shortcuts = function() {
if ($("#a-show").length) {
$(document).bind("keydown", "e", function(e) {
Danbooru.keydown("e", "edit", function(e) {
$("#wiki-page-edit-link")[0].click();
e.preventDefault();
});