-
Notifications
You must be signed in to change notification settings - Fork 10
/
RockMigrations.js
72 lines (66 loc) · 2.65 KB
/
RockMigrations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* This file is loaded in the PW backend
*/
(() => {
// add tooltips in the backend
$(document).ready(() => {
let addTooltip = function (el) {
let name = el.name;
if (name == "templateLabel") name = "label";
else if (name == "field_label") name = "label";
else if (name == "asmSelect0") return;
// add comment?
let comment = "";
if (el.type == "radio") comment = $(el).parent().text();
else if (el.type == "select-one")
comment = $(el).find("option:selected").text();
else if (el.type == "checkbox") comment = $(el).parent().text().trim();
if (comment) comment = " // " + comment;
// value
let value = el.value;
if (el.type == "checkbox") value = $(el).is(":checked") ? 1 : 0;
let code = "'" + name + "' => '" + value + "'," + comment;
$(el).attr("title", code + " (shift-click to copy)");
$(el).attr("rockmigrations-code", code);
UIkit.tooltip(el);
// console.log("added tooltip", el, el.value);
};
$(
".rm-hints input[name], .rm-hints textarea[name], .rm-hints select[name]"
).each((i, el) => {
// don't add hints on asm select fields
// this is to fix this issue: https://processwire.com/talk/topic/29462-no-title-field-with-add-new-page-in-pw-anymore-after-hidetitle-true/?do=findComment&comment=238531
if (el.closest(".InputfieldAsmSelect")) return;
addTooltip(el);
});
// on shift-click copy the attribute "rockmigrations-code" of the clicked element to the clipboard
$(document).on("click", "[rockmigrations-code]", function (e) {
if (!e.shiftKey) return;
addTooltip(e.target);
const codeToCopy = $(this).attr("rockmigrations-code");
copyToClipboard(codeToCopy);
});
});
// copy page id and template name on click (if tweaks are enabled)
$(document).on("mousedown", ".PageListTemplate, .PageListId", (e) => {
if (!e.shiftKey) return;
let contentToCopy = $(e.target).text().trim();
if (contentToCopy.startsWith("#")) {
contentToCopy = contentToCopy.substring(1);
}
if (contentToCopy.startsWith("[") && contentToCopy.endsWith("]")) {
contentToCopy = contentToCopy.substring(1, contentToCopy.length - 1);
}
copyToClipboard(contentToCopy);
});
// Utility function to copy text to clipboard and show notification
function copyToClipboard(text) {
const textarea = document.createElement("textarea");
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
document.body.removeChild(textarea);
UIkit.notification("Copied: " + text, { status: "success" });
}
})();