Files
danbooru/app/javascript/src/javascripts/utility.js
evazion 5ba03ba359 replacements: fix submit button not working in Chrome.
Fix the submit button in the post replacement dialog doing nothing in Chrome.

When the submit button in a dialog box is clicked, we tried to fire a 'submit' event on
the form to submit it. Apparently this no longer works in Chrome. `requestSubmit` is the
modern way to programmatically submit a form, but it's not supported by Safari, or by
versions of Firefox or Chrome released before 2019-2020.

https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/requestSubmit
https://caniuse.com/mdn-api_htmlformelement_requestsubmit
2022-02-07 22:24:54 -06:00

144 lines
3.4 KiB
JavaScript

import Rails from '@rails/ujs';
import { hideAll } from 'tippy.js';
import words from "lodash/words";
let Utility = {};
export function clamp(value, low, high) {
return Math.max(low, Math.min(value, high));
}
Utility.delay = function(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}
Utility.meta = function(key) {
return $("meta[name=" + key + "]").attr("content");
}
Utility.test_max_width = function(width) {
if (!window.matchMedia) {
return false;
}
var mq = window.matchMedia('(max-width: ' + width + 'px)');
return mq.matches;
}
Utility.notice_timeout_id = undefined;
Utility.notice = function(msg, permanent) {
$('#notice').addClass("notice-info").removeClass("notice-error").fadeIn("fast").children("span").html(msg);
if (Utility.notice_timeout_id !== undefined) {
clearTimeout(Utility.notice_timeout_id)
}
if (!permanent) {
Utility.notice_timeout_id = setTimeout(function() {
$("#close-notice-link").click();
Utility.notice_timeout_id = undefined;
}, 6000);
}
}
Utility.error = function(msg) {
$('#notice').removeClass("notice-info").addClass("notice-error").fadeIn("fast").children("span").html(msg);
if (Utility.notice_timeout_id !== undefined) {
clearTimeout(Utility.notice_timeout_id)
}
}
Utility.dialog = function(title, html) {
const $dialog = $(html).dialog({
title: title,
width: 700,
modal: true,
close: function() {
// Defer removing the dialog to avoid detaching the <form> tag before the
// form is submitted (which would prevent the submission from going through).
$(() => $dialog.dialog("destroy"));
},
buttons: {
"Submit": function() {
let form = $dialog.find("form").get(0);
if (form.requestSubmit) {
form.requestSubmit();
} else {
form.submit();
Rails.fire(form, "submit");
}
},
"Cancel": function() {
$dialog.dialog("close");
}
}
});
$dialog.find("form").on("submit.danbooru", function() {
$dialog.dialog("close");
});
// XXX hides the popup menu when the Report comment button is clicked.
hideAll({ duration: 0 });
}
Utility.keydown = function(keys, namespace, handler, selector = document) {
$(selector).on("keydown.danbooru." + namespace, null, keys, handler);
};
Utility.is_subset = function(array, subarray) {
var all = true;
$.each(subarray, function(i, val) {
if ($.inArray(val, array) === -1) {
all = false;
}
});
return all;
}
Utility.intersect = function(a, b) {
a = a.slice(0).sort();
b = b.slice(0).sort();
var result = [];
while (a.length > 0 && b.length > 0) {
if (a[0] < b[0]) {
a.shift();
} else if (a[0] > b[0]) {
b.shift();
} else {
result.push(a.shift());
b.shift();
}
}
return result;
}
Utility.regexp_escape = function(string) {
return string.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
}
Utility.splitWords = function(string) {
return words(string, /\S+/g);
}
Utility.copyToClipboard = async function(text, message = "Copied!") {
try {
await navigator.clipboard.writeText(text);
Utility.notice(message);
} catch (error) {
Utility.error("Couldn't copy to clipboard");
}
}
$.fn.selectEnd = function() {
return this.each(function() {
this.focus();
this.setSelectionRange(this.value.length, this.value.length);
})
}
export default Utility