-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathstories.js
More file actions
94 lines (82 loc) · 2.8 KB
/
stories.js
File metadata and controls
94 lines (82 loc) · 2.8 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
document.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll("[data-has-preview]").forEach((element) => {
let debounceTimer;
element.addEventListener("input", (e) => {
const updateMarkdown = () => {
const form = element.closest("form");
const preview = form.querySelector(
"." + element.dataset.previewTarget + " .content"
);
if (preview) {
Rails.ajax({
type: "POST",
url: "/stories/render_markdown",
data: `markdown=${encodeURIComponent(element.value)}`,
dataType: "text",
success: (response) => {
preview.innerHTML = response;
},
});
}
};
window.clearTimeout(debounceTimer);
debounceTimer = window.setTimeout(updateMarkdown, 300);
});
});
const form = document.querySelector(".edit_story");
const backButton = document.getElementById("back");
const logo = document.getElementById("logo");
let isDirty = false;
if (form) {
// Mark the form as dirty when any input changes
form.addEventListener("input", function () {
isDirty = true;
addBeforeUnloadEventListener(isDirty);
});
// Reset isDirty on form submission
form.addEventListener("submit", function () {
isDirty = false;
addBeforeUnloadEventListener(isDirty);
});
}
// Attach a click event to the custom back button
[backButton, logo].forEach(element => {
element.addEventListener("click", function (event) {
if (isDirty) {
const confirmLeave = confirm("You have unsaved changes. Are you sure you want to go back?");
if (confirmLeave) {
// Optionally, reset isDirty if leaving
isDirty = false;
addBeforeUnloadEventListener(isDirty)
} else {
// Prevent navigation if the user chooses not to leave
event.preventDefault();
}
}
})
});
});
function addBeforeUnloadEventListener(isDirty) {
if (isDirty) {
window.addEventListener("beforeunload", warnUserIfUnsavedEdits);
} else {
window.removeEventListener("beforeunload", warnUserIfUnsavedEdits);
}
}
function warnUserIfUnsavedEdits(event) {
event.preventDefault();
event.returnValue = '';
}
function updateStatusButton(color, status) {
const button = document.querySelector(".story-title .dropdown-wrapper > button");
button.className = `button ${color}`;
const span = button.querySelector("span");
span.textContent = status;
document.querySelector(":focus").blur();
}
function updateStatusLabel(status, storyId) {
let row = document.getElementById(`story_${storyId}`)
status_label = row.querySelector(".status > .story-status-badge")
status_label.textContent = status
status_label.classList.value = `story-status-badge ${status}`
}