Skip to content

Commit

Permalink
Merge pull request #2510 from sascha-karnatz/move-dirty.js-into-app-j…
Browse files Browse the repository at this point in the history
…avascript

Convert Dirty from Coffeescript to Javascript
  • Loading branch information
tvdeyen authored Aug 2, 2023
2 parents 0671cef + 04740e0 commit 8596f74
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 62 deletions.
1 change: 0 additions & 1 deletion app/assets/javascripts/alchemy/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
//= require alchemy/alchemy.dialog
//= require alchemy/alchemy.char_counter
//= require alchemy/alchemy.confirm_dialog
//= require alchemy/alchemy.dirty
//= require alchemy/alchemy.dragndrop
//= require alchemy/alchemy.element_editors
//= require alchemy/alchemy.elements_window
Expand Down
59 changes: 0 additions & 59 deletions app/assets/javascripts/alchemy/alchemy.dirty.js.coffee

This file was deleted.

5 changes: 3 additions & 2 deletions app/javascript/alchemy_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "@hotwired/turbo-rails"

import Buttons from "alchemy_admin/buttons"
import translate from "alchemy_admin/i18n"
import Dirty from "alchemy_admin/dirty"
import translationData from "alchemy_admin/translations"
import fileEditors from "alchemy_admin/file_editors"
import IngredientAnchorLink from "alchemy_admin/ingredient_anchor_link"
Expand All @@ -21,9 +22,9 @@ if (typeof window.Alchemy === "undefined") {

// Enhance the global Alchemy object with imported features
Object.assign(Alchemy, {
// Global utility method for translating a given string
Buttons,
t: translate,
...Dirty,
t: translate, // Global utility method for translating a given string
translations: Object.assign(Alchemy.translations || {}, translationData),
fileEditors,
pictureEditors,
Expand Down
79 changes: 79 additions & 0 deletions app/javascript/alchemy_admin/dirty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
function ElementDirtyObserver(selector) {
$(selector)
.find('input[type="text"], select')
.change(function (event) {
const $content = $(event.target)
$content.addClass("dirty")
setElementDirty($content.closest(".element-editor"))
})
}

function setElementDirty(element) {
$(element).addClass("dirty")
window.onbeforeunload = () => Alchemy.t("page_dirty_notice")
}

function setElementClean(element) {
const $element = $(element)
$element.removeClass("dirty")
$element.find("> .element-body .dirty").removeClass("dirty")
window.onbeforeunload = () => {}
}

function isPageDirty() {
return $("#element_area").find(".element-editor.dirty").length > 0
}

function isElementDirty(element) {
return $(element).hasClass("dirty")
}

function checkPageDirtyness(element) {
let callback = () => {}

if ($(element).is("form")) {
callback = function () {
const $form = $(
`<form action="${element.action}" method="POST" style="display: none" />`
)
$form.append($(element).find("input"))
$form.appendTo("body")

Alchemy.pleaseWaitOverlay()
$form.submit()
}
} else if ($(element).is("a")) {
callback = () => Turbo.visit(element.pathname)
}

if (isPageDirty()) {
Alchemy.openConfirmDialog(Alchemy.t("page_dirty_notice"), {
title: Alchemy.t("warning"),
ok_label: Alchemy.t("ok"),
cancel_label: Alchemy.t("cancel"),
on_ok: function () {
window.onbeforeunload = void 0
callback()
}
})
return false
}
return true
}

function PageLeaveObserver() {
$("#main_navi a").click(function (event) {
if (!checkPageDirtyness(event.currentTarget)) {
event.preventDefault()
}
})
}

export default {
ElementDirtyObserver,
setElementDirty,
setElementClean,
isElementDirty,
checkPageDirtyness,
PageLeaveObserver
}

0 comments on commit 8596f74

Please sign in to comment.